C端小程序
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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