C端小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

813 line
24 KiB

  1. var config = require("../../config/config.js");
  2. var app = getApp();
  3. const Http = require("../../utils/HttpBasics");
  4. const util = require("../../utils/util");
  5. const imgurl = require("../../utils/imgurl");
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. cancelH: imgurl.cancelH.url,
  12. bannerUrl: imgurl.banner.url,
  13. wmhome: imgurl.wmhome.url,
  14. wmhelp: imgurl.wmhelp.url,
  15. wmsuccess01: imgurl.wmsuccess01.url,
  16. fail: imgurl.fail.url,
  17. data: null,
  18. from: null,
  19. day: null,
  20. hour: null,
  21. min: null,
  22. sec: null,
  23. showPage: false,
  24. discountStatus: null,
  25. disabled: false,
  26. display: "block!important",
  27. showButton1: false
  28. },
  29. /**
  30. * 生命周期函数--监听页面加载
  31. */
  32. onLoad: function(options) {
  33. let that = this;
  34. if (options.orderId || options.orderId && options.from == 'discount') {
  35. if (options.from) {
  36. that.setData({
  37. orderId: options.orderId,
  38. from: "discount"
  39. })
  40. } else {
  41. that.setData({
  42. orderId: options.orderId
  43. })
  44. }
  45. app.globalData.wmorder = options.orderId
  46. that.pressOrderDetail(options.orderId);
  47. }
  48. this.getUserInfo()
  49. },
  50. getUserInfo: function() {
  51. let that = this;
  52. // 获取用户信息
  53. Http.get({
  54. url: config.api.getScore,
  55. data: {}
  56. })
  57. .then(res => {
  58. console.log(res)
  59. that.setData({
  60. nickName: res.data.nickName,
  61. avatarUrl: res.data.avatarUrl
  62. })
  63. })
  64. },
  65. onShow: function() {
  66. let that = this;
  67. var todayDate = new Date().getTime();
  68. that.setData({
  69. todayDate: todayDate,
  70. showButton1: false
  71. })
  72. if (that.data.pressEndDate) {
  73. that.countdown(that.data.pressEndDate)
  74. }
  75. },
  76. goback: function() {
  77. wx.switchTab({
  78. url: '/pages/main/index',
  79. })
  80. },
  81. /**
  82. * 更多砍价商品
  83. */
  84. gotomore: function() {
  85. wx.navigateTo({
  86. url: '/pages/bargain/bargain',
  87. })
  88. },
  89. /**
  90. * @param {砍价状态(1:我发起的砍价,2:未参与的砍价, 3:已参与的砍价)} orderId
  91. */
  92. getPressOrderStatus: function(orderId) {
  93. let that = this;
  94. Http.get({
  95. url: config.api.getPressOrderStatus,
  96. data: {
  97. orderId: orderId
  98. }
  99. })
  100. .then(res => {
  101. if (res.data.status == 1) {
  102. that.setData({
  103. discountStatus: 1
  104. })
  105. } else if (res.data.status == 2) {
  106. that.setData({
  107. discountStatus: 2
  108. })
  109. } else if (res.data.status == 3) {
  110. that.setData({
  111. discountStatus: 3
  112. })
  113. }
  114. wx.stopPullDownRefresh();
  115. })
  116. .catch(err => {
  117. console.log(err)
  118. wx.stopPullDownRefresh();
  119. wx.showToast({
  120. title: err.errMsg,
  121. icon: "none"
  122. })
  123. })
  124. },
  125. helpDiscount: function() {
  126. let that = this;
  127. that.pressOrderJoin(that.data.orderId);
  128. },
  129. //参与砍价
  130. pressOrderJoin: function(orderId) {
  131. let that = this;
  132. Http.post({
  133. url: config.api.pressOrderJoin,
  134. data: {
  135. orderId: orderId
  136. }
  137. })
  138. .then(res => {
  139. console.log(res);
  140. // id 是订单号
  141. that.pressOrderDetail(orderId);
  142. })
  143. .catch(err => {
  144. console.log(err)
  145. wx.showToast({
  146. title: err.message,
  147. icon: "none"
  148. })
  149. })
  150. },
  151. // 订单详情
  152. pressOrderDetail: function(orderId) {
  153. let that = this;
  154. Http.get({
  155. url: config.api.pressOrderDetail,
  156. data: {
  157. orderId: orderId
  158. }
  159. })
  160. .then(res => {
  161. console.log(res);
  162. res.data.orderPressList.map(file => {
  163. file.createDate = diffTime(file.createDate)
  164. })
  165. that.countdown(res.data.pressEndDate);
  166. that.setData({
  167. pressEndDate: res.data.pressEndDate
  168. })
  169. res.data.orderPressList.map(file => {
  170. compare(file.first);
  171. })
  172. console.log(res.data.orderPressList.sort(compare('first')))
  173. that.setData({
  174. data: res.data,
  175. already: (res.data.price / 100 - res.data.salePrice / 100 - res.data.pressCurrentValue / 100).toFixed(2),
  176. remain: (res.data.pressCurrentValue / 100).toFixed(2),
  177. showPage: true
  178. })
  179. that.getPressOrderStatus(orderId);
  180. })
  181. .catch(err => {
  182. wx.showToast({
  183. title: err.message,
  184. icon: "none"
  185. })
  186. wx.stopPullDownRefresh();
  187. })
  188. },
  189. setIntervalTime(end_time) {
  190. let that = this;
  191. var EndTime = end_time;
  192. var NowTime = new Date().getTime();
  193. var total_micro_second = EndTime - NowTime || [];
  194. // 渲染倒计时时钟
  195. let obj = that.dateformat(total_micro_second);
  196. if (total_micro_second > 0) {
  197. that.setData({
  198. clock: obj,
  199. day: obj.a1,
  200. hour: obj.b1,
  201. min: obj.c1,
  202. sec: obj.d1,
  203. })
  204. } else {
  205. that.setData({
  206. clock: "00",
  207. day: "00",
  208. hour: "00",
  209. min: "00",
  210. sec: "00",
  211. })
  212. //如果倒计时结束,需要重新查询一下券的状态
  213. // 给getDetail一个标识
  214. }
  215. total_micro_second -= 1000;
  216. },
  217. countdown: function(end_time) {
  218. let that = this;
  219. that.setIntervalTime(end_time);
  220. that.data.setInterval = setInterval(function() {
  221. that.setIntervalTime(end_time);
  222. }, 1000)
  223. },
  224. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  225. dateformat(micro_second) {
  226. // 总秒数
  227. var second = Math.floor(micro_second / 1000);
  228. // 天数
  229. var day = Math.floor(second / 3600 / 24) < 10 ? "0" + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
  230. // 小时
  231. var hr = Math.floor(second / 3600 % 24) < 10 ? "0" + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
  232. // 分钟
  233. var min = Math.floor(second / 60 % 60) < 10 ? "0" + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
  234. // 秒
  235. var sec = Math.floor(second % 60) < 10 ? "0" + Math.floor(second % 60) : Math.floor(second % 60);
  236. // return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  237. return {
  238. a1: day,
  239. b1: hr,
  240. c1: min,
  241. d1: sec
  242. }
  243. },
  244. payOrderUpdate: (orderId, payOrderId, status, reason, type, _this) => {
  245. let that = this;
  246. console.log(that)
  247. console.log(_this)
  248. // 支付成功
  249. Http.post({
  250. url: config.api.payOrderUpdate,
  251. data: {
  252. payOrderId: payOrderId,
  253. orderId: orderId,
  254. status: status,
  255. reason: reason
  256. }
  257. })
  258. .then(res => {
  259. wx.hideLoading();
  260. _this.setData({
  261. display: "none!important",
  262. })
  263. // 有价券
  264. if (!type && type != 'free') {
  265. wx.navigateTo({
  266. url: `/pages/order/detail/index?orderId=${
  267. orderId
  268. }`
  269. });
  270. } else if (type == 'free') {
  271. wx.navigateTo({
  272. url: `/pages/order/detail/index?orderId=${
  273. orderId
  274. }`
  275. });
  276. }
  277. })
  278. .catch(err => {
  279. console.log(err);
  280. if (!type) {
  281. setTimeout(function() {
  282. _this.payOrderUpdate(orderId, payOrderId, status, reason, type, _this);
  283. }, 2000)
  284. }
  285. })
  286. },
  287. /**
  288. * 按原价付款
  289. */
  290. // orderFunc1(e) {
  291. // var that = this;
  292. // let formId = e.detail.formId;
  293. // that.setData({
  294. // showButton: true
  295. // })
  296. // Http.get({
  297. // url: config.api.checkPhoneStatus,
  298. // data: {}
  299. // })
  300. // .then(res => {
  301. // that.setData({
  302. // showButton: false
  303. // })
  304. // /**
  305. // * orderSave 下单
  306. // */
  307. // var data = {
  308. // couponChannelId:e.currentTarget.dataset.couponchannelid,
  309. // couponId:e.currentTarget.dataset.couponid,
  310. // formId:formId
  311. // }
  312. // return Http.post({
  313. // url: config.api.orderSave,
  314. // data: data
  315. // });
  316. // })
  317. // .catch(err => {
  318. // that.setData({
  319. // showButton: true
  320. // })
  321. // if (err.code == 2011) {
  322. // wx.showToast({
  323. // title: "商户信息没找到",
  324. // image: './../../../assets/images/fail.png',
  325. // duration: 2000,
  326. // mask: false
  327. // });
  328. // } else if (err.code == 2013) {
  329. // wx.showToast({
  330. // title: "商户信息禁用",
  331. // image: './../../../assets/images/fail.png',
  332. // duration: 2000,
  333. // mask: false
  334. // });
  335. // } else if (err.code == 3000) {
  336. // wx.showToast({
  337. // title: "库存不足",
  338. // image: './../../../assets/images/fail.png',
  339. // duration: 2000,
  340. // mask: false
  341. // });
  342. // } else if (err.code == 3001) {
  343. // wx.showToast({
  344. // title: "您已超过限购",
  345. // image: './../../../assets/images/fail.png',
  346. // duration: 2000,
  347. // mask: false
  348. // });
  349. // } else if (err.code == 3002) {
  350. // wx.showToast({
  351. // title: "订单失败",
  352. // image: './../../../assets/images/fail.png',
  353. // duration: 2000,
  354. // mask: false
  355. // });
  356. // } else if (err.code == 3003) {
  357. // wx.showToast({
  358. // title: "订单不存在",
  359. // image: './../../../assets/images/fail.png',
  360. // duration: 2000,
  361. // mask: false
  362. // });
  363. // } else if (err.code == 3004) {
  364. // wx.showToast({
  365. // title: "订单不存在",
  366. // image: './../../../assets/images/fail.png',
  367. // duration: 2000,
  368. // mask: false
  369. // });
  370. // } else if (err.code == 4003) {
  371. // wx.showToast({
  372. // title: "卡券已作废",
  373. // image: './../../../assets/images/fail.png',
  374. // duration: 2000,
  375. // mask: false
  376. // });
  377. // } else if (err.code == 3012) {
  378. // wx.showModal({
  379. // title: '提示',
  380. // content: "您有未支付订单进行支付",
  381. // showCancel: false,
  382. // success: function (res) {
  383. // console.log(res.confirm)
  384. // if (res.confirm) {
  385. // }
  386. // }
  387. // })
  388. // }else if (err.code == 11005) {
  389. // /**
  390. // * 将值传到用户手机号授权的页面
  391. // *
  392. // */
  393. // wx.redirectTo({
  394. // url: "/pages/getphoneInfo/index?couponChannelId=" +
  395. // that.data.couponChannelId +
  396. // "&couponId=" +
  397. // that.data.couponId
  398. // });
  399. // } else if (err.code == 11006) {
  400. // // 用户手机已加密
  401. // wx.redirectTo({
  402. // url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  403. // that.data.couponChannelId +
  404. // "&couponId=" +
  405. // that.data.couponId
  406. // });
  407. // } else {
  408. // wx.showToast({
  409. // title: err.message,
  410. // icon: 'none',
  411. // duration: 2000,
  412. // mask: false
  413. // });
  414. // }
  415. // }).then(res => {
  416. // console.log(res);
  417. // if (typeof(res) != "undefined") {
  418. // let orderId = "" + res.data.id;
  419. // that.setData({
  420. // orderId: orderId
  421. // });
  422. // if (e.currentTarget.dataset.saleprice / 100 > 0 || e.currentTarget.dataset.price / 100 > 0) {
  423. // /**
  424. // * 支付订单创建
  425. // */
  426. // Http.post({
  427. // url: config.api.payOrderCreate,
  428. // data: {
  429. // orderId: orderId
  430. // }
  431. // })
  432. // .then(res => {
  433. // var payOrderId = "" + res.data.payOrderId;
  434. // wx.hideLoading();
  435. // wx.requestPayment({
  436. // timeStamp: res.data.timeStamp,
  437. // nonceStr: res.data.nonceStr,
  438. // package: res.data.package,
  439. // signType: (res.data.signType) ? res.data.signType : "MD5",
  440. // paySign: res.data.paySign,
  441. // success: res => {
  442. // wx.showLoading({
  443. // title: '订单正在处理中...',
  444. // })
  445. // setTimeout(function() {
  446. // wx.hideLoading()
  447. // }, 5000)
  448. // that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', '', that);
  449. // if (res.errMsg == "requestPayment:ok") {
  450. // setTimeout(function() {
  451. // wx.hideLoading();
  452. // }, 2000);
  453. // /**
  454. // * 用户支付成功以后跳转到券包列表
  455. // */
  456. // if (that.data.cardType == 100) {
  457. // wx.setStorage({
  458. // key: 'couponNum2',
  459. // data: "couponNum2"
  460. // })
  461. // } else {
  462. // wx.setStorage({
  463. // key: 'couponNum',
  464. // data: "couponNum"
  465. // })
  466. // }
  467. // }
  468. // },
  469. // fail: res => {
  470. // /**
  471. // * 支付失败,需要更新订单的状态
  472. // */
  473. // that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', 'fail', that);
  474. // that.setData({
  475. // showbutton: false
  476. // })
  477. // return;
  478. // },
  479. // complete: res => {}
  480. // });
  481. // /// End payment --------
  482. // })
  483. // .catch(err => {
  484. // console.log(err);
  485. // wx.showToast({
  486. // title: err.message,
  487. // icon: 'none',
  488. // duration: 2000,
  489. // mask: false
  490. // });
  491. // })
  492. // } else {
  493. // // 免费券
  494. // that.payOrderUpdate(orderId, "0", 1, '', 'free', that);
  495. // if (that.data.cardType == 100) {
  496. // wx.setStorage({
  497. // key: 'couponNum2',
  498. // data: "couponNum2"
  499. // })
  500. // } else {
  501. // wx.setStorage({
  502. // key: 'couponNum',
  503. // data: "couponNum"
  504. // })
  505. // }
  506. // }
  507. // }
  508. // })
  509. // },
  510. /**
  511. * 发起支付
  512. */
  513. orderFunc(e) {
  514. var that = this;
  515. that.setData({
  516. showButton1: true
  517. })
  518. Http.get({
  519. url: config.api.checkPhoneStatus,
  520. data: {}
  521. })
  522. .then(res => {
  523. that.setData({
  524. showButton1: false
  525. })
  526. if (typeof(res) != "undefined") {
  527. let orderId = "" + e.currentTarget.dataset.orderid;
  528. that.setData({
  529. orderId: orderId
  530. });
  531. if (e.currentTarget.dataset.saleprice / 100 > 0 || e.currentTarget.dataset.price / 100 > 0) {
  532. /**
  533. * 支付订单创建
  534. */
  535. Http.post({
  536. url: config.api.payOrderCreate,
  537. data: {
  538. orderId: orderId
  539. }
  540. })
  541. .then(res => {
  542. var payOrderId = "" + res.data.payOrderId;
  543. wx.hideLoading();
  544. wx.requestPayment({
  545. timeStamp: res.data.timeStamp,
  546. nonceStr: res.data.nonceStr,
  547. package: res.data.package,
  548. signType: (res.data.signType) ? res.data.signType : "MD5",
  549. paySign: res.data.paySign,
  550. success: res => {
  551. wx.showLoading({
  552. title: '订单正在处理中...',
  553. })
  554. setTimeout(function() {
  555. wx.hideLoading()
  556. }, 5000)
  557. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', '', that);
  558. if (res.errMsg == "requestPayment:ok") {
  559. setTimeout(function() {
  560. wx.hideLoading();
  561. }, 2000);
  562. /**
  563. * 用户支付成功以后跳转到券包列表
  564. */
  565. if (that.data.cardType == 100) {
  566. wx.setStorage({
  567. key: 'couponNum2',
  568. data: "couponNum2"
  569. })
  570. } else {
  571. wx.setStorage({
  572. key: 'couponNum',
  573. data: "couponNum"
  574. })
  575. }
  576. }
  577. },
  578. fail: res => {
  579. /**
  580. * 支付失败,需要更新订单的状态
  581. */
  582. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', 'fail', that);
  583. that.setData({
  584. showbutton1: false
  585. })
  586. return;
  587. },
  588. complete: res => {}
  589. });
  590. /// End payment --------
  591. })
  592. .catch(err => {
  593. console.log(err);
  594. wx.showToast({
  595. title: err.message,
  596. icon: 'none',
  597. duration: 2000,
  598. mask: false
  599. });
  600. })
  601. } else {
  602. // 免费券
  603. that.payOrderUpdate(orderId, "0", 1, '', 'free', that);
  604. if (that.data.cardType == 100) {
  605. wx.setStorage({
  606. key: 'couponNum2',
  607. data: "couponNum2"
  608. })
  609. } else {
  610. wx.setStorage({
  611. key: 'couponNum',
  612. data: "couponNum"
  613. })
  614. }
  615. }
  616. }
  617. })
  618. .catch(err => {
  619. that.setData({
  620. showButton1: true
  621. })
  622. if (err.code == 2011) {
  623. wx.showToast({
  624. title: "商户信息没找到",
  625. image: './../../../assets/images/fail.png',
  626. duration: 2000,
  627. mask: false
  628. });
  629. } else if (err.code == 2013) {
  630. wx.showToast({
  631. title: "商户信息禁用",
  632. image: './../../../assets/images/fail.png',
  633. duration: 2000,
  634. mask: false
  635. });
  636. } else if (err.code == 3000) {
  637. wx.showToast({
  638. title: "库存不足",
  639. image: './../../../assets/images/fail.png',
  640. duration: 2000,
  641. mask: false
  642. });
  643. } else if (err.code == 3001) {
  644. wx.showToast({
  645. title: "您已超过限购",
  646. image: './../../../assets/images/fail.png',
  647. duration: 2000,
  648. mask: false
  649. });
  650. } else if (err.code == 3002) {
  651. wx.showToast({
  652. title: "订单失败",
  653. image: './../../../assets/images/fail.png',
  654. duration: 2000,
  655. mask: false
  656. });
  657. } else if (err.code == 3003) {
  658. wx.showToast({
  659. title: "订单不存在",
  660. image: './../../../assets/images/fail.png',
  661. duration: 2000,
  662. mask: false
  663. });
  664. } else if (err.code == 3004) {
  665. wx.showToast({
  666. title: "订单不存在",
  667. image: './../../../assets/images/fail.png',
  668. duration: 2000,
  669. mask: false
  670. });
  671. } else if (err.code == 4003) {
  672. wx.showToast({
  673. title: "卡券已作废",
  674. image: './../../../assets/images/fail.png',
  675. duration: 2000,
  676. mask: false
  677. });
  678. } else if (err.code == 3012) {
  679. wx.showModal({
  680. title: '提示',
  681. content: '您有未支付订单,请先进行支付',
  682. confirmText: "去支付",
  683. success: function (res) {
  684. console.log(res.confirm)
  685. if (res.confirm) {
  686. wx.navigateTo({
  687. url: '/pages/order/index/index?id=all',
  688. })
  689. }
  690. }
  691. })
  692. }else if (err.code == 11005) {
  693. /**
  694. * 将值传到用户手机号授权的页面
  695. *
  696. */
  697. wx.redirectTo({
  698. url: "/pages/getphoneInfo/index?couponChannelId=" +
  699. that.data.couponChannelId +
  700. "&couponId=" +
  701. that.data.couponId
  702. });
  703. } else if (err.code == 11006) {
  704. // 用户手机已加密
  705. wx.redirectTo({
  706. url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  707. that.data.couponChannelId +
  708. "&couponId=" +
  709. that.data.couponId
  710. });
  711. } else {
  712. wx.showToast({
  713. title: err.message,
  714. icon: 'none',
  715. duration: 2000,
  716. mask: false
  717. });
  718. }
  719. })
  720. },
  721. onUnload: function() {
  722. let that = this;
  723. clearInterval(that.data.setInterval)
  724. },
  725. onHide: function() {
  726. let that = this;
  727. clearInterval(that.data.setInterval)
  728. },
  729. /**
  730. *
  731. * @param {朋友邀请砍价} e
  732. */
  733. barginAgain: function(e) {
  734. let that = this;
  735. let couponChannelId = e.currentTarget.dataset.couponchannelid;
  736. let couponId = e.currentTarget.dataset.couponid;
  737. wx.navigateTo({
  738. url: `/pages/coupon/detail/index?couponChannelId=${couponChannelId}&couponId=${couponId}`,
  739. })
  740. },
  741. /**
  742. * 刷新
  743. */
  744. onPullDownRefresh: function(e) {
  745. let that = this;
  746. console.log(app.globalData.wmorder);
  747. if (app.globalData.wmorder) {
  748. that.pressOrderDetail(app.globalData.wmorder);
  749. }
  750. },
  751. onShareAppMessage: function(options) {
  752. var that = this;
  753. var shareObj = {
  754. title: that.data.nickName + '邀请您帮砍' + that.data.data.title,
  755. path: `/pages/index/index?orderId=${that.data.data.id}&from=${"discount"}`,
  756. success: function(res) {
  757. if (res.errMsg == 'shareAppMessage:ok') {}
  758. },
  759. fail: function(error) {
  760. if (res.errMsg == 'shareAppMessage:fail cancel') {} else if (res.errMsg == 'shareAppMessage:fail') {}
  761. }
  762. };
  763. // 来自页面内的按钮的转发
  764. if (options.from == 'button') {
  765. var orderId = options.target.dataset.orderid;
  766. shareObj.path = `/pages/index/index?orderId=${orderId}&from=${"discount"}`;
  767. }
  768. // 返回shareObj
  769. return shareObj;
  770. },
  771. })
  772. function diffTime(startDate) {
  773. var diff = new Date().getTime() - startDate;
  774. var days = Math.floor(diff / (24 * 3600 * 1000));
  775. var leave1 = diff % (24 * 3600 * 1000);
  776. var hours = Math.floor(leave1 / (3600 * 1000));
  777. var leave2 = leave1 % (3600 * 1000);
  778. var minutes = Math.floor(leave2 / (60 * 1000));
  779. var leave3 = leave2 % (60 * 1000);
  780. var seconds = Math.round(leave3 / 1000);
  781. var returnStr = seconds + "秒";
  782. if (minutes > 0) {
  783. returnStr = minutes + "分" + returnStr;
  784. }
  785. if (hours > 0) {
  786. returnStr = hours + "小时" + returnStr;
  787. }
  788. if (days > 0) {
  789. returnStr = days + "天" + returnStr;
  790. }
  791. if (returnStr.indexOf('-1') == -1) {
  792. return returnStr;
  793. } else {
  794. return '1秒'
  795. }
  796. }
  797. function compare(property) {
  798. return function(a, b) {
  799. var value1 = a[property];
  800. var value2 = b[property];
  801. return value2 - value1;
  802. }
  803. }