Files
xk-doctor-wx/common/common.js
李琦 97bc48c17c feat: 按 API 拆分规则新建 api/workbench.js(xk-api、unwrapXkApi 取值)。
新增 components/ai-brief-card/ai-brief-card.vue:卡片 + 全文底部弹层(page-container + v-if 拦截返回键),每日首弹用 storage 按天记录;模板遵守小程序限制(无 ?./??、无 :class 函数调用)。
      工作台页面挂载卡片并接入 refreshWorkbenchData,组件内部按「门店+日期」去重——页面 onShow 反复触发也只拉一次,切店/跨天自动重拉(跨天首次会重新生成并触发新一天的首弹)。
2026-08-17 09:51:49 +08:00

162 lines
3.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import $store from '@/store/index.js';
class common {
constructor(arg) {
this.timer = null
this.url = arg.url
this.isOnline = false //是否在线
this.socket = null //socket对象
this.reconnectTime = 0
this.isOpenReconnect = true //是否重连
this.loadFinish = false;
// 获取当前用户相关信息
let user = uni.getStorageSync('userInfo')
this.user_token = uni.getStorageSync('token')
this.user = user ? user : {}
// 初始化聊天对象
this.TO = false;
// 连接和监听
if (this.user) {
this.connectSocket()
}
}
// 断线重连
reconnect() {
console.log(this.isOnline)
console.log("进入reconnect准备重新链接")
if (this.isOnline) {
return
}
if (this.reconnectTime >= 3) {
return this.reconnectConfirm()
}
this.reconnectTime += 1
console.log("重新链接")
// this.connectSocket();
}
// 连接socket
connectSocket() {
console.log("链接connectSocket")
this.socket = uni.connectSocket({
url: this.url,
header: {
authorization: `Bearer ${uni.getStorageSync('token')}`
},
method: 'POST',
complete: () => {}
})
// 监听连接成功
this.socket.onOpen && this.socket.onOpen(() => this.onOpen())
// 监听接收信息
this.socket.onMessage && this.socket.onMessage((res) => this.onMessage(res))
// 监听断开
this.socket.onClose && this.socket.onClose((e) => this.onClose(e))
// 监听错误
this.socket.onError && this.socket.onError(() => this.onError())
}
onOpen() {
// 用户上线
console.log('websocket连接成功')
this.isOnline = true
this.isOpenReconnect = true
console.log(this.url, 'doctor');
console.log(this.user, 'user')
// 获取用户离线消息
if (this.socket != null) {
var login = {
cmd: 'login',
userId: Number(this.user.su_id),
imStatus: Number(this.user.im_status),
userType: Number(this.user.role),
data: ''
};
this.initSocketLogin(login);
}
}
// 监听关闭
onClose(e) {
// 用户下线
this.isOnline = false
this.socket = null
console.log('socket连接已关闭' + e)
if (this.isOpenReconnect) {
var that = this;
setTimeout(function() {
that.reconnect();
}, 3000);
}
}
// 监听连接错误
onError() {
// 用户下线
this.isOnline = false
this.socket = null
console.log('socket连接错误' + e)
if (!this.socket) {
var that = this;
setTimeout(function() {
that.reconnect();
}, 3000);
}
}
// 监听接收消息
onMessage(data) {
console.log('监听接收消息:' + data);
// let res = JSON.parse(data.data)
// uni.$emit('onMessage', res)
}
// 关闭连接
close() {
if (this.socket) {
this.socket.close()
this.isOpenReconnect = false
} else if (uni.onSocketClose) {
uni.onSocketClose()
this.isOpenReconnect = false
}
this.destoryChatObject();
}
initSocketLogin(login) {
this.socket.send({
data: JSON.stringify(login),
success() {
console.log('用户IM登录成功:success')
},
fail(e) {
console.log('用户IM登录失败fail:' + e)
}
})
// setTimeout(() => {
// this.socket.send({
// data: JSON.stringify({
// data: '发送内容是:测试测试测试',
// }),
// })
// }, 4000)
}
// 创建聊天对象
createChatObject(detail) {
this.TO = detail
}
// 销毁聊天对象
destoryChatObject() {
this.TO = false
}
// 断线重连提示
reconnectConfirm() {
// this.close()
// this.connectSocket()
// this.reconnectTime = 0
}
// 验证是否上线
checkOnline() {
if (!this.isOnline) {
// 断线重连提示
this.reconnectConfirm()
return false
}
return true
}
}
export default common