businessGoodsManage.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. const { getOrderList, postOrderDelete, postOrderDiffPay, postOrderCancel } = 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. booCancelOrder: false,
  33. booPayment: false,
  34. itemData: {} // 当前选中的订单
  35. },
  36. // 下拉刷新加锁
  37. freshing: false,
  38. timer: null,
  39. /**
  40. * 生命周期函数--监听页面加载
  41. */
  42. onLoad: function (options) {
  43. this.setOriginScrollViewData()
  44. },
  45. /**
  46. * 生命周期函数--监听页面初次渲染完成
  47. */
  48. onReady: function () {
  49. },
  50. /**
  51. * 生命周期函数--监听页面显示
  52. */
  53. onShow: function () {
  54. },
  55. /**
  56. * 生命周期函数--监听页面隐藏
  57. */
  58. onHide: function () {
  59. },
  60. /**
  61. * 生命周期函数--监听页面卸载
  62. */
  63. onUnload: function () {
  64. clearTimeout(this.timer)
  65. },
  66. bindCallBack() {
  67. this.onRefresh({ detail: { dy: this.data.refresherThreshold } })
  68. },
  69. jumpOrderDetail(e) {
  70. const { orderId } = e.currentTarget.dataset
  71. wx.navigateTo({
  72. url: '/pages/orderDetail/orderDetail?orderId=' + orderId
  73. })
  74. },
  75. setOriginScrollViewData() {
  76. const temp = []
  77. for (let i = 0; i < this.data.tabs.length; i++) {
  78. temp.push({
  79. finished: false, // 所有数据是否加载完
  80. isRefresh: false, // 是否下拉刷新
  81. isFetchLock: false, // 接口调用加锁
  82. pageNum: 0,
  83. list: [],
  84. scrollY: 0,
  85. isFirst: true // 是否首次获取列表
  86. })
  87. }
  88. this.setData({
  89. originScrollViewData: temp
  90. }, () => {
  91. // this.fetOrderList()
  92. })
  93. },
  94. changeTabs(e) {
  95. const value = e.currentTarget.dataset.value
  96. const index = this.data.tabs.findIndex(item => item.value === value)
  97. const temp = this.data.originScrollViewData[index]
  98. this.setData({
  99. active: value,
  100. current: index
  101. }, () => {
  102. if (temp.isFirst) {
  103. // this.fetOrderList()
  104. }
  105. })
  106. },
  107. handleSwiper(e) {
  108. const { current, source } = e.detail
  109. const item = this.data.tabs[current]
  110. if (source) {
  111. this.setData({
  112. active: item.value
  113. }, () => {
  114. const temp = this.data.originScrollViewData[current]
  115. if (temp.isFirst) {
  116. // this.fetOrderList()
  117. }
  118. })
  119. }
  120. },
  121. async fetOrderList() {
  122. const that = this
  123. const _tab = that.data.tabs.findIndex(item => item.value === that.data.active)
  124. const temp = that.data.originScrollViewData[_tab]
  125. if (Object.prototype.toString.call(temp) !== '[object Object]') {
  126. return
  127. }
  128. if (temp.finished) {
  129. return
  130. }
  131. if (temp.isFetchLock) {
  132. return
  133. }
  134. that.setData({
  135. ['originScrollViewData[' + _tab + '].isFetchLock']: true,
  136. ['originScrollViewData[' + _tab + '].pageNum']: temp.pageNum + 1
  137. })
  138. try {
  139. const { status, data, msg } = await getOrderList({
  140. pageNum: that.data.originScrollViewData[_tab].pageNum,
  141. pageSize: that.data.pageSize,
  142. type: that.data.tabs[_tab].value
  143. })
  144. if (status) {
  145. const { list } = data
  146. if (Array.isArray(list)) {
  147. const arr = list.map(item => {
  148. return {
  149. ...item,
  150. created_at: item.created_at.replace(/(.*)(:\d{2})/, '$1')
  151. }
  152. })
  153. const _list = temp.isRefresh
  154. ? [].concat(arr)
  155. : that.data.originScrollViewData[_tab].list.concat(arr)
  156. that.setData({
  157. ['originScrollViewData[' + _tab + '].list']: _list,
  158. ['originScrollViewData[' + _tab + '].finished']: list.length < 10,
  159. ['originScrollViewData[' + _tab + '].isRefresh']: false,
  160. ['originScrollViewData[' + _tab + '].isFetchLock']: false,
  161. ['originScrollViewData[' + _tab + '].isFirst']: false
  162. })
  163. }
  164. } else {
  165. wx.showToast({
  166. title: msg,
  167. icon: 'none'
  168. })
  169. }
  170. } catch (e) {}
  171. if (that.freshing) {
  172. that.freshing = false
  173. }
  174. },
  175. onRefresh(e) {
  176. const { dy } = e.detail
  177. if (dy < this.data.refresherThreshold || this.freshing) {
  178. return
  179. }
  180. const _tab = this.data.tabs.findIndex(item => item.value === this.data.active)
  181. this.freshing = true
  182. // 这里设置延时的目的:页面数据不足一屏时,更好的展示下拉刷新动画
  183. this.timer = setTimeout(() => {
  184. clearTimeout(this.timer)
  185. this.setData({
  186. ['originScrollViewData[' + _tab + '].pageNum']: 0,
  187. pageSize: 20,
  188. ['originScrollViewData[' + _tab + '].finished']: false,
  189. ['originScrollViewData[' + _tab + '].isRefresh']: true,
  190. ['originScrollViewData[' + _tab + '].isFetchLock']: false
  191. }, () => {
  192. // this.fetOrderList()
  193. })
  194. }, 1000)
  195. },
  196. showDeleteOrder(e) {
  197. const { item } = e.currentTarget.dataset
  198. this.setData({
  199. booDeleteOrder: true,
  200. itemData: item
  201. })
  202. },
  203. hideDeleteOrder() {
  204. this.setData({
  205. booDeleteOrder: false,
  206. itemData: {}
  207. })
  208. },
  209. async confirmDeleteOrder() {
  210. const { id } = this.data.itemData
  211. this.setData({
  212. booLock: true
  213. })
  214. try {
  215. const { status, msg } = await postOrderDelete(id)
  216. let _msg = ''
  217. if (status) {
  218. _msg = '订单删除成功'
  219. // 更新全部:去除各个列表中有相同id的
  220. const originScrollViewData = this.data.originScrollViewData
  221. for (let i = 0; i < originScrollViewData.length; i++) {
  222. const _list = originScrollViewData[i].list
  223. const _delIndex = _list.findIndex(item => item.id === id)
  224. if (_delIndex > -1) {
  225. _list.splice(_delIndex, 1)
  226. this.setData({
  227. ['originScrollViewData[' + i + '].list']: _list
  228. })
  229. }
  230. }
  231. this.hideDeleteOrder()
  232. } else {
  233. _msg = msg
  234. }
  235. wx.showToast({
  236. title: _msg,
  237. icon: 'none'
  238. })
  239. } catch (err) {}
  240. this.setData({
  241. booLock: false
  242. })
  243. },
  244. showPayment(e) {
  245. const { item } = e.currentTarget.dataset
  246. this.setData({
  247. itemData: item,
  248. booPayment: true
  249. })
  250. },
  251. hidePayment() {
  252. this.setData({
  253. booPayment: false,
  254. itemData: {}
  255. })
  256. },
  257. async handleOrderDiffPay() {
  258. const { order_number, id } = this.data.itemData
  259. this.setData({
  260. booLock: true
  261. })
  262. try {
  263. const { status, data, msg } = await postOrderDiffPay(order_number)
  264. if (status) {
  265. const { time_stamp, nonce_str, sign_type, pay_sign } = data.pay_data
  266. wx.requestPayment({
  267. timeStamp: time_stamp,
  268. nonceStr: nonce_str,
  269. package: data.pay_data.package,
  270. signType: sign_type,
  271. paySign: pay_sign,
  272. success(res) {
  273. if (res.errMsg === 'requestPayment:ok') {
  274. wx.redirectTo({
  275. url: '/pages/orderDetail/orderDetail?orderId=' + id
  276. })
  277. } else {
  278. wx.showToast({
  279. title: '支付出错,请重试~',
  280. icon: 'none'
  281. })
  282. }
  283. },
  284. fail(err) {
  285. console.log(err)
  286. }
  287. })
  288. } else {
  289. wx.showToast({
  290. title: msg,
  291. icon: 'none'
  292. })
  293. }
  294. } catch (err) {}
  295. this.setData({
  296. booLock: false
  297. })
  298. },
  299. showCancelOrder(e) {
  300. const { item } = e.currentTarget.dataset
  301. this.setData({
  302. itemData: item,
  303. booCancelOrder: true
  304. })
  305. },
  306. hideCancelOrder() {
  307. this.setData({
  308. booCancelOrder: false,
  309. itemData: {}
  310. })
  311. },
  312. async confirmCancelOrder() {
  313. const { id } = this.data.itemData
  314. this.setData({
  315. booLock: true
  316. })
  317. try {
  318. const { status, msg } = await postOrderCancel(id)
  319. if (status) {
  320. this.hideCancelOrder()
  321. wx.redirectTo({
  322. url: '/pages/orderCancel/orderCancel?orderId=' + id
  323. })
  324. } else {
  325. wx.showToast({
  326. title: msg,
  327. icon: 'none'
  328. })
  329. }
  330. } catch (err) {}
  331. this.setData({
  332. booLock: false
  333. })
  334. }
  335. })