import { ref } from "vue"; import { defineStore } from "pinia"; import api from '@/utils/api'; import { toast } from '@/utils/widget'; import { getInitIMPromise, clearInitIMPromise } from "@/utils/tim-chat.js"; const env = __VITE_ENV__; export default defineStore("accountStore", () => { const appid = env.MP_WX_APP_ID; const corpId = env.MP_CORP_ID; const account = ref(null); const loading = ref(false); const loginPromise = ref(null); // IM 相关 const openid = ref(""); // 医生信息 const doctorInfo = ref(null); function getLoginPromise(phoneCode = '') { if (loginPromise.value) return loginPromise.value; loginPromise.value = loginByCode(phoneCode); return loginPromise.value; } async function login(phoneCode) { await getLoginPromise(phoneCode); loginPromise.value = null; } async function loginByCode(phoneCode = '') { try { const { code } = await uni.login({ appid, provider: "weixin", scope: "snsapi_base", }); if (code) { const res = await api('wxAppLogin', { phoneCode, code, corpId, }); if (res.success && res.data) { if (!res.data.mobile) { const target = '/pages/login/login'; uni.redirectTo({ url: target }); return; } account.value = res.data; openid.value = res.data.openid; await getDoctorInfo(openid.value); return res.data } } toast('登录失败,请重新登录'); } catch (e) { toast('登录失败,请重新登录'); } return Promise.reject() } async function getDoctorInfo() { try { const res = await api('getCorpMemberData', { weChatOpenId: account.value.openid, }); if (res.success && res.data) { doctorInfo.value = res.data; } } catch (e) { console.error('获取医生信息失败:', e); } } return { account, openid, doctorInfo, login, getDoctorInfo } })