login.js 5.9 KB

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