ykt-wxapp/store/account.js

122 lines
3.3 KiB
JavaScript
Raw Normal View History

2026-01-19 18:52:18 +08:00
import { ref } from "vue";
import { defineStore } from "pinia";
import api from '@/utils/api';
import { toast } from '@/utils/widget';
2026-01-22 15:13:26 +08:00
import { initGlobalTIM, globalTimChatManager } from "@/utils/tim-chat.js";
2026-01-19 18:52:18 +08:00
const env = __VITE_ENV__;
export default defineStore("accountStore", () => {
const appid = env.MP_WX_APP_ID;
2026-01-23 14:36:28 +08:00
const corpId = env.MP_CORP_ID;
2026-01-19 18:52:18 +08:00
const account = ref(null);
2026-01-23 14:36:28 +08:00
const loading = ref(false);
const loginPromise = ref(null);
2026-01-20 13:21:50 +08:00
// IM 相关
const openid = ref("");
2026-01-22 15:13:26 +08:00
const isIMInitialized = ref(false);
2026-01-21 13:37:54 +08:00
// 医生信息
const doctorInfo = ref(null);
2026-01-23 14:36:28 +08:00
function getLoginPromise(phoneCode = '') {
if (loginPromise.value) return loginPromise.value;
loginPromise.value = loginByCode(phoneCode);
return loginPromise.value;
}
async function login(phoneCode) {
2026-01-23 18:08:14 +08:00
const res = await getLoginPromise(phoneCode);
2026-01-23 14:36:28 +08:00
loginPromise.value = null;
2026-01-23 18:08:14 +08:00
return res
2026-01-23 14:36:28 +08:00
}
async function loginByCode(phoneCode = '') {
2026-01-19 18:52:18 +08:00
try {
const { code } = await uni.login({
appid,
provider: "weixin",
scope: "snsapi_base",
});
if (code) {
const res = await api('wxAppLogin', {
phoneCode,
code,
2026-01-23 14:36:28 +08:00
corpId,
2026-01-19 18:52:18 +08:00
});
2026-01-21 13:37:54 +08:00
if (res.success && res.data) {
if (!res.data.mobile) {
2026-01-21 15:27:18 +08:00
const target = '/pages/login/login';
2026-01-21 13:37:54 +08:00
uni.redirectTo({ url: target });
return;
2026-01-20 16:30:03 +08:00
}
2026-01-21 13:37:54 +08:00
account.value = res.data;
openid.value = res.data.openid;
2026-01-22 15:13:26 +08:00
// 登录成功后初始化腾讯IM
2026-01-27 17:09:31 +08:00
try {
console.log('开始初始化腾讯IMuserID:', res.data.openid);
await initGlobalTIM(res.data.openid);
isIMInitialized.value = true;
console.log('腾讯IM初始化成功');
} catch (imError) {
console.error('腾讯IM初始化失败:', imError);
// IM初始化失败不影响登录流程
}
2026-01-21 13:37:54 +08:00
await getDoctorInfo(openid.value);
2026-01-19 18:52:18 +08:00
return res.data
}
}
toast('登录失败,请重新登录');
2026-01-23 14:36:28 +08:00
2026-01-19 18:52:18 +08:00
} catch (e) {
toast('登录失败,请重新登录');
}
2026-01-23 14:36:28 +08:00
return Promise.reject()
2026-01-19 18:52:18 +08:00
}
2026-01-23 14:36:28 +08:00
2026-01-21 15:27:18 +08:00
async function getDoctorInfo() {
2026-01-20 13:21:50 +08:00
try {
2026-01-21 13:37:54 +08:00
const res = await api('getCorpMemberData', {
2026-01-21 15:27:18 +08:00
weChatOpenId: account.value.openid,
2026-01-21 13:37:54 +08:00
});
2026-01-27 17:09:31 +08:00
doctorInfo.value = res?.data || null;
2026-01-20 13:21:50 +08:00
} catch (e) {
2026-01-21 13:37:54 +08:00
console.error('获取医生信息失败:', e);
2026-01-20 13:21:50 +08:00
}
}
2026-01-22 15:13:26 +08:00
async function initIMAfterLogin(userID) {
if (isIMInitialized.value) {
return true;
}
try {
await initGlobalTIM(userID);
isIMInitialized.value = true;
return true;
} catch (error) {
console.error('IM初始化失败:', error);
return false;
}
}
2026-01-22 16:35:05 +08:00
2026-01-22 15:13:26 +08:00
// 退出登录
async function logout() {
try {
// 退出腾讯IM
if (globalTimChatManager && globalTimChatManager.tim) {
console.log('开始退出腾讯IM');
await globalTimChatManager.destroy();
console.log('腾讯IM退出成功');
}
} catch (error) {
console.error('退出腾讯IM失败:', error);
}
2026-01-22 16:35:05 +08:00
2026-01-22 15:13:26 +08:00
// 清空账户信息
account.value = null;
openid.value = "";
isIMInitialized.value = false;
doctorInfo.value = null;
}
return { account, openid, isIMInitialized, doctorInfo, login, getDoctorInfo, initIMAfterLogin, logout }
2026-01-19 18:52:18 +08:00
})