| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- "use strict";
- const common_vendor = require("../../common/vendor.js");
- const utils_api = require("../../utils/api.js");
- const utils_auth = require("../../utils/auth.js");
- const _sfc_main = {
- data() {
- return {
- phone: "",
- code: "",
- countdown: 0,
- agreed: false,
- canUseWxLogin: true,
- // 是否支持微信登录
- timer: null
- };
- },
- onLoad() {
- this.canUseWxLogin = true;
- },
- onUnload() {
- if (this.timer) {
- clearInterval(this.timer);
- }
- },
- methods: {
- /**
- * 处理微信一键登录
- */
- async handleWxLogin() {
- if (!this.agreed) {
- common_vendor.index.showToast({
- title: "请先同意用户协议和隐私政策",
- icon: "none"
- });
- return;
- }
- try {
- common_vendor.index.showLoading({ title: "登录中..." });
- const loginRes = await common_vendor.index.login();
- console.log("获取登录code成功:", loginRes.code);
- const result = await utils_api.wxLogin({
- code: loginRes.code
- });
- console.log("登录成功:", result);
- utils_auth.setToken(result.data.token);
- utils_auth.setUserInfo(result.data.userInfo);
- common_vendor.index.hideLoading();
- common_vendor.index.showToast({
- title: "登录成功",
- icon: "success"
- });
- setTimeout(() => {
- this.navigateBack();
- }, 1500);
- } catch (error) {
- console.error("登录失败:", error);
- common_vendor.index.hideLoading();
- common_vendor.index.showToast({
- title: error.message || "登录失败",
- icon: "none"
- });
- }
- },
- /**
- * 发送验证码
- */
- async sendCode() {
- if (!this.phone) {
- common_vendor.index.showToast({
- title: "请输入手机号",
- icon: "none"
- });
- return;
- }
- if (!/^1[3-9]\d{9}$/.test(this.phone)) {
- common_vendor.index.showToast({
- title: "请输入正确的手机号",
- icon: "none"
- });
- return;
- }
- if (!this.agreed) {
- common_vendor.index.showToast({
- title: "请先同意用户协议和隐私政策",
- icon: "none"
- });
- return;
- }
- try {
- common_vendor.index.showLoading({ title: "发送中..." });
- await utils_api.sendSmsCode(this.phone);
- common_vendor.index.hideLoading();
- common_vendor.index.showToast({
- title: "验证码已发送",
- icon: "success"
- });
- this.countdown = 60;
- this.timer = setInterval(() => {
- this.countdown--;
- if (this.countdown <= 0) {
- clearInterval(this.timer);
- }
- }, 1e3);
- } catch (error) {
- common_vendor.index.hideLoading();
- common_vendor.index.showToast({
- title: error.message || "发送失败",
- icon: "none"
- });
- }
- },
- /**
- * 处理手机号登录
- */
- async handlePhoneLogin() {
- if (!this.phone) {
- common_vendor.index.showToast({
- title: "请输入手机号",
- icon: "none"
- });
- return;
- }
- if (!/^1[3-9]\d{9}$/.test(this.phone)) {
- common_vendor.index.showToast({
- title: "请输入正确的手机号",
- icon: "none"
- });
- return;
- }
- if (!this.code) {
- common_vendor.index.showToast({
- title: "请输入验证码",
- icon: "none"
- });
- return;
- }
- if (!this.agreed) {
- common_vendor.index.showToast({
- title: "请先同意用户协议和隐私政策",
- icon: "none"
- });
- return;
- }
- try {
- common_vendor.index.showLoading({ title: "登录中..." });
- console.log("准备登录,phone:", this.phone, "code:", this.code);
- const result = await utils_api.phoneLogin(this.phone, this.code);
- console.log("登录成功,result:", result);
- utils_auth.setToken(result.data.token);
- utils_auth.setUserInfo(result.data.userInfo);
- common_vendor.index.hideLoading();
- common_vendor.index.showToast({
- title: "登录成功",
- icon: "success"
- });
- setTimeout(() => {
- this.navigateBack();
- }, 1500);
- } catch (error) {
- common_vendor.index.hideLoading();
- common_vendor.index.showToast({
- title: error.message || "登录失败",
- icon: "none"
- });
- }
- },
- /**
- * 处理协议勾选变化
- */
- handleAgreeChange(e) {
- this.agreed = e.detail.value.length > 0;
- },
- /**
- * 返回上一页或首页
- */
- navigateBack() {
- const pages = getCurrentPages();
- if (pages.length > 1) {
- common_vendor.index.navigateBack();
- const app = getApp();
- if (app.globalData.loginCallback) {
- app.globalData.loginCallback();
- app.globalData.loginCallback = null;
- }
- } else {
- common_vendor.index.switchTab({
- url: "/pages/index/index"
- });
- }
- }
- }
- };
- function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
- return common_vendor.e({
- a: $data.canUseWxLogin
- }, $data.canUseWxLogin ? {
- b: common_vendor.o((...args) => $options.handleWxLogin && $options.handleWxLogin(...args))
- } : {}, {
- c: $data.phone,
- d: common_vendor.o(($event) => $data.phone = $event.detail.value),
- e: $data.code,
- f: common_vendor.o(($event) => $data.code = $event.detail.value),
- g: common_vendor.t($data.countdown > 0 ? `${$data.countdown}秒后重试` : "获取验证码"),
- h: common_vendor.o((...args) => $options.sendCode && $options.sendCode(...args)),
- i: $data.countdown > 0,
- j: common_vendor.o((...args) => $options.handlePhoneLogin && $options.handlePhoneLogin(...args)),
- k: $data.agreed,
- l: common_vendor.o((...args) => $options.handleAgreeChange && $options.handleAgreeChange(...args))
- });
- }
- const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-cdfe2409"], ["__file", "D:/program/gupiao-wx/src/pages/login/login.vue"]]);
- wx.createPage(MiniProgramPage);
|