"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 handleLogin() {
      if (!agreed.value) {
        common_vendor.index.showToast({ title: "请先同意协议", icon: "none" });
        return;
      }
    }
    function openProtocol(type) {
      const urls = {
        service: "/pages/protocol/service",
        privacy: "/pages/protocol/privacy"
      };
      common_vendor.index.navigateTo({ url: urls[type] });
    }
    function socialLogin(type) {
    }
    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:159", "开始获取验证码...");
      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:165", "验证码响应:", 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:173", "获取验证码失败:", 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:196", "开始获取注册验证码...");
      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:202", "验证码响应:", 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:210", "获取验证码失败:", 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:226", "开始登录请求...");
        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:235", "登录响应数据:", res.data);
            common_vendor.index.__f__("log", "at pages/login/index.vue:236", "响应状态码:", 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:255", "开始跳转到首页...");
              common_vendor.index.reLaunch({
                url: "/pages/index/index",
                success: () => {
                  common_vendor.index.__f__("log", "at pages/login/index.vue:259", "跳转成功");
                },
                fail: (err) => {
                  common_vendor.index.__f__("error", "at pages/login/index.vue:262", "reLaunch 跳转失败:", err);
                  common_vendor.index.__f__("log", "at pages/login/index.vue:264", "尝试使用 switchTab 跳转...");
                  common_vendor.index.switchTab({
                    url: "/pages/index/index",
                    success: () => {
                      common_vendor.index.__f__("log", "at pages/login/index.vue:268", "switchTab 跳转成功");
                    },
                    fail: (err2) => {
                      common_vendor.index.__f__("error", "at pages/login/index.vue:271", "switchTab 跳转失败:", err2);
                      common_vendor.index.showToast({
                        title: "页面跳转失败，请重试",
                        icon: "none"
                      });
                    }
                  });
                }
              });
            } else {
              common_vendor.index.__f__("log", "at pages/login/index.vue:281", "登录失败，状态码:", res.statusCode);
              common_vendor.index.showToast({
                title: res.data.msg || "登录失败，请重试",
                icon: "none"
              });
            }
          },
          fail(err) {
            common_vendor.index.__f__("error", "at pages/login/index.vue:289", "登录请求失败:", 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:303", "开始登录请求...");
        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:312", "登录响应数据:", res.data);
            common_vendor.index.__f__("log", "at pages/login/index.vue:313", "响应状态码:", 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:332", "开始跳转到首页...");
              common_vendor.index.reLaunch({
                url: "/pages/index/index",
                success: () => {
                  common_vendor.index.__f__("log", "at pages/login/index.vue:336", "跳转成功");
                },
                fail: (err) => {
                  common_vendor.index.__f__("error", "at pages/login/index.vue:339", "reLaunch 跳转失败:", err);
                  common_vendor.index.__f__("log", "at pages/login/index.vue:341", "尝试使用 switchTab 跳转...");
                  common_vendor.index.switchTab({
                    url: "/pages/index/index",
                    success: () => {
                      common_vendor.index.__f__("log", "at pages/login/index.vue:345", "switchTab 跳转成功");
                    },
                    fail: (err2) => {
                      common_vendor.index.__f__("error", "at pages/login/index.vue:348", "switchTab 跳转失败:", err2);
                      common_vendor.index.showToast({
                        title: "页面跳转失败，请重试",
                        icon: "none"
                      });
                    }
                  });
                }
              });
            } else {
              common_vendor.index.__f__("log", "at pages/login/index.vue:358", "登录失败，状态码:", res.statusCode);
              common_vendor.index.__f__("log", "at pages/login/index.vue:359", "登录失败，错误信息:", 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:368", "登录请求失败:", 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:396", "开始注册请求...");
      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:407", "注册响应数据:", res.data);
          common_vendor.index.__f__("log", "at pages/login/index.vue:408", "响应状态码:", 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:429", "注册失败，状态码:", res.statusCode);
            common_vendor.index.showToast({
              title: res.data.msg || "注册失败，请重试",
              icon: "none"
            });
          }
        },
        fail(err) {
          common_vendor.index.__f__("error", "at pages/login/index.vue:437", "注册请求失败:", 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_0,
        b: !agreed.value,
        c: common_vendor.o(handleLogin),
        d: common_assets._imports_1,
        e: !agreed.value,
        f: common_vendor.o(($event) => socialLogin()),
        g: agreed.value,
        h: common_vendor.o(($event) => agreed.value = !agreed.value),
        i: common_vendor.o(($event) => openProtocol("service")),
        j: common_vendor.o(($event) => openProtocol("privacy")),
        k: common_assets._imports_2,
        l: common_vendor.o(($event) => showLoginPopup.value = true),
        m: common_assets._imports_3,
        n: showLoginPopup.value
      }, showLoginPopup.value ? common_vendor.e({
        o: common_vendor.o(($event) => showLoginPopup.value = false),
        p: loginTab.value === "phone" ? 1 : "",
        q: common_vendor.o(($event) => loginTab.value = "phone"),
        r: loginTab.value === "account" ? 1 : "",
        s: common_vendor.o(($event) => loginTab.value = "account"),
        t: loginTab.value === "phone"
      }, loginTab.value === "phone" ? {
        v: loginPhone.value,
        w: common_vendor.o(($event) => loginPhone.value = $event.detail.value),
        x: loginCode.value,
        y: common_vendor.o(($event) => loginCode.value = $event.detail.value),
        z: common_vendor.t(loginCodeCountdown.value > 0 ? loginCodeCountdown.value + "s" : "获取验证码"),
        A: loginCodeCountdown.value > 0,
        B: common_vendor.o(getLoginCode)
      } : {
        C: loginAccount.value,
        D: common_vendor.o(($event) => loginAccount.value = $event.detail.value),
        E: loginPassword.value,
        F: common_vendor.o(($event) => loginPassword.value = $event.detail.value)
      }, {
        G: common_vendor.o(handlePopupLogin),
        H: common_vendor.o(openRegister)
      }) : {}, {
        I: showRegisterPopup.value
      }, showRegisterPopup.value ? {
        J: common_vendor.o(($event) => showRegisterPopup.value = false),
        K: regPhone.value,
        L: common_vendor.o(($event) => regPhone.value = $event.detail.value),
        M: regCode.value,
        N: common_vendor.o(($event) => regCode.value = $event.detail.value),
        O: common_vendor.t(regCodeCountdown.value > 0 ? regCodeCountdown.value + "s" : "获取验证码"),
        P: common_vendor.o(getRegCode),
        Q: regAccount.value,
        R: common_vendor.o(($event) => regAccount.value = $event.detail.value),
        S: regPassword.value,
        T: common_vendor.o(($event) => regPassword.value = $event.detail.value),
        U: 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
