|
|
|
|
@ -1,7 +1,7 @@
|
|
|
|
|
<script lang="ts">
|
|
|
|
|
import RootStore from "@xkit-yx/im-store-v2";
|
|
|
|
|
import V2NIM, { V2NIMConst } from "nim-web-sdk-ng/dist/v2/NIM_UNIAPP_SDK";
|
|
|
|
|
import { getMsgContentTipByType,setTabUnread } from "./pages/NEUIKit/utils/msg";
|
|
|
|
|
import { getMsgContentTipByType, setTabUnread } from "./pages/NEUIKit/utils/msg";
|
|
|
|
|
import { STORAGE_KEY } from "./pages/NEUIKit/utils/constants";
|
|
|
|
|
import { isWxApp } from "./pages/NEUIKit/utils";
|
|
|
|
|
import { V2NIMMessage, V2NIMMessagePushConfig, V2NIMConversation } from "nim-web-sdk-ng/dist/v2/NIM_UNIAPP_SDK/V2NIMMessageService";
|
|
|
|
|
@ -14,10 +14,10 @@ export default {
|
|
|
|
|
themeColor: "#00b666",
|
|
|
|
|
themeBackgroundColor: "#00b66621",
|
|
|
|
|
},
|
|
|
|
|
onLaunch() {
|
|
|
|
|
onLaunch() {
|
|
|
|
|
let that = this;
|
|
|
|
|
uni.removeStorageSync("selectedCity");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 监听路由跳转
|
|
|
|
|
uni.addInterceptor("navigateTo", {
|
|
|
|
|
invoke(args) {
|
|
|
|
|
@ -41,7 +41,9 @@ export default {
|
|
|
|
|
onShow: function (options) {
|
|
|
|
|
let that = this;
|
|
|
|
|
console.log("show 项目init:", options, decodeURIComponent(options.query.scene));
|
|
|
|
|
|
|
|
|
|
let requestComeFrom = this.G.globalConstantData.requestComeFrom;
|
|
|
|
|
uni.setStorageSync("requestComeFrom", (requestComeFrom && (requestComeFrom == "assistant_miniapp" || requestComeFrom == "supply_chain_miniapp")) ? 'isToB' : 'isToC');
|
|
|
|
|
|
|
|
|
|
// 清除扫码获取的id信息
|
|
|
|
|
uni.removeStorageSync("scene");
|
|
|
|
|
uni.removeStorageSync("user_scene");
|
|
|
|
|
@ -65,16 +67,16 @@ export default {
|
|
|
|
|
} else {
|
|
|
|
|
uni.setStorageSync("apply-invite-code", "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (uni.getStorageSync("apply-token")) {
|
|
|
|
|
this.G.checkToken();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化定时器 - 合并检查任务
|
|
|
|
|
if (!that.globalData.timer) {
|
|
|
|
|
that.checkNum();
|
|
|
|
|
that.checkAndReconnectIM(); // 立即检查一次连接
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 缩短检查间隔到15秒,兼顾消息提醒和连接检查
|
|
|
|
|
that.globalData.timer = setInterval(() => {
|
|
|
|
|
that.checkNum();
|
|
|
|
|
@ -88,7 +90,7 @@ export default {
|
|
|
|
|
that.initWyyx();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 应用从后台返回时检查连接
|
|
|
|
|
if (uni.getStorageSync("apply-token")) {
|
|
|
|
|
// console.log("app.vue 检查IM连接状态");
|
|
|
|
|
@ -98,15 +100,17 @@ export default {
|
|
|
|
|
onHide: function () {
|
|
|
|
|
// 应用进入后台时不清除定时器,保持心跳
|
|
|
|
|
console.log("应用进入后台,保持IM连接检测");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 发送一次心跳包
|
|
|
|
|
if (uni.$UIKitNIM && uni.$UIKitNIM.V2NIMLoginService) {
|
|
|
|
|
uni.$UIKitNIM.V2NIMLoginService.ping().then(() => {
|
|
|
|
|
// console.log("后台心跳发送成功");
|
|
|
|
|
}).catch(err => {
|
|
|
|
|
// console.error("后台心跳发送失败,准备重连", err);
|
|
|
|
|
this.reconnectIM();
|
|
|
|
|
});
|
|
|
|
|
uni.$UIKitNIM.V2NIMLoginService.ping()
|
|
|
|
|
.then(() => {
|
|
|
|
|
// console.log("后台心跳发送成功");
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
// console.error("后台心跳发送失败,准备重连", err);
|
|
|
|
|
this.reconnectIM();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
onUnload() {
|
|
|
|
|
@ -116,7 +120,7 @@ export default {
|
|
|
|
|
this.globalData.timer = null;
|
|
|
|
|
console.log("定时器已清除");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (this.globalData.reconnectTimer) {
|
|
|
|
|
clearInterval(this.globalData.reconnectTimer);
|
|
|
|
|
this.globalData.reconnectTimer = null;
|
|
|
|
|
@ -131,7 +135,7 @@ export default {
|
|
|
|
|
if (uni.$UIKitNIM && uni.$UIKitStore) {
|
|
|
|
|
const connectStatus = uni.$UIKitStore.connectStore.connectStatus;
|
|
|
|
|
// console.log('当前IM连接状态:', connectStatus);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 连接状态码:
|
|
|
|
|
// 0: 初始状态
|
|
|
|
|
// 1: 已断开
|
|
|
|
|
@ -147,17 +151,17 @@ export default {
|
|
|
|
|
}
|
|
|
|
|
}, 1000);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 重新连接IM
|
|
|
|
|
reconnectIM() {
|
|
|
|
|
let that = this;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果已经在重连中,则不再重复发起
|
|
|
|
|
if (this.globalData.reconnectTimer) {
|
|
|
|
|
// console.log("正在重连中,无需重复操作");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 尝试重连
|
|
|
|
|
const attemptReconnect = () => {
|
|
|
|
|
if (!uni.getStorageSync("apply-token") || !uni.getStorageSync("apply-uid")) {
|
|
|
|
|
@ -166,11 +170,11 @@ export default {
|
|
|
|
|
that.globalData.reconnectTimer = null;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (uni.$UIKitNIM && uni.$UIKitNIM.V2NIMLoginService) {
|
|
|
|
|
const account = uni.getStorageSync("im-accid");
|
|
|
|
|
const token = uni.getStorageSync("im-token");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (account && token) {
|
|
|
|
|
console.log(`尝试重连IM (account: ${account})`);
|
|
|
|
|
uni.$UIKitNIM.V2NIMLoginService.login(account, token)
|
|
|
|
|
@ -191,10 +195,10 @@ export default {
|
|
|
|
|
that.initWyyx();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 立即尝试一次重连
|
|
|
|
|
// attemptReconnect();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置重连定时器,每5秒尝试一次,最多尝试10次
|
|
|
|
|
let reconnectAttempts = 0;
|
|
|
|
|
this.globalData.reconnectTimer = setInterval(() => {
|
|
|
|
|
@ -219,35 +223,35 @@ export default {
|
|
|
|
|
},
|
|
|
|
|
(res) => {
|
|
|
|
|
console.log("wyyx_getConfig", res);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查返回数据有效性
|
|
|
|
|
if (!res || !res.appKey || !res.accid || !res.token) {
|
|
|
|
|
console.error("获取IM配置失败,参数不完整");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let resData = {
|
|
|
|
|
appkey: res.appKey,
|
|
|
|
|
accid: res.accid,
|
|
|
|
|
token: res.token,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 保存IM配置信息,用于重连
|
|
|
|
|
uni.setStorageSync('im-appkey', resData.appkey);
|
|
|
|
|
uni.setStorageSync('im-accid', resData.accid);
|
|
|
|
|
uni.setStorageSync('im-token', resData.token);
|
|
|
|
|
|
|
|
|
|
uni.setStorageSync("im-appkey", resData.appkey);
|
|
|
|
|
uni.setStorageSync("im-accid", resData.accid);
|
|
|
|
|
uni.setStorageSync("im-token", resData.token);
|
|
|
|
|
|
|
|
|
|
const imOptions = {
|
|
|
|
|
appkey: resData.appkey,
|
|
|
|
|
account: resData.accid,
|
|
|
|
|
token: resData.token,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.initNim(imOptions);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async initNim(opts) {
|
|
|
|
|
let that = this;
|
|
|
|
|
// 初始化 nim sdk,增强重连配置
|
|
|
|
|
@ -409,9 +413,9 @@ export default {
|
|
|
|
|
that.playAudio();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
setTabUnread()
|
|
|
|
|
setTabUnread();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
playAudio() {
|
|
|
|
|
let that = this;
|
|
|
|
|
const innerAudioContext = uni.createInnerAudioContext();
|
|
|
|
|
@ -426,7 +430,7 @@ export default {
|
|
|
|
|
console.log("提示音播放错误:", res.errMsg, res.errCode);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
checkNum() {
|
|
|
|
|
let that = this;
|
|
|
|
|
if (uni.getStorageSync("apply-token")) {
|
|
|
|
|
|