You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
apply-assistant-v3/App.vue

567 lines
17 KiB
Vue

11 months ago
<script lang="ts">
2 weeks ago
import { V2NIMMessage, V2NIMMessagePushConfig, V2NIMConversation } from "nim-web-sdk-ng/dist/v2/NIM_UNIAPP_SDK/V2NIMMessageService";
10 months ago
import { unix } from "dayjs";
2 weeks ago
import { V2NIMConst, NIM } from "./utils/nim";
let isWxApp = uni.getAppBaseInfo().uniPlatform == "mp-weixin";
11 months ago
12 months ago
export default {
10 months ago
globalData: {
timer: null, // 定时器变量
9 months ago
reconnectTimer: null, // 重连专用定时器
10 months ago
themeColor: "#00b666",
themeBackgroundColor: "#00b66621",
},
9 months ago
onLaunch() {
10 months ago
let that = this;
uni.removeStorageSync("selectedCity");
9 months ago
9 months ago
// 监听路由跳转
uni.addInterceptor("navigateTo", {
invoke(args) {
9 months ago
// console.log("navigateTo 跳转前", args);
9 months ago
that.G.watchUserPage(args.url);
},
});
uni.addInterceptor("reLaunch", {
invoke(args) {
console.log("reLaunch 跳转前", args);
that.G.watchUserPage(args.url);
},
});
uni.addInterceptor("redirectTo", {
invoke(args) {
console.log("redirectTo 跳转前", args);
that.G.watchUserPage(args.url);
},
});
5 months ago
// 应用从后台返回时检查连接
if (uni.getStorageSync("apply-token")) {
// console.log("app.vue 检查IM连接状态");
that.checkAndReconnectIM();
}
5 months ago
uni.$on("isGlogin", function (data) {
// console.log("app.vue 接收", data);
if (uni.getStorageSync("apply-token")) {
2 months ago
// that.initWyyx();
5 months ago
}
});
10 months ago
},
onShow: function (options) {
let that = this;
console.log("show 项目init", options, decodeURIComponent(options.query.scene));
9 months ago
let requestComeFrom = this.G.globalConstantData.requestComeFrom;
6 months ago
uni.setStorageSync("requestComeFrom", requestComeFrom && (requestComeFrom == "assistant_miniapp" || requestComeFrom == "supply_chain_miniapp") ? "isToB" : "isToC");
9 months ago
10 months ago
// 清除扫码获取的id信息
uni.removeStorageSync("scene");
uni.removeStorageSync("user_scene");
uni.removeStorageSync("user_options", options);
11 months ago
10 months ago
if (options.query.id) {
uni.setStorageSync("apply-jobdetail-id", options.query.id);
} else {
uni.setStorageSync("apply-jobdetail-id", 0);
}
11 months ago
10 months ago
if (options.query.scene) {
let str = decodeURIComponent(options.query.scene);
uni.setStorageSync(
"apply-invite-code",
JSON.stringify({
key: str.split("=")[0],
value: str.split("=")[1],
})
);
} else {
uni.setStorageSync("apply-invite-code", "");
}
9 months ago
10 months ago
if (uni.getStorageSync("apply-token")) {
this.G.checkToken();
}
9 months ago
9 months ago
// 初始化定时器 - 合并检查任务
10 months ago
if (!that.globalData.timer) {
9 months ago
that.checkNum();
9 months ago
that.checkAndReconnectIM(); // 立即检查一次连接
9 months ago
9 months ago
// 缩短检查间隔到15秒兼顾消息提醒和连接检查
10 months ago
that.globalData.timer = setInterval(() => {
9 months ago
that.checkNum();
9 months ago
that.checkAndReconnectIM();
}, 15 * 1000);
10 months ago
}
11 months ago
6 months ago
const updateManager = uni.getUpdateManager();
updateManager.onCheckForUpdate(function (res) {
console.log("检测是否有新版本:", res.hasUpdate);
});
updateManager.onUpdateReady(function (res) {
uni.showModal({
title: "更新提示",
content: "新版本已经准备好,需重启应用",
showCancel: false,
success(res) {
if (res.confirm) {
console.log("新版本已经准备好,需重启应用");
updateManager.applyUpdate();
}
},
});
});
updateManager.onUpdateFailed(function (res) {
uni.showModal({
title: "更新提示",
content: "新版本下载异常,需重启应用",
showCancel: false,
success(res) {
if (res.confirm) {
console.log("新版本下载异常,需重启应用");
updateManager.applyUpdate();
}
},
});
});
10 months ago
},
10 months ago
onHide: function () {
9 months ago
// 应用进入后台时不清除定时器,保持心跳
console.log("应用进入后台保持IM连接检测");
9 months ago
9 months ago
// 发送一次心跳包
if (uni.$UIKitNIM && uni.$UIKitNIM.V2NIMLoginService) {
9 months ago
uni.$UIKitNIM.V2NIMLoginService.ping()
.then(() => {
// console.log("后台心跳发送成功");
})
.catch((err) => {
// console.error("后台心跳发送失败,准备重连", err);
this.reconnectIM();
});
9 months ago
}
},
onUnload() {
// 页面卸载时才清除定时器
9 months ago
if (this.globalData.timer) {
clearInterval(this.globalData.timer);
this.globalData.timer = null;
9 months ago
console.log("定时器已清除");
}
9 months ago
9 months ago
if (this.globalData.reconnectTimer) {
clearInterval(this.globalData.reconnectTimer);
this.globalData.reconnectTimer = null;
9 months ago
}
},
10 months ago
methods: {
9 months ago
// 检查并重新连接IM
checkAndReconnectIM() {
let that = this;
// 延迟执行确保SDK初始化完成
setTimeout(() => {
if (uni.$UIKitNIM && uni.$UIKitStore) {
const connectStatus = uni.$UIKitStore.connectStore.connectStatus;
9 months ago
// console.log('当前IM连接状态:', connectStatus);
9 months ago
9 months ago
// 连接状态码:
// 0: 初始状态
// 1: 已断开
// 2: 已连接
// 3: 连接中
if (connectStatus !== 2 && connectStatus !== 3) {
9 months ago
// console.log("IM连接已断开尝试重连...");
9 months ago
that.reconnectIM();
}
5 months ago
} else if (uni.getStorageSync("apply-token") ) {
9 months ago
// console.log("IM SDK未初始化重新初始化...");
2 weeks ago
that.initWyyx();
9 months ago
}
}, 1000);
},
9 months ago
9 months ago
// 重新连接IM
reconnectIM() {
let that = this;
9 months ago
9 months ago
// 如果已经在重连中,则不再重复发起
if (this.globalData.reconnectTimer) {
9 months ago
// console.log("正在重连中,无需重复操作");
9 months ago
return;
}
9 months ago
9 months ago
// 尝试重连
const attemptReconnect = () => {
if (!uni.getStorageSync("apply-token") || !uni.getStorageSync("apply-uid")) {
9 months ago
// console.log("用户未登录,停止重连");
9 months ago
clearInterval(that.globalData.reconnectTimer);
that.globalData.reconnectTimer = null;
return;
}
9 months ago
9 months ago
if (uni.$UIKitNIM && uni.$UIKitNIM.V2NIMLoginService) {
const account = uni.getStorageSync("im-accid");
const token = uni.getStorageSync("im-token");
9 months ago
9 months ago
if (account && token) {
console.log(`尝试重连IM (account: ${account})`);
uni.$UIKitNIM.V2NIMLoginService.login(account, token)
.then(() => {
9 months ago
// console.log("IM重连成功");
9 months ago
clearInterval(that.globalData.reconnectTimer);
that.globalData.reconnectTimer = null;
})
.catch((error) => {
9 months ago
// console.error("IM重连失败将再次尝试", error);
9 months ago
});
} else {
9 months ago
// console.log("重连信息不全重新初始化IM");
2 weeks ago
that.initWyyx();
9 months ago
}
} else {
console.log("SDK未初始化重新初始化IM");
2 weeks ago
that.initWyyx();
9 months ago
}
};
9 months ago
9 months ago
// 立即尝试一次重连
// attemptReconnect();
9 months ago
9 months ago
// 设置重连定时器每5秒尝试一次最多尝试10次
let reconnectAttempts = 0;
this.globalData.reconnectTimer = setInterval(() => {
reconnectAttempts++;
if (reconnectAttempts >= 10) {
9 months ago
// console.log("重连尝试次数已达上限,停止重连");
9 months ago
clearInterval(that.globalData.reconnectTimer);
that.globalData.reconnectTimer = null;
return;
}
attemptReconnect();
9 months ago
}, 50000);
9 months ago
},
10 months ago
initWyyx() {
2 weeks ago
let that = this;
that.F.wyyxPost(
that.api.wyyx_getConfig,
{
userId: uni.getStorageSync("apply-uid"),
},
(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);
const imOptions = {
appkey: resData.appkey,
account: resData.accid,
token: resData.token,
};
this.initNim(imOptions);
},
);
10 months ago
},
2 weeks ago
async initNim(opts) {
let that = this;
const account = opts.account;
const token = opts.token;
const appkey = opts.appkey;
const params1 = {
appkey: appkey,
debugLevel: "warning",
apiVersion: "v2",
readReceiptEnabled: true,
enableV2CloudConversation: true,
10 months ago
};
2 weeks ago
console.log("ems NIM", NIM);
const nim = NIM.getInstance(params1, {
V2NIMLoginServiceConfig: {
lbsUrls: isWxApp ? ["https://lbs.netease.im/lbs/wxwebconf.jsp"] : ["https://lbs.netease.im/lbs/webconf.jsp"],
linkUrl: isWxApp ? "wlnimsc0.netease.im" : "weblink.netease.im",
isFixedDeviceId: true,
autoLogin: true,
},
});
this.offWatchIM(nim);
this.watchIMLoginStatus(nim);
this.watchIMFriendChange(nim);
this.watchIMMessageChange(nim);
try {
await nim.V2NIMLoginService.login(account, token, {
forceMode: false,
timeout: 60000,
retryCount: 3,
authType: 0,
10 months ago
});
2 weeks ago
that.globalData.nim = nim;
that.G.nim = nim;
} catch (err) {
console.error("error", err);
}
10 months ago
},
2 weeks ago
/**
* IM信息更新监听回调
*/
watchIMMessageChange(nim) {
let that = this;
// 消息接收回调
nim.V2NIMMessageService.on("onReceiveMessages", function (messages) {
console.log("收到 V2NIMMessageService 模块的 onReceiveMessages 事件", messages);
let message = messages[0];
uni.$emit("updateMessageList", { ...message, messageFrom: "yunxin" });
});
// 发送消息接收监听
nim.V2NIMMessageService.on("onSendMessage", function (messages) {
console.log("收到 V2NIMMessageService 模块的 onSendMessage 事件", messages);
});
// 监听单聊已读回执
nim.V2NIMMessageService.on("onReceiveP2PMessageReadReceipts", function (readReceipts) {
console.log("收到 V2NIMMessageService 模块的 onReceiveP2PMessageReadReceipts 事件", readReceipts);
uni.$emit("updateMessageReadInfo", readReceipts[0]);
});
// 撤回成功回调
nim.V2NIMMessageService.on("onMessageRevokeNotifications", function (messages) {
console.log("收到 V2NIMMessageService 模块的 onMessageRevokeNotifications 事件", messages);
});
// 修改消息字段的回调
nim.V2NIMMessageService.on("onReceiveMessagesModified", function (messages) {
console.log("收到 V2NIMMessageService 模块的 onReceiveMessagesModified 事件", messages);
uni.$emit("updateMessage", messages);
});
// 会话列表变化改变
nim.V2NIMConversationService.on("onConversationChanged", function (conversationList: V2NIMConversation[]) {
console.log("初始化监听会话:", conversationList);
uni.$emit("updateConversationList");
let unreadConversations = conversationList.filter((item) => {
return Number(item.unreadCount) > 0;
});
console.log("未读会话数量:", unreadConversations);
if (unreadConversations.length > 0) {
that.playAudio();
}
});
// 未读消息总数变化
nim.V2NIMConversationService.on("onTotalUnreadCountChanged", function (messages) {
console.log("收到 V2NIMMessageService 模块的 onTotalUnreadCountChanged 事件", messages);
uni.$emit("newMessage", { count: messages, type: "message" });
uni.setStorageSync("chat_info", { count: messages, type: "message" });
});
// 删除回话的监听
nim.V2NIMConversationService.on("onConversationDeleted", function (conversationIds) {
uni.$emit("deleteConversationList", conversationIds);
console.log("conversationIds", conversationIds);
});
10 months ago
},
2 weeks ago
offWatchIM(nim) {
// 信息监听
nim.V2NIMMessageService.off("onReceiveMessages");
nim.V2NIMMessageService.off("onMessageRevokeNotifications");
nim.V2NIMMessageService.off("onReceiveMessagesModified");
nim.V2NIMConversationService.off("onConversationChanged");
nim.V2NIMConversationService.off("onConversationDeleted");
nim.V2NIMConversationService.off("onReceiveP2PMessageReadReceipts");
// 好友信息监听
nim.V2NIMFriendService.off("onFriendAdded");
nim.V2NIMFriendService.off("onFriendDeleted");
nim.V2NIMFriendService.off("onFriendAddApplication");
nim.V2NIMFriendService.off("onFriendAddRejected");
nim.V2NIMFriendService.off("onFriendInfoChanged");
10 months ago
},
2 weeks ago
/**
* IM好友信息更新监听回调
*/
watchIMFriendChange(nim) {
// 添加好友成功回调,返回添加成功的好友信息列表
nim.V2NIMFriendService.on("onFriendAdded", function (friend) {
console.log("watchIMFriend", friend);
});
// 删除好友回调,返回删除的好友信息
nim.V2NIMFriendService.on("onFriendDeleted", function (accountId, deletionType) {
console.log("watchIMFriend", accountId);
console.log("watchIMFriend", deletionType);
});
// 申请添加好友回调,返回申请添加为好友的信息
nim.V2NIMFriendService.on("onFriendAddApplication", function (application) {
console.log("watchIMFriend", application);
});
// 被对方拒绝好友添加申请的回调,被拒绝的好友申请信息
nim.V2NIMFriendService.on("onFriendAddRejected", function (rejection) {
console.log("watchIMFriend", rejection);
});
// 好友信息更新回调,返回变更的好友信息
nim.V2NIMFriendService.on("onFriendInfoChanged", function (friend) {
console.log("watchIMFriend", friend);
});
10 months ago
},
2 weeks ago
/**
* IM登录状态监听回调
*/
watchIMLoginStatus(nim) {
/**
* 登录瞬时状态监听
*/
// 登录状态变化回调
nim.V2NIMLoginService.on("onLoginStatus", function (arg1) {
console.log("收到 V2NIMLoginService 模块的 onLoginStatus 事件", arg1);
});
// 登录失败回调
nim.V2NIMLoginService.on("onLoginFailed", function (arg1) {
console.log("收到 V2NIMLoginService 模块的 onLoginFailed 事件", arg1);
});
// 登录终端被其他端踢下线回调
nim.V2NIMLoginService.on("onKickedOffline", function (arg1) {
console.log("收到 V2NIMLoginService 模块的 onKickedOffline 事件", arg1);
});
// 登录终端登录信息变更回调
nim.V2NIMLoginService.on("onLoginClientChanged", function (arg1) {
console.log("收到 V2NIMLoginService 模块的 onLoginClientChanged 事件", arg1);
});
/**
* 登录持续性状态监听
*/
// 登录连接状态变化回调
nim.V2NIMLoginService.on("onConnectStatus", function (arg1) {
console.log("收到 V2NIMLoginService 模块的 onConnectStatus 事件", arg1);
});
// 登录连接断开回调
nim.V2NIMLoginService.on("onDisconnected", function (arg1) {
console.log("收到 V2NIMLoginService 模块的 onDisconnected 事件", arg1);
});
// 登录连接失败回调
nim.V2NIMLoginService.on("onConnectFailed", function (arg1) {
console.log("收到 V2NIMLoginService 模块的 onConnectFailed 事件", arg1);
});
// 数据同步状态变化回调
nim.V2NIMLoginService.on("onDataSync", function (arg1) {
console.log("收到 V2NIMLoginService 模块的 onDataSync 事件", arg1);
});
10 months ago
},
2 weeks ago
playAudio() {
let that = this;
const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.autoplay = true;
innerAudioContext.src = "https://bocai-cms.oss-cn-beijing.aliyuncs.com/web-resource/ibocai/tip.mp3";
innerAudioContext.onPlay(() => {
// console.log("开始播放提示音");
});
uni.vibrateShort();
innerAudioContext.onError((res) => {
// console.log("提示音播放错误:", res.errMsg, res.errCode);
});
10 months ago
},
checkNum() {
let that = this;
if (uni.getStorageSync("apply-token")) {
that.G.Get(that.api.bind_getApplyNum, {}, (res) => {
2 months ago
// console.log("获取待处理数量:", res);
9 months ago
that.G.Get(that.api.person_applyNum, {}, (applyNum) => {
if (res.approvePassHasNotRed + applyNum > 0) {
uni.setTabBarBadge({
index: 2,
9 months ago
text: String(res.approvePassHasNotRed + applyNum),
9 months ago
fail(err) {
9 months ago
console.log("设置tabBar徽章失败:", err);
9 months ago
},
});
} else {
uni.removeTabBarBadge({
index: 2,
fail(err) {
9 months ago
console.log("移除tabBar徽章失败:", err);
9 months ago
},
});
}
});
10 months ago
});
}
},
},
};
12 months ago
</script>
<style lang="scss">
10 months ago
@import "./static/css/iconfont.css";
@import "./uni_modules/vk-uview-ui/index.scss";
@import "./static/css/base.scss";
12 months ago
// #ifdef MP-WEIXIN
page {
10 months ago
width: 100vw;
height: 100vh;
12 months ago
}
// #endif
9 months ago
12 months ago
// #ifdef H5 || APP-PLUS || MP-TOUTIAO || MP-KUAISHOU
page {
10 months ago
width: 100vw;
height: calc(100% - 0px);
--color-ysd: #ff4400;
--color-hover: #ff4400cc;
--color-be: #ff4400;
--color-027: #ff4400;
--color-href: #576b95;
12 months ago
}
// #endif
// uview默认样式覆盖
.u-badge-mini {
10 months ago
top: 22rpx !important;
right: 102rpx !important;
color: transparent !important;
width: 10px !important;
height: 10px !important;
border-radius: 50% !important;
12 months ago
}
.u-radio {
10 months ago
&:last-child {
.u-radio__label {
margin-right: 0;
}
}
12 months ago
}
.g-apply-tab {
10 months ago
.u-tab-bar {
bottom: -2px !important;
}
12 months ago
}
11 months ago
.g_bg_f_5 {
10 months ago
background-color: #ededed !important;
11 months ago
}
11 months ago
.g_mt_30 {
10 months ago
margin-top: 30px;
11 months ago
}
11 months ago
.g_mt_84 {
10 months ago
margin-top: 84px;
11 months ago
}
11 months ago
.g_pt_90 {
10 months ago
padding-top: 90px;
11 months ago
}
5 months ago
5 months ago
.g-rh-login-false-list {
.bocai_btn_type_primary {
5 months ago
background-color: #00b666 !important;
border: 1px solid #00b666 !important;
}
}
12 months ago
</style>