import WebSocket from '@/common/utils/socket.js'
import store from '@/store/index'
import commonMethods from '@/common/commonMethods.js'
import Vue from 'vue'
const socket = {
  initSocket: function() {
    Vue.prototype.socket = Vue.prototype.socket || new WebSocket()
    this.onMessage(Vue.prototype.socket)
  },
  onMessage: (ws) => {
    ws.onMessage = message => {
      if (typeof message !== 'object') {
        return false
      }
      if (message.code === 400) {
        uni.hideLoading()
        store.dispatch('setConnect', true)
        uni.showToast({ title: message.msg, icon: 'none', duration: 2000 })
        return false
      }
      if (message.push_type === 'reload_order') {
        store.dispatch('setReload', true)
      }
      if (message.push_type === 'order') {
        uni.hideLoading()
        store.dispatch('setConnect', true)
        commonMethods.globalNavigateTo('order', { type: 1 })
      }
    }
  }
}

export default socket
