list.js 4.2 KB

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