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

519 lines
14 KiB
Vue

5 months ago
<script lang="ts">
4 months ago
import RootStore from "@xkit-yx/im-store-v2";
import V2NIM, { V2NIMConst } from "nim-web-sdk-ng/dist/v2/NIM_UNIAPP_SDK";
import { getMsgContentTipByType } from "./pages/NEUIKit/utils/msg";
import { STORAGE_KEY } from "./pages/NEUIKit/utils/constants";
import { isWxApp } from "./pages/NEUIKit/utils";
3 months ago
import { V2NIMMessage, V2NIMMessagePushConfig, V2NIMConversation } from "nim-web-sdk-ng/dist/v2/NIM_UNIAPP_SDK/V2NIMMessageService";
4 months ago
import { unix } from "dayjs";
5 months ago
6 months ago
export default {
4 months ago
globalData: {
timer: null, // 定时器变量
3 months ago
reconnectTimer: null, // 重连专用定时器
4 months ago
themeColor: "#00b666",
themeBackgroundColor: "#00b66621",
},
3 months ago
onLaunch() {
4 months ago
let that = this;
uni.removeStorageSync("selectedCity");
3 months ago
3 months ago
// 监听路由跳转
uni.addInterceptor("navigateTo", {
invoke(args) {
3 months ago
// console.log("navigateTo 跳转前", args);
3 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);
},
});
4 months ago
},
onShow: function (options) {
let that = this;
console.log("show 项目init", options, decodeURIComponent(options.query.scene));
3 months ago
4 months ago
// 清除扫码获取的id信息
uni.removeStorageSync("scene");
uni.removeStorageSync("user_scene");
uni.removeStorageSync("user_options", options);
5 months ago
4 months ago
if (options.query.id) {
uni.setStorageSync("apply-jobdetail-id", options.query.id);
} else {
uni.setStorageSync("apply-jobdetail-id", 0);
}
5 months ago
4 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", "");
}
3 months ago
4 months ago
if (uni.getStorageSync("apply-token")) {
this.G.checkToken();
}
3 months ago
// 初始化定时器 - 合并检查任务
4 months ago
if (!that.globalData.timer) {
3 months ago
that.checkNum();
3 months ago
that.checkAndReconnectIM(); // 立即检查一次连接
// 缩短检查间隔到15秒兼顾消息提醒和连接检查
4 months ago
that.globalData.timer = setInterval(() => {
3 months ago
that.checkNum();
3 months ago
that.checkAndReconnectIM();
}, 15 * 1000);
4 months ago
}
5 months ago
4 months ago
uni.$on("isGlogin", function (data) {
3 months ago
// console.log("app.vue 接收", data);
4 months ago
if (uni.getStorageSync("apply-uid")) {
3 months ago
that.initWyyx();
4 months ago
}
});
3 months ago
// 应用从后台返回时检查连接
4 months ago
if (uni.getStorageSync("apply-token")) {
3 months ago
// console.log("app.vue 检查IM连接状态");
3 months ago
that.checkAndReconnectIM();
4 months ago
}
},
onHide: function () {
3 months ago
// 应用进入后台时不清除定时器,保持心跳
console.log("应用进入后台保持IM连接检测");
// 发送一次心跳包
if (uni.$UIKitNIM && uni.$UIKitNIM.V2NIMLoginService) {
uni.$UIKitNIM.V2NIMLoginService.ping().then(() => {
3 months ago
// console.log("后台心跳发送成功");
3 months ago
}).catch(err => {
3 months ago
// console.error("后台心跳发送失败,准备重连", err);
3 months ago
this.reconnectIM();
});
}
},
onUnload() {
// 页面卸载时才清除定时器
3 months ago
if (this.globalData.timer) {
clearInterval(this.globalData.timer);
this.globalData.timer = null;
3 months ago
console.log("定时器已清除");
}
if (this.globalData.reconnectTimer) {
clearInterval(this.globalData.reconnectTimer);
this.globalData.reconnectTimer = null;
3 months ago
}
},
4 months ago
methods: {
3 months ago
// 检查并重新连接IM
checkAndReconnectIM() {
let that = this;
// 延迟执行确保SDK初始化完成
setTimeout(() => {
if (uni.$UIKitNIM && uni.$UIKitStore) {
const connectStatus = uni.$UIKitStore.connectStore.connectStatus;
3 months ago
// console.log('当前IM连接状态:', connectStatus);
3 months ago
// 连接状态码:
// 0: 初始状态
// 1: 已断开
// 2: 已连接
// 3: 连接中
if (connectStatus !== 2 && connectStatus !== 3) {
3 months ago
// console.log("IM连接已断开尝试重连...");
3 months ago
that.reconnectIM();
}
} else if (uni.getStorageSync("apply-token") && uni.getStorageSync("apply-uid")) {
3 months ago
// console.log("IM SDK未初始化重新初始化...");
3 months ago
that.initWyyx();
}
}, 1000);
},
// 重新连接IM
reconnectIM() {
let that = this;
// 如果已经在重连中,则不再重复发起
if (this.globalData.reconnectTimer) {
3 months ago
// console.log("正在重连中,无需重复操作");
3 months ago
return;
}
// 尝试重连
const attemptReconnect = () => {
if (!uni.getStorageSync("apply-token") || !uni.getStorageSync("apply-uid")) {
3 months ago
// console.log("用户未登录,停止重连");
3 months ago
clearInterval(that.globalData.reconnectTimer);
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)
.then(() => {
3 months ago
// console.log("IM重连成功");
3 months ago
clearInterval(that.globalData.reconnectTimer);
that.globalData.reconnectTimer = null;
})
.catch((error) => {
3 months ago
// console.error("IM重连失败将再次尝试", error);
3 months ago
});
} else {
3 months ago
// console.log("重连信息不全重新初始化IM");
3 months ago
that.initWyyx();
}
} else {
console.log("SDK未初始化重新初始化IM");
that.initWyyx();
}
};
// 立即尝试一次重连
// attemptReconnect();
// 设置重连定时器每5秒尝试一次最多尝试10次
let reconnectAttempts = 0;
this.globalData.reconnectTimer = setInterval(() => {
reconnectAttempts++;
if (reconnectAttempts >= 10) {
3 months ago
// console.log("重连尝试次数已达上限,停止重连");
3 months ago
clearInterval(that.globalData.reconnectTimer);
that.globalData.reconnectTimer = null;
return;
}
attemptReconnect();
}, 5000);
},
4 months ago
initWyyx() {
let that = this;
5 months ago
4 months ago
that.F.wyyxPost(
that.api.wyyx_getConfig,
{
userId: uni.getStorageSync("apply-uid"),
},
(res) => {
console.log("wyyx_getConfig", res);
3 months ago
// 检查返回数据有效性
if (!res || !res.appKey || !res.accid || !res.token) {
console.error("获取IM配置失败参数不完整");
return;
}
4 months ago
let resData = {
appkey: res.appKey,
accid: res.accid,
token: res.token,
};
3 months ago
// 保存IM配置信息用于重连
uni.setStorageSync('im-appkey', resData.appkey);
uni.setStorageSync('im-accid', resData.accid);
uni.setStorageSync('im-token', resData.token);
4 months ago
const imOptions = {
3 months ago
appkey: resData.appkey,
account: resData.accid,
token: resData.token,
4 months ago
};
3 months ago
this.initNim(imOptions);
4 months ago
}
);
},
3 months ago
4 months ago
async initNim(opts) {
let that = this;
3 months ago
// 初始化 nim sdk增强重连配置
4 months ago
const nim = (uni.$UIKitNIM = V2NIM.getInstance(
{
appkey: opts.appkey,
needReconnect: true,
3 months ago
reconnectionAttempts: 10, // 增加重连次数
reconnectionDelay: 3000, // 重连间隔3秒
heartbeatInterval: 20000, // 缩短心跳间隔为20秒
debugLevel: "warn", // 提高日志级别便于调试
4 months ago
apiVersion: "v2",
enableV2CloudConversation: true,
},
{
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",
3 months ago
isFixedDeviceId: true, // 使用固定设备ID
autoLogin: true, // 允许自动登录
4 months ago
},
}
));
5 months ago
4 months ago
that.globalData.nim = nim;
5 months ago
4 months ago
// 初始化 store
const store = (uni.$UIKitStore = new RootStore(
nim,
{
addFriendNeedVerify: false,
p2pMsgReceiptVisible: true,
teamMsgReceiptVisible: true,
teamAgreeMode: V2NIMConst.V2NIMTeamAgreeMode.V2NIM_TEAM_AGREE_MODE_NO_AUTH,
sendMsgBefore: async (options: { msg: V2NIMMessage; conversationId: string; serverExtension?: Record<string, unknown> }) => {
const pushContent = getMsgContentTipByType({
text: options.msg.text,
messageType: options.msg.messageType,
});
const yxAitMsg = options.serverExtension ? options.serverExtension.yxAitMsg : { forcePushIDsList: "[]", needForcePush: false };
5 months ago
4 months ago
const { forcePushIDsList, needForcePush } = yxAitMsg
? // @ts-ignore
store.msgStore._formatExtAitToPushInfo(yxAitMsg, options.msg.text)
: { forcePushIDsList: "[]", needForcePush: false };
5 months ago
4 months ago
console.log("forcePushIDsList: ", forcePushIDsList);
6 months ago
4 months ago
const { conversationId } = options;
const conversationType = nim.V2NIMConversationIdUtil.parseConversationType(conversationId);
const targetId = nim.V2NIMConversationIdUtil.parseConversationTargetId(conversationId);
6 months ago
4 months ago
const pushPayload = JSON.stringify({
oppoField: {
3 months ago
click_action_type: 4,
click_action_activity: "",
4 months ago
action_parameters: {
sessionId: targetId,
sessionType: conversationType,
3 months ago
},
4 months ago
},
vivoField: {
3 months ago
pushMode: 0,
4 months ago
},
hwField: {
click_action: {
type: 1,
3 months ago
action: "",
4 months ago
},
androidConfig: {
category: "IM",
data: JSON.stringify({
sessionId: targetId,
sessionType: conversationType,
}),
},
},
sessionId: targetId,
sessionType: conversationType,
});
5 months ago
4 months ago
const pushConfig: V2NIMMessagePushConfig = {
pushEnabled: true,
pushNickEnabled: true,
forcePush: needForcePush,
forcePushContent: pushContent,
forcePushAccountIds: forcePushIDsList,
pushPayload: "{}",
pushContent,
};
5 months ago
4 months ago
return { ...options, pushConfig };
},
},
"UniApp"
));
5 months ago
3 months ago
// 监听连接状态变化
nim.V2NIMLoginService.on("onConnectStatusChanged", (status) => {
console.log("IM连接状态变化:", status);
// 状态1表示已断开连接
if (status === 1) {
console.log("检测到连接断开,触发重连");
that.reconnectIM();
}
});
4 months ago
// #ifdef APP-PLUS
const nimPushPlugin = uni.requireNativePlugin("NIMUniPlugin-PluginModule");
nim.V2NIMSettingService.setOfflinePushConfig(nimPushPlugin, {
miPush: {
appId: "2882303761520397320",
appKey: "5102039734320",
certificateName: "xiaomiCertificateName",
},
vivoPush: {
certificateName: "vivoCertificateName",
},
oppoPush: {
appId: "32967155",
appKey: "b98618cb272944dea324af6421d42a79",
secret: "0b7ce705a2304a17b78f20011c18890c",
certificateName: "opopCertificateName",
},
honorPush: {
certificateName: "rongyaoCertificateName",
},
apns: {
3 months ago
certificateName: "AuthKey_98P8URRXUD",
4 months ago
},
});
3 months ago
console.log("推送配置已设置");
4 months ago
// #endif
5 months ago
3 months ago
// 登录IM
nim.V2NIMLoginService.login(opts.account, opts.token)
.then(() => {
3 months ago
// console.log("IM登录成功");
3 months ago
// 清除重连定时器
if (that.globalData.reconnectTimer) {
clearInterval(that.globalData.reconnectTimer);
that.globalData.reconnectTimer = null;
}
})
.catch((error) => {
3 months ago
// console.error("IM登录失败", error);
3 months ago
// 登录失败时启动重连机制
that.reconnectIM();
});
5 months ago
3 months ago
// 监听会话变化
4 months ago
nim.V2NIMConversationService.on("onConversationChanged", function (conversationList: V2NIMConversation[]) {
3 months ago
// console.log("初始化监听会话:", conversationList);
3 months ago
let unreadConversations = conversationList.filter((item) => {
4 months ago
return Number(item.unreadCount) > 0;
});
3 months ago
// console.log("未读会话数量:", unreadConversations.length);
3 months ago
if (unreadConversations.length > 0) {
that.playAudio();
4 months ago
}
});
},
3 months ago
4 months ago
playAudio() {
let that = this;
const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.autoplay = true;
innerAudioContext.src = "https://matripe-cms.oss-cn-beijing.aliyuncs.com/ibocai/tip.mp3";
innerAudioContext.onPlay(() => {
3 months ago
console.log("开始播放提示音");
4 months ago
});
uni.vibrateShort();
5 months ago
4 months ago
innerAudioContext.onError((res) => {
3 months ago
console.log("提示音播放错误:", res.errMsg, res.errCode);
4 months ago
});
},
3 months ago
4 months ago
checkNum() {
let that = this;
if (uni.getStorageSync("apply-token")) {
that.G.Get(that.api.bind_getApplyNum, {}, (res) => {
console.log("获取待处理数量:", res);
3 months ago
that.G.Get(that.api.person_applyNum, {}, (applyNum) => {
if (res.approvePassHasNotRed + applyNum > 0) {
uni.setTabBarBadge({
index: 2,
3 months ago
text: String(res.approvePassHasNotRed + applyNum),
3 months ago
fail(err) {
3 months ago
console.log("设置tabBar徽章失败:", err);
3 months ago
},
});
} else {
uni.removeTabBarBadge({
index: 2,
fail(err) {
3 months ago
console.log("移除tabBar徽章失败:", err);
3 months ago
},
});
}
});
4 months ago
});
}
},
},
};
6 months ago
</script>
<style lang="scss">
4 months ago
@import "./static/css/iconfont.css";
@import "./uni_modules/vk-uview-ui/index.scss";
@import "./static/css/base.scss";
6 months ago
// #ifdef MP-WEIXIN
page {
4 months ago
width: 100vw;
height: 100vh;
6 months ago
}
// #endif
3 months ago
6 months ago
// #ifdef H5 || APP-PLUS || MP-TOUTIAO || MP-KUAISHOU
page {
4 months ago
width: 100vw;
height: calc(100% - 0px);
--color-ysd: #ff4400;
--color-hover: #ff4400cc;
--color-be: #ff4400;
--color-027: #ff4400;
--color-href: #576b95;
6 months ago
}
// #endif
// uview默认样式覆盖
.u-badge-mini {
4 months ago
top: 22rpx !important;
right: 102rpx !important;
color: transparent !important;
width: 10px !important;
height: 10px !important;
border-radius: 50% !important;
6 months ago
}
.u-radio {
4 months ago
&:last-child {
.u-radio__label {
margin-right: 0;
}
}
6 months ago
}
.g-apply-tab {
4 months ago
.u-tab-bar {
bottom: -2px !important;
}
6 months ago
}
5 months ago
.g_bg_f_5 {
4 months ago
background-color: #ededed !important;
5 months ago
}
5 months ago
.g_mt_30 {
4 months ago
margin-top: 30px;
5 months ago
}
5 months ago
.g_mt_84 {
4 months ago
margin-top: 84px;
5 months ago
}
5 months ago
.g_pt_90 {
4 months ago
padding-top: 90px;
5 months ago
}
6 months ago
</style>