"use strict";
const common_vendor = require("../../common/vendor.js");
const common_assets = require("../../common/assets.js");
const _sfc_main = {
  __name: "index",
  setup(__props) {
    const agreed = common_vendor.ref(false);
    const showLoginPopup = common_vendor.ref(false);
    const showRegisterPopup = common_vendor.ref(false);
    const loginTab = common_vendor.ref("phone");
    const loginPhone = common_vendor.ref("");
    const loginCode = common_vendor.ref("");
    const loginAccount = common_vendor.ref("");
    const loginPassword = common_vendor.ref("");
    const regPhone = common_vendor.ref("");
    const regCode = common_vendor.ref("");
    const regAccount = common_vendor.ref("");
    const regPassword = common_vendor.ref("");
    const loginCodeCountdown = common_vendor.ref(0);
    const regCodeCountdown = common_vendor.ref(0);
    let loginCodeTimer = null;
    let regCodeTimer = null;
    function openProtocol(type) {
      const urls = {
        service: "/pages/protocol/service",
        privacy: "/pages/protocol/privacy"
      };
      common_vendor.index.navigateTo({ url: urls[type] });
    }
    function socialLogin(type) {
      if (!agreed.value) {
        common_vendor.index.showToast({
          title: "请先同意服务协议和隐私政策",
          icon: "none"
        });
        return;
      }
      if (type === "wechat") {
        common_vendor.index.login({
          provider: "weixin",
          success: async (loginRes) => {
            common_vendor.index.__f__("log", "at pages/login/index.vue:148", "uni.login result:", loginRes);
            if (loginRes.code) {
              try {
                const userInfoRes = await common_vendor.index.getUserProfile({
                  desc: "用于完善用户资料"
                }).catch((err) => {
                  common_vendor.index.__f__("error", "at pages/login/index.vue:154", "getUserProfile error:", err);
                  common_vendor.index.showToast({
                    title: "已取消授权",
                    icon: "none"
                  });
                  throw err;
                });
                common_vendor.index.__f__("log", "at pages/login/index.vue:162", "userInfoRes:", userInfoRes);
                if (!userInfoRes || !userInfoRes.userInfo) {
                  common_vendor.index.showToast({
                    title: "未获取到用户信息",
                    icon: "none"
                  });
                  return;
                }
                const wxLoginDto = {
                  avatarUrl: userInfoRes.userInfo.avatarUrl,
                  gender: userInfoRes.userInfo.gender,
                  nickName: userInfoRes.userInfo.nickName
                };
                const requestData = {
                  code: loginRes.code,
                  wxLoginDto
                };
                if (userInfoRes.encryptedData)
                  requestData.encryptedData = userInfoRes.encryptedData;
                if (userInfoRes.iv)
                  requestData.iv = userInfoRes.iv;
                common_vendor.index.__f__("log", "at pages/login/index.vue:186", "requestData:", requestData);
                common_vendor.index.request({
                  url: "http://localhost:9500/wechat/login",
                  method: "POST",
                  data: requestData,
                  success: (res) => {
                    common_vendor.index.__f__("log", "at pages/login/index.vue:193", "后端返回:", res);
                    if (res.statusCode === 200 && res.data.code === 200) {
                      if (res.data.token) {
                        common_vendor.index.setStorageSync("token", res.data.token);
                      }
                      common_vendor.index.setStorageSync("isLoggedIn", true);
                      common_vendor.index.setStorageSync("userInfo", wxLoginDto);
                      common_vendor.index.showToast({
                        title: "登录成功",
                        icon: "success",
                        duration: 1500
                      });
                      common_vendor.index.reLaunch({
                        url: "/pages/index/index"
                      });
                    } else {
                      common_vendor.index.showToast({
                        title: res.data.msg || "登录失败，请重试",
                        icon: "none"
                      });
                    }
                  },
                  fail: (err) => {
                    common_vendor.index.__f__("error", "at pages/login/index.vue:218", "uni.request error:", err);
                    common_vendor.index.showToast({
                      title: "网络错误，请稍后重试",
                      icon: "none"
                    });
                  }
                });
              } catch (err) {
                common_vendor.index.__f__("error", "at pages/login/index.vue:227", "catch error:", err);
              }
            } else {
              common_vendor.index.showToast({
                title: "微信登录失败",
                icon: "none"
              });
            }
          },
          fail: (err) => {
            common_vendor.index.__f__("error", "at pages/login/index.vue:237", "uni.login error:", err);
            common_vendor.index.showToast({
              title: "微信登录失败，请重试",
              icon: "none"
            });
          }
        });
      }
    }
    function openRegister() {
      showLoginPopup.value = false;
      showRegisterPopup.value = true;
    }
    function getLoginCode() {
      if (loginCodeCountdown.value > 0)
        return;
      if (!/^1[3-9]\d{9}$/.test(loginPhone.value)) {
        common_vendor.index.showToast({ title: "请输入正确手机号", icon: "none" });
        return;
      }
      loginCodeCountdown.value = 60;
      loginCodeTimer = setInterval(() => {
        if (loginCodeCountdown.value > 0) {
          loginCodeCountdown.value--;
        } else {
          clearInterval(loginCodeTimer);
          loginCodeTimer = null;
        }
      }, 1e3);
      common_vendor.index.__f__("log", "at pages/login/index.vue:268", "开始获取验证码...");
      common_vendor.index.request({
        url: "http://localhost:9500/user/code",
        method: "POST",
        data: { phone: loginPhone.value },
        success(res) {
          common_vendor.index.__f__("log", "at pages/login/index.vue:274", "验证码响应:", res.data);
          if (res.statusCode === 200) {
            common_vendor.index.showToast({ title: "验证码已发送", icon: "none" });
          } else {
            common_vendor.index.showToast({ title: res.data.msg || "发送失败", icon: "none" });
          }
        },
        fail(err) {
          common_vendor.index.__f__("error", "at pages/login/index.vue:282", "获取验证码失败:", err);
          common_vendor.index.showToast({ title: "网络错误", icon: "none" });
        }
      });
    }
    function getRegCode() {
      if (regCodeCountdown.value > 0)
        return;
      if (!/^1[3-9]\d{9}$/.test(regPhone.value)) {
        common_vendor.index.showToast({ title: "请输入正确手机号", icon: "none" });
        return;
      }
      regCodeCountdown.value = 60;
      regCodeTimer = setInterval(() => {
        if (regCodeCountdown.value > 0) {
          regCodeCountdown.value--;
        } else {
          clearInterval(regCodeTimer);
          regCodeTimer = null;
        }
      }, 1e3);
      common_vendor.index.__f__("log", "at pages/login/index.vue:305", "开始获取注册验证码...");
      common_vendor.index.request({
        url: "http://localhost:9500/user/code",
        method: "POST",
        data: { phone: regPhone.value },
        success(res) {
          common_vendor.index.__f__("log", "at pages/login/index.vue:311", "验证码响应:", res.data);
          if (res.statusCode === 200) {
            common_vendor.index.showToast({ title: "验证码已发送", icon: "none" });
          } else {
            common_vendor.index.showToast({ title: res.data.msg || "发送失败", icon: "none" });
          }
        },
        fail(err) {
          common_vendor.index.__f__("error", "at pages/login/index.vue:319", "获取验证码失败:", err);
          common_vendor.index.showToast({ title: "网络错误", icon: "none" });
        }
      });
    }
    function handlePopupLogin() {
      if (loginTab.value === "phone") {
        if (!/^1[3-9]\d{9}$/.test(loginPhone.value)) {
          common_vendor.index.showToast({ title: "请输入正确手机号", icon: "none" });
          return;
        }
        if (!loginCode.value) {
          common_vendor.index.showToast({ title: "请输入验证码", icon: "none" });
          return;
        }
        common_vendor.index.__f__("log", "at pages/login/index.vue:335", "开始登录请求...");
        common_vendor.index.request({
          url: "http://localhost:9500/user/login",
          method: "POST",
          data: {
            phone: loginPhone.value,
            code: loginCode.value
          },
          success(res) {
            common_vendor.index.__f__("log", "at pages/login/index.vue:344", "登录响应数据:", res.data);
            common_vendor.index.__f__("log", "at pages/login/index.vue:345", "响应状态码:", res.statusCode);
            if (res.statusCode === 200) {
              common_vendor.index.setStorageSync("userPhone", loginPhone.value);
              common_vendor.index.setStorageSync("isLoggedIn", true);
              showLoginPopup.value = false;
              common_vendor.index.showToast({
                title: "登录成功",
                icon: "success",
                duration: 1500
              });
              common_vendor.index.__f__("log", "at pages/login/index.vue:364", "开始跳转到首页...");
              common_vendor.index.reLaunch({
                url: "/pages/index/index",
                success: () => {
                  common_vendor.index.__f__("log", "at pages/login/index.vue:368", "跳转成功");
                },
                fail: (err) => {
                  common_vendor.index.__f__("error", "at pages/login/index.vue:371", "reLaunch 跳转失败:", err);
                  common_vendor.index.__f__("log", "at pages/login/index.vue:373", "尝试使用 switchTab 跳转...");
                  common_vendor.index.switchTab({
                    url: "/pages/index/index",
                    success: () => {
                      common_vendor.index.__f__("log", "at pages/login/index.vue:377", "switchTab 跳转成功");
                    },
                    fail: (err2) => {
                      common_vendor.index.__f__("error", "at pages/login/index.vue:380", "switchTab 跳转失败:", err2);
                      common_vendor.index.showToast({
                        title: "页面跳转失败，请重试",
                        icon: "none"
                      });
                    }
                  });
                }
              });
            } else {
              common_vendor.index.__f__("log", "at pages/login/index.vue:390", "登录失败，状态码:", res.statusCode);
              common_vendor.index.showToast({
                title: res.data.msg || "登录失败，请重试",
                icon: "none"
              });
            }
          },
          fail(err) {
            common_vendor.index.__f__("error", "at pages/login/index.vue:398", "登录请求失败:", err);
            common_vendor.index.showToast({ title: "网络错误", icon: "none" });
          }
        });
      } else {
        if (!loginAccount.value) {
          common_vendor.index.showToast({ title: "请输入账号", icon: "none" });
          return;
        }
        if (!loginPassword.value) {
          common_vendor.index.showToast({ title: "请输入密码", icon: "none" });
          return;
        }
        common_vendor.index.__f__("log", "at pages/login/index.vue:412", "开始登录请求...");
        common_vendor.index.request({
          url: "http://localhost:9500/user/loginup",
          method: "POST",
          data: {
            username: loginAccount.value,
            password: loginPassword.value
          },
          success(res) {
            common_vendor.index.__f__("log", "at pages/login/index.vue:421", "登录响应数据:", res.data);
            common_vendor.index.__f__("log", "at pages/login/index.vue:422", "响应状态码:", res.statusCode);
            if (res.statusCode === 200 && res.data.code === 200) {
              common_vendor.index.setStorageSync("userPhone", loginAccount.value);
              common_vendor.index.setStorageSync("isLoggedIn", true);
              showLoginPopup.value = false;
              common_vendor.index.showToast({
                title: "登录成功",
                icon: "success",
                duration: 1500
              });
              common_vendor.index.__f__("log", "at pages/login/index.vue:441", "开始跳转到首页...");
              common_vendor.index.reLaunch({
                url: "/pages/index/index",
                success: () => {
                  common_vendor.index.__f__("log", "at pages/login/index.vue:445", "跳转成功");
                },
                fail: (err) => {
                  common_vendor.index.__f__("error", "at pages/login/index.vue:448", "reLaunch 跳转失败:", err);
                  common_vendor.index.__f__("log", "at pages/login/index.vue:450", "尝试使用 switchTab 跳转...");
                  common_vendor.index.switchTab({
                    url: "/pages/index/index",
                    success: () => {
                      common_vendor.index.__f__("log", "at pages/login/index.vue:454", "switchTab 跳转成功");
                    },
                    fail: (err2) => {
                      common_vendor.index.__f__("error", "at pages/login/index.vue:457", "switchTab 跳转失败:", err2);
                      common_vendor.index.showToast({
                        title: "页面跳转失败，请重试",
                        icon: "none"
                      });
                    }
                  });
                }
              });
            } else {
              common_vendor.index.__f__("log", "at pages/login/index.vue:467", "登录失败，状态码:", res.statusCode);
              common_vendor.index.__f__("log", "at pages/login/index.vue:468", "登录失败，错误信息:", res.data.msg);
              common_vendor.index.showToast({
                title: res.data.msg || "账号或密码错误",
                icon: "none",
                duration: 2e3
              });
            }
          },
          fail(err) {
            common_vendor.index.__f__("error", "at pages/login/index.vue:477", "登录请求失败:", err);
            common_vendor.index.showToast({
              title: "网络错误，请稍后重试",
              icon: "none",
              duration: 2e3
            });
          }
        });
      }
    }
    function handleRegister() {
      if (!/^1[3-9]\d{9}$/.test(regPhone.value)) {
        common_vendor.index.showToast({ title: "请输入正确手机号", icon: "none" });
        return;
      }
      if (!regCode.value) {
        common_vendor.index.showToast({ title: "请输入验证码", icon: "none" });
        return;
      }
      if (!regAccount.value) {
        common_vendor.index.showToast({ title: "请输入账号", icon: "none" });
        return;
      }
      if (!regPassword.value) {
        common_vendor.index.showToast({ title: "请输入密码", icon: "none" });
        return;
      }
      common_vendor.index.__f__("log", "at pages/login/index.vue:505", "开始注册请求...");
      common_vendor.index.request({
        url: "http://localhost:9500/user/register",
        method: "POST",
        data: {
          phone: regPhone.value,
          code: regCode.value,
          username: regAccount.value,
          password: regPassword.value
        },
        success(res) {
          common_vendor.index.__f__("log", "at pages/login/index.vue:516", "注册响应数据:", res.data);
          common_vendor.index.__f__("log", "at pages/login/index.vue:517", "响应状态码:", res.statusCode);
          if (res.statusCode === 200) {
            common_vendor.index.showToast({
              title: "注册成功",
              icon: "success",
              duration: 1500
            });
            showRegisterPopup.value = false;
            setTimeout(() => {
              showLoginPopup.value = true;
              loginTab.value = "phone";
              loginPhone.value = regPhone.value;
            }, 1500);
          } else {
            common_vendor.index.__f__("log", "at pages/login/index.vue:538", "注册失败，状态码:", res.statusCode);
            common_vendor.index.showToast({
              title: res.data.msg || "注册失败，请重试",
              icon: "none"
            });
          }
        },
        fail(err) {
          common_vendor.index.__f__("error", "at pages/login/index.vue:546", "注册请求失败:", err);
          common_vendor.index.showToast({ title: "网络错误", icon: "none" });
        }
      });
    }
    common_vendor.onUnmounted(() => {
      if (loginCodeTimer)
        clearInterval(loginCodeTimer);
      if (regCodeTimer)
        clearInterval(regCodeTimer);
    });
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: common_assets._imports_1,
        b: common_assets._imports_1$1,
        c: !agreed.value,
        d: common_vendor.o(($event) => socialLogin("wechat")),
        e: agreed.value,
        f: common_vendor.o(($event) => agreed.value = !agreed.value),
        g: common_vendor.o(($event) => openProtocol("service")),
        h: common_vendor.o(($event) => openProtocol("privacy")),
        i: common_assets._imports_2,
        j: common_vendor.o(($event) => showLoginPopup.value = true),
        k: common_assets._imports_3,
        l: showLoginPopup.value
      }, showLoginPopup.value ? common_vendor.e({
        m: common_vendor.o(($event) => showLoginPopup.value = false),
        n: loginTab.value === "phone" ? 1 : "",
        o: common_vendor.o(($event) => loginTab.value = "phone"),
        p: loginTab.value === "account" ? 1 : "",
        q: common_vendor.o(($event) => loginTab.value = "account"),
        r: loginTab.value === "phone"
      }, loginTab.value === "phone" ? {
        s: loginPhone.value,
        t: common_vendor.o(($event) => loginPhone.value = $event.detail.value),
        v: loginCode.value,
        w: common_vendor.o(($event) => loginCode.value = $event.detail.value),
        x: common_vendor.t(loginCodeCountdown.value > 0 ? loginCodeCountdown.value + "s" : "获取验证码"),
        y: loginCodeCountdown.value > 0,
        z: common_vendor.o(getLoginCode)
      } : {
        A: loginAccount.value,
        B: common_vendor.o(($event) => loginAccount.value = $event.detail.value),
        C: loginPassword.value,
        D: common_vendor.o(($event) => loginPassword.value = $event.detail.value)
      }, {
        E: common_vendor.o(handlePopupLogin),
        F: common_vendor.o(openRegister)
      }) : {}, {
        G: showRegisterPopup.value
      }, showRegisterPopup.value ? {
        H: common_vendor.o(($event) => showRegisterPopup.value = false),
        I: regPhone.value,
        J: common_vendor.o(($event) => regPhone.value = $event.detail.value),
        K: regCode.value,
        L: common_vendor.o(($event) => regCode.value = $event.detail.value),
        M: common_vendor.t(regCodeCountdown.value > 0 ? regCodeCountdown.value + "s" : "获取验证码"),
        N: common_vendor.o(getRegCode),
        O: regAccount.value,
        P: common_vendor.o(($event) => regAccount.value = $event.detail.value),
        Q: regPassword.value,
        R: common_vendor.o(($event) => regPassword.value = $event.detail.value),
        S: common_vendor.o(handleRegister)
      } : {});
    };
  }
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-d08ef7d4"]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/login/index.js.map
