order.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. const { getOrderList, postOrderDelete } = require('./api/index')
  2. const { postOrderDiffPay, postOrderCancel } = require('../orderDetail/api/index')
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. tabs: [
  9. {
  10. name: '全部',
  11. value: '0'
  12. },
  13. {
  14. name: '待支付',
  15. value: '1'
  16. },
  17. {
  18. name: '待出发',
  19. value: '2'
  20. },
  21. {
  22. name: '进行中',
  23. value: '3'
  24. },
  25. {
  26. name: '已结束',
  27. value: '4'
  28. }
  29. ],
  30. active: '0', // 顶部菜单栏默认选中,订单状态:(0全部1待支付2待出发3进行中4已结束)
  31. current: 0, // 轮播图默认选中
  32. pageSize: 20,
  33. originScrollViewData: [],
  34. booDeleteOrder: false,
  35. booLock: false,
  36. refresherThreshold: 60, // 自定义下拉刷新阈值
  37. booCancelOrder: false,
  38. booPayment: false,
  39. itemData: {} // 当前选中的订单
  40. },
  41. // 下拉刷新加锁
  42. freshing: false,
  43. timer: null,
  44. /**
  45. * 生命周期函数--监听页面加载
  46. */
  47. onLoad: function (options) {
  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 { orderId } = e.currentTarget.dataset
  76. wx.navigateTo({
  77. url: '/pages/orderDetail/orderDetail?orderId=' + orderId
  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 getOrderList({
  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 arr = list.map(item => {
  153. return {
  154. ...item,
  155. created_at: item.created_at.replace(/(.*)(:\d{2})/, '$1')
  156. }
  157. })
  158. const _list = temp.isRefresh
  159. ? [].concat(arr)
  160. : that.data.originScrollViewData[_tab].list.concat(arr)
  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 postOrderDelete(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. showPayment(e) {
  250. const { item } = e.currentTarget.dataset
  251. this.setData({
  252. itemData: item,
  253. booPayment: true
  254. })
  255. },
  256. hidePayment() {
  257. this.setData({
  258. booPayment: false,
  259. itemData: {}
  260. })
  261. },
  262. async handleOrderDiffPay() {
  263. const { order_number, id } = this.data.itemData
  264. this.setData({
  265. booLock: true
  266. })
  267. try {
  268. const { status, data, msg } = await postOrderDiffPay(order_number)
  269. if (status) {
  270. const { time_stamp, nonce_str, sign_type, pay_sign } = data.pay_data
  271. wx.requestPayment({
  272. timeStamp: time_stamp,
  273. nonceStr: nonce_str,
  274. package: data.pay_data.package,
  275. signType: sign_type,
  276. paySign: pay_sign,
  277. success(res) {
  278. if (res.errMsg === 'requestPayment:ok') {
  279. wx.redirectTo({
  280. url: '/pages/orderDetail/orderDetail?orderId=' + id
  281. })
  282. } else {
  283. wx.showToast({
  284. title: '支付出错,请重试~',
  285. icon: 'none'
  286. })
  287. }
  288. },
  289. fail(err) {
  290. console.log(err)
  291. }
  292. })
  293. } else {
  294. wx.showToast({
  295. title: msg,
  296. icon: 'none'
  297. })
  298. }
  299. } catch (err) {}
  300. this.setData({
  301. booLock: false
  302. })
  303. },
  304. showCancelOrder(e) {
  305. const { item } = e.currentTarget.dataset
  306. this.setData({
  307. itemData: item,
  308. booCancelOrder: true
  309. })
  310. },
  311. hideCancelOrder() {
  312. this.setData({
  313. booCancelOrder: false,
  314. itemData: {}
  315. })
  316. },
  317. async confirmCancelOrder() {
  318. const { id } = this.data.itemData
  319. this.setData({
  320. booLock: true
  321. })
  322. try {
  323. const { status, msg } = await postOrderCancel(id)
  324. if (status) {
  325. this.hideCancelOrder()
  326. wx.redirectTo({
  327. url: '/pages/orderCancel/orderCancel?orderId=' + id
  328. })
  329. } else {
  330. wx.showToast({
  331. title: msg,
  332. icon: 'none'
  333. })
  334. }
  335. } catch (err) {}
  336. this.setData({
  337. booLock: false
  338. })
  339. }
  340. })