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.

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