logins.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { models, db } from '../../utils/cloudbase.js'
  2. import Dialog from '@vant/weapp/dialog/dialog';
  3. Page({
  4. data: {
  5. phone: '', // 用户输入的手机号码
  6. name: '',
  7. yuan: '',
  8. falog: false,
  9. },
  10. // 监听手机号码输入
  11. onInputPhone(e) {
  12. this.setData({
  13. phone: e.detail.value
  14. });
  15. },
  16. onInputName(e) {
  17. this.setData({
  18. name: e.detail.value
  19. });
  20. },
  21. onInputYuan(e) {
  22. this.setData({
  23. yuan: e.detail.value
  24. });
  25. },
  26. // 确定按钮点击事件
  27. async onConfirm() {
  28. const { phone } = this.data;
  29. if (!phone) {
  30. wx.showToast({
  31. title: '请输入手机号码',
  32. icon: 'none'
  33. });
  34. return;
  35. }
  36. if (!/^1[3-9]\d{9}$/.test(phone)) {
  37. wx.showToast({
  38. title: '输入的手机号码有误',
  39. icon: 'none'
  40. });
  41. return;
  42. }
  43. const childComponent = this.selectComponent('#loginCaptcha');
  44. const result = childComponent.validCaptcha()
  45. if (!result.flag) {
  46. wx.showToast({
  47. title: result.msg,
  48. icon: 'none'
  49. });
  50. return
  51. }
  52. const { data } = await models.wx_teacher_user.list({
  53. filter: {
  54. where: {
  55. phone: this.data.phone
  56. }
  57. },
  58. pageSize: 100, // 分页大小,建议指定,如需设置为其它值,需要和 pageNumber 配合使用,两者同时指定才会生效
  59. pageNumber: 1, // 第几页
  60. getCount: true, // 开启用来获取总数
  61. // envType: pre 体验环境, prod 正式环境
  62. envType: "prod",
  63. });
  64. let users = data.records || [];
  65. // 如果查不到任何数据
  66. if (users.length === 0) {
  67. wx.showToast({
  68. title: '未查询该用户,请换其他手机号登录',
  69. icon: 'none'
  70. });
  71. return;
  72. }
  73. // ✅ 如果查到了,但全部都是 delete === 1,则视为无有效账号
  74. const allDeleted = users.every(user => user.delete === 1);
  75. if (allDeleted) {
  76. wx.showToast({
  77. title: '该手机号未注册,请换其他手机号登录',
  78. icon: 'none'
  79. });
  80. return;
  81. }
  82. // ✅ 只要有一个 delete === 0,就继续登录
  83. const validUser = users.find(user => user.delete === 0);
  84. if (validUser.bound === 0) {
  85. console.log(users[0].school_id, 'users._idusers._idusers._id');
  86. const { datass } = await models.wx_teacher_user.update({
  87. data: {
  88. bound: 1, // 是否绑定
  89. },
  90. filter: {
  91. where: {
  92. $and: [
  93. {
  94. _id: {
  95. $eq: users[0]._id, // 推荐传入_id数据标识进行操作
  96. },
  97. },
  98. ]
  99. }
  100. },
  101. envType: "prod",
  102. });
  103. const schoolRes = await models.wx_school.get({
  104. filter: {
  105. where: {
  106. _id: users[0].school_id
  107. }
  108. },
  109. envType: "prod",
  110. });
  111. console.log(schoolRes, 'schoolRes');
  112. const school = schoolRes.data; // 这里才是你要的园所信息
  113. this.setData({
  114. name: users[0].name,
  115. yuan: school.name,
  116. falog: true,
  117. })
  118. // onConfirm 成功时
  119. wx.setStorageSync('userInfo', validUser);
  120. // Dialog.confirm({
  121. // title: '用户信息',
  122. // message: `姓名:${validUser.name}\n园所:${school.name || '未知园所'}`,
  123. // showCancelButton: false,
  124. // }).then(() => {
  125. // wx.setStorageSync('userInfo', {
  126. // ...validUser,
  127. // bound: 1
  128. // });
  129. // wx.switchTab({
  130. // url: '/pages/index/index'
  131. // });
  132. // });
  133. // setTimeout(() => {
  134. // wx.navigateTo({
  135. // url: `/pages/login/login?phone=${encodeURIComponent(this.data.phone)}`
  136. // });
  137. // }, 1500); // 等提示展示完再跳转
  138. } else {
  139. // 登录成功,存储用户信息
  140. wx.setStorageSync('userInfo', validUser);
  141. wx.showToast({
  142. title: '登录成功',
  143. icon: 'success'
  144. });
  145. wx.switchTab({
  146. url: '/pages/index/index'
  147. });
  148. }
  149. },
  150. gotuhome() {
  151. const validUser = wx.getStorageSync('userInfo');
  152. if (!validUser) {
  153. wx.showToast({
  154. title: '用户信息丢失,请重新登录',
  155. icon: 'none'
  156. });
  157. return;
  158. }
  159. wx.showToast({
  160. title: '登录成功',
  161. icon: 'success'
  162. });
  163. wx.switchTab({
  164. url: '/pages/index/index'
  165. });
  166. }
  167. })