login.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { models, db } from '../../utils/cloudbase.js'
  2. Page({
  3. data: {
  4. phone: '',
  5. gardenList: [], // 这里替换为实际园所名称数组
  6. gardenIndex: null,
  7. contactsname: '',
  8. selected: {}
  9. },
  10. // 监听手机号输入框
  11. onPhoneInput(e) {
  12. this.setData({
  13. phone: e.detail.value
  14. });
  15. },
  16. // 监听姓名输入框
  17. onContactsInput(e) {
  18. this.setData({
  19. contactsname: e.detail.value
  20. });
  21. },
  22. onLoad(options) {
  23. this.onScholl();
  24. this.setData({
  25. phone: options.phone || ''
  26. });
  27. },
  28. // 获取学校数据
  29. async onScholl() {
  30. try {
  31. let allRecords = [];
  32. let pageNumber = 1;
  33. const pageSize = 100; // 每次查询 100 条
  34. while (true) {
  35. const { data } = await models.wx_school.list({
  36. filter: {
  37. where: {}
  38. },
  39. pageSize: pageSize,
  40. pageNumber: pageNumber,
  41. getCount: true,
  42. envType: "prod",
  43. });
  44. if (data.records.length === 0) {
  45. break; // 没有更多数据,退出循环
  46. }
  47. allRecords = allRecords.concat(data.records); // 将当前页数据添加到全部数据中
  48. pageNumber++; // 查询下一页
  49. }
  50. this.setData({
  51. gardenList: allRecords // 按实际字段替换
  52. });
  53. console.log(this.data.gardenList, 'gardenList');
  54. } catch (error) {
  55. console.error('获取数据失败:', error);
  56. wx.showToast({
  57. title: '获取数据失败,请稍后再试',
  58. icon: 'none'
  59. });
  60. }
  61. },
  62. // 选择学校下拉框
  63. async onGardenChange(e) {
  64. const index = parseInt(e.detail.value);
  65. this.setData({
  66. gardenIndex: index
  67. });
  68. const selected = this.data.gardenList[index];
  69. this.setData({
  70. selected: selected
  71. })
  72. },
  73. // 确定
  74. async bindingevents() {
  75. const { phone, contactsname, selected } = this.data;
  76. if (!phone || !contactsname || !selected) {
  77. wx.showToast({
  78. title: '请填写完整信息',
  79. icon: 'none'
  80. });
  81. return;
  82. }
  83. if (!/^1[3-9]\d{9}$/.test(phone)) {
  84. wx.showToast({
  85. title: '输入的手机号码有误',
  86. icon: 'none'
  87. });
  88. return;
  89. }
  90. const childComponent = this.selectComponent('#registorCaptcha');
  91. const result = childComponent.validCaptcha()
  92. if (!result.flag) {
  93. wx.showToast({
  94. title: result.msg,
  95. icon: 'none'
  96. });
  97. return
  98. }
  99. try {
  100. // 查询校验
  101. const { data } = await models.wx_teacher_user.get({
  102. filter: {
  103. where: {
  104. phone: phone,
  105. school_id: selected._id,
  106. name: contactsname
  107. }
  108. },
  109. envType: "prod",
  110. });
  111. if (data && Object.keys(data).length > 0) {
  112. const user = Array.isArray(data) ? data[0] : data;
  113. if (user.delete === 1) {
  114. wx.showToast({
  115. title: '该手机号未注册,请换其他手机号登录',
  116. icon: 'none'
  117. });
  118. return;
  119. }
  120. // 更新
  121. await models.wx_teacher_user.update({
  122. filter: {
  123. where: {
  124. $and: [
  125. {
  126. _id: {
  127. $eq: user._id, // 推荐传入_id数据标识进行操作
  128. },
  129. },
  130. ]
  131. }
  132. },
  133. data: {
  134. bound: 1
  135. },
  136. envType: "prod"
  137. });
  138. // 登录成功,保存用户信息
  139. wx.setStorageSync('userInfo', user);
  140. wx.showToast({
  141. title: '绑定成功',
  142. icon: 'success',
  143. duration: 1500
  144. });
  145. setTimeout(() => {
  146. wx.reLaunch({
  147. url: '/pages/index/index'
  148. });
  149. }, 1500);
  150. } else {
  151. wx.showToast({
  152. title: '绑定失败,请确认信息是否正确',
  153. icon: 'none'
  154. });
  155. }
  156. } catch (error) {
  157. console.error('绑定失败:', error);
  158. wx.showToast({
  159. title: '请求失败,请稍后再试',
  160. icon: 'none'
  161. });
  162. }
  163. }
  164. })