orderDetail.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // pages/goods/orderDetail/orderDetail.js
  2. import {
  3. get,
  4. post
  5. } from '../../../utils/http'
  6. import {
  7. $wuxCountDown
  8. } from '../../../components/dist/index'
  9. import {
  10. toQrcode
  11. } from '../../../utils/util'
  12. import {
  13. timestampToDate
  14. } from '../../../utils/time'
  15. Page({
  16. /**
  17. * 页面的初始数据
  18. */
  19. data: {
  20. time: '',
  21. sum: 0, // 防止进入死循环
  22. detail: {},
  23. statusTexts: {
  24. // 状态,-1已取消,0待支付,1已支付,3.已结算
  25. s_0: '待支付',
  26. s_1: '待取货',
  27. s_2: '已完成',
  28. s_3: '已完成'
  29. },
  30. showTips: false, // 是否展示页面顶部提示
  31. tips: {
  32. status_0: ['待支付', '逾期未付款,订单将自动取消!'],
  33. status_1: ['预约成功', '您的预约单已生效,请准时到店体验!'],
  34. status_2: ['预约成功', '您的预约单已生效,请准时到店体验!']
  35. },
  36. pay_methon: 1,
  37. payNow: false,
  38. productNum: 0, //订单详情商品总数量
  39. level: '', //用户充值折扣等级 (0是未拥有等级)
  40. levelPrice: '', //充值等级折扣优惠价格
  41. toLevelPrice: '', //充值折扣扣除金额
  42. notSalePrice: 0, //不是秒杀产品的总价
  43. salePrice: 0, //秒杀产品总价
  44. goods_price: '', //美妆产品总价格
  45. countdownTime: 900*1000, //秒杀剩余支付倒计时
  46. },
  47. pay_methonw() {
  48. this.setData({
  49. pay_methon: 1
  50. })
  51. },
  52. pay_methonc() {
  53. this.setData({
  54. pay_methon: 2
  55. })
  56. },
  57. onClosePay() {
  58. this.setData({
  59. payNow: false
  60. })
  61. },
  62. /**
  63. * 获取我的储值
  64. * api/user
  65. */
  66. getAmount() {
  67. get('api/user/amount', {}, (res) => {
  68. if (res.data) {
  69. this.setData({
  70. inserllAmount: res.data.amount
  71. })
  72. }
  73. })
  74. },
  75. /**
  76. * 生命周期函数--监听页面加载
  77. */
  78. onLoad: function (options) {
  79. console.log(options)
  80. this.getOrderDetail(options.id)
  81. },
  82. /**
  83. * 生命周期函数--监听页面初次渲染完成
  84. */
  85. onReady: function () {},
  86. /**
  87. * 生命周期函数--监听页面显示
  88. */
  89. onShow: function () {
  90. this.getAmount()
  91. },
  92. /**
  93. * 生命周期函数--监听页面隐藏
  94. */
  95. onHide: function () {},
  96. /**
  97. * 生命周期函数--监听页面卸载
  98. */
  99. onUnload: function () {
  100. if (this.c1) this.c1.stop()
  101. },
  102. /**
  103. * 页面相关事件处理函数--监听用户下拉动作
  104. */
  105. onPullDownRefresh: function () {},
  106. /**
  107. * 页面上拉触底事件的处理函数
  108. */
  109. onReachBottom: function () {},
  110. /**
  111. * 用户点击右上角分享
  112. */
  113. onShareAppMessage: function () {},
  114. /**
  115. * 复制订单号
  116. */
  117. onCopyOrderNum(e) {
  118. wx.setClipboardData({
  119. data: e.currentTarget.dataset.num || '',
  120. success(res) {
  121. console.log(res)
  122. }
  123. })
  124. },
  125. /**
  126. * 取消订单
  127. */
  128. onCancelOrder() {
  129. let that = this
  130. wx.showModal({
  131. title: '提示',
  132. content: '是否取消当前订单',
  133. success(res) {
  134. if (res.confirm) {
  135. console.log('用户点击确定')
  136. get(
  137. 'v2/api/goods_order/cancel', {
  138. order_id: that.data.detail.id
  139. },
  140. (res) => {
  141. wx.showToast({
  142. title: res.msg,
  143. icon: 'success',
  144. duration: 2000
  145. })
  146. that.getOrderDetail(that.data.detail.id)
  147. }
  148. )
  149. } else if (res.cancel) {
  150. console.log('用户点击取消')
  151. }
  152. }
  153. })
  154. },
  155. // 获取用户充值折扣等级
  156. getLevel(sale) {
  157. //sale 是否秒杀价格,1是,0否
  158. get('/v2/api/user/discount_level', {}, (res) => {
  159. if (res.code == 200) {
  160. res.data = Number(res.data)
  161. if (res.data == 0 || this.data.detail.meet_discount == 1 || sale == 1) {
  162. this.setData({
  163. level: 0
  164. })
  165. return
  166. }
  167. this.setData({
  168. // 优惠的等级
  169. level: Number(res.data * 0.1)
  170. })
  171. this.setData({
  172. // 优惠的折扣 (等级*10)
  173. tolevel: (this.data.level * 10).toFixed(1)
  174. })
  175. }
  176. // 计算充值折扣
  177. this.rechargeDiscount()
  178. })
  179. },
  180. // 判断订单里的秒杀产品统计
  181. salePrice(list) {
  182. let notSalePrice = '' //非秒杀产品总价格
  183. let salePrice = '' // 秒杀产品总价格
  184. let sale = 1
  185. let goods_price = 0
  186. list.forEach(item => {
  187. if (item.sale == 0) {
  188. sale = 0
  189. return
  190. }
  191. })
  192. list.forEach(item => {
  193. if (item.sale == 0) {
  194. notSalePrice = Number(notSalePrice) + Number(item.price) * Number(item.num)
  195. } else {
  196. salePrice = Number(salePrice) + Number(item.price) * Number(item.num)
  197. }
  198. })
  199. // 计算美妆产品总价
  200. list.forEach(item => {
  201. goods_price = Number(goods_price) + Number(item.price * item.num)
  202. })
  203. this.setData({
  204. notSalePrice: notSalePrice,
  205. salePrice: salePrice,
  206. goods_price: goods_price
  207. })
  208. // 获取用户折扣等级
  209. this.getLevel(sale)
  210. },
  211. // 计算充值折扣
  212. rechargeDiscount() {
  213. // 计算折扣
  214. this.setData({
  215. // 优惠完的价格
  216. levelPrice: (this.data.notSalePrice * this.data.level + Number(this.data.salePrice)).toFixed(2),
  217. })
  218. this.setData({
  219. // 优惠了多少
  220. toLevelPrice: (this.data.goods_price - this.data.levelPrice).toFixed(2)
  221. })
  222. console.log(this.data.levelPrice, this.data.toLevelPrice);
  223. },
  224. onPay: function (e) {
  225. this.setData({
  226. payNow: true
  227. })
  228. },
  229. /**
  230. * 去支付
  231. */
  232. payNow(e) {
  233. let that = this
  234. post(
  235. 'v2/api/order/goods/pay', {
  236. pay_way: this.data.pay_methon == 1 ? 'weixin' : 'amount',
  237. order_id: that.data.detail.id
  238. },
  239. (res) => {
  240. that.setData({
  241. payNow: false
  242. })
  243. if (that.data.pay_methon == 2) {
  244. wx.showToast({
  245. title: '支付成功',
  246. icon: 'success'
  247. })
  248. that.getOrderDetail(that.data.detail.id)
  249. return
  250. }
  251. wx.requestPayment({
  252. timeStamp: res.data.pay_data.timeStamp,
  253. nonceStr: res.data.pay_data.nonceStr,
  254. package: res.data.pay_data.package,
  255. signType: res.data.pay_data.signType,
  256. paySign: res.data.pay_data.paySign,
  257. success(res) {
  258. if (res.errMsg == 'requestPayment:ok') {
  259. wx.showToast({
  260. title: '支付成功',
  261. icon: 'success'
  262. })
  263. that.getOrderDetail(that.data.detail.id)
  264. }
  265. },
  266. fail(res) {
  267. wx.showToast({
  268. title: '支付失败',
  269. icon: 'error'
  270. })
  271. }
  272. })
  273. }
  274. )
  275. },
  276. /**
  277. * 申请退款
  278. */
  279. onRefundOrder() {
  280. let that = this
  281. wx.showModal({
  282. title: '提示',
  283. content: '是否退款当前订单',
  284. success(res) {
  285. if (res.confirm) {
  286. console.log('用户点击确定')
  287. post(
  288. 'api/goods/refund', {
  289. order_id: that.data.detail.id
  290. },
  291. (res) => {
  292. wx.showToast({
  293. title: res.msg,
  294. icon: 'success',
  295. duration: 2000
  296. })
  297. that.getOrderDetail(that.data.detail.id)
  298. }
  299. )
  300. } else if (res.cancel) {
  301. console.log('用户点击取消')
  302. }
  303. }
  304. })
  305. },
  306. /**
  307. * 获取详情数据
  308. * /api/order/info
  309. */
  310. getOrderDetail(order_id) {
  311. let that = this
  312. get(
  313. 'v2/api/goods_order/detail', {
  314. order_id
  315. },
  316. (res) => {
  317. console.log(res)
  318. // 判断订单里的秒杀产品统计
  319. this.salePrice(res.data.good)
  320. //pay_way:1.支付宝支付,2微信支付,3余额支付
  321. let pay_way_list = ['支付宝支付', '微信支付', '余额支付']
  322. res.data['status_text'] = that.data.statusTexts['s_' + res.data.status]
  323. res.data['pay_way'] = pay_way_list[res.data.pay_way - 1]
  324. res.data['_pay_time'] = timestampToDate(res.data.pay_time, 'Y-m-d H:i')
  325. res.data.seconds = Number(res.data.seconds)
  326. this.setData({
  327. detail: res.data,
  328. showTips: [0].indexOf(res.data.status) > -1
  329. })
  330. console.log(res.data.good);
  331. // 设置订单详情商品总数量
  332. let num = 0
  333. res.data.good.map(item => {
  334. num += item.num
  335. })
  336. num = Number(num) + Number(res.data.plus.length)
  337. this.setData({
  338. productNum: num
  339. })
  340. if (res.data.status == 0 && this.data.sum <= 0) {
  341. this.data.sum++
  342. that.c1 = new $wuxCountDown({
  343. date: +new Date() + res.data.invalid_time_seconds * 1000,
  344. render(date) {
  345. const hours = this.leadingZeros(date.hours, 2)
  346. const min = this.leadingZeros(date.min, 2)
  347. const sec = this.leadingZeros(date.sec, 2)
  348. that.setData({
  349. time: `${hours}:${min}:${sec}`
  350. })
  351. },
  352. onEnd() {
  353. that.c1.stop()
  354. that.getOrderDetail(that.data.detail.id)
  355. }
  356. })
  357. }
  358. if (res.data.status == 1) {
  359. // 生成二维码和条形码。 这里可以是发起后台请求,拿到要转的字符再调用方法来转二维码和条形码
  360. setTimeout(function () {
  361. toQrcode('qrcode', res.data.coupon_code, 400, 400)
  362. }, 200)
  363. }
  364. }
  365. )
  366. },
  367. /**
  368. * 用户允许消息通知
  369. * /api/message/agree
  370. */
  371. messageAgree(ids, order_id) {
  372. post(
  373. 'api/message/agree', {
  374. order_id,
  375. ids
  376. },
  377. () => {}
  378. )
  379. },
  380. // 计算倒计时结束
  381. finishTime() {
  382. // 时间结束重新刷新页面订单
  383. this.getOrderDetail(this.data.detail.id)
  384. }
  385. })