| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- /**
- * Chat
- *
- */
- // var config = require('/config.js');
- import { login } from '../pages/login/login'
- const config = require('../config/config.js');
- const app = new getApp();
- export default class Chat {
- constructor(app) {
- this.chat_id = null // chat_id
- this.connectStatus = 0 // websocket 连接状态 0:未连接,1:已连接
- this.heartListen = null // 心跳
- this.watcherList = [] // 订阅者
- this.app = app // 方便在Chat内部操作app
- }
- /* 初始化连接 */
- connectSocket() {
- let type = app.globalData.SystemInfo.model === 'microsoft' ? 'wx' : 'phone';
- const openId = app.globalData.open_id;
- //被踢掉后 openId被清空,此时不需要再进行连接
- if (openId == null || openId == '' || openId == undefined){
- return;
- }
- const url = `${config.ws_url}:${config.ws_port}/websocket/wechat/${openId}-${type}`
-
- this.chat_id = new Date().getTime()
- // websocket连接
- wx.connectSocket({
- url: url,
- header: {
- 'content-type': 'application/json'
- },
- method: 'post',
- success: res => {
- // 设置连接状态
- this.connectStatus = 1
- // 心跳
- clearInterval(this.heartListen)
- this.heartListen = setInterval(() => {
- if (this.connectStatus === 0) {
- clearInterval(this.heartListen)
- this.reconnect()
- } else {
- }
- }, 500)
- app.globalData.wsId = this.heartListen;
- },
- fail: err => {
- console.error('连接失败')
- }
- })
- // 监听webSocket错误
- wx.onSocketError(res => {
- // 修改连接状态
- this.connectStatus = 0
- })
- // 监听WebSocket关闭
- wx.onSocketClose(res => {
- this.connectStatus = 0
- })
- // websocket打开
- wx.onSocketOpen(res => {
- })
- // 收到websocket消息
- wx.onSocketMessage(res => {
- res = JSON.parse(res.data);
- if (res.code === 'logout'){
- app.globalData.user = null;
- app.globalData.customerInfo = null;
- wx.showModal({
- title: '安全提示',
- content: '您的账号在另一台设备上登录,您将被迫下线。如非本人操作,您的密码很有可能已经泄露,请及时修改密码',
- showCancel:false,
- confirmText:'确定',
- success(res) {
- wx.reLaunch({
- url: '/pages/login/login',
- })
- }
- })
- }
- if (res.code === 'unbind_by_admin'){
- app.globalData.user = null;
- app.globalData.customerInfo = null;
- wx.showModal({
- title: '退出提示',
- content: '您的账号已被管理员解绑,您将被迫退出系统。如有问题,请联系管理员',
- showCancel: false,
- confirmText: '确定',
- success(res) {
- wx.reLaunch({
- url: '/pages/login/login',
- })
- }
- })
- }
- })
- }
- /* 重连 */
- reconnect() {
- // wx.closeSocket() // 重连之前手动关闭一次
- this.connectSocket()
- }
- /* 关闭websocket */
- closeSocket(removeChat) {
- wx.closeSocket({
- success: res => {
- clearInterval(app.globalData.wsId);
- // code
- }
- })
- }
- /* 添加watcher */
- addWatcher(fn) {
- this.watcherList.push(fn)
- return this.watcherList.length - 1 // 返回添加位置的下标,Page unload的时候方便删除List成员
- }
- /* 删除watcher */
- delWatcher(index) {
- this.watcherList.splice(index, 1)
- }
- /* 收到消息 */
- getSocketMsg(data) {
- if (data.code === 5100) { // 处理登录过期
- login()
- .then(res => {
- // 重新登录成功,发起重连
- this.reconnect()
- })
- .catch(err => {
- console.error('登录失败', err)
- })
- // 正确状态
- } else if (data.code === 0) {
- // 给每个订阅者发消息
- const list = this.watcherList
- for (let i = 0; i < list.length; i++) {
- list[i](data)
- }
- // 其他返回类型
- } else {
- // balabalabala
- }
- }
- // 这里可以写一些方法,如发送消息等
- // code
- }
|