businessGoodsManage.js 6.4 KB

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