"use strict";
const common_vendor = require("../common/vendor.js");
const settings_enums = require("../settings/enums.js");
const useUserStore = common_vendor.defineStore({
  id: "app-user",
  state: () => ({
    userInfo: null,
    token: void 0,
    siteId: void 0,
    openId: void 0
  }),
  getters: {
    getUserInfo() {
      return this.userInfo || common_vendor.index.getStorageSync(settings_enums.USER_INFO) || {};
    },
    getToken() {
      return this.token || common_vendor.index.getStorageSync(settings_enums.TOKEN) || "";
    },
    getOpenId() {
      return this.openId || common_vendor.index.getStorageSync(settings_enums.OPEN_ID) || "";
    }
  },
  actions: {
    // 修改用户信息
    setUserInfo(e) {
      this.userInfo = e;
      common_vendor.index.setStorageSync(settings_enums.USER_INFO, e);
    },
    // 登录
    login(e) {
      this.userInfo = e;
      this.token = e.token;
      this.siteId = e.siteId;
      common_vendor.index.setStorageSync(settings_enums.TOKEN, e.token);
      common_vendor.index.setStorageSync(settings_enums.USER_INFO, e);
      setTimeout(() => {
        common_vendor.index.showToast({
          title: "登录成功",
          duration: 1e3
        });
      });
    },
    // 退出之前的操作
    beforeLogout() {
      common_vendor.index.showModal({
        title: "提示",
        content: "确定退出登录吗？",
        confirmText: "确定",
        success: (res) => {
          if (res.confirm) {
            this.logout();
          } else if (res.cancel)
            ;
        }
      });
    },
    // 退出登录
    async logout(option = {}) {
      const options = {
        redirct: true,
        ...option
      };
      try {
        const keys = [settings_enums.USER_INFO, settings_enums.TOKEN];
        await Promise.all(keys.map((key) => common_vendor.index.removeStorageSync(key))).then(
          () => {
            this.userInfo = null;
            this.token = "";
            options.redirct && common_vendor.index.reLaunch({
              url: "/pages/index/index"
            });
          }
        );
        return Promise.resolve();
      } catch (e) {
        common_vendor.index.__f__("log", "at store/user.js:80", "eeee login", e);
      }
    }
  }
});
exports.useUserStore = useUserStore;
//# sourceMappingURL=../../.sourcemap/mp-weixin/store/user.js.map
