list.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. const app = getApp()
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. frameStyle: { useTop: true, name: '账号管理', leftArrow: true, useBar: false },
  8. list: [],
  9. total: 0,
  10. page: 0,
  11. skip: 0,
  12. limit: 5,
  13. typeList: [
  14. { label: '超级管理员', value: '-1' },
  15. { label: '普通用户', value: '0' },
  16. { label: '管理员', value: '1' },
  17. { label: '教练', value: '2' },
  18. { label: '学员', value: '3' },
  19. { label: '游客', value: '10' },
  20. ]
  21. },
  22. // 返回
  23. back: function () {
  24. wx.navigateBack({ delta: 1 })
  25. },
  26. // 信息添加
  27. toAdd: function () {
  28. that.setData({ skip: 0, page: 0, list: [] })
  29. wx.navigateTo({ url: `/pages/suser/add` })
  30. },
  31. // 信息维护
  32. toEdit: function (e) {
  33. const that = this;
  34. const { item } = e.currentTarget.dataset;
  35. that.setData({ skip: 0, page: 0, list: [] })
  36. wx.navigateTo({ url: `/pages/suser/add?id=${item.id}` })
  37. },
  38. // 信息删除
  39. toDel: async function (e) {
  40. const that = this;
  41. const { item } = e.currentTarget.dataset;
  42. wx.showModal({
  43. title: '提示',
  44. content: '是否确认删除该条数据?',
  45. async success(res) {
  46. if (res.confirm) {
  47. const arr = await app.$delete(`/user/${item._id}`);
  48. if (arr.errcode == '0') {
  49. wx.showToast({ title: `删除信息成功`, icon: 'success', duration: 2000 })
  50. that.setData({ skip: 0, page: 0, list: [] })
  51. that.watchLogin()
  52. } else {
  53. wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  54. }
  55. }
  56. }
  57. })
  58. },
  59. // 分页
  60. toPage: function () {
  61. const that = this;
  62. let list = that.data.list;
  63. let limit = that.data.limit;
  64. if (that.data.total > list.length) {
  65. wx.showLoading({ title: '加载中', mask: true })
  66. let page = that.data.page + 1;
  67. that.setData({ page: page })
  68. let skip = page * limit;
  69. that.setData({ skip: skip })
  70. that.watchLogin();
  71. wx.hideLoading()
  72. } else { wx.showToast({ title: '没有更多数据了', icon: 'none', duration: 2000 }) }
  73. },
  74. /**
  75. * 生命周期函数--监听页面加载
  76. */
  77. onLoad: function (options) {
  78. },
  79. // 监听用户是否登录
  80. watchLogin: async function () {
  81. const that = this;
  82. wx.getStorage({
  83. key: 'user',
  84. success: async res => {
  85. let info = { skip: that.data.skip, limit: that.data.limit };
  86. const arr = await app.$get(`/user`, { ...info });
  87. if (arr.errcode == '0') {
  88. for (const val of arr.data) { val.zHtype = this.searchType(val.type) }
  89. that.setData({ list: [...that.data.list, ...arr.data] });
  90. that.setData({ total: arr.total })
  91. }
  92. else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }); }
  93. },
  94. fail: async res => {
  95. wx.redirectTo({ url: '/pages/index/index' })
  96. }
  97. })
  98. },
  99. // 用户类型
  100. searchType: function (e) {
  101. const that = this;
  102. let data = that.data.typeList.find(i => i.value == e);
  103. if (data) return data.label;
  104. else return '暂无';
  105. },
  106. /**
  107. * 生命周期函数--监听页面初次渲染完成
  108. */
  109. onReady: function () {
  110. },
  111. /**
  112. * 生命周期函数--监听页面显示
  113. */
  114. onShow: function () {
  115. const that = this;
  116. // 监听用户是否登录
  117. that.watchLogin();
  118. },
  119. /**
  120. * 生命周期函数--监听页面隐藏
  121. */
  122. onHide: function () {
  123. },
  124. /**
  125. * 生命周期函数--监听页面卸载
  126. */
  127. onUnload: function () {
  128. },
  129. /**
  130. * 页面相关事件处理函数--监听用户下拉动作
  131. */
  132. onPullDownRefresh: function () {
  133. },
  134. /**
  135. * 页面上拉触底事件的处理函数
  136. */
  137. onReachBottom: function () {
  138. },
  139. /**
  140. * 用户点击右上角分享
  141. */
  142. onShareAppMessage: function () {
  143. }
  144. })