businessGoodsManage.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. const { getProductList, postProductDelete, postProductUp } = 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',
  26. current: 0, // 轮播图默认选中
  27. pageSize: 20,
  28. originScrollViewData: [],
  29. booDeleteOrder: false,
  30. booLock: false,
  31. refresherThreshold: 60, // 自定义下拉刷新阈值
  32. itemData: {}, // 当前选中的商品
  33. booProductUp: false
  34. },
  35. // 下拉刷新加锁
  36. freshing: false,
  37. timer: null,
  38. /**
  39. * 生命周期函数--监听页面加载
  40. */
  41. onLoad: function (options) {
  42. const { tabvalue = '' } = options
  43. if (tabvalue && this.data.tabs.findIndex(item => item.value === tabvalue) > -1) {
  44. this.setData({
  45. active: tabvalue,
  46. current: tabvalue * 1
  47. })
  48. }
  49. this.setOriginScrollViewData()
  50. },
  51. /**
  52. * 生命周期函数--监听页面初次渲染完成
  53. */
  54. onReady: function () {
  55. },
  56. /**
  57. * 生命周期函数--监听页面显示
  58. */
  59. onShow: function () {
  60. },
  61. /**
  62. * 生命周期函数--监听页面隐藏
  63. */
  64. onHide: function () {
  65. },
  66. /**
  67. * 生命周期函数--监听页面卸载
  68. */
  69. onUnload: function () {
  70. clearTimeout(this.timer)
  71. },
  72. bindCallBack() {
  73. this.onRefresh({ detail: { dy: this.data.refresherThreshold } })
  74. },
  75. jumpOrderDetail(e) {
  76. const { item } = e.currentTarget.dataset
  77. wx.navigateTo({
  78. url: '/pages/businessGoodsEdit/businessGoodsEdit?product_id=' + item.id
  79. })
  80. },
  81. setOriginScrollViewData() {
  82. const temp = []
  83. for (let i = 0; i < this.data.tabs.length; i++) {
  84. temp.push({
  85. finished: false, // 所有数据是否加载完
  86. isRefresh: false, // 是否下拉刷新
  87. isFetchLock: false, // 接口调用加锁
  88. pageNum: 0,
  89. list: [],
  90. scrollY: 0,
  91. isFirst: true // 是否首次获取列表
  92. })
  93. }
  94. this.setData({
  95. originScrollViewData: temp
  96. }, () => {
  97. this.fetOrderList()
  98. })
  99. },
  100. changeTabs(e) {
  101. const value = e.currentTarget.dataset.value
  102. const index = this.data.tabs.findIndex(item => item.value === value)
  103. const temp = this.data.originScrollViewData[index]
  104. this.setData({
  105. active: value,
  106. current: index
  107. }, () => {
  108. if (temp.isFirst) {
  109. this.fetOrderList()
  110. }
  111. })
  112. },
  113. handleSwiper(e) {
  114. const { current, source } = e.detail
  115. const item = this.data.tabs[current]
  116. if (source) {
  117. this.setData({
  118. active: item.value
  119. }, () => {
  120. const temp = this.data.originScrollViewData[current]
  121. if (temp.isFirst) {
  122. this.fetOrderList()
  123. }
  124. })
  125. }
  126. },
  127. async fetOrderList() {
  128. const that = this
  129. const _tab = that.data.tabs.findIndex(item => item.value === that.data.active)
  130. const temp = that.data.originScrollViewData[_tab]
  131. if (Object.prototype.toString.call(temp) !== '[object Object]') {
  132. return
  133. }
  134. if (temp.finished) {
  135. return
  136. }
  137. if (temp.isFetchLock) {
  138. return
  139. }
  140. that.setData({
  141. ['originScrollViewData[' + _tab + '].isFetchLock']: true,
  142. ['originScrollViewData[' + _tab + '].pageNum']: temp.pageNum + 1
  143. })
  144. try {
  145. const { status, data, msg } = await getProductList({
  146. pageNum: that.data.originScrollViewData[_tab].pageNum,
  147. pageSize: that.data.pageSize,
  148. type: that.data.tabs[_tab].value
  149. })
  150. if (status) {
  151. const { list } = data
  152. if (Array.isArray(list)) {
  153. // product_status 商品状态(0未上架1已上架2审核中3审核未通过)
  154. // product_sale_at 预售(0非预售 时间戳代表预售时间)
  155. const tempList = list.map(item => {
  156. return {
  157. ...item,
  158. ...that.getStatusTextAndColor(item.product_status, item.product_check_status)
  159. }
  160. })
  161. const _list = temp.isRefresh ? [].concat(tempList) : that.data.originScrollViewData[_tab].list.concat(tempList)
  162. that.setData({
  163. ['originScrollViewData[' + _tab + '].list']: _list,
  164. ['originScrollViewData[' + _tab + '].finished']: list.length < 10,
  165. ['originScrollViewData[' + _tab + '].isRefresh']: false,
  166. ['originScrollViewData[' + _tab + '].isFetchLock']: false,
  167. ['originScrollViewData[' + _tab + '].isFirst']: false
  168. })
  169. }
  170. } else {
  171. wx.showToast({
  172. title: msg,
  173. icon: 'none'
  174. })
  175. }
  176. } catch (e) {}
  177. if (that.freshing) {
  178. that.freshing = false
  179. }
  180. },
  181. onRefresh(e) {
  182. const { dy } = e.detail
  183. if (dy < this.data.refresherThreshold || this.freshing) {
  184. return
  185. }
  186. const _tab = this.data.tabs.findIndex(item => item.value === this.data.active)
  187. this.freshing = true
  188. // 这里设置延时的目的:页面数据不足一屏时,更好的展示下拉刷新动画
  189. this.timer = setTimeout(() => {
  190. clearTimeout(this.timer)
  191. this.setData({
  192. ['originScrollViewData[' + _tab + '].pageNum']: 0,
  193. pageSize: 20,
  194. ['originScrollViewData[' + _tab + '].finished']: false,
  195. ['originScrollViewData[' + _tab + '].isRefresh']: true,
  196. ['originScrollViewData[' + _tab + '].isFetchLock']: false
  197. }, () => {
  198. this.fetOrderList()
  199. })
  200. }, 1000)
  201. },
  202. showDeleteOrder(e) {
  203. const { item } = e.currentTarget.dataset
  204. this.setData({
  205. booDeleteOrder: true,
  206. itemData: item
  207. })
  208. },
  209. hideDeleteOrder() {
  210. this.setData({
  211. booDeleteOrder: false,
  212. itemData: {}
  213. })
  214. },
  215. handleProductUp(e) {
  216. const { item } = e.currentTarget.dataset
  217. this.setData({
  218. booProductUp: true,
  219. itemData: item
  220. })
  221. },
  222. hideProductUp() {
  223. this.setData({
  224. booProductUp: false,
  225. itemData: {}
  226. })
  227. },
  228. async confirmProductUp() {
  229. const { id, product_status } = this.data.itemData
  230. this.setData({
  231. booLock: true
  232. })
  233. try {
  234. const { status, msg } = await postProductUp(id, product_status === 0 ? 1 : 0)
  235. let _msg = ''
  236. if (status) {
  237. _msg = '商品' + (product_status === 0 ? '上架' : '下架') + '成功'
  238. // 更新全部:去除各个列表中有相同id的
  239. const originScrollViewData = this.data.originScrollViewData
  240. for (let i = 0; i < originScrollViewData.length; i++) {
  241. const _list = originScrollViewData[i].list
  242. const _delIndex = _list.findIndex(item => item.id === id)
  243. if (_delIndex > -1) {
  244. _list.splice(_delIndex, 1, {
  245. ...this.data.itemData,
  246. product_status: product_status === 0 ? 1 : 0,
  247. ...this.getStatusTextAndColor(product_status === 0 ? 1 : 0, this.data.itemData.product_check_status)
  248. })
  249. this.setData({
  250. ['originScrollViewData[' + i + '].list']: _list
  251. })
  252. }
  253. }
  254. this.hideProductUp()
  255. } else {
  256. _msg = msg
  257. }
  258. wx.showToast({
  259. title: _msg,
  260. icon: 'none'
  261. })
  262. } catch (err) {}
  263. this.setData({
  264. booLock: false
  265. })
  266. },
  267. async confirmDeleteOrder() {
  268. const { id } = this.data.itemData
  269. this.setData({
  270. booLock: true
  271. })
  272. try {
  273. const { status, msg } = await postProductDelete(id)
  274. let _msg = ''
  275. if (status) {
  276. _msg = '商品删除成功'
  277. // 更新全部:去除各个列表中有相同id的
  278. const originScrollViewData = this.data.originScrollViewData
  279. for (let i = 0; i < originScrollViewData.length; i++) {
  280. const _list = originScrollViewData[i].list
  281. const _delIndex = _list.findIndex(item => item.id === id)
  282. if (_delIndex > -1) {
  283. _list.splice(_delIndex, 1)
  284. this.setData({
  285. ['originScrollViewData[' + i + '].list']: _list
  286. })
  287. }
  288. }
  289. this.hideDeleteOrder()
  290. } else {
  291. _msg = msg
  292. }
  293. wx.showToast({
  294. title: _msg,
  295. icon: 'none'
  296. })
  297. } catch (err) {}
  298. this.setData({
  299. booLock: false
  300. })
  301. },
  302. getStatusTextAndColor(productStatus, productCheckStatus) {
  303. // product_check_status 0未审核 1审核成功 2审核失败
  304. // product_status 0 未上架 1 上架
  305. let color = 'col-0'
  306. let text = ''
  307. if (productCheckStatus === 0) {
  308. color = 'col-1'
  309. text = '审核中'
  310. } else if (productCheckStatus === 2) {
  311. color = 'col-3'
  312. text = '审核未通过'
  313. } else if (productCheckStatus === 1 && productStatus === 1) {
  314. color = 'col-2'
  315. text = '已上架'
  316. } else if (productCheckStatus === 1 && productStatus === 0) {
  317. text = '未上架'
  318. }
  319. return {
  320. product_status_text: text,
  321. product_status_color: color
  322. }
  323. }
  324. })