list.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import {dibootApi} from '@/utils/dibootApi'
  2. import more from './more'
  3. export default {
  4. mixins: [more],
  5. data() {
  6. return {
  7. primaryKey: 'id',
  8. // 请求接口基础路径
  9. baseApi: '/',
  10. // 列表数据接口
  11. listApi: '',
  12. // 删除接口
  13. deleteApiPrefix: '',
  14. // 是否在页面初始化时自动加载列表数据
  15. getListFromMixin: true,
  16. // 与查询条件绑定的参数(会被查询表单重置和改变的参数)
  17. queryParam: {},
  18. // 用户自定义查询条件
  19. customQueryParam: {},
  20. // //下拉刷新的状态
  21. triggered: false,
  22. // load状态
  23. status: 'loadmore',
  24. loadText: {
  25. loadmore: '上拉加载更多',
  26. loading: '努力加载中',
  27. nomore: '没有更多了'
  28. },
  29. // 分页
  30. page: {
  31. pageIndex: 1,
  32. pageSize: 20,
  33. totalCount: 0,
  34. totalPage: 0
  35. },
  36. // 激活的Index
  37. activeIndex: -100,
  38. // 是否弹出删除
  39. deleteShow: false,
  40. // 右滑菜单列表
  41. actionOptions: [{
  42. text: '编辑',
  43. type: 'handleUpdate',
  44. style: {
  45. backgroundColor: this.$color.warning
  46. }
  47. }, {
  48. text: '删除',
  49. type: 'handleDelete',
  50. style: {
  51. backgroundColor: this.$color.error
  52. }
  53. }],
  54. // 数据列表
  55. list: [],
  56. // 是否允许访问详情
  57. allowGoDetail: true,
  58. // 状态栏高度
  59. diStatusBarHeight: 0,
  60. // 阻止重复发送
  61. keyMap: {}
  62. }
  63. },
  64. onLoad() {
  65. this.diStatusBarHeight = uni.getSystemInfoSync().statusBarHeight
  66. },
  67. onShow() {
  68. this.activeIndex = -100
  69. this.getListFromMixin && this.getList(true)
  70. },
  71. methods: {
  72. /**
  73. * 新增
  74. */
  75. handleCreate() {
  76. uni.navigateTo({
  77. url: './form'
  78. })
  79. },
  80. /*
  81. * 详情
  82. */
  83. handleDetail(id) {
  84. if(!this.allowGoDetail) {
  85. return
  86. }
  87. uni.navigateTo({
  88. url:`./detail?id=${id}`
  89. })
  90. },
  91. /*
  92. * 编辑
  93. */
  94. handleUpdate(id) {
  95. uni.navigateTo({
  96. url: `./form?id=${id}`
  97. })
  98. },
  99. /**
  100. * 删除
  101. */
  102. handleDelete(id) {
  103. this.deleteShow = true
  104. this.activeIndex = id
  105. },
  106. /**
  107. * 确认删除
  108. * @param {Object} id
  109. */
  110. async handleConfirmDel() {
  111. try{
  112. const deleteApiPrefix = this.deleteApiPrefix ? this.deleteApiPrefix : ''
  113. const res = await dibootApi.delete(`${this.baseApi}${deleteApiPrefix}/${this.activeIndex}`)
  114. this.showToast(res.msg, res.code === 0 ? 'success' : 'error')
  115. }catch(e){
  116. console.log(e)
  117. this.showToast('网络异常!')
  118. } finally {
  119. this.page.pageIndex = 1
  120. this.getList(true)
  121. this.handleCancelDel()
  122. }
  123. },
  124. /**
  125. * 取消删除
  126. */
  127. handleCancelDel() {
  128. this.deleteShow = false
  129. this.activeIndex = -100
  130. },
  131. /*
  132. * 打开左滑操作
  133. */
  134. handleActiveSwipeAction(index) {
  135. this.activeIndex = index
  136. },
  137. /**
  138. * 点击左滑按钮
  139. * @param {Number} index 所在列表的primaryKey
  140. * @param {Number} optionIdx 操作列表actionOptions的下标
  141. */
  142. handleActionClick(index, optionIdx) {
  143. this[this.actionOptions[optionIdx]['type']](index)
  144. },
  145. /**
  146. * 下拉刷新
  147. */
  148. handlePullDownRefresh() {
  149. if (this.triggered) return
  150. this.triggered = true
  151. this.page.pageIndex = 1
  152. this.getList(true)
  153. },
  154. /**
  155. * 触底加载
  156. */
  157. handleOnreachBottom() {
  158. // 将当前pageIndex制作成下标,并查看缓存中是否存在指定下标的值
  159. const key = `_${this.page.pageIndex}`
  160. const value = this.keyMap[key]
  161. // 如果value存在,表示缓存有值,那么阻止请求
  162. if(value) {
  163. return
  164. }
  165. // value不存在,表示第一次请求,设置占位
  166. this.keyMap[key] = 'temp'
  167. this.status = 'nomore'
  168. if (this.page.pageIndex <= this.page.totalPage) {
  169. this.getList()
  170. }
  171. },
  172. /**
  173. * 获取数据列表
  174. */
  175. async getList(replace = false) {
  176. try{
  177. this.status = 'loading'
  178. const tempQueryParam = this.buildQueryParamPage()
  179. const res = await dibootApi.get(this.listApi ? `${this.baseApi}/${this.listApi}` : `${this.baseApi}/list`, tempQueryParam)
  180. if (res.code === 0) {
  181. this.list = replace ? res.data : this.list.concat(res.data)
  182. this.page = res.page
  183. this.page.pageIndex++
  184. console.log('获取到的数据列表是',this.list)
  185. } else {
  186. this.showToast(res.msg)
  187. }
  188. }catch(e){
  189. //TODO handle the exception
  190. } finally {
  191. this.triggered = false
  192. this.status = (this.list || []).length == this.page.totalCount ? 'nomore' : 'loadmore'
  193. }
  194. },
  195. buildQueryParamPage() {
  196. this.queryParam.pageIndex = this.page.pageIndex
  197. return Object.assign(this.queryParam, this.customQueryParam)
  198. },
  199. /**
  200. * 展示提示
  201. * @param {Object} title 提示内容
  202. * @param {Object} icon 提示icon, 默认使用error
  203. */
  204. showToast(title, icon = 'error') {
  205. uni.showToast({
  206. title,
  207. icon
  208. });
  209. }
  210. },
  211. computed: {
  212. listMargin() {
  213. return `margin: ${this.list.length === 0 ? 0 : 20}rpx`
  214. }
  215. }
  216. }