index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. const wxpay = require('../../utils/pay.js')
  2. const WXAPI = require('apifm-wxapi')
  3. const AUTH = require('../../utils/auth')
  4. Page({
  5. data: {
  6. page: 1,
  7. tabIndex: 0,
  8. statusType: [
  9. {
  10. status: 9999,
  11. label: '全部'
  12. },
  13. {
  14. status: 0,
  15. label: '待付款'
  16. },
  17. {
  18. status: 1,
  19. label: '待发货'
  20. },
  21. {
  22. status: 2,
  23. label: '待收货'
  24. },
  25. {
  26. status: 3,
  27. label: '待评价'
  28. },
  29. ],
  30. status: 9999,
  31. hasRefund: false,
  32. badges: [0, 0, 0, 0, 0]
  33. },
  34. statusTap: function(e) {
  35. const index = e.detail.index
  36. const status = this.data.statusType[index].status
  37. this.setData({
  38. page: 1,
  39. status
  40. });
  41. this.orderList();
  42. },
  43. cancelOrderTap: function(e) {
  44. const that = this;
  45. const orderId = e.currentTarget.dataset.id;
  46. wx.showModal({
  47. title: '确定要取消该订单吗?',
  48. content: '',
  49. success: function(res) {
  50. if (res.confirm) {
  51. WXAPI.orderClose(wx.getStorageSync('token'), orderId).then(function(res) {
  52. if (res.code == 0) {
  53. that.data.page = 1
  54. that.orderList()
  55. that.getOrderStatistics()
  56. }
  57. })
  58. }
  59. }
  60. })
  61. },
  62. refundApply (e) {
  63. // 申请售后
  64. const orderId = e.currentTarget.dataset.id;
  65. const amount = e.currentTarget.dataset.amount;
  66. wx.navigateTo({
  67. url: "/pages/order/refundApply?id=" + orderId + "&amount=" + amount
  68. })
  69. },
  70. toPayTap: function(e) {
  71. // 防止连续点击--开始
  72. if (this.data.payButtonClicked) {
  73. wx.showToast({
  74. title: '休息一下~',
  75. icon: 'none'
  76. })
  77. return
  78. }
  79. this.data.payButtonClicked = true
  80. setTimeout(() => {
  81. this.data.payButtonClicked = false
  82. }, 3000) // 可自行修改时间间隔(目前是3秒内只能点击一次支付按钮)
  83. // 防止连续点击--结束
  84. const that = this;
  85. const orderId = e.currentTarget.dataset.id;
  86. let money = e.currentTarget.dataset.money;
  87. const needScore = e.currentTarget.dataset.score;
  88. WXAPI.userAmount(wx.getStorageSync('token')).then(function(res) {
  89. if (res.code == 0) {
  90. const order_pay_user_balance = wx.getStorageSync('order_pay_user_balance')
  91. if (order_pay_user_balance != '1') {
  92. res.data.balance = 0
  93. }
  94. // 增加提示框
  95. if (res.data.score < needScore) {
  96. wx.showToast({
  97. title: '您的积分不足,无法支付',
  98. icon: 'none'
  99. })
  100. return;
  101. }
  102. let _msg = '订单金额: ' + money +' 元'
  103. if (res.data.balance > 0) {
  104. _msg += ',可用余额为 ' + res.data.balance +' 元'
  105. if (money - res.data.balance > 0) {
  106. _msg += ',仍需微信支付 ' + (money - res.data.balance).toFixed(2) + ' 元'
  107. }
  108. }
  109. if (needScore > 0) {
  110. _msg += ',并扣除 ' + needScore + ' 积分'
  111. }
  112. money = money - res.data.balance
  113. wx.showModal({
  114. title: '请确认支付',
  115. content: _msg,
  116. confirmText: "确认支付",
  117. cancelText: "取消支付",
  118. success: function (res) {
  119. console.log(res);
  120. if (res.confirm) {
  121. that._toPayTap(orderId, money)
  122. } else {
  123. console.log('用户点击取消支付')
  124. }
  125. }
  126. });
  127. } else {
  128. wx.showModal({
  129. title: '错误',
  130. content: '无法获取用户资金信息',
  131. showCancel: false
  132. })
  133. }
  134. })
  135. },
  136. async wxSphGetpaymentparams(e) {
  137. const orderId = e.currentTarget.dataset.id
  138. const res = await WXAPI.wxSphGetpaymentparams(wx.getStorageSync('token'), orderId)
  139. if (res.code != 0) {
  140. wx.showToast({
  141. title: res.msg,
  142. icon: 'none'
  143. })
  144. return;
  145. }
  146. // 发起支付
  147. wx.requestPayment({
  148. timeStamp: res.data.timeStamp,
  149. nonceStr: res.data.nonceStr,
  150. package: res.data.package,
  151. signType: res.data.signType,
  152. paySign: res.data.paySign,
  153. fail: aaa => {
  154. console.error(aaa)
  155. wx.showToast({
  156. title: '支付失败:' + aaa
  157. })
  158. },
  159. success: () => {
  160. // 提示支付成功
  161. wx.showToast({
  162. title: '支付成功'
  163. })
  164. this.orderList()
  165. }
  166. })
  167. },
  168. _toPayTap: function (orderId, money){
  169. const _this = this
  170. if (money <= 0) {
  171. // 直接使用余额支付
  172. WXAPI.orderPay(wx.getStorageSync('token'), orderId).then(function (res) {
  173. _this.data.page = 1
  174. _this.orderList()
  175. _this.getOrderStatistics()
  176. })
  177. } else {
  178. wxpay.wxpay('order', money, orderId, "/pages/order-list/index");
  179. }
  180. },
  181. onLoad: function(options) {
  182. if (options && options.type) {
  183. if (options.type == 99) {
  184. this.setData({
  185. hasRefund: true
  186. });
  187. } else {
  188. const tabIndex = this.data.statusType.findIndex(ele => {
  189. return ele.status == options.type
  190. })
  191. this.setData({
  192. status: options.type,
  193. tabIndex
  194. });
  195. }
  196. }
  197. this.getOrderStatistics();
  198. this.orderList();
  199. this.setData({
  200. sphpay_open: wx.getStorageSync('sphpay_open')
  201. })
  202. },
  203. onReady: function() {
  204. // 生命周期函数--监听页面初次渲染完成
  205. },
  206. getOrderStatistics() {
  207. WXAPI.orderStatistics(wx.getStorageSync('token')).then(res => {
  208. if (res.code == 0) {
  209. const badges = this.data.badges;
  210. badges[1] = res.data.count_id_no_pay
  211. badges[2] = res.data.count_id_no_transfer
  212. badges[3] = res.data.count_id_no_confirm
  213. badges[4] = res.data.count_id_no_reputation
  214. this.setData({
  215. badges
  216. })
  217. }
  218. })
  219. },
  220. onShow: function() {
  221. },
  222. onPullDownRefresh: function () {
  223. this.data.page = 1
  224. this.getOrderStatistics()
  225. this.orderList()
  226. wx.stopPullDownRefresh()
  227. },
  228. onReachBottom() {
  229. this.setData({
  230. page: this.data.page + 1
  231. });
  232. this.orderList()
  233. },
  234. async orderList(){
  235. wx.showLoading({
  236. title: '',
  237. })
  238. var postData = {
  239. page: this.data.page,
  240. pageSize: 20,
  241. token: wx.getStorageSync('token')
  242. };
  243. if (this.data.hasRefund) {
  244. postData.hasRefund = true
  245. }
  246. if (!postData.hasRefund) {
  247. postData.status = this.data.status;
  248. }
  249. if (postData.status == 9999) {
  250. postData.status = ''
  251. }
  252. const res = await WXAPI.orderList(postData)
  253. wx.hideLoading()
  254. if (res.code == 0) {
  255. if (this.data.page == 1) {
  256. this.setData({
  257. orderList: res.data.orderList,
  258. logisticsMap: res.data.logisticsMap,
  259. goodsMap: res.data.goodsMap
  260. })
  261. } else {
  262. this.setData({
  263. orderList: this.data.orderList.concat(res.data.orderList),
  264. logisticsMap: Object.assign(this.data.logisticsMap, res.data.logisticsMap),
  265. goodsMap: Object.assign(this.data.goodsMap, res.data.goodsMap)
  266. })
  267. }
  268. } else {
  269. if (this.data.page == 1) {
  270. this.setData({
  271. orderList: null,
  272. logisticsMap: {},
  273. goodsMap: {}
  274. })
  275. } else {
  276. wx.showToast({
  277. title: '没有更多了',
  278. icon: 'none'
  279. })
  280. }
  281. }
  282. },
  283. })