businessGoodsManage.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. // product_status 商品状态(0未上架1已上架2审核中3审核未通过)
  153. // product_sale_at 预售(0非预售 时间戳代表预售时间)
  154. const tempList = list.map(item => {
  155. return {
  156. ...item,
  157. ...that.getStatusTextAndColor(item.product_status)
  158. }
  159. })
  160. const _list = temp.isRefresh ? [].concat(tempList) : that.data.originScrollViewData[_tab].list.concat(tempList)
  161. that.setData({
  162. ['originScrollViewData[' + _tab + '].list']: _list,
  163. ['originScrollViewData[' + _tab + '].finished']: list.length < 10,
  164. ['originScrollViewData[' + _tab + '].isRefresh']: false,
  165. ['originScrollViewData[' + _tab + '].isFetchLock']: false,
  166. ['originScrollViewData[' + _tab + '].isFirst']: false
  167. })
  168. }
  169. } else {
  170. wx.showToast({
  171. title: msg,
  172. icon: 'none'
  173. })
  174. }
  175. } catch (e) {}
  176. if (that.freshing) {
  177. that.freshing = false
  178. }
  179. },
  180. onRefresh(e) {
  181. const { dy } = e.detail
  182. if (dy < this.data.refresherThreshold || this.freshing) {
  183. return
  184. }
  185. const _tab = this.data.tabs.findIndex(item => item.value === this.data.active)
  186. this.freshing = true
  187. // 这里设置延时的目的:页面数据不足一屏时,更好的展示下拉刷新动画
  188. this.timer = setTimeout(() => {
  189. clearTimeout(this.timer)
  190. this.setData({
  191. ['originScrollViewData[' + _tab + '].pageNum']: 0,
  192. pageSize: 20,
  193. ['originScrollViewData[' + _tab + '].finished']: false,
  194. ['originScrollViewData[' + _tab + '].isRefresh']: true,
  195. ['originScrollViewData[' + _tab + '].isFetchLock']: false
  196. }, () => {
  197. this.fetOrderList()
  198. })
  199. }, 1000)
  200. },
  201. showDeleteOrder(e) {
  202. const { item } = e.currentTarget.dataset
  203. this.setData({
  204. booDeleteOrder: true,
  205. itemData: item
  206. })
  207. },
  208. hideDeleteOrder() {
  209. this.setData({
  210. booDeleteOrder: false,
  211. itemData: {}
  212. })
  213. },
  214. async confirmDeleteOrder() {
  215. const { id } = this.data.itemData
  216. this.setData({
  217. booLock: true
  218. })
  219. try {
  220. const { status, msg } = await postProductDelete(id)
  221. let _msg = ''
  222. if (status) {
  223. _msg = '商品删除成功'
  224. // 更新全部:去除各个列表中有相同id的
  225. const originScrollViewData = this.data.originScrollViewData
  226. for (let i = 0; i < originScrollViewData.length; i++) {
  227. const _list = originScrollViewData[i].list
  228. const _delIndex = _list.findIndex(item => item.id === id)
  229. if (_delIndex > -1) {
  230. _list.splice(_delIndex, 1)
  231. this.setData({
  232. ['originScrollViewData[' + i + '].list']: _list
  233. })
  234. }
  235. }
  236. this.hideDeleteOrder()
  237. } else {
  238. _msg = msg
  239. }
  240. wx.showToast({
  241. title: _msg,
  242. icon: 'none'
  243. })
  244. } catch (err) {}
  245. this.setData({
  246. booLock: false
  247. })
  248. },
  249. getStatusTextAndColor(val) {
  250. const arr = ['未上架', '已上架', '审核中', '审核未通过']
  251. let color = 'col-0'
  252. switch (val) {
  253. case 1:
  254. color = 'col-2'
  255. break
  256. case 2:
  257. color = 'col-1'
  258. break
  259. case 3:
  260. color = 'col-3'
  261. break
  262. default:
  263. }
  264. return {
  265. product_status_text: arr[val] || '',
  266. product_status_color: color
  267. }
  268. }
  269. })