Files
xk-doctor-wx/App.vue

192 lines
5.9 KiB
Vue
Raw Normal View History

2024-09-19 12:57:55 +08:00
<template>
<view></view>
</template>
<script>
import {
info,
personalData,
leadInfo,
servInfo,
} from "@/api/all.js"
2026-03-06 13:59:22 +08:00
import { initWebSocket, disconnectWebSocket, rebindUser, addMessageHandler, isWebSocketConnected } from '@/utils/ws/initWebSocket.js';
import { chatRoomManager } from '@/utils/chat/chatRoomManager.js';
2024-09-19 12:57:55 +08:00
export default {
onLaunch() {
2026-03-06 13:59:22 +08:00
// 初始化WebSocket如果已登录
// 参考患者端的方式,直接初始化,不延迟
this.initWebSocketConnection();
2024-09-19 12:57:55 +08:00
// 获取小程序更新机制兼容
if (uni.canIUse('getUpdateManager')) {
const updateManager = uni.getUpdateManager()
// 检查是否有新版本发布
updateManager.onCheckForUpdate(function(res) {
if (res.hasUpdate) {
//小程序有新版本,则静默下载新版本,做好更新准备
updateManager.onUpdateReady(function() {
uni.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success: function(res) {
if (res.confirm) {
//新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
} else if (res.cancel) {
//如果需要强制更新,则给出二次弹窗,如果不需要,则这里的代码都可以删掉了
uni.showModal({
title: '温馨提示',
content: '我们已经做了新的优化,请及时更新哦~',
showCancel: false, //隐藏取消按钮也可显示取消会走res.cancel然后从新开始提示
success: function(res) {
//第二次提示后,强制更新
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
uni.clearStorageSync()
} else if (res.cancel) {
//重新回到版本更新提示
autoUpdate()
}
}
})
}
}
})
})
// 新的版本下载失败
updateManager.onUpdateFailed(function() {
uni.showModal({
title: '温馨提示',
content: '新版本已经上线,请您删除当前小程序,重新搜索打开',
})
})
}
})
} else {
// 提示用户在最新版本的客户端上体验
uni.showModal({
title: '温馨提示',
content: '当前微信版本过低,可能无法使用该功能,请升级到最新版本后重试。'
})
}
},
created() {
if (uni.getStorageSync('token')) {
this.getInfo()
}
2026-03-06 13:59:22 +08:00
// 监听登录事件
uni.$on('user-login', () => {
this.initWebSocketConnection();
});
// 监听登出事件
uni.$on('user-logout', () => {
disconnectWebSocket();
});
// 监听token刷新事件
uni.$on('token-refreshed', () => {
rebindUser();
});
// 注册聊天室消息处理器
addMessageHandler((data) => {
chatRoomManager.handleWebSocketMessage(data);
});
},
onShow: function() {
// 应用显示时如果已登录但WebSocket未连接尝试连接
if (uni.getStorageSync('token') && !isWebSocketConnected()) {
this.initWebSocketConnection();
}
2024-09-19 12:57:55 +08:00
},
onHide: function() {
2026-03-06 13:59:22 +08:00
// 应用隐藏时不断开WebSocket保持连接以便接收消息
// 如果需要断开,可以调用 disconnectWebSocket()
2024-09-19 12:57:55 +08:00
},
methods: {
2026-03-06 13:59:22 +08:00
/**
* 初始化WebSocket连接
* 职责检查登录状态如果已登录则初始化WebSocket
* 参考患者端的方式直接连接不延迟
*/
initWebSocketConnection() {
const token = uni.getStorageSync('token');
const doctorId = uni.getStorageSync('doctor_id') || uni.getStorageSync('user_id');
if (token && doctorId) {
// 直接初始化,不延迟(参考患者端的方式)
initWebSocket();
}
},
2024-09-19 12:57:55 +08:00
async getInfo() {
let role = uni.getStorageSync('identity')
let req = {};
const data = {
store_id: uni.getStorageSync('store_id') || '11001',
}
role == 1 && (req = await info(data))
role == 2 && (req = await personalData(data))
if (req.errcode == 0) {
console.log(req, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
// 未完善信息
if (req.data.user?.status == 3) {
2024-09-19 12:57:55 +08:00
// this.$toast('您还没有完善信息,请先完善信息')
// this.$go('../../subPackages/sub_workbench/workbench_upInfo/index')
uni.navigateTo({
url: '/pages/workbench/examine'
})
return
}
if (role == 2 && req.data.user?.status == 2) {
2024-09-19 12:57:55 +08:00
let logInfo = uni.getStorageSync('logInfo') || {}
let userInfo = {
...logInfo,
...(uni.getStorageSync('userInfo') || {}),
...req.data
}
userInfo.status = userInfo.user && userInfo.user.status != '' ? userInfo.user.status : userInfo
.status
uni.setStorageSync('identity', userInfo['role'])
uni.removeStorageSync('logInfo')
uni.setStorageSync('userInfo', userInfo)
setTimeout(() => {
console.log('mainnnnnnnnnnnnnnnnnnnnnnnn');
role == 2 && this.$go('/pages/pharmacist/index', 3)
}, 100)
}
if (role == 1 && req.data.DoctorInfo.user?.status == 2) {
2024-09-19 12:57:55 +08:00
let logInfo = uni.getStorageSync('logInfo') || {}
let userInfo = {
...logInfo,
...(uni.getStorageSync('userInfo') || {}),
...req.data
}
userInfo.status = userInfo.user && userInfo.user?.status != '' ? userInfo.user.status : userInfo
2024-09-19 12:57:55 +08:00
.status
uni.setStorageSync('identity', userInfo['role'])
uni.removeStorageSync('logInfo')
uni.setStorageSync('userInfo', userInfo)
setTimeout(() => {
console.log('mainnnnnnnnnnnnnnnnnnnnnnnn');
role == 1 && this.$go('/pages/workbench/index', 3)
}, 100)
}
} else {
this.$toast(req.msg)
}
}
}
}
</script>
<style lang="scss">
@import "uview-ui/index.scss";
@import "common/css/app.css";
</style>