dictionary.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. },
  14. // 返回
  15. back: function () {
  16. wx.navigateBack({ delta: 1 })
  17. },
  18. // 信息添加
  19. toAdd: function () {
  20. const that = this;
  21. that.setData({ skip: 0, page: 0, list: [] })
  22. wx.navigateTo({ url: `/pages/superAdmin/system/adddictionary` })
  23. },
  24. // 信息维护
  25. toEdit: function (e) {
  26. const that = this;
  27. const { item } = e.currentTarget.dataset;
  28. that.setData({ skip: 0, page: 0, list: [] })
  29. wx.navigateTo({ url: `/pages/superAdmin/system/adddictionary?id=${item.id}` })
  30. },
  31. // 信息删除
  32. toDel: async function (e) {
  33. const that = this;
  34. const { item } = e.currentTarget.dataset;
  35. wx.showModal({
  36. title: '提示',
  37. content: '是否确认删除该条数据?',
  38. async success(res) {
  39. if (res.confirm) {
  40. const arr = await app.$delete(`/dict/${item._id}`);
  41. if (arr.errcode == '0') {
  42. wx.showToast({ title: `删除信息成功`, icon: 'success', duration: 2000 })
  43. that.setData({ skip: 0, page: 0, list: [] })
  44. that.watchLogin()
  45. } else {
  46. wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  47. }
  48. }
  49. }
  50. })
  51. },
  52. // 分页
  53. toPage: function () {
  54. const that = this;
  55. let list = that.data.list;
  56. let limit = that.data.limit;
  57. if (that.data.total > list.length) {
  58. wx.showLoading({ title: '加载中', mask: true })
  59. let page = that.data.page + 1;
  60. that.setData({ page: page })
  61. let skip = page * limit;
  62. that.setData({ skip: skip })
  63. that.watchLogin();
  64. wx.hideLoading()
  65. } else { wx.showToast({ title: '没有更多数据了', icon: 'none', duration: 2000 }) }
  66. },
  67. /**
  68. * 生命周期函数--监听页面加载
  69. */
  70. onLoad: function (options) {
  71. },
  72. // 监听用户是否登录
  73. watchLogin: async function () {
  74. const that = this;
  75. wx.getStorage({
  76. key: 'user',
  77. success: async res => {
  78. let info = { skip: that.data.skip, limit: that.data.limit };
  79. const arr = await app.$get(`/dict`, { ...info });
  80. if (arr.errcode == '0') {
  81. that.setData({ list: [...that.data.list, ...arr.data] });
  82. that.setData({ total: arr.total })
  83. }
  84. else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }); }
  85. },
  86. fail: async res => {
  87. wx.redirectTo({ url: '/pages/index/index' })
  88. }
  89. })
  90. },
  91. /**
  92. * 生命周期函数--监听页面初次渲染完成
  93. */
  94. onReady: function () {
  95. },
  96. /**
  97. * 生命周期函数--监听页面显示
  98. */
  99. onShow: function () {
  100. const that = this;
  101. // 监听用户是否登录
  102. that.watchLogin();
  103. },
  104. /**
  105. * 生命周期函数--监听页面隐藏
  106. */
  107. onHide: function () {
  108. },
  109. /**
  110. * 生命周期函数--监听页面卸载
  111. */
  112. onUnload: function () {
  113. },
  114. /**
  115. * 页面相关事件处理函数--监听用户下拉动作
  116. */
  117. onPullDownRefresh: function () {
  118. },
  119. /**
  120. * 页面上拉触底事件的处理函数
  121. */
  122. onReachBottom: function () {
  123. },
  124. /**
  125. * 用户点击右上角分享
  126. */
  127. onShareAppMessage: function () {
  128. }
  129. })