index.js 8.0 KB

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