2026-07-27 11:26:39 +08:00

247 lines
6.8 KiB
JavaScript

import { computed, ref, watch } from "vue";
import { defineStore } from "pinia";
import dayjs from "dayjs";
import {
getHisCustomer,
getUserInfo,
getDrugStoreList,
getFamilyProfileList,
addAlipayMiniAppLaunchLog,
getHlwPatientList
} from "@/utils/api";
import authStore from "./auth";
import { isChinaId } from "@/utils/validator";
import { confirm, toast } from "@/utils/widget";
export default defineStore("userStore", () => {
// 门店信息
const storeId = ref(uni.getStorageSync("storeId") || ""); // 店铺id
const drugStore = ref(null);
const launchOptions = ref({});
// 登录信息
const logined = ref(false); // 是否登录
const loginPromise = ref(null); // 登录Promise
// 支付宝用户信息
const userInfo = ref({}); // { avatar, certNo, mobile, userName, userId }
// his档案信息
const patients = ref([]); // 就诊人档案
const patientList = computed(() => patients.value.map(i => {
const { birthday, age, sex } = getInfoFromIdNo({ socialno: i.certNo });
const maskInfo = getMaskInfo({
name: i.name,
socialno: i.certNo,
tel: i.mobile,
});
return {
...i,
...maskInfo,
birthday,
age,
sex,
_id: i.id,
socialno: i.certNo,
tel: i.mobile,
isSelf: i.certNo === userInfo.value.certNo,
}
}))
async function login(remindTobind = true) {
if (!userInfo.value.certNo) {
const authCode = await getAuthCode();
const auth = authStore();
await auth.login({
authCode,
});
const data = await getUserByAuthCode(authCode);
// const data = {
// "avatar": "https://tfs.alipayobjects.com/images/partner/T1bC0cXmKeXXXXXXXX",
// "certNo": "330523199506266439",
// "mobile": "17855833126",
// "userName": "胡学俭",
// "userId": "2088612594454930"
// }
console.log(data)
uni.setStorageSync("hasAuthed", "hasAuthed");
const maskData = {
name: data.userName,
tel: data.mobile,
socialno: data.certNo,
};
userInfo.value = { ...data, ...getMaskInfo(maskData) };
}
logined.value = true;
// const patients = await queryHisPatients(userInfo.value.certNo);
// const profile = patients.find((i) => i.socialno === userInfo.value.certNo);
// if (profile) {
// myProfile.value = getProfileData(profile);
// logined.value = true;
// addLaunchLog()
// } else if (remindTobind) {
// toBuild();
// }
}
async function addLaunchLog() {
if (launchOptions.value && launchOptions.value.scene && launchOptions.value.storeId) {
const res = await addAlipayMiniAppLaunchLog({
accountId: userInfo.value.userId,
scene: launchOptions.value.scene,
storeId: launchOptions.value.storeId
});
if (res && res.success) {
launchOptions.value = null;
}
}
}
function loginout() {
logined.value = false;
userInfo.value = {};
patients.value = [];
uni.removeStorageSync("hasAuthed");
}
function getLoginPromise(...scopes) {
loginPromise.value =
loginPromise.value ||
uni.getAuthCode({
scopes: ["auth_user", ...scopes], // nhsamp
});
return loginPromise.value;
}
function getInfoFromIdNo({ socialno: idNo }) {
const [isIdCard, birthday, gender] = isChinaId(idNo);
if (isIdCard) {
const age = dayjs().diff(dayjs(birthday), "year");
return {
birthday,
age: Math.max(1, age),
sex: gender === "MALE" ? "男" : "女",
};
}
return { birthday: "", age: "", sex: "" };
}
function getMaskInfo(profile) {
const name = typeof profile.name == "string" ? profile.name : "";
const maskName = name.length
? "*".repeat(name.length - 1) + name.slice(-1)
: "";
const mobile = typeof profile.tel == "string" ? profile.tel : "";
const maskMobile = mobile.length
? `${mobile.slice(0, 3)}****${mobile.slice(-4)}`
: "";
const socialno =
typeof profile.socialno == "string" ? profile.socialno : "";
const maskCertNo = socialno.length
? `${socialno.slice(0, 4)}****${socialno.slice(-3)}`
: "";
return { maskName, maskMobile, maskCertNo };
}
async function getAuthCode(...scopes) {
try {
const { authCode } = await getLoginPromise(...scopes);
loginPromise.value = null;
return authCode;
} catch (e) {
loginPromise.value = null;
return Promise.reject(e.errMsg || e.message || "获取授权失败");
}
}
/**
* 通过authCode 获取支付宝的用户信息
* @param {*} code
* @returns
*/
async function getUserByAuthCode(code) {
const { success, message, data } = await getUserInfo(code, launchOptions.value);
if (success) {
return data;
}
return Promise.reject(message);
}
/**
* 通过 身份证号查询 his 的 患者信息
* @param {*} idCard
* @returns
*/
async function queryHisPatients(idCard) {
const { success, message, list } = await getHisCustomer(idCard);
if (success) {
return Array.isArray(list) ? list : [];
}
return Promise.reject(message);
}
async function getStore() {
if (!storeId.value) {
await confirm("请支付宝扫一扫所在店铺二维码重新进入小程序!", {
showCancel: false,
});
return Promise.reject("未选择药房");
}
const { success, list, message } = await getDrugStoreList(storeId.value);
if (!success) {
await confirm(message || "查询药房信息失败", { showCancel: false });
return Promise.reject(message);
}
drugStore.value = Array.isArray(list)
? list.find((i) => i.store_id === storeId.value)
: null;
if (!drugStore.value) {
await confirm("未查询到药房信息", { showCancel: false });
return Promise.reject("未查询到药房信息");
}
if (drugStore.value.status !== "正常营业") {
await confirm("当前药房未正常营业", { showCancel: false });
return Promise.reject("未正常营业");
}
}
async function loadPatientList() {
const accountId = userInfo.value && userInfo.value.userId;
if (!accountId) return;
try {
const res = await getHlwPatientList(accountId);
if (!res || !res.success) {
patients.value = [];
toast((res && res.message) || "查询就诊人失败");
return;
}
patients.value = Array.isArray(res.data) ? res.data : [];
} catch (error) {
patients.value = [];
toast(error.message || error || "查询就诊人失败");
}
}
watch(storeId, (n) => {
uni.setStorageSync("storeId", n);
});
return {
drugStore,
logined,
storeId,
userInfo,
patients,
launchOptions,
patientList,
getAuthCode,
getStore,
login,
loginout,
loadPatientList
};
});