import Config from '@/config'
import Store from '@/store'

export const webSocket = {
  methods: {
    connectWebSocket(userId) {
      // let webSocket = new WebSocket(`ws://192.168.1.3:8877/api/xiaoshi/ws/` + userId)
      let webSocket = new WebSocket(`ws://${this.$c.WebSocketPath}/api/xiaoshi/ws/` + userId)
      Store.commit('SET_WEB_SOCKET', webSocket)
      webSocket.onopen = () => {
        console.log('WebSocket连接成功')
      }
      webSocket.onmessage = async (e) => {
        if(e.data.indexOf('{') == -1){
          return false
      }
        const { code, data, message } = JSON.parse(e.data)
        // 603导出专利完成605导出PDF完成
        if(code == 903 || code == 600|| code == 603|| code == 605){
          if (data.complete) {
              this.$message.success(`导入完成`)
          }else{
          }
        }
      }
      webSocket.onerror = () => {
        console.log('WebSocket连接失败')
      }
      webSocket.onclose = () => {
        console.log('WebSocket连接关闭')
      }
    }
  }
}

export const commonData={
  mounted() {
    // this.DictMessage()
    // this.getUserInfo()
    // this.getScenario()
  },
  methods:{
    //获取个人信息
    getUserInfo() {
      this.$api.getUserInfo().then(response => {
        localStorage.tenant = response.data.tenantId
        this.$store.commit('SET_USERINFO', response.data)
        
      })
    },
    //获取字典项
    DictMessage() {
      this.$api.getOsSystemDict([]).then(response => {
        this.$store.commit("SET_DictMessage",response.data)
      })
    },
    //获取应用场景
    getScenario(){
      this.$api.getScenario().then(response => {
        this.$store.commit("SET_Scenario",response.data.data)
      })
    },
    //获取调查类型
    getMatter(){
      this.$api.getMatter([]).then(response => {
        this.$store.commit("SET_Matter",response.data.data)
      })
    },
    //获取权限
    getPermissionList(){
      var code = "e424f3cc4f8138f0f68bef092b92212f,6a8d3f4d1d5f11eda41c00163e2f0200"
      this.$api.getPermissionList({code:code}).then(response=>{
        this.$store.commit('SET_PERMISSION', response.data)
      }).catch(error=>{
        this.$store.commit('SET_PERMISSION', [])
      })
    },
  }
}

