| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- "use strict";
- const common_vendor = require("../../common/vendor.js");
- const utils_auth = require("../../utils/auth.js");
- const utils_api = require("../../utils/api.js");
- const UserInfoPopup = () => "../../components/UserInfoPopup.js";
- const _sfc_main = {
- components: {
- UserInfoPopup
- },
- data() {
- return {
- showOneClickLogin: true,
- // 是否显示一键登录按钮
- showPhoneAuth: false,
- // 是否显示手机号授权按钮(新用户第二步)
- agreedToTerms: false,
- // 是否同意协议
- loginCode: "",
- // 微信登录code
- tempUserData: null,
- // 临时存储的用户数据(openid, unionid等)
- tempUserProfile: null
- // 临时存储的用户头像昵称信息
- };
- },
- methods: {
- /**
- * 返回上一页
- */
- handleBack() {
- const pages = getCurrentPages();
- if (pages.length > 1) {
- common_vendor.index.navigateBack();
- } else {
- common_vendor.index.switchTab({
- url: "/pages/index/index"
- });
- }
- },
- /**
- * 第一步:微信一键登录(老用户静默登录)
- */
- async handleWxLogin() {
- if (!this.agreedToTerms) {
- common_vendor.index.showToast({
- title: "请先阅读并同意用户协议",
- icon: "none",
- duration: 2e3
- });
- return;
- }
- try {
- common_vendor.index.showLoading({ title: "登录中..." });
- const loginRes = await this.wxLoginAsync();
- this.loginCode = loginRes.code;
- console.log("[登录] 获取到微信code:", this.loginCode);
- const result = await utils_auth.wxSilentLogin(this.loginCode);
- common_vendor.index.hideLoading();
- if (result && result.isSign === "true") {
- console.log("[登录] 老用户登录成功");
- this.handleLoginSuccess();
- } else if (result && result.isSign === "false") {
- console.log("[登录] 新用户,需要先获取头像昵称");
- this.showOneClickLogin = false;
- this.showPhoneAuth = false;
- } else if (result && result.code === 103) {
- common_vendor.index.showModal({
- title: "账号异常",
- content: "您的账号已被禁用,如有疑问请联系客服",
- showCancel: false
- });
- } else {
- throw new Error("登录接口返回数据异常");
- }
- } catch (error) {
- common_vendor.index.hideLoading();
- console.error("[登录] 微信登录失败:", error);
- common_vendor.index.showToast({
- title: error.message || "登录失败,请重试",
- icon: "none",
- duration: 2e3
- });
- }
- },
- /**
- * 第二步:获取用户头像昵称(新用户)
- */
- handleGetUserProfile() {
- if (!this.agreedToTerms) {
- common_vendor.index.showToast({
- title: "请先阅读并同意用户协议",
- icon: "none",
- duration: 2e3
- });
- return;
- }
- console.log("[登录] 打开用户信息弹窗");
- this.$refs.userInfoPopup.open();
- },
- /**
- * 用户信息弹窗确认回调(新用户第二步完成后)
- */
- handleUserInfoConfirm(userInfo) {
- console.log("[登录] 用户信息已获取:", userInfo);
- this.tempUserProfile = {
- nickname: userInfo.nickname,
- avatarUrl: userInfo.avatarUrl,
- tempAvatarPath: userInfo.tempAvatarPath
- };
- this.showPhoneAuth = true;
- common_vendor.index.showToast({
- title: "请继续授权手机号",
- icon: "none",
- duration: 2e3
- });
- },
- /**
- * 第三步:获取手机号授权(新用户)
- */
- async handleGetPhoneNumber(e) {
- console.log("[登录] 手机号授权回调:", e);
- if (e.detail.errMsg !== "getPhoneNumber:ok") {
- if (e.detail.errMsg.includes("no permission")) {
- common_vendor.index.showModal({
- title: "权限不足",
- content: '获取手机号功能需要:\n1. 小程序企业认证"权限 \n2. 在真机上测试',
- showCancel: false
- });
- } else {
- common_vendor.index.showToast({
- title: "授权失败,请重试",
- icon: "none"
- });
- }
- return;
- }
- try {
- common_vendor.index.showLoading({ title: "验证中..." });
- const loginRes = await this.wxLoginAsync();
- const newLoginCode = loginRes.code;
- const params = {
- loginCode: newLoginCode,
- phoneCode: e.detail.code,
- encryptedData: e.detail.encryptedData,
- iv: e.detail.iv
- };
- console.log("[登录] 发送手机号验证请求");
- const result = await utils_auth.wxPhoneLogin(params);
- common_vendor.index.hideLoading();
- if (result && result.isSign === "false") {
- console.log("[登录] 需要完成注册");
- this.tempUserData = {
- openid: result.openid,
- unionid: result.unionid,
- phoneNumber: result.phoneNumber
- };
- await this.submitRegistration();
- } else if (result && result.isSign === "true") {
- console.log("[登录] 已注册用户,登录成功");
- this.handleLoginSuccess();
- } else {
- throw new Error("验证接口返回数据异常");
- }
- } catch (error) {
- common_vendor.index.hideLoading();
- console.error("[登录] 手机号验证失败:", error);
- let errorMsg = "验证失败,请重试";
- if (error.message) {
- if (error.message.includes("48001") || error.message.includes("未开通手机号快速验证组件")) {
- common_vendor.index.showModal({
- title: "权限未开通",
- content: '小程序未开通"手机号快速验证组件"权限\n\n请前往微信公众平台:\n开发 → 开发管理 → 接口设置\n开通"手机号快速验证组件"',
- showCancel: false
- });
- return;
- } else if (error.message.includes("40029") || error.message.includes("code无效")) {
- errorMsg = "phoneCode已失效,请重新授权";
- } else if (error.message.includes("40001") || error.message.includes("access_token")) {
- errorMsg = "access_token无效,请重试";
- } else if (error.message.includes("45011")) {
- errorMsg = "操作过于频繁,请稍后重试";
- } else {
- errorMsg = error.message;
- }
- }
- common_vendor.index.showToast({
- title: errorMsg,
- icon: "none",
- duration: 3e3
- });
- }
- },
- /**
- * 第四步:提交注册(新用户完成所有授权后)
- */
- async submitRegistration() {
- try {
- common_vendor.index.showLoading({ title: "注册中..." });
- const completeInfo = {
- openid: this.tempUserData.openid,
- unionid: this.tempUserData.unionid,
- phoneNumber: this.tempUserData.phoneNumber,
- nickname: this.tempUserProfile.nickname,
- avatarUrl: this.tempUserProfile.avatarUrl
- };
- console.log("[登录] 提交用户信息");
- await utils_auth.wxCompleteUserInfo(completeInfo);
- if (this.tempUserProfile.tempAvatarPath) {
- console.log("[登录] 开始上传头像到服务器");
- try {
- const uploadedUrl = await this.uploadAvatarWithToken(this.tempUserProfile.tempAvatarPath);
- console.log("[登录] 头像上传成功:", uploadedUrl);
- if (uploadedUrl) {
- const updateResult = await utils_api.updateUserProfile({ avatar: uploadedUrl });
- console.log("[登录] updateUserProfile返回:", updateResult);
- const userInfoLocal = common_vendor.index.getStorageSync("user_info");
- if (userInfoLocal) {
- const parsed = JSON.parse(userInfoLocal);
- parsed.avatar = uploadedUrl;
- common_vendor.index.setStorageSync("user_info", JSON.stringify(parsed));
- }
- }
- } catch (uploadErr) {
- console.warn("[登录] 头像上传失败:", uploadErr);
- }
- }
- common_vendor.index.hideLoading();
- console.log("[登录] 注册成功");
- this.handleLoginSuccess();
- } catch (error) {
- common_vendor.index.hideLoading();
- console.error("[登录] 注册失败:", error);
- common_vendor.index.showToast({
- title: error.message || "注册失败,请重试",
- icon: "none",
- duration: 2e3
- });
- }
- },
- /**
- * 带token上传头像
- */
- uploadAvatarWithToken(filePath) {
- return new Promise((resolve, reject) => {
- common_vendor.index.uploadFile({
- url: utils_api.uploadFile.url,
- filePath,
- name: "file",
- header: {
- "Authorization": `Bearer ${common_vendor.index.getStorageSync("user_token") || ""}`
- },
- success: (res) => {
- const data = JSON.parse(res.data);
- if (data.code === 200 && data.data && data.data.url) {
- resolve(data.data.url);
- } else {
- reject(new Error(data.message || "上传失败"));
- }
- },
- fail: (err) => {
- reject(err);
- }
- });
- });
- },
- /**
- * 登录成功后的处理
- */
- handleLoginSuccess() {
- common_vendor.index.showToast({
- title: "登录成功",
- icon: "success",
- duration: 1500
- });
- setTimeout(() => {
- const pages = getCurrentPages();
- if (pages.length > 1) {
- common_vendor.index.navigateBack();
- } else {
- common_vendor.index.switchTab({
- url: "/pages/index/index"
- });
- }
- }, 1500);
- },
- /**
- * 协议勾选变化
- */
- handleAgreementChange(e) {
- this.agreedToTerms = e.detail.value.length > 0;
- },
- /**
- * 显示协议内容
- */
- showAgreement(type) {
- const title = type === "user" ? "用户协议" : "隐私政策";
- common_vendor.index.showModal({
- title,
- content: "这里显示协议内容...",
- showCancel: false
- });
- },
- /**
- * 封装 wx.login 为 Promise
- */
- wxLoginAsync() {
- return new Promise((resolve, reject) => {
- common_vendor.index.login({
- provider: "weixin",
- success: (res) => {
- resolve(res);
- },
- fail: (err) => {
- reject(err);
- }
- });
- });
- }
- }
- };
- if (!Array) {
- const _component_user_info_popup = common_vendor.resolveComponent("user-info-popup");
- _component_user_info_popup();
- }
- function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
- return common_vendor.e({
- a: common_vendor.o((...args) => $options.handleBack && $options.handleBack(...args)),
- b: $data.showOneClickLogin
- }, $data.showOneClickLogin ? {
- c: common_vendor.o((...args) => $options.handleWxLogin && $options.handleWxLogin(...args))
- } : $data.showPhoneAuth ? {
- e: common_vendor.o((...args) => $options.handleGetPhoneNumber && $options.handleGetPhoneNumber(...args))
- } : {
- f: common_vendor.o((...args) => $options.handleGetUserProfile && $options.handleGetUserProfile(...args))
- }, {
- d: $data.showPhoneAuth,
- g: $data.agreedToTerms,
- h: common_vendor.o(($event) => $options.showAgreement("user")),
- i: common_vendor.o(($event) => $options.showAgreement("privacy")),
- j: common_vendor.o((...args) => $options.handleAgreementChange && $options.handleAgreementChange(...args)),
- k: common_vendor.sr("userInfoPopup", "cdfe2409-0"),
- l: common_vendor.o($options.handleUserInfoConfirm)
- });
- }
- 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);
|