"use strict";
const common_vendor = require("../../common/vendor.js");
const api_user = require("../../api/user.js");
const useUserStore = common_vendor.defineStore("user", () => {
  const userInfo = common_vendor.ref(common_vendor.index.getStorageSync("userInfo") || null);
  const loginPopupVisible = common_vendor.ref(false);
  const isLoggedIn = common_vendor.computed(() => {
    var _a;
    return !!((_a = userInfo.value) == null ? void 0 : _a.openid);
  });
  const userId = common_vendor.computed(() => {
    var _a;
    return (_a = userInfo.value) == null ? void 0 : _a.userId;
  });
  const userName = common_vendor.computed(() => {
    var _a;
    return (_a = userInfo.value) == null ? void 0 : _a.username;
  });
  const showLoginPopup = () => {
    loginPopupVisible.value = true;
  };
  const hideLoginPopup = () => {
    loginPopupVisible.value = false;
  };
  const setUserInfo = (info) => {
    userInfo.value = info;
    common_vendor.index.setStorageSync("userInfo", info);
  };
  const clearUserInfo = () => {
    userInfo.value = null;
    common_vendor.index.removeStorageSync("userInfo");
  };
  const wechatLogin = async (code) => {
    common_vendor.index.__f__("log", "at store/modules/user.js:36", "wechatLogin", code);
    try {
      const data = await api_user.userApi.login({ code });
      setUserInfo(data);
      return data;
    } catch (error) {
      common_vendor.index.__f__("error", "at store/modules/user.js:43", "微信登录失败：", error);
      throw error;
    }
  };
  const accountLogin = async (username, password) => {
    try {
      const data = await api_user.userApi.mobileLogin({ username, password });
      setUserInfo(data);
      return data;
    } catch (error) {
      common_vendor.index.__f__("error", "at store/modules/user.js:54", "账号登录失败：", error);
      throw error;
    }
  };
  const logout = () => {
    clearUserInfo();
    common_vendor.index.reLaunch({
      url: "/pages/parent/index"
    });
  };
  return {
    // 状态
    userInfo,
    loginPopupVisible,
    // 计算属性
    isLoggedIn,
    userId,
    userName,
    // 方法
    showLoginPopup,
    hideLoginPopup,
    setUserInfo,
    clearUserInfo,
    wechatLogin,
    accountLogin,
    logout
  };
});
exports.useUserStore = useUserStore;
//# sourceMappingURL=../../../.sourcemap/mp-weixin/store/modules/user.js.map
