list.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. const app = getApp()
  2. import QRCode from '../../../utils/weapp-qrcode.js';
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. frameStyle: { useTop: true, name: '学员信息', leftArrow: true, useBar: false },
  9. list: [],
  10. total: 0,
  11. page: 0,
  12. skip: 0,
  13. limit: 5,
  14. //运动等级
  15. levelList: [],
  16. // dialog弹框
  17. dialog: { title: '账号绑定', show: false, type: '1' },
  18. form: {}
  19. },
  20. // 返回
  21. back: function () {
  22. wx.navigateBack({ delta: 1 })
  23. },
  24. //添加或修改
  25. toCommon: function (e) {
  26. const that = this;
  27. const { route, item } = e.currentTarget.dataset;
  28. that.setData({ skip: 0, page: 0, list: [] })
  29. if (item && item._id) wx.navigateTo({ url: `/pages/${route}?id=${item._id}` })
  30. else wx.navigateTo({ url: `/pages/${route}` })
  31. },
  32. // 删除
  33. toDel: async function (e) {
  34. const that = this;
  35. const { item } = e.currentTarget.dataset;
  36. wx.showModal({
  37. title: '提示',
  38. content: '是否确认删除该条数据?',
  39. async success(res) {
  40. if (res.confirm) {
  41. const arr = await app.$delete(`/student/${item._id}`);
  42. if (arr.errcode == '0') {
  43. wx.showToast({ title: `删除信息成功`, icon: 'success', duration: 2000 })
  44. that.setData({ skip: 0, page: 0, list: [] })
  45. that.watchLogin()
  46. } else {
  47. wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  48. }
  49. }
  50. }
  51. })
  52. },
  53. // 绑定账号
  54. toBind: async function (e) {
  55. const that = this;
  56. let { item } = e.currentTarget.dataset;
  57. const arr = await app.$get(`/student/${item.id}`);
  58. if (arr.errcode == '0') {
  59. that.setData({ form: arr.data })
  60. // 生成二维码
  61. that.makeQRCode();
  62. that.setData({ dialog: { title: '账号绑定', show: true, type: '1' } })
  63. } else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) }
  64. },
  65. makeQRCode(template = 0) {
  66. const that = this;
  67. const url = `${that.data.form._id}&&'3'`;
  68. var qrcode = new QRCode(`myQrcode`, {
  69. text: url,
  70. width: 110,
  71. height: 110,
  72. padding: 3,
  73. colorDark: "#000000",
  74. colorLight: "#ffffff",
  75. correctLevel: QRCode.CorrectLevel.L,
  76. });
  77. },
  78. // 关闭弹框
  79. toClose: function () {
  80. const that = this;
  81. that.setData({ form: {} })
  82. that.setData({ dialog: { title: '账号绑定', show: false, type: '1' } })
  83. },
  84. // 分页
  85. toPage: function () {
  86. const that = this;
  87. let list = that.data.list;
  88. let limit = that.data.limit;
  89. if (that.data.total > list.length) {
  90. wx.showLoading({ title: '加载中', mask: true })
  91. let page = that.data.page + 1;
  92. that.setData({ page: page })
  93. let skip = page * limit;
  94. that.setData({ skip: skip })
  95. that.watchLogin();
  96. wx.hideLoading()
  97. } else { wx.showToast({ title: '没有更多数据了', icon: 'none', duration: 2000 }) }
  98. },
  99. /**
  100. * 生命周期函数--监听页面加载
  101. */
  102. onLoad: function (options) {
  103. },
  104. /**
  105. * 生命周期函数--监听页面初次渲染完成
  106. */
  107. onReady: function () {
  108. },
  109. /**
  110. * 生命周期函数--监听页面显示
  111. */
  112. onShow: async function () {
  113. const that = this;
  114. // 查询其他信息
  115. await that.searchOther();
  116. // 监听用户是否登录
  117. await that.watchLogin();
  118. },
  119. searchOther: async function () {
  120. const that = this;
  121. let arr;
  122. // 运动等级
  123. arr = await app.$get(`/dict`, { code: 'student_grade' });
  124. if (arr.errcode == '0' && arr.total > 0) {
  125. let list = arr.data[0].list;
  126. that.setData({ levelList: list })
  127. }
  128. },
  129. // 监听用户是否登录
  130. watchLogin: async function () {
  131. const that = this;
  132. let levelList = that.data.levelList;
  133. wx.getStorage({
  134. key: 'user',
  135. success: async res => {
  136. let info = { skip: that.data.skip, limit: that.data.limit };
  137. let arr = await app.$get(`/student`, { ...info });
  138. if (arr.errcode == '0') {
  139. for (const val of arr.data) { let level = levelList.find(i => i.value == val.level); if (level) val.zhLevel = level.label; }
  140. that.setData({ list: [...that.data.list, ...arr.data] })
  141. that.setData({ total: arr.total })
  142. } else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) }
  143. },
  144. fail: async res => {
  145. wx.redirectTo({ url: '/pages/index/index' })
  146. }
  147. })
  148. },
  149. /**
  150. * 生命周期函数--监听页面隐藏
  151. */
  152. onHide: function () {
  153. },
  154. /**
  155. * 生命周期函数--监听页面卸载
  156. */
  157. onUnload: function () {
  158. },
  159. /**
  160. * 页面相关事件处理函数--监听用户下拉动作
  161. */
  162. onPullDownRefresh: function () {
  163. },
  164. /**
  165. * 页面上拉触底事件的处理函数
  166. */
  167. onReachBottom: function () {
  168. },
  169. /**
  170. * 用户点击右上角分享
  171. */
  172. onShareAppMessage: function () {
  173. }
  174. })