logins.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_teacher_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. if (validUser.bound === 0) {
  71. // 第一次登录去绑定
  72. wx.showToast({
  73. title: '首次登录,请先绑定信息',
  74. icon: 'none',
  75. duration: 1500
  76. });
  77. setTimeout(() => {
  78. wx.navigateTo({
  79. url: `/pages/login/login?phone=${encodeURIComponent(this.data.phone)}`
  80. });
  81. }, 1500); // 等提示展示完再跳转
  82. } else {
  83. // 登录成功,存储用户信息
  84. wx.setStorageSync('userInfo', validUser);
  85. wx.showToast({
  86. title: '登录成功',
  87. icon: 'success'
  88. });
  89. wx.switchTab({
  90. url: '/pages/index/index'
  91. });
  92. }
  93. }
  94. })