WXL
14 小时以前 05c363fdd7ab04e3bd9a753e2c5d5bfff04d681c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"use strict";
const common_vendor = require("../common/vendor.js");
const useUserStore = common_vendor.defineStore("user", () => {
  const token = common_vendor.ref(null);
  const userInfo = common_vendor.ref(null);
  const roleKeyInfo = common_vendor.ref(null);
  const baseUrlHt = common_vendor.ref("https://opo.qduh.cn:443");
  const isLoggedIn = common_vendor.computed(() => !!token.value);
  const userName = common_vendor.computed(() => {
    var _a;
    return ((_a = userInfo.value) == null ? void 0 : _a.name) || "";
  });
  const ADMIN_ROLES = ["admin", "medical_admin"];
  const USER_ROLES = ["user", "patient"];
  const DOCTOR_ROLES = ["doctor", "specialist"];
  function setToken(newToken) {
    token.value = newToken;
    common_vendor.index.setStorageSync("token", newToken);
  }
  function setUserInfo(info) {
    userInfo.value = info;
    common_vendor.index.setStorageSync("userInfo", info);
  }
  function setroleKey(roleKey) {
    if (ADMIN_ROLES.includes(roleKey)) {
      roleKeyInfo.value = 1;
    } else if (DOCTOR_ROLES.includes(roleKey)) {
      roleKeyInfo.value = 2;
    } else if (USER_ROLES.includes(roleKey)) {
      roleKeyInfo.value = 0;
    } else {
      roleKeyInfo.value = 0;
    }
    common_vendor.index.setStorageSync("roleKeyInfo", roleKeyInfo.value);
  }
  function clearUser() {
    token.value = null;
    userInfo.value = null;
    roleKeyInfo.value = null;
    common_vendor.index.removeStorageSync("token");
    common_vendor.index.removeStorageSync("roleKeyInfo");
    common_vendor.index.removeStorageSync("userInfo");
  }
  function initFromStorage() {
    const localToken = common_vendor.index.getStorageSync("token");
    const localUser = common_vendor.index.getStorageSync("userInfo");
    const localroleKey = common_vendor.index.getStorageSync("roleKeyInfo");
    if (localToken)
      token.value = localToken;
    if (localUser)
      userInfo.value = localUser;
    if (localroleKey)
      roleKeyInfo.value = localroleKey;
  }
  async function refreshUserInfo() {
    try {
      const res = await common_vendor.index.$uapi.get("/getInfo");
      if (res && res.user) {
        setUserInfo(res.user);
        if (res.roles)
          setroleKey(res.roles);
        return true;
      }
      return false;
    } catch (error) {
      common_vendor.index.__f__("error", "at stores/user.js:73", "刷新用户信息失败", error);
      return false;
    }
  }
  return {
    token,
    userInfo,
    roleKeyInfo,
    isLoggedIn,
    userName,
    baseUrlHt,
    setToken,
    setUserInfo,
    clearUser,
    initFromStorage,
    setroleKey,
    refreshUserInfo
  };
});
exports.useUserStore = useUserStore;
//# sourceMappingURL=../../.sourcemap/mp-weixin/stores/user.js.map