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;
|
|
|
|
|
|
const account = ref(null);
|
|
|
|
|
|
const loading = ref(false)
|
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-22 16:35:05 +08:00
|
|
|
|
|
2026-01-19 18:52:18 +08:00
|
|
|
|
async function login(phoneCode = '') {
|
|
|
|
|
|
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', {
|
|
|
|
|
|
phoneCode,
|
|
|
|
|
|
code,
|
|
|
|
|
|
});
|
2026-01-21 13:37:54 +08:00
|
|
|
|
loading.value = false;
|
|
|
|
|
|
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 16:35:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
2026-01-22 15:13:26 +08:00
|
|
|
|
// 登录成功后初始化腾讯IM
|
|
|
|
|
|
try {
|
|
|
|
|
|
console.log('开始初始化腾讯IM,userID:', res.data.openid);
|
|
|
|
|
|
await initGlobalTIM(res.data.openid);
|
|
|
|
|
|
isIMInitialized.value = true;
|
|
|
|
|
|
console.log('腾讯IM初始化成功');
|
|
|
|
|
|
} catch (imError) {
|
|
|
|
|
|
console.error('腾讯IM初始化失败:', imError);
|
|
|
|
|
|
// IM初始化失败不影响登录流程
|
|
|
|
|
|
}
|
2026-01-22 16:35:05 +08:00
|
|
|
|
await getDoctorInfo(openid.value);
|
2026-01-19 18:52:18 +08:00
|
|
|
|
return res.data
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
toast('登录失败,请重新登录');
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
toast('登录失败,请重新登录');
|
|
|
|
|
|
}
|
|
|
|
|
|
loading.value = false
|
|
|
|
|
|
}
|
2026-01-22 16:35:05 +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
|
|
|
|
});
|
|
|
|
|
|
if (res.success && res.data) {
|
|
|
|
|
|
doctorInfo.value = res.data;
|
|
|
|
|
|
}
|
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
|
|
|
|
})
|