Chat.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * Chat
  3. *
  4. */
  5. // var config = require('/config.js');
  6. import { login } from '../pages/login/login'
  7. const config = require('../config/config.js');
  8. const app = new getApp();
  9. export default class Chat {
  10. constructor(app) {
  11. this.chat_id = null // chat_id
  12. this.connectStatus = 0 // websocket 连接状态 0:未连接,1:已连接
  13. this.heartListen = null // 心跳
  14. this.watcherList = [] // 订阅者
  15. this.app = app // 方便在Chat内部操作app
  16. }
  17. /* 初始化连接 */
  18. connectSocket() {
  19. let type = app.globalData.SystemInfo.model === 'microsoft' ? 'wx' : 'phone';
  20. const openId = app.globalData.open_id;
  21. //被踢掉后 openId被清空,此时不需要再进行连接
  22. if (openId == null || openId == '' || openId == undefined){
  23. return;
  24. }
  25. const url = `${config.ws_url}:${config.ws_port}/websocket/wechat/${openId}-${type}`
  26. this.chat_id = new Date().getTime()
  27. // websocket连接
  28. wx.connectSocket({
  29. url: url,
  30. header: {
  31. 'content-type': 'application/json'
  32. },
  33. method: 'post',
  34. success: res => {
  35. // 设置连接状态
  36. this.connectStatus = 1
  37. // 心跳
  38. clearInterval(this.heartListen)
  39. this.heartListen = setInterval(() => {
  40. if (this.connectStatus === 0) {
  41. clearInterval(this.heartListen)
  42. this.reconnect()
  43. } else {
  44. }
  45. }, 500)
  46. app.globalData.wsId = this.heartListen;
  47. },
  48. fail: err => {
  49. console.error('连接失败')
  50. }
  51. })
  52. // 监听webSocket错误
  53. wx.onSocketError(res => {
  54. // 修改连接状态
  55. this.connectStatus = 0
  56. })
  57. // 监听WebSocket关闭
  58. wx.onSocketClose(res => {
  59. this.connectStatus = 0
  60. })
  61. // websocket打开
  62. wx.onSocketOpen(res => {
  63. })
  64. // 收到websocket消息
  65. wx.onSocketMessage(res => {
  66. res = JSON.parse(res.data);
  67. if (res.code === 'logout'){
  68. app.globalData.user = null;
  69. app.globalData.customerInfo = null;
  70. wx.showModal({
  71. title: '安全提示',
  72. content: '您的账号在另一台设备上登录,您将被迫下线。如非本人操作,您的密码很有可能已经泄露,请及时修改密码',
  73. showCancel:false,
  74. confirmText:'确定',
  75. success(res) {
  76. wx.reLaunch({
  77. url: '/pages/login/login',
  78. })
  79. }
  80. })
  81. }
  82. if (res.code === 'unbind_by_admin'){
  83. app.globalData.user = null;
  84. app.globalData.customerInfo = null;
  85. wx.showModal({
  86. title: '退出提示',
  87. content: '您的账号已被管理员解绑,您将被迫退出系统。如有问题,请联系管理员',
  88. showCancel: false,
  89. confirmText: '确定',
  90. success(res) {
  91. wx.reLaunch({
  92. url: '/pages/login/login',
  93. })
  94. }
  95. })
  96. }
  97. })
  98. }
  99. /* 重连 */
  100. reconnect() {
  101. // wx.closeSocket() // 重连之前手动关闭一次
  102. this.connectSocket()
  103. }
  104. /* 关闭websocket */
  105. closeSocket(removeChat) {
  106. wx.closeSocket({
  107. success: res => {
  108. clearInterval(app.globalData.wsId);
  109. // code
  110. }
  111. })
  112. }
  113. /* 添加watcher */
  114. addWatcher(fn) {
  115. this.watcherList.push(fn)
  116. return this.watcherList.length - 1 // 返回添加位置的下标,Page unload的时候方便删除List成员
  117. }
  118. /* 删除watcher */
  119. delWatcher(index) {
  120. this.watcherList.splice(index, 1)
  121. }
  122. /* 收到消息 */
  123. getSocketMsg(data) {
  124. if (data.code === 5100) { // 处理登录过期
  125. login()
  126. .then(res => {
  127. // 重新登录成功,发起重连
  128. this.reconnect()
  129. })
  130. .catch(err => {
  131. console.error('登录失败', err)
  132. })
  133. // 正确状态
  134. } else if (data.code === 0) {
  135. // 给每个订阅者发消息
  136. const list = this.watcherList
  137. for (let i = 0; i < list.length; i++) {
  138. list[i](data)
  139. }
  140. // 其他返回类型
  141. } else {
  142. // balabalabala
  143. }
  144. }
  145. // 这里可以写一些方法,如发送消息等
  146. // code
  147. }