Files
xk-client-wx/App.vue
2026-01-15 12:33:46 +08:00

340 lines
13 KiB
Vue
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.
<script>
import {
getVersion,
getSystemConfig
} from './request/api/api'
import { webSocketManager } from '@/utils/ws/websocket.js';
import { ChatManager } from '@/store/chat/chat.js';
import {checkDev} from "@/utils/utils";
import {bindUserApi} from "@/request/api/salesperson";
import { silentLogin } from '@/utils/login.js';
import { isGuestMode } from '@/common/utils/auth.js';
export default {
// //初始化状态-如果登录过,保持登录状态
onLaunch: async function() {
silentLogin().then((loginResult) => {
if (loginResult.success) {
} else {
console.error('静默登录失败:', loginResult.message);
}
}).catch((error) => {
console.error('静默登录异常:', error);
});
const version = 4 // 如果当前版本和后台返回的版本一致,那就是审核中,审核通过后 后台设置加1如果后台的比本地的大 那就是生产版本
const res = await getVersion()
const val = Number(res.data.data.value)
// const accountInfo = uni.getAccountInfoSync();
// if (accountInfo.miniProgram.envVersion === 'develop') {
// // 跳转预留好的页面-调试使用中
// // wx.navigateTo() // 开发时,可以注释本行,方便自己预览
// // console.log(accountInfo,'aaaaaaaaaaaaaaaa');
// uni.navigateTo({
// url:'/pages/home/index',
// success() {
// // console.log(accountInfo,'aaaaaaaaaaaaaaaa');
// }
// })
// }
// 生产
const isShow = val <= version;
// 测试
// const isShow = accountInfo.miniProgram.envVersion != 'develop';
// 存储当前版本,也可以使用globalData
uni.setStorageSync('isShow_envVersion', isShow)
// // 检查是否已登录,如果未登录才检查配置并决定是否静默登录
// const token = uni.getStorageSync('token');
// const userInfo = uni.getStorageSync('userinfo');
// if (!token || !userInfo) {
// 未登录,获取系统配置,判断是否需要静默登录
// try {
// const configRes = getSystemConfig({
// method: "get",
// data: {
// store_id: uni.getStorageSync('store_id') || '11001'
// }
// });
//
// 根据配置决定是否静默登录
// 注意:需要根据实际接口返回格式解析 need_login 字段
// const isGuest = isGuestMode();
// let needLogin = false; // 默认需要登录页
//
// if (isGuest && (configRes.data.code === 0 || configRes.data.code === '0')) {
// // 新格式:从 result 中获取
// const configData = configRes.data.result || {};
// needLogin = configData.need_login !== false; // false 表示静默登录
// } else if (!isGuest && (configRes.data?.errcode === 0 || configRes.data?.errcode === '0')) {
// // 旧格式:从 data 中获取
// const configData = configRes.data.data || {};
// needLogin = configData.need_login !== false; // false 表示静默登录
// }
//
// // 如果配置为静默登录need_login: false直接执行静默登录
// if (!needLogin) {
// // 执行静默登录(不显示提示,不跳转)
// silentLogin().then((loginResult) => {
// if (loginResult.success) {
// console.log('静默登录成功');
// } else {
// console.error('静默登录失败:', loginResult.message);
// }
// }).catch((error) => {
// console.error('静默登录异常:', error);
// });
// }
// // 如果配置为需要登录页need_login: true不执行任何操作等待用户手动登录
// } catch (error) {
// // 配置接口调用失败,不影响应用启动,仅记录日志
// console.error('获取系统配置失败:', error);
// }
// }
console.log('获取更新机制', uni.canIUse('getUpdateManager'))
// 获取小程序更新机制兼容
if (uni.canIUse('getUpdateManager')) {
const updateManager = uni.getUpdateManager()
console.log('updateManager', updateManager)
// 检查是否有新版本发布
updateManager.onCheckForUpdate(function(res) {
console.log('检查是否有新版本发布', 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()
} else if (res.cancel) {
//重新回到版本更新提示
autoUpdate()
}
}
})
}
}
})
})
// 新的版本下载失败
updateManager.onUpdateFailed(function() {
uni.showModal({
title: '温馨提示',
content: '新版本已经上线,请您删除当前小程序,重新搜索打开',
})
})
}
})
} else {
// 提示用户在最新版本的客户端上体验
uni.showModal({
title: '温馨提示',
content: '当前微信版本过低,可能无法使用该功能,请升级到最新版本后重试。'
})
}
if (checkDev('open-im')) {
this.initWebSocket();
}
},
onShow: function(e) {
// console.log('App Show')
// this.$store.dispatch('initLogin')
// 解析scene里面的参数
if (e === undefined)
return {};
// 检查是否已登录,如果未登录才检查配置并决定是否静默登录
// const token = uni.getStorageSync('token');
// const userInfo = uni.getStorageSync('userinfo');
// if (!token || !userInfo) {
// // 未登录,获取系统配置,判断是否需要静默登录
// try {
// const configRes = getSystemConfig({
// method: "get",
// data: {
// store_id: uni.getStorageSync('store_id') || '11001'
// }
// });
//
// // 根据配置决定是否静默登录
// // 注意:需要根据实际接口返回格式解析 need_login 字段
// const isGuest = isGuestMode();
// let needLogin = false; // 默认需要登录页
//
// if (isGuest && (configRes.data.code === 0 || configRes.data.code === '0')) {
// // 新格式:从 result 中获取
// const configData = configRes.data.result || {};
// needLogin = configData.need_login !== false; // false 表示静默登录
// } else if (!isGuest && (configRes.data?.errcode === 0 || configRes.data?.errcode === '0')) {
// // 旧格式:从 data 中获取
// const configData = configRes.data.data || {};
// needLogin = configData.need_login !== false; // false 表示静默登录
// }
//
// // 如果配置为静默登录need_login: false直接执行静默登录
// if (!needLogin) {
// // 执行静默登录(不显示提示,不跳转)
// silentLogin().then((loginResult) => {
// if (loginResult.success) {
// console.log('静默登录成功');
// } else {
// console.error('静默登录失败:', loginResult.message);
// }
// }).catch((error) => {
// console.error('静默登录异常:', error);
// });
// }
// // 如果配置为需要登录页need_login: true不执行任何操作等待用户手动登录
// } catch (error) {
// // 配置接口调用失败,不影响应用启动,仅记录日志
// console.error('获取系统配置失败:', error);
// }
// }
const scene = decodeURIComponent(e.query.scene)
// 这里接收扫码参数
if (scene) {
// 判断scene中是否包含sal_id
let isSalespersonId = decodeURIComponent(e.query.scene).includes('sal_id');
const scene = decodeURIComponent(e.query.scene).split('STORE_QR_CODE_')
if (scene[1]) {
uni.setStorageSync('stores_id', scene[1])
uni.setStorageSync('store_id', scene[1])
}
// uni.setStorageSync('stores_id', scene[1])
const st_id = decodeURIComponent(e.query.scene).split('&')
let store_id = decodeURIComponent(e.query.scene).split('STORE_QR_CODE_')
store_id = decodeURIComponent(store_id[1]).split('&')
const doctors_id = decodeURIComponent(e.query.scene).split('=')
if (store_id[0] !== 'undefined') {
uni.setStorageSync('stores_id', store_id[0])
uni.setStorageSync('store_id', store_id[0])
}
uni.setStorageSync('doctors_id', st_id[0].split('su_id=')[1])
if (isSalespersonId) {
uni.setStorageSync('sal_id', st_id[1].split('sal_id=')[1])
}
uni.setStorageSync('st_id', doctors_id[2])
let userId = uni.getStorageSync('user_id')
if (isSalespersonId && userId) {
bindUserApi({
user_id: userId,
salesperson_id: st_id[1].split('sal_id=')[1],
}).then((res) => {
console.log('userId', userId, st_id[0].split('su_id=')[1], res);
})
}
// console.log(st_id, '4444111111111');
// console.log(scene, '4444');
}
// const scene = decodeURIComponent(e.query.scene).split('STORE_QR_CODE_')
// console.log(scene, '11111');
},
onHide: function() {
// console.log('App Hide')
// this.$store.dispatch('closeWebSocket')
},
onUnload:function() {
// uni.removeStorageSync('token')
},
methods: {
initWebSocket() {
webSocketManager.connect();
// 注册全局消息处理器
webSocketManager.addMessageHandler((data) => {
// 只处理聊天消息(包含 room_id 的消息)
if (data.room_id) {
// 获取当前页面路径
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
const currentPath = currentPage ? currentPage.route : '';
// 如果用户在聊天页面,不调用 ChatManagerchat.vue 会自己处理)
// 避免消息重复推送
if (currentPath && currentPath.includes('chat/chat')) {
return;
}
ChatManager.handleIncomingMessage(data);
}
});
// 监听全局通知事件
uni.$on('show-global-notification', this.handleShowNotification);
},
/**
* 处理显示通知
* @param {Object} message 消息对象
*/
handleShowNotification(message) {
// 获取当前页面路径
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
const currentPath = currentPage ? currentPage.route : '';
// 如果在聊天页面,不显示顶部通知
if (currentPath && currentPath.includes('chat/chat')) {
return;
}
// 获取消息预览文本
const messagePreview = ChatManager.getMessagePreview(message);
// 从消息中提取发送者信息
let senderName = '新消息';
let senderAvatar = '/static/xx/ysxx.png';
// 尝试解析发送者ID获取更多信息
const senderId = message.sender_user_id || '';
if (senderId.startsWith('doctor-')) {
senderName = '医生消息';
}
// 触发通知组件显示
uni.$emit('show-message-notification', {
avatar: senderAvatar,
title: senderName,
message: messagePreview,
roomId: message.room_id,
roomData: {
nick_name: senderName,
user_id: senderId,
avatar: senderAvatar
}
});
}
},
onUnload() {
// 移除全局通知事件监听
uni.$off('show-global-notification', this.handleShowNotification);
}
}
</script>
<style lang="scss">
/* 注意要写在第一行同时给style标签加入lang="scss"属性 */
@import "uview-ui/index.scss";
@import url("static/iconfont/index.css");
</style>