ykt-team-wxapp/store/account.js

128 lines
3.8 KiB
JavaScript
Raw Normal View History

2026-02-04 09:16:36 +08:00
import { ref, watch } from "vue";
2026-01-20 19:36:49 +08:00
import { defineStore } from "pinia";
import api from '@/utils/api';
import { toast } from '@/utils/widget';
2026-01-28 13:38:05 +08:00
import { initGlobalTIM, globalTimChatManager } from "@/utils/tim-chat.js";
2026-01-20 19:36:49 +08:00
const env = __VITE_ENV__;
export default defineStore("accountStore", () => {
const appid = env.MP_WX_APP_ID;
2026-02-09 16:54:59 +08:00
const corpId = env.MP_CORP_ID;
2026-01-20 19:36:49 +08:00
const account = ref(null);
const loading = ref(false)
2026-01-28 13:38:05 +08:00
const isIMInitialized = ref(false);
const openid = ref("");
2026-02-04 09:16:36 +08:00
const externalUserId = ref('');
2026-01-28 13:38:05 +08:00
2026-02-04 09:16:36 +08:00
async function login(phoneCode = '') {
2026-01-20 19:36:49 +08:00
if (loading.value) return;
loading.value = true;
try {
const { code } = await uni.login({
appid,
provider: "weixin",
scope: "snsapi_base",
});
if (code) {
const res = await api('wxAppLogin', {
2026-02-04 09:16:36 +08:00
appId: appid,
2026-01-20 19:36:49 +08:00
phoneCode,
2026-02-09 16:54:59 +08:00
code,
corpId
2026-01-20 19:36:49 +08:00
});
loading.value = false
if (res.success && res.data && res.data.mobile) {
account.value = res.data;
2026-01-28 13:38:05 +08:00
openid.value = res.data.openid;
2026-02-04 09:16:36 +08:00
2026-01-29 11:42:56 +08:00
// 保存账户信息和 openId 到本地存储
uni.setStorageSync('account', res.data);
uni.setStorageSync('openid', res.data.openid);
2026-02-04 09:16:36 +08:00
2026-01-28 13:38:05 +08:00
initIMAfterLogin(openid.value)
2026-01-20 19:36:49 +08:00
return res.data
}
}
toast('登录失败,请重新登录');
} catch (e) {
toast('登录失败,请重新登录');
}
loading.value = false
}
2026-01-28 16:20:19 +08:00
async function initIMAfterLogin() {
2026-01-28 13:38:05 +08:00
if (isIMInitialized.value) {
return true;
}
try {
2026-01-29 11:42:56 +08:00
// 使用 openid 作为 userID 初始化 IM
const userID = openid.value || uni.getStorageSync('openid');
if (!userID) {
2026-02-09 09:18:33 +08:00
console.log('未获取到有效的 userId跳过 IM 初始化');
2026-01-29 11:42:56 +08:00
return false;
}
2026-02-04 09:16:36 +08:00
2026-01-29 11:42:56 +08:00
console.log('开始初始化 IMuserID:', userID);
const success = await initGlobalTIM(userID);
2026-02-04 09:16:36 +08:00
2026-01-29 11:42:56 +08:00
if (!success) {
2026-02-09 09:18:33 +08:00
console.log('initGlobalTIM 返回失败,跳过 IM 初始化');
2026-01-29 11:42:56 +08:00
return false;
}
2026-02-04 09:16:36 +08:00
2026-01-29 11:42:56 +08:00
// 验证 TIM 实例是否真正创建成功
if (!globalTimChatManager || !globalTimChatManager.tim) {
2026-02-09 09:18:33 +08:00
console.log('IM 初始化后 TIM 实例不存在,跳过 IM 初始化');
2026-01-29 11:42:56 +08:00
return false;
}
2026-02-04 09:16:36 +08:00
2026-01-28 13:38:05 +08:00
isIMInitialized.value = true;
2026-01-29 11:42:56 +08:00
console.log('IM 初始化成功');
2026-01-28 13:38:05 +08:00
return true;
} catch (error) {
2026-02-09 09:18:33 +08:00
console.log('IM初始化异常跳过 IM 初始化:', error.message);
2026-01-28 13:38:05 +08:00
return false;
}
}
// 退出登录
async function logout() {
try {
// 退出腾讯IM
if (globalTimChatManager && globalTimChatManager.tim) {
console.log('开始退出腾讯IM');
await globalTimChatManager.destroy();
console.log('腾讯IM退出成功');
}
} catch (error) {
console.error('退出腾讯IM失败:', error);
}
// 清空账户信息
account.value = null;
openid.value = "";
isIMInitialized.value = false;
2026-02-04 09:16:36 +08:00
2026-01-29 11:42:56 +08:00
// 清除本地存储
uni.removeStorageSync('account');
uni.removeStorageSync('openid');
2026-01-28 13:38:05 +08:00
}
2026-02-04 09:16:36 +08:00
async function getExternalUserId() {
const corpId = account.value?.corpId;
const unionid = account.value?.unionid;
const openid = account.value?.openid;
2026-02-09 16:54:59 +08:00
console.clear()
console.log('corpId', corpId, ',unionid', unionid, ',openid', openid)
2026-02-04 09:16:36 +08:00
if (!(corpId && unionid && openid) || externalUserId.value) return;
const res = await api('getUnionidToExternalUserid', { unionid, openid, corpId }, false);
if (res && res.success && typeof res.data === 'string' && res.data.trim()) {
externalUserId.value = res.data.trim();
}
}
watch(account, n => {
getExternalUserId()
}, { immediate: true })
return { account, login, initIMAfterLogin, logout, openid, isIMInitialized, externalUserId, getExternalUserId }
2026-01-20 19:36:49 +08:00
})