ykt-wxapp/store/account.js

76 lines
2.2 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 = '') {
debugger
if (loading.value) return;
loading.value = true;
try {
const { code } = await uni.login({
appid,
provider: "weixin",
scope: "snsapi_base",
});
console.log('logincode: ', code)
if (code) {
const res = await api('wxAppLogin', {
phoneCode,
code,
});
loading.value = false;
if (res.success && res.data) {
if (!res.data.mobile) {
const pages = getCurrentPages();
const current = pages[pages.length - 1];
const params = current && current.options
? Object.keys(current.options).map(key => `${key}=${current.options[key]}`).join('&')
: '';
const redirectUrl = current && current.route ? `/${current.route}${params ? `?${params}` : ''}` : '';
const target = redirectUrl ? `/pages/login/login?redirect=${encodeURIComponent(redirectUrl)}` : '/pages/login/login';
uni.redirectTo({ url: target });
return;
}
account.value = res.data;
openid.value = res.data.openid;
debugger;
await getDoctorInfo(openid.value);
return res.data
}
}
toast('登录失败,请重新登录');
} catch (e) {
toast('登录失败,请重新登录');
}
loading.value = false
}
async function getDoctorInfo(weChatOpenId) {
try {
const res = await api('getCorpMemberData', {
weChatOpenId,
});
if (res.success && res.data) {
doctorInfo.value = res.data;
}
} catch (e) {
console.error('获取医生信息失败:', e);
}
}
return { account, openid, doctorInfo, login }
})