| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- // import { models, db } from '../../utils/cloudbase.js'
- import { getDB, getModels, getCommand, getTempFileURLs } from '../../utils/cloudbase.js'
- Page({
- data: {
- phone: '', // 用户输入的手机号码
- logins: true
- },
- async onLoad(options) {
- if (options.phone) {
- console.log('注册跳转手机号:', options.phone);
- this.setData({
- phone: options.phone,
- logins: false // 隐藏验证码
- });
- // 自动登录
- await this.autoLogin(options.phone);
- }
- },
- // 监听手机号码输入
- onInputPhone(e) {
- this.setData({
- phone: e.detail.value
- });
- },
- // 确定按钮点击事件
- async onConfirm() {
- const { phone } = this.data;
- if (!phone) {
- wx.showToast({
- title: '请输入手机号码',
- icon: 'none'
- });
- return;
- }
- if (!/^1[3-9]\d{9}$/.test(phone)) {
- wx.showToast({
- title: '输入的手机号码有误',
- icon: 'none'
- });
- return;
- }
- const childComponent = this.selectComponent('#loginCaptcha');
- const result = childComponent.validCaptcha()
- if (!result.flag) {
- wx.showToast({
- title: result.msg,
- icon: 'none'
- });
- return
- }
- const models = await getModels()
-
- const { data } = await models.wx_user.list({
- filter: {
- where: {
- phone: this.data.phone
- }
- },
- pageSize: 100, // 分页大小,建议指定,如需设置为其它值,需要和 pageNumber 配合使用,两者同时指定才会生效
- pageNumber: 1, // 第几页
- getCount: true, // 开启用来获取总数
- // envType: pre 体验环境, prod 正式环境
- envType: "prod",
- });
- const users = data.records || [];
- // 如果查不到任何数据
- if (users.length === 0) {
- wx.showToast({
- title: '该手机号未注册,请换其他手机号登录',
- icon: 'none'
- });
- return;
- }
- // ✅ 如果查到了,但全部都是 delete === 1,则视为无有效账号
- const allDeleted = users.every(user => user.delete === 1);
- if (allDeleted) {
- wx.showToast({
- title: '该手机号未注册,请换其他手机号登录',
- icon: 'none'
- });
- return;
- }
- // ✅ 只要有一个 delete === 0,就继续登录
- const validUser = users.find(user => user.delete === 0);
- wx.setStorageSync('userInfo', validUser);
-
- wx.showToast({
- title: '登录成功',
- icon: 'success'
- });
-
- wx.switchTab({
- url: '/pages/index/index'
- });
- },
- // 自动登录逻辑
- async autoLogin(phone) {
- try {
- if (!/^1[3-9]\d{9}$/.test(phone)) {
- wx.showToast({
- title: '手机号格式错误',
- icon: 'none'
- });
- return;
- }
- const models = await getModels();
- const { data } = await models.wx_user.list({
- filter: { where: { phone } },
- pageSize: 100,
- pageNumber: 1,
- getCount: true,
- envType: "prod",
- });
- const users = data.records || [];
- if (users.length === 0 || users.every(user => user.delete === 1)) {
- wx.showToast({
- title: '该手机号未注册,请重新注册',
- icon: 'none'
- });
- return;
- }
- // 找到有效账号
- const validUser = users.find(user => user.delete === 0);
- wx.setStorageSync('userInfo', validUser);
- wx.showToast({
- title: '登录成功',
- icon: 'success'
- });
- wx.switchTab({
- url: '/pages/index/index'
- });
- } catch (err) {
- console.error('自动登录失败:', err);
- wx.showToast({
- title: '登录失败,请稍后重试',
- icon: 'none'
- });
- }
- }
- })
|