order.js 8.6 KB

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