1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- import { models, db } from '../../utils/cloudbase.js'
- Page({
- data: {
- phone: '', // 用户输入的手机号码
- code: '' // 用户输入的验证码
- },
- // 监听手机号码输入
- onInputPhone(e) {
- this.setData({
- phone: e.detail.value
- });
- },
- // 监听验证码输入
- onInputCode(e) {
- this.setData({
- code: e.detail.value
- });
- },
- // 获取验证码
- getCode() {
- const { phone } = this.data;
- if (!phone || !/^1[3-9]\d{9}$/.test(phone)) {
- wx.showToast({
- title: '请输入有效的手机号码',
- icon: 'none'
- });
- return;
- }
- const code = this.generateCode(); // 本地生成验证码(用于发送)
- // 保存验证码到 data
- this.setData({ code });
- console.log(code, 'codecode');
- },
-
- generateCode() {
- return Math.floor(100000 + Math.random() * 900000).toString();
- },
- // 确定按钮点击事件
- async onConfirm() {
- const { phone, code } = this.data;
- if (!phone || !code) {
- wx.showToast({
- title: '请输入手机号码和验证码',
- icon: 'none'
- });
- return;
- }
- const { data } = await models.wx_user.get({
- filter: {
- where: {
- phone: this.data.phone
- }
- },
- // envType: pre 体验环境, prod 正式环境
- envType: "prod",
- });
- console.log('data',data);
- if (!data || Object.keys(data).length === 0) {
- wx.showToast({
- title: '该手机号未注册,请换其他手机号进行登录',
- icon: 'none'
- });
- return;
- }
- const user = data; // 取第一条用户数据
- // 登录成功,存储用户信息
- wx.setStorageSync('userInfo', user);
-
- wx.showToast({
- title: '登录成功',
- icon: 'success'
- });
-
- wx.switchTab({
- url: '/pages/index/index'
- });
- }
- })
|