"use strict";
const common_vendor = require("../../common/vendor.js");
const modules_api_AppServer = require("../../modules/api/AppServer.js");
const utils_auth = require("../../utils/auth.js");
const modules_Config = require("../../modules/Config.js");
const common_assets = require("../../common/assets.js");
const _sfc_main = {
  data() {
    return {
      isLoading: false,
      agreeToTerms: false,
      statusBarHeight: 0,
      appLogo: ""
    };
  },
  onLoad() {
    const systemInfo = common_vendor.index.getSystemInfoSync();
    this.statusBarHeight = systemInfo.statusBarHeight || 0;
    this.loadConfig();
  },
  methods: {
    back() {
      common_vendor.index.navigateBack({
        delta: 1
      });
    },
    // Handle WeChat login
    async handleWechatLogin() {
      if (this.isLoading) {
        return;
      }
      if (!this.agreeToTerms) {
        common_vendor.index.showToast({
          title: this.$t("login.mustAgreeToTerms"),
          icon: "none"
        });
        return;
      }
      this.isLoading = true;
      try {
        const loginRes = await this.getWechatLoginCode();
        common_vendor.index.__f__("log", "at pages/login/login-page.vue:96", "微信登录 code:", loginRes);
        if (!loginRes || !loginRes.code) {
          common_vendor.index.showToast({
            title: this.$t("login.wechatLoginFailed"),
            icon: "none"
          });
          this.isLoading = false;
          return;
        }
        const appserver = new modules_api_AppServer.AppServer();
        const data = await appserver.WechatLogin(loginRes.code);
        common_vendor.index.__f__("log", "at pages/login/login-page.vue:111", "登录接口返回:", data);
        if (!data) {
          common_vendor.index.__f__("error", "at pages/login/login-page.vue:115", "登录接口无响应");
          common_vendor.index.showToast({
            title: this.$t("login.loginFailed"),
            icon: "none"
          });
          this.isLoading = false;
          return;
        }
        if (data.code !== 0) {
          common_vendor.index.__f__("error", "at pages/login/login-page.vue:126", "登录失败，code:", data.code, "message:", data.message);
          common_vendor.index.showToast({
            title: data.message || this.$t("login.loginFailed"),
            icon: "none"
          });
          this.isLoading = false;
          return;
        }
        if (!data.data || !data.data.token) {
          common_vendor.index.__f__("error", "at pages/login/login-page.vue:137", "Token 数据缺失:", data.data);
          common_vendor.index.showToast({
            title: this.$t("login.loginFailed"),
            icon: "none"
          });
          this.isLoading = false;
          return;
        }
        common_vendor.index.__f__("log", "at pages/login/login-page.vue:146", "登录成功，保存认证信息");
        const token = "Bearer " + data.data.token;
        utils_auth.saveAuthData(token, data.data.refreshToken, data.data.user);
        common_vendor.index.showToast({
          title: this.$t("login.loginSuccess") || "登录成功",
          icon: "success",
          duration: 1500
        });
        setTimeout(() => {
          this.redirectToHome();
        }, 500);
      } catch (error) {
        common_vendor.index.__f__("error", "at pages/login/login-page.vue:165", "登录异常:", error);
        common_vendor.index.__f__("error", "at pages/login/login-page.vue:166", "错误堆栈:", error.stack);
        common_vendor.index.showToast({
          title: this.$t("login.loginError") + ": " + (error.message || ""),
          icon: "none",
          duration: 3e3
        });
      } finally {
        this.isLoading = false;
      }
    },
    // 获取微信登录code（Promise封装）
    getWechatLoginCode() {
      return new Promise((resolve, reject) => {
        common_vendor.index.login({
          provider: "weixin",
          success: (res) => {
            resolve(res);
          },
          fail: (err) => {
            common_vendor.index.__f__("error", "at pages/login/login-page.vue:186", "uni.login 失败:", err);
            reject(err);
          }
        });
      });
    },
    // 登录成功后跳转到首页
    redirectToHome() {
      const app = getApp();
      if (app.globalData) {
        app.globalData.shouldRefresh = true;
        app.globalData.loginTime = Date.now();
      }
      common_vendor.index.switchTab({
        url: "/pages/index/index",
        success: () => {
          common_vendor.index.__f__("log", "at pages/login/login-page.vue:206", "登录成功，已跳转到首页");
        },
        fail: (err) => {
          common_vendor.index.__f__("error", "at pages/login/login-page.vue:209", "跳转首页失败:", err);
          common_vendor.index.reLaunch({
            url: "/pages/index/index"
          });
        }
      });
    },
    // Toggle agreement checkbox
    toggleAgreement() {
      this.agreeToTerms = !this.agreeToTerms;
    },
    // Go to user agreement page
    goToUserAgreement() {
      common_vendor.index.navigateTo({
        url: "/pages/me/user-agreement-page"
      });
    },
    // Go to privacy policy page
    goToPrivacyPolicy() {
      common_vendor.index.navigateTo({
        url: "/pages/me/privacy-policy-page"
      });
    },
    // 加载配置
    async loadConfig() {
      try {
        const config = await modules_Config.Config.getPublicConfig();
        if (config.app_logo) {
          this.appLogo = modules_Config.Config.getImageUrl(config.app_logo);
        }
      } catch (error) {
        common_vendor.index.__f__("error", "at pages/login/login-page.vue:245", "加载配置失败:", error);
      }
    }
  }
};
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return common_vendor.e({
    a: common_assets._imports_0,
    b: common_vendor.o((...args) => $options.back && $options.back(...args)),
    c: common_vendor.t(_ctx.$t("login.title")),
    d: common_vendor.o((...args) => _ctx.markAllRead && _ctx.markAllRead(...args)),
    e: $data.appLogo
  }, $data.appLogo ? {
    f: $data.appLogo
  } : {}, {
    g: common_vendor.t(_ctx.$t("login.oneClickLogin")),
    h: common_vendor.o((...args) => $options.handleWechatLogin && $options.handleWechatLogin(...args)),
    i: $data.isLoading,
    j: $data.agreeToTerms ? "/static/ic_checked.png" : "/static/ic_check.png",
    k: common_vendor.t(_ctx.$t("login.agreeToTerms")),
    l: common_vendor.t(_ctx.$t("login.userAgreement")),
    m: common_vendor.o((...args) => $options.goToUserAgreement && $options.goToUserAgreement(...args)),
    n: common_vendor.t(_ctx.$t("login.and")),
    o: common_vendor.t(_ctx.$t("login.privacyPolicy")),
    p: common_vendor.o((...args) => $options.goToPrivacyPolicy && $options.goToPrivacyPolicy(...args)),
    q: common_vendor.o((...args) => $options.toggleAgreement && $options.toggleAgreement(...args))
  });
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-6337d1cb"]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/login/login-page.js.map
