login.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { models, db } from '../../utils/cloudbase.js'
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. levelOptions: ['小班', '中班', '大班'],
  8. selectedLevelIndex: 0,
  9. xiala: '',
  10. phone: '',
  11. gardenList: [], // 这里替换为实际园所名称数组
  12. gardenIndex: null, // 选中的园所
  13. contactsname: '',
  14. selected: {},
  15. },
  16. // 监听手机号输入框
  17. onPhoneInput(e) {
  18. this.setData({
  19. phone: e.detail.value
  20. });
  21. },
  22. // 监听姓名输入框
  23. onContactsInput(e) {
  24. this.setData({
  25. contactsname: e.detail.value
  26. });
  27. },
  28. onLoad(options) {
  29. this.onScholl();
  30. const fileIDs = [
  31. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/xiala.png',
  32. ]
  33. // 并发下载多个 fileID
  34. Promise.all(
  35. fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  36. ).then(results => {
  37. // 每个 result 对应一个下载结果
  38. const tempFilePaths = results.map(r => r.tempFilePath);
  39. console.log('全部下载成功:', tempFilePaths);
  40. this.setData({
  41. xiala: tempFilePaths[0],
  42. });
  43. }).catch(err => {
  44. console.error('有文件下载失败:', err);
  45. });
  46. },
  47. // 获取学校数据
  48. async onScholl() {
  49. try {
  50. let allRecords = [];
  51. let pageNumber = 1;
  52. const pageSize = 100; // 每次查询 100 条
  53. while (true) {
  54. const { data } = await models.wx_school.list({
  55. filter: {
  56. where: {}
  57. },
  58. pageSize: pageSize,
  59. pageNumber: pageNumber,
  60. getCount: true,
  61. envType: "prod",
  62. });
  63. if (data.records.length === 0) {
  64. break; // 没有更多数据,退出循环
  65. }
  66. allRecords = allRecords.concat(data.records); // 将当前页数据添加到全部数据中
  67. pageNumber++; // 查询下一页
  68. }
  69. this.setData({
  70. gardenList: allRecords // 按实际字段替换
  71. });
  72. console.log(this.data.gardenList, 'gardenList');
  73. } catch (error) {
  74. console.error('获取数据失败:', error);
  75. wx.showToast({
  76. title: '获取数据失败,请稍后再试',
  77. icon: 'none'
  78. });
  79. }
  80. },
  81. // 选择学校下拉框
  82. async onGardenChange(e) {
  83. const index = parseInt(e.detail.value);
  84. this.setData({
  85. gardenIndex: index
  86. });
  87. const selected = this.data.gardenList[index];
  88. this.setData({
  89. selected: selected
  90. })
  91. },
  92. // 注册
  93. async bindingevents() {
  94. try {
  95. // 选中的学校, 姓名, 电话号码
  96. const { gardenIndex, contactsname, phone } = this.data;
  97. if (gardenIndex == null) {
  98. wx.showToast({
  99. title: '请选择学校',
  100. icon: 'none'
  101. });
  102. return;
  103. }
  104. if (!contactsname) {
  105. wx.showToast({
  106. title: '请输入姓名',
  107. icon: 'none'
  108. });
  109. return;
  110. }
  111. if (!phone) {
  112. wx.showToast({
  113. title: '请输入手机号码',
  114. icon: 'none'
  115. });
  116. return;
  117. }
  118. if (!/^1[3-9]\d{9}$/.test(phone)) {
  119. wx.showToast({
  120. title: '输入的手机号码有误',
  121. icon: 'none'
  122. });
  123. return;
  124. }
  125. const childComponent = this.selectComponent('#registorCaptcha');
  126. const result = childComponent.validCaptcha()
  127. if (!result.flag) {
  128. wx.showToast({
  129. title: result.msg,
  130. icon: 'none'
  131. });
  132. return
  133. }
  134. const { data } = await models.wx_user.create({
  135. data: {
  136. img: "", // 头像
  137. gender: 1, // 性别
  138. teacher_id: "", // 老师id
  139. user_name: this.data.contactsname, // 用户名
  140. remark: "", // 备注
  141. delete: 0, // 逻辑删除
  142. school_id: this.data.selected._id, // 学校id
  143. phone: this.data.phone, // 手机号
  144. grade: this.data.selectedLevelIndex, // 班级
  145. log_off: "", // 注销原因
  146. name: this.data.contactsname, // 姓名
  147. },
  148. envType: "prod",
  149. });
  150. // 注册成功提示
  151. wx.showToast({
  152. title: '注册成功,去登录',
  153. icon: 'success',
  154. duration: 1500, // 提示显示 1.5 秒
  155. });
  156. // 提示消失后跳转到登录页面
  157. setTimeout(() => {
  158. wx.reLaunch({
  159. url: '/pages/logins/logins', // 替换为你的登录页面路径
  160. });
  161. }, 1500);
  162. } catch (error) {
  163. console.error('注册失败:', error);
  164. wx.showToast({
  165. title: '注册失败,请稍后再试',
  166. icon: 'none',
  167. });
  168. }
  169. }
  170. })