index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import { getOrderList, orderData, orderCancel, orderDel, orderPay } from '../../api/order.js'
  2. import { getUserInfo } from '../../api/user.js'
  3. import { openOrderSubscribe } from '../../utils/SubscribeMessage.js'
  4. const app = getApp()
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. parameter: {
  11. navbar: '1',
  12. return: '1',
  13. title: '我的订单',
  14. color: true,
  15. class: '0'
  16. },
  17. loading: false, //是否加载中
  18. loadend: false, //是否加载完毕
  19. loadTitle: '加载更多', //提示语
  20. orderList: [], //订单数组
  21. orderData: {}, //订单详细统计
  22. orderStatus: 0, //订单状态
  23. page: 1,
  24. limit: 10,
  25. isClose: false,
  26. payMode: [
  27. {
  28. name: '微信支付',
  29. icon: 'icon-weixinzhifu',
  30. value: 'weixin',
  31. title: '微信快捷支付'
  32. },
  33. {
  34. name: '余额支付',
  35. icon: 'icon-yuezhifu',
  36. value: 'yue',
  37. title: '可用余额:',
  38. number: 0
  39. }
  40. ],
  41. pay_close: false,
  42. pay_order_id: '',
  43. totalPrice: '0'
  44. },
  45. /**
  46. * 登录回调
  47. */
  48. onLoadFun: function () {
  49. this.getOrderData()
  50. this.getOrderList()
  51. this.getUserInfo()
  52. },
  53. /**
  54. * 事件回调
  55. */
  56. onChangeFun: function (e) {
  57. let opt = e.detail
  58. let action = opt.action || null
  59. let value = opt.value != undefined ? opt.value : null
  60. action && this[action] && this[action](value)
  61. },
  62. /**
  63. * 获取用户信息
  64. */
  65. getUserInfo: function () {
  66. let that = this
  67. getUserInfo().then(res => {
  68. that.data.payMode[1].number = res.data.now_money
  69. that.setData({
  70. payMode: that.data.payMode
  71. })
  72. // app.globalData.unread = res.data.notice
  73. app.setUnread(res.data.notice)
  74. })
  75. },
  76. /**
  77. * 关闭支付组件
  78. */
  79. pay_close: function () {
  80. this.setData({
  81. pay_close: false
  82. })
  83. },
  84. /**
  85. * 生命周期函数--监听页面加载
  86. */
  87. onLoad: function (options) {
  88. if (options.status)
  89. this.setData({
  90. orderStatus: options.status
  91. })
  92. this.setData({
  93. 'parameter.return': options.is_return ? '0' : '1'
  94. })
  95. },
  96. /**
  97. * 获取订单统计数据
  98. */
  99. getOrderData: function () {
  100. var that = this
  101. orderData().then(res => {
  102. that.setData({
  103. orderData: res.data
  104. })
  105. })
  106. },
  107. /**
  108. * 取消订单
  109. */
  110. cancelOrder: function (e) {
  111. var order_id = e.currentTarget.dataset.order_id
  112. var index = e.currentTarget.dataset.index,
  113. that = this
  114. if (!order_id)
  115. return app.Tips({
  116. title: '缺少订单号无法取消订单'
  117. })
  118. orderCancel(order_id)
  119. .then(res => {
  120. return app.Tips(
  121. {
  122. title: res.msg,
  123. icon: 'success'
  124. },
  125. function () {
  126. that.data.orderList.splice(index, 1)
  127. that.setData({
  128. orderList: that.data.orderList,
  129. 'orderData.unpaid_count': that.data.orderData.unpaid_count - 1
  130. })
  131. that.getOrderData()
  132. }
  133. )
  134. })
  135. .catch(err => {
  136. return app.Tips({
  137. title: err
  138. })
  139. })
  140. },
  141. /**
  142. * 打开支付组件
  143. */
  144. goPay: function (e) {
  145. let order_id = e.currentTarget.dataset.order_id,
  146. pay_price = e.currentTarget.dataset.pay_price
  147. this.setData({
  148. pay_close: true,
  149. pay_order_id: order_id,
  150. totalPrice: pay_price
  151. })
  152. },
  153. /**
  154. * 支付成功回调
  155. */
  156. pay_complete: function () {
  157. this.setData({
  158. loadend: false,
  159. page: 1,
  160. orderList: [],
  161. pay_close: false,
  162. pay_order_id: ''
  163. })
  164. this.getOrderData()
  165. this.getOrderList()
  166. },
  167. /**
  168. * 支付失败回调
  169. */
  170. pay_fail: function () {
  171. this.setData({
  172. pay_close: false,
  173. pay_order_id: ''
  174. })
  175. },
  176. /**
  177. * 去订单详情
  178. */
  179. goOrderDetails: function (e) {
  180. var order_id = e.currentTarget.dataset.order_id
  181. if (!order_id)
  182. return app.Tips({
  183. title: '缺少订单号无法查看订单详情'
  184. })
  185. wx.showLoading({
  186. title: '正在加载'
  187. })
  188. openOrderSubscribe()
  189. .then(() => {
  190. wx.hideLoading()
  191. wx.navigateTo({
  192. url: '/pages/order_details/index?order_id=' + order_id
  193. })
  194. })
  195. .catch(() => {
  196. wx.hideLoading()
  197. })
  198. },
  199. /**
  200. * 切换类型
  201. */
  202. statusClick: function (e) {
  203. var status = e.currentTarget.dataset.status
  204. if (status == this.data.orderStatus) return
  205. this.setData({
  206. orderStatus: status,
  207. loadend: false,
  208. page: 1,
  209. orderList: []
  210. })
  211. this.getOrderList()
  212. },
  213. /**
  214. * 获取订单列表
  215. */
  216. getOrderList: function () {
  217. var that = this
  218. if (that.data.loadend) return
  219. if (that.data.loading) return
  220. that.setData({
  221. loading: true,
  222. loadTitle: ''
  223. })
  224. getOrderList({
  225. type: that.data.orderStatus,
  226. page: that.data.page,
  227. limit: that.data.limit
  228. })
  229. .then(res => {
  230. var list = res.data || []
  231. var loadend = list.length < that.data.limit
  232. that.data.orderList = app.SplitArray(list, that.data.orderList)
  233. that.setData({
  234. orderList: that.data.orderList,
  235. loadend: loadend,
  236. loading: false,
  237. loadTitle: loadend ? '我也是有底线的' : '加载更多',
  238. page: that.data.page + 1
  239. })
  240. })
  241. .catch(err => {
  242. that.setData({
  243. loading: false,
  244. loadTitle: '加载更多'
  245. })
  246. })
  247. },
  248. /**
  249. * 删除订单
  250. */
  251. delOrder: function (e) {
  252. var order_id = e.currentTarget.dataset.order_id
  253. var index = e.currentTarget.dataset.index,
  254. that = this
  255. orderDel(order_id)
  256. .then(res => {
  257. that.data.orderList.splice(index, 1)
  258. that.setData({
  259. orderList: that.data.orderList,
  260. 'orderData.unpaid_count': that.data.orderData.unpaid_count - 1
  261. })
  262. that.getOrderData()
  263. return app.Tips({
  264. title: '删除成功',
  265. icon: 'success'
  266. })
  267. })
  268. .catch(err => {
  269. return app.Tips({
  270. title: err
  271. })
  272. })
  273. },
  274. /**
  275. * 生命周期函数--监听页面初次渲染完成
  276. */
  277. onReady: function () {},
  278. /**
  279. * 生命周期函数--监听页面显示
  280. */
  281. onShow: function () {
  282. if (app.globalData.isLog && this.data.isClose) {
  283. this.getOrderData()
  284. this.setData({
  285. loadend: false,
  286. page: 1,
  287. orderList: []
  288. })
  289. this.getOrderList()
  290. }
  291. },
  292. /**
  293. * 生命周期函数--监听页面隐藏
  294. */
  295. onHide: function () {
  296. this.setData({
  297. isClose: true
  298. })
  299. },
  300. /**
  301. * 生命周期函数--监听页面卸载
  302. */
  303. onUnload: function () {},
  304. /**
  305. * 页面相关事件处理函数--监听用户下拉动作
  306. */
  307. onPullDownRefresh: function () {},
  308. /**
  309. * 页面上拉触底事件的处理函数
  310. */
  311. onReachBottom: function () {
  312. this.getOrderList()
  313. }
  314. })