ykt-wxapp/store/account.js

64 lines
1.7 KiB
JavaScript

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 account = ref(null);
const loading = ref(false)
// IM 相关
const openid = ref("");
// 医生信息
const doctorInfo = ref(null);
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,
});
loading.value = false;
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('登录失败,请重新登录');
}
loading.value = false
}
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 }
})