188 lines
5.2 KiB
JavaScript
188 lines
5.2 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 { 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 maskInfo = getMaskInfo({
|
|
name: i.name,
|
|
socialno: i.certNo,
|
|
tel: i.mobile,
|
|
});
|
|
return { ...i, ...maskInfo, isSelf: i.certNo && i.certNo === userInfo.value.certNo }
|
|
}))
|
|
|
|
async function login() {
|
|
if (!userInfo.value.certNo) {
|
|
const authCode = await getAuthCode();
|
|
const auth = authStore();
|
|
await auth.login({ authCode });
|
|
const data = await getUserByAuthCode(authCode);
|
|
uni.setStorageSync("hasAuthed", "hasAuthed");
|
|
const maskData = {
|
|
name: data.userName,
|
|
tel: data.mobile,
|
|
socialno: data.certNo,
|
|
};
|
|
userInfo.value = { ...data, ...getMaskInfo(maskData) };
|
|
}
|
|
logined.value = true;
|
|
}
|
|
|
|
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 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);
|
|
}
|
|
|
|
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) {
|
|
patients.value = [];
|
|
return
|
|
}
|
|
try {
|
|
const res = await getHlwPatientList(accountId);
|
|
if (!res || !res.success) {
|
|
|
|
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
|
|
};
|
|
});
|