C端小程序
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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