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