list.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. const app = getApp()
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. frameStyle: { useTop: true, name: '话题信息管理', leftArrow: true, useBar: false },
  8. searchInfo: {},
  9. list: [],
  10. total: 0,
  11. limit: 5,
  12. page: 0,
  13. skip: 0,
  14. },
  15. // 跳转菜单
  16. back(e) {
  17. wx.navigateBack({ delta: 1 });
  18. },
  19. //查询
  20. onSearch: function (e) {
  21. const that = this;
  22. that.setData({ skip: 0, page: 0, list: [] })
  23. that.setData({ 'searchInfo.title': e.detail.value });
  24. that.watchLogin()
  25. },
  26. // 添加
  27. toAdd() {
  28. const that = this;
  29. that.setData({ skip: 0, page: 0, list: [] })
  30. wx.navigateTo({ url: '/pages/topic/add' })
  31. },
  32. // 修改
  33. toEdit: function (e) {
  34. const that = this;
  35. that.setData({ skip: 0, page: 0, list: [] })
  36. let { id } = e.currentTarget.dataset;
  37. wx.navigateTo({ url: `/pages/topic/add?id=${id}` })
  38. },
  39. // 删除
  40. toDel: async function (e) {
  41. const that = this;
  42. const { id } = e.currentTarget.dataset;
  43. wx.showModal({
  44. title: '提示',
  45. content: '是否确认删除该条数据?',
  46. async success(res) {
  47. if (res.confirm) {
  48. const arr = await app.$delete(`/newCourt/api/topic/${id}`);
  49. if (arr.errcode == '0') {
  50. wx.showToast({ title: `删除信息成功`, icon: 'success', duration: 2000 })
  51. that.setData({ skip: 0, page: 0, list: [] })
  52. that.watchLogin()
  53. } else {
  54. wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  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. let searchInfo = that.data.searchInfo;
  83. wx.getStorage({
  84. key: 'user',
  85. success: async res => {
  86. let info = { skip: that.data.skip, limit: that.data.limit };
  87. if (searchInfo && searchInfo.title) info.title = searchInfo.title;
  88. let arr;
  89. arr = await app.$get(`/newCourt/api/topic`, { ...info });
  90. if (arr.errcode == '0') {
  91. that.setData({ list: [...that.data.list, ...arr.data] })
  92. that.setData({ total: arr.total })
  93. }
  94. else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) }
  95. },
  96. fail: async res => {
  97. wx.redirectTo({ url: '/pages/index/index' })
  98. }
  99. })
  100. },
  101. /**
  102. * 生命周期函数--监听页面初次渲染完成
  103. */
  104. onReady: function () {
  105. },
  106. /**
  107. * 生命周期函数--监听页面显示
  108. */
  109. onShow: function () {
  110. const that = this;
  111. // 监听用户是否登录
  112. that.watchLogin();
  113. },
  114. /**
  115. * 生命周期函数--监听页面隐藏
  116. */
  117. onHide: function () {
  118. },
  119. /**
  120. * 生命周期函数--监听页面卸载
  121. */
  122. onUnload: function () {
  123. },
  124. /**
  125. * 页面相关事件处理函数--监听用户下拉动作
  126. */
  127. onPullDownRefresh: function () {
  128. },
  129. /**
  130. * 页面上拉触底事件的处理函数
  131. */
  132. onReachBottom: function () {
  133. },
  134. /**
  135. * 用户点击右上角分享
  136. */
  137. onShareAppMessage: function () {
  138. }
  139. })