login.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. try {
  84. // 查询校验
  85. const { data } = await models.wx_teacher_user.get({
  86. filter: {
  87. where: {
  88. phone: phone,
  89. school_id: selected._id,
  90. name: contactsname
  91. }
  92. },
  93. envType: "prod",
  94. });
  95. if (data && Object.keys(data).length > 0) {
  96. const user = Array.isArray(data) ? data[0] : data;
  97. // 更新
  98. await models.wx_teacher_user.update({
  99. filter: {
  100. where: {
  101. $and: [
  102. {
  103. _id: {
  104. $eq: user._id, // 推荐传入_id数据标识进行操作
  105. },
  106. },
  107. ]
  108. }
  109. },
  110. data: {
  111. bound: 1
  112. },
  113. envType: "prod"
  114. });
  115. // 登录成功,保存用户信息
  116. wx.setStorageSync('userInfo', user);
  117. wx.showToast({
  118. title: '绑定成功',
  119. icon: 'success',
  120. duration: 1500
  121. });
  122. setTimeout(() => {
  123. wx.reLaunch({
  124. url: '/pages/index/index'
  125. });
  126. }, 1500);
  127. } else {
  128. wx.showToast({
  129. title: '绑定失败,请确认信息是否正确',
  130. icon: 'none'
  131. });
  132. }
  133. } catch (error) {
  134. console.error('绑定失败:', error);
  135. wx.showToast({
  136. title: '请求失败,请稍后再试',
  137. icon: 'none'
  138. });
  139. }
  140. }
  141. })