ykt-wxapp/store/account.js

64 lines
1.7 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-20 13:21:50 +08:00
import { getInitIMPromise, clearInitIMPromise } 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-21 13:37:54 +08:00
// 医生信息
const doctorInfo = ref(null);
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;
await getDoctorInfo(openid.value);
2026-01-19 18:52:18 +08:00
return res.data
}
}
toast('登录失败,请重新登录');
} catch (e) {
toast('登录失败,请重新登录');
}
loading.value = false
}
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-21 15:27:18 +08:00
return { account, openid, doctorInfo, login, getDoctorInfo }
2026-01-19 18:52:18 +08:00
})