2024-05-07 10:37:12 +08:00

384 lines
13 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 Aegis from './TUIKit/lib/aegis';
import TIM from './miniprogram_npm/tim-wx-sdk/index';
import TIMUploadPlugin from './miniprogram_npm/tim-upload-plugin/index';
import { API } from './utils/network/api'
import constant from './utils/constant';
const { GDXZ_CUSTOM_MSEEAGE } = constant;
const api = new API();
App({
onLaunch: function (options) {
// 判断是否由分享进入小程序
if (options.scene != 1001) {
this.globalData.share = true
} else {
this.globalData.share = false
};
this.globalData.scene = options.scene;
wx.getSystemInfo({
success: (res) => {
this.globalData.height = res.statusBarHeight
}
})
wx.onCopyUrl(() => {
console.log("onCopyUrlonCopyUrlonCopyUrl")
setTimeout(()=>{
wx.setClipboardData({
data: '暂不支持分享',
success (res) {
},
complete(){
wx.hideToast()
}
})
},1000)
return
})
},
onShow(){
},
imInit(options={},type=1,callback=()=>{}){
console.log("imInit")
let usertype = wx.getStorageSync('usertype');
let userID = wx.getStorageSync('user_id_'+usertype);
if(usertype != 2){//只登录医师端
return;
}
if(userID == ""){
return;
}
this.globalData.config.userID = userID;
// let sign = genTestUserSig(this.globalData.config);
// console.log("sign: ", sign);
// const userSig = sign.userSig;
if(!wx.$TUIKit){
//获取im签名数据
return api.getSignIM().then(response => {
let from = response.from
if(from != "cache"){
wx.setStorageSync(userID + '_imsign', {"sign":response.data,"date":new Date().getTime()})
}
return response.data;
}).then((userSig) => {
// console.log("userSig: ", userSig);
wx.$TUIKit = TIM.create({
SDKAppID: this.globalData.config.SDKAPPID
});
wx.$chat_userID = this.globalData.config.userID;
wx.$chat_SDKAppID = this.globalData.config.SDKAPPID;
wx.$chat_userSig = userSig;
wx.$TUIKitTIM = TIM;
console.log("TIM: ", wx.$TUIKitTIM)
wx.$TUIKit.registerPlugin({ 'tim-upload-plugin': TIMUploadPlugin });
let login_promise = wx.$TUIKit.login({
userID: this.globalData.config.userID,
userSig
});
let THIS=this;
login_promise.then(async(imResponse)=> {
// console.log(imResponse.data); // 登录成功
await require.async('./TUICallKit/TUICallService/serve/callManager').then(res => {
console.log(res)
wx.callManager = new res.CallManager();
}).catch(({mod, errMsg}) => {
console.error(`path: ${mod}, ${errMsg}`)
})
await wx.callManager.init({
sdkAppID: Number(this.globalData.config.SDKAPPID), // 请填入 sdkAppID
userID: this.globalData.config.userID, // 请填入 userID
userSig: userSig, // 请填入 userSig
globalCallPagePath: "TUICallKit/pages/globalCall/globalCall",
tim: wx.$TUIKit
})
callback();
THIS.globalData.isLogin=true;
}).catch(function(imError) {
console.log('login error:', imError); // 登录失败的相关信息
if(type==1 && imError.indexOf("重复登录")!=-1){
wx.showToast({
title: '登陆失败',
icon:"none"
})
};
});
//wx.$TUIKit.off(wx.$TUIKitTIM.EVENT.SDK_READY, this.onSDKReady);
wx.$TUIKit.setLogLevel(3);//设置日志级别
wx.$TUIKit.on(wx.$TUIKitTIM.EVENT.SDK_READY, this.onSDKReady, this);
wx.$TUIKit.on(wx.$TUIKitTIM.EVENT.TOTAL_UNREAD_MESSAGE_COUNT_UPDATED, this.onTotalUnreadMessageCountUpdated);
}).catch(errors => {console.error(errors);})
}else{
let promise = new Promise((resolve, reject) => {
resolve("success");
}).then(res => {
// console.log("from app js:", res);
return "from app js";
});
return promise;
}
},
imLogout(){
if(wx.$TUIKit){
wx.$TUIKit.logout();
wx.$TUIKit = undefined;
}
this.clearAllInterval();
this.globalData.chat_sdk_ready = false;
this.globalData.config.userID='';
},
clearAllInterval(){
this.globalData.app_inter.forEach(item => {
clearInterval(item)
})
},
onTotalUnreadMessageCountUpdated(){
// console.log("onTotalUnreadMessageCountUpdated");
let totalUnreadCount = wx.$TUIKit.getTotalUnreadMessageCount();
// console.log("未读消息数量:", totalUnreadCount)
},
onSDKReady(){
console.log("onSDKReady from app.js");
this.globalData.chat_sdk_ready = true;
let usertype = wx.getStorageSync('usertype');
let userID = wx.getStorageSync('user_id_'+usertype);
//初始化tab-bar角标
let index_info = wx.getStorageSync(userID+'_index_info');
let my_info = wx.getStorageSync(userID+'_my_info');
if(!index_info) wx.setStorageSync(userID+'_index_info',"");
if(!my_info) wx.setStorageSync(userID+'_my_info', "");
this.formatWenZhenInfo();
wx.$TUIKit.on(wx.$TUIKitTIM.EVENT.MESSAGE_RECEIVED, this.$onSystemPushReceived, this);
//互踢
console.log("互踢");
wx.$TUIKit.on(wx.$TUIKitTIM.EVENT.KICKED_OUT, this.onKickedOut,this);
},
onUnload(){
wx.$TUIKit.off(wx.$TUIKitTIM.EVENT.SDK_READY, this.onSDKReady);
wx.$TUIKit.off(wx.$TUIKitTIM.EVENT.MESSAGE_RECEIVED, this.$onSystemPushReceived);
wx.$TUIKit.off(wx.$TUIKitTIM.EVENT.TOTAL_UNREAD_MESSAGE_COUNT_UPDATED, this.onTotalUnreadMessageCountUpdated);
wx.$TUIKit.off(wx.$TUIKitTIM.EVENT.KICKED_OUT,this.$onKickedOut);
},
onKickedOut(){
// console.log("onKickedOutonKickedOutonKickedOutonKickedOut");
let usertype = wx.getStorageSync('usertype');
// wx.setStorageSync('AUTH_TOKEN_'+usertype, "");
const { envVersion } = wx.getAccountInfoSync().miniProgram;
if(envVersion=='release'){
wx.setStorageSync('AUTH_TOKEN_'+usertype, '');
}else{
wx.setStorageSync('DEV_AUTH_TOKEN_'+usertype, '');
}
wx.setStorageSync('user_id_'+usertype, "");
wx.setStorageSync('client_user_id_'+usertype, "");
if(wx.$TUIKit){
wx.$TUIKit.off(wx.$TUIKitTIM.EVENT.MESSAGE_RECEIVED, this.$onSystemPushReceived);
wx.$TUIKit = undefined
wx.callManager.destroyed();
}
this.globalData.chat_sdk_ready = false
this.globalData.isLogin=false;
this.go("/user/pages/kickedout/index");
},
$onSystemPushReceived(e){
// console.log("onSystemPushReceived from app.js");
// console.log(e)
let usertype = wx.getStorageSync('usertype');
let userID = wx.getStorageSync('user_id_'+usertype);
let message = e.data[0];
// console.log(message)
let type = message.type;
// console.log(type);
let unread_name = "";
if(type == TIM.TYPES.MSG_CUSTOM){
let payload = message.payload;
let payloadData = payload.data;
let payloadDataJson = JSON.parse(payloadData);
let message_type = payloadDataJson.message_type;
// console.log("message_type: ", message_type);
switch (message_type) {
case GDXZ_CUSTOM_MSEEAGE.TRABECULA:
unread_name = userID+"_wenzhen_info"; //1、消息内页横条
break;
case GDXZ_CUSTOM_MSEEAGE.ORDER_EVALUATION:
unread_name = userID+"_wenzhen_info"; //2、订单结束评价弹出
break;
case GDXZ_CUSTOM_MSEEAGE.DOCTOR_SYSTEM_NOTIFICATION:
unread_name = userID+"_system_notice_unreadnnum"; //3、医生端系统通知
break;
case GDXZ_CUSTOM_MSEEAGE.DOCTOR_SERVICE_NOTIFICATION:
unread_name = userID+"_service_notice_unreadnnum"; //4、医生端服务通知
break;
case GDXZ_CUSTOM_MSEEAGE.PRESCRIBE:
unread_name = userID+"_wenzhen_info"; //6、处方开具成功
break;
default:
break;
}
}else{
unread_name = userID+"_wenzhen_info";//tabbar 问诊角标
}
this.formatUnReadNum(unread_name);
},
formatUnReadNum(name, increment=1){
try {
let val = wx.getStorageSync(name);
if(val == "") val = 0;
if(increment > 0){
val = val + increment;
}else{
val = increment;
}
if(val == "+99") val = 100;
if(val > 99) val = '+99';
if(val == 0) val = '';
wx.setStorageSync(name, val);
} catch (error) {
}
},
aegisInit() {
wx.aegis = new Aegis({
id: 'iHWefAYquFxvklBblC', // 项目key
reportApiSpeed: false, // 接口测速
reportAssetSpeed: false, // 静态资源测速
pagePerformance: false, // 开启页面测速
});
},
globalData: {
share: false, // 分享默认为false
height: 0,
scene: "",//场景值
login_url: "/user/pages/login/index",
chat_sdk_ready: false,
jiesuan_dialog_show: false,
config: {
userID: "",
SDKAPPID: api.getIMappId(), // Your SDKAppID
SECRETKEY: api.getIMsecretkey(), // Your secretKey
EXPIRETIME: 604800,
},
app_inter: []
},
go(url, ...params){
if (getCurrentPages().length >= 9) {
wx.redirectTo({
url: url
})
} else {
wx.navigateTo({
url: url
})
}
},
formatWenZhenInfo(){
this.getConversationList();
},
onHide(){
if(!this.globalData.isLogin){
wx.setStorageSync('usertype','');
}
},
getConversationList(){
wx.$TUIKit.getConversationList().then((imResponse) => {
// console.log("getConversationList from app js: ", imResponse.data.conversationList);
return imResponse.data.conversationList;
}).then(conversationList => {
// console.log("开始查询订单状态")
this.formatStatus(conversationList);
}).catch(error => {
console.error(error)
});
},
formatStatus(conversationList){
let order_inquiry_id_list = [];
order_inquiry_id_list.push("");//初始化一个,防止失败
conversationList.forEach(item => {
let conversationID = item.conversationID;
if(conversationID == "C2Cadministrator") return;//如果是管理员消息直接跳过
let lastMessage = item.lastMessage;
if(!lastMessage) return;
let cloudCustomData = lastMessage.cloudCustomData;
if(!cloudCustomData) return;
let cloudCustomDataJson;
try {
cloudCustomDataJson = JSON.parse(cloudCustomData);
} catch (error) {
console.error(error);
return;
}
let order_inquiry_id = cloudCustomDataJson.order_inquiry_id;
order_inquiry_id_list.push(order_inquiry_id);//准备去请求后端那订单状态
})
//获取医生问诊消息用户属性
api.getDoctorInquiryMessageAttr({order_inquiry_ids: order_inquiry_id_list.join(",")}).then(response => {
let new_conversationList = [];
if(response.data.length > 0){
conversationList.forEach(item => {
let conversationID = item.conversationID;
if(conversationID == "C2Cadministrator") return;//如果是管理员消息直接跳过
let lastMessage = item.lastMessage;
if(!lastMessage) return;
let cloudCustomData = lastMessage.cloudCustomData;
if(!cloudCustomData) return;
let cloudCustomDataJson;
try {
cloudCustomDataJson = JSON.parse(item.lastMessage.cloudCustomData);
} catch (error) {
return;
}
let order_inquiry_id = cloudCustomDataJson.order_inquiry_id;
let order = response.data.find(attr_item => {
return attr_item.order_inquiry_id == order_inquiry_id;
})
if(!order) return;
let inquiry_status = order.inquiry_status;
//inquiry_status 状态
if(inquiry_status !=3 && inquiry_status != 4){
return;
}
item.order_inquiry_id = order_inquiry_id;
item.inquiry_status = inquiry_status;
item.patient_age = order.patient_age;
item.patient_name = order.patient_name;
item.patient_sex = order.patient_sex;
new_conversationList.push(item);
})
conversationList = [];
}
return new_conversationList;
}).then(new_conversationList => {
// console.log("开始计算问诊角标");
let wenzhen_info = 0;
new_conversationList.forEach(item => {
let unreadCount = item.unreadCount;
if(unreadCount > 0){
wenzhen_info = wenzhen_info + unreadCount;
}else{//若为0则继续判断订单状态
let inquiry_status = item.inquiry_status;
if(inquiry_status == 3){//若为待接诊,则+1
wenzhen_info = wenzhen_info + 1;
}
}
})
// console.log("角标的数量:", wenzhen_info);
let usertype = wx.getStorageSync('usertype');
let userID = wx.getStorageSync('user_id_'+usertype);
wx.setStorageSync(userID+'_wenzhen_info', wenzhen_info);
}).catch(errors => {console.error(errors);})
},
})