businessGoodsManage.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. const { getProductList, postProductDelete } = require('./api/index')
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. tabs: [
  8. {
  9. name: '全部',
  10. value: '0'
  11. },
  12. {
  13. name: '已上架',
  14. value: '1'
  15. },
  16. {
  17. name: '未上架',
  18. value: '2'
  19. },
  20. {
  21. name: '预售',
  22. value: '3'
  23. }
  24. ],
  25. active: '0', // 顶部菜单栏默认选中,订单状态:(0全部1待支付2待出发3进行中4已结束)
  26. current: 0, // 轮播图默认选中
  27. pageSize: 20,
  28. originScrollViewData: [],
  29. booDeleteOrder: false,
  30. booLock: false,
  31. refresherThreshold: 60, // 自定义下拉刷新阈值
  32. itemData: {} // 当前选中的订单
  33. },
  34. // 下拉刷新加锁
  35. freshing: false,
  36. timer: null,
  37. /**
  38. * 生命周期函数--监听页面加载
  39. */
  40. onLoad: function (options) {
  41. this.setOriginScrollViewData()
  42. },
  43. /**
  44. * 生命周期函数--监听页面初次渲染完成
  45. */
  46. onReady: function () {
  47. },
  48. /**
  49. * 生命周期函数--监听页面显示
  50. */
  51. onShow: function () {
  52. },
  53. /**
  54. * 生命周期函数--监听页面隐藏
  55. */
  56. onHide: function () {
  57. },
  58. /**
  59. * 生命周期函数--监听页面卸载
  60. */
  61. onUnload: function () {
  62. clearTimeout(this.timer)
  63. },
  64. bindCallBack() {
  65. this.onRefresh({ detail: { dy: this.data.refresherThreshold } })
  66. },
  67. jumpOrderDetail(e) {
  68. const { item } = e.currentTarget.dataset
  69. wx.navigateTo({
  70. url: '/pages/orderDetail/orderDetail?orderId=' + item.id
  71. })
  72. },
  73. setOriginScrollViewData() {
  74. const temp = []
  75. for (let i = 0; i < this.data.tabs.length; i++) {
  76. temp.push({
  77. finished: false, // 所有数据是否加载完
  78. isRefresh: false, // 是否下拉刷新
  79. isFetchLock: false, // 接口调用加锁
  80. pageNum: 0,
  81. list: [],
  82. scrollY: 0,
  83. isFirst: true // 是否首次获取列表
  84. })
  85. }
  86. this.setData({
  87. originScrollViewData: temp
  88. }, () => {
  89. this.fetOrderList()
  90. })
  91. },
  92. changeTabs(e) {
  93. const value = e.currentTarget.dataset.value
  94. const index = this.data.tabs.findIndex(item => item.value === value)
  95. const temp = this.data.originScrollViewData[index]
  96. this.setData({
  97. active: value,
  98. current: index
  99. }, () => {
  100. if (temp.isFirst) {
  101. this.fetOrderList()
  102. }
  103. })
  104. },
  105. handleSwiper(e) {
  106. const { current, source } = e.detail
  107. const item = this.data.tabs[current]
  108. if (source) {
  109. this.setData({
  110. active: item.value
  111. }, () => {
  112. const temp = this.data.originScrollViewData[current]
  113. if (temp.isFirst) {
  114. this.fetOrderList()
  115. }
  116. })
  117. }
  118. },
  119. async fetOrderList() {
  120. const that = this
  121. const _tab = that.data.tabs.findIndex(item => item.value === that.data.active)
  122. const temp = that.data.originScrollViewData[_tab]
  123. if (Object.prototype.toString.call(temp) !== '[object Object]') {
  124. return
  125. }
  126. if (temp.finished) {
  127. return
  128. }
  129. if (temp.isFetchLock) {
  130. return
  131. }
  132. that.setData({
  133. ['originScrollViewData[' + _tab + '].isFetchLock']: true,
  134. ['originScrollViewData[' + _tab + '].pageNum']: temp.pageNum + 1
  135. })
  136. try {
  137. const { status, data, msg } = await getProductList({
  138. pageNum: that.data.originScrollViewData[_tab].pageNum,
  139. pageSize: that.data.pageSize,
  140. type: that.data.tabs[_tab].value
  141. })
  142. if (status) {
  143. const { list } = data
  144. if (Array.isArray(list)) {
  145. const _list = temp.isRefresh
  146. ? [].concat(list)
  147. : that.data.originScrollViewData[_tab].list.concat(list)
  148. that.setData({
  149. ['originScrollViewData[' + _tab + '].list']: _list,
  150. ['originScrollViewData[' + _tab + '].finished']: list.length < 10,
  151. ['originScrollViewData[' + _tab + '].isRefresh']: false,
  152. ['originScrollViewData[' + _tab + '].isFetchLock']: false,
  153. ['originScrollViewData[' + _tab + '].isFirst']: false
  154. })
  155. }
  156. } else {
  157. wx.showToast({
  158. title: msg,
  159. icon: 'none'
  160. })
  161. }
  162. } catch (e) {}
  163. if (that.freshing) {
  164. that.freshing = false
  165. }
  166. },
  167. onRefresh(e) {
  168. const { dy } = e.detail
  169. if (dy < this.data.refresherThreshold || this.freshing) {
  170. return
  171. }
  172. const _tab = this.data.tabs.findIndex(item => item.value === this.data.active)
  173. this.freshing = true
  174. // 这里设置延时的目的:页面数据不足一屏时,更好的展示下拉刷新动画
  175. this.timer = setTimeout(() => {
  176. clearTimeout(this.timer)
  177. this.setData({
  178. ['originScrollViewData[' + _tab + '].pageNum']: 0,
  179. pageSize: 20,
  180. ['originScrollViewData[' + _tab + '].finished']: false,
  181. ['originScrollViewData[' + _tab + '].isRefresh']: true,
  182. ['originScrollViewData[' + _tab + '].isFetchLock']: false
  183. }, () => {
  184. this.fetOrderList()
  185. })
  186. }, 1000)
  187. },
  188. showDeleteOrder(e) {
  189. const { item } = e.currentTarget.dataset
  190. this.setData({
  191. booDeleteOrder: true,
  192. itemData: item
  193. })
  194. },
  195. hideDeleteOrder() {
  196. this.setData({
  197. booDeleteOrder: false,
  198. itemData: {}
  199. })
  200. },
  201. async confirmDeleteOrder() {
  202. const { id } = this.data.itemData
  203. this.setData({
  204. booLock: true
  205. })
  206. try {
  207. const { status, msg } = await postProductDelete(id)
  208. let _msg = ''
  209. if (status) {
  210. _msg = '订单删除成功'
  211. // 更新全部:去除各个列表中有相同id的
  212. const originScrollViewData = this.data.originScrollViewData
  213. for (let i = 0; i < originScrollViewData.length; i++) {
  214. const _list = originScrollViewData[i].list
  215. const _delIndex = _list.findIndex(item => item.id === id)
  216. if (_delIndex > -1) {
  217. _list.splice(_delIndex, 1)
  218. this.setData({
  219. ['originScrollViewData[' + i + '].list']: _list
  220. })
  221. }
  222. }
  223. this.hideDeleteOrder()
  224. } else {
  225. _msg = msg
  226. }
  227. wx.showToast({
  228. title: _msg,
  229. icon: 'none'
  230. })
  231. } catch (err) {}
  232. this.setData({
  233. booLock: false
  234. })
  235. }
  236. })