logins.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { models, db } from '../../utils/cloudbase.js'
  2. Page({
  3. data: {
  4. phone: '', // 用户输入的手机号码
  5. code: '' // 用户输入的验证码
  6. },
  7. // 监听手机号码输入
  8. onInputPhone(e) {
  9. this.setData({
  10. phone: e.detail.value
  11. });
  12. },
  13. // 监听验证码输入
  14. onInputCode(e) {
  15. this.setData({
  16. code: e.detail.value
  17. });
  18. },
  19. // 获取验证码
  20. getCode() {
  21. const { phone } = this.data;
  22. if (!phone || !/^1[3-9]\d{9}$/.test(phone)) {
  23. wx.showToast({
  24. title: '请输入有效的手机号码',
  25. icon: 'none'
  26. });
  27. return;
  28. }
  29. const code = this.generateCode(); // 本地生成验证码(用于发送)
  30. // 保存验证码到 data
  31. this.setData({ code });
  32. console.log(code, 'codecode');
  33. },
  34. generateCode() {
  35. return Math.floor(100000 + Math.random() * 900000).toString();
  36. },
  37. // 确定按钮点击事件
  38. async onConfirm() {
  39. const { phone, code } = this.data;
  40. if (!phone || !code) {
  41. wx.showToast({
  42. title: '请输入手机号码和验证码',
  43. icon: 'none'
  44. });
  45. return;
  46. }
  47. const { data } = await models.wx_user.get({
  48. filter: {
  49. where: {
  50. phone: this.data.phone
  51. }
  52. },
  53. // envType: pre 体验环境, prod 正式环境
  54. envType: "prod",
  55. });
  56. console.log('data',data);
  57. if (!data || Object.keys(data).length === 0) {
  58. wx.showToast({
  59. title: '该手机号未注册,请换其他手机号进行登录',
  60. icon: 'none'
  61. });
  62. return;
  63. }
  64. const user = data; // 取第一条用户数据
  65. // 登录成功,存储用户信息
  66. wx.setStorageSync('userInfo', user);
  67. wx.showToast({
  68. title: '登录成功',
  69. icon: 'success'
  70. });
  71. wx.switchTab({
  72. url: '/pages/index/index'
  73. });
  74. }
  75. })