logins.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { models, db } from '../../utils/cloudbase.js'
  2. Page({
  3. data: {
  4. phone: '', // 用户输入的手机号码
  5. },
  6. // 监听手机号码输入
  7. onInputPhone(e) {
  8. this.setData({
  9. phone: e.detail.value
  10. });
  11. },
  12. // 确定按钮点击事件
  13. async onConfirm() {
  14. const { phone } = this.data;
  15. if (!phone) {
  16. wx.showToast({
  17. title: '请输入手机号码',
  18. icon: 'none'
  19. });
  20. return;
  21. }
  22. if (!/^1[3-9]\d{9}$/.test(phone)) {
  23. wx.showToast({
  24. title: '输入的手机号码有误',
  25. icon: 'none'
  26. });
  27. return;
  28. }
  29. const childComponent = this.selectComponent('#loginCaptcha');
  30. const result = childComponent.validCaptcha()
  31. if (!result.flag) {
  32. wx.showToast({
  33. title: result.msg,
  34. icon: 'none'
  35. });
  36. return
  37. }
  38. const { data } = await models.wx_user.list({
  39. filter: {
  40. where: {
  41. phone: this.data.phone
  42. }
  43. },
  44. pageSize: 100, // 分页大小,建议指定,如需设置为其它值,需要和 pageNumber 配合使用,两者同时指定才会生效
  45. pageNumber: 1, // 第几页
  46. getCount: true, // 开启用来获取总数
  47. // envType: pre 体验环境, prod 正式环境
  48. envType: "prod",
  49. });
  50. const users = data.records || [];
  51. // 如果查不到任何数据
  52. if (users.length === 0) {
  53. wx.showToast({
  54. title: '该手机号未注册,请换其他手机号登录',
  55. icon: 'none'
  56. });
  57. return;
  58. }
  59. // ✅ 如果查到了,但全部都是 delete === 1,则视为无有效账号
  60. const allDeleted = users.every(user => user.delete === 1);
  61. if (allDeleted) {
  62. wx.showToast({
  63. title: '该手机号未注册,请换其他手机号登录',
  64. icon: 'none'
  65. });
  66. return;
  67. }
  68. // ✅ 只要有一个 delete === 0,就继续登录
  69. const validUser = users.find(user => user.delete === 0);
  70. wx.setStorageSync('userInfo', validUser);
  71. wx.showToast({
  72. title: '登录成功',
  73. icon: 'success'
  74. });
  75. wx.switchTab({
  76. url: '/pages/index/index'
  77. });
  78. }
  79. })