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.

806 lines
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.showToast({
  381. title: "您有未支付订单",
  382. image: './../../../assets/images/fail.png',
  383. duration: 2000,
  384. mask: false
  385. });
  386. }else if (err.code == 11005) {
  387. /**
  388. * 将值传到用户手机号授权的页面
  389. *
  390. */
  391. wx.redirectTo({
  392. url: "/pages/getphoneInfo/index?couponChannelId=" +
  393. that.data.couponChannelId +
  394. "&couponId=" +
  395. that.data.couponId
  396. });
  397. } else if (err.code == 11006) {
  398. // 用户手机已加密
  399. wx.redirectTo({
  400. url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  401. that.data.couponChannelId +
  402. "&couponId=" +
  403. that.data.couponId
  404. });
  405. } else {
  406. wx.showToast({
  407. title: err.message,
  408. icon: 'none',
  409. duration: 2000,
  410. mask: false
  411. });
  412. }
  413. }).then(res => {
  414. console.log(res);
  415. if (typeof(res) != "undefined") {
  416. let orderId = "" + res.data.id;
  417. that.setData({
  418. orderId: orderId
  419. });
  420. if (e.currentTarget.dataset.saleprice / 100 > 0 || e.currentTarget.dataset.price / 100 > 0) {
  421. /**
  422. * 支付订单创建
  423. */
  424. Http.post({
  425. url: config.api.payOrderCreate,
  426. data: {
  427. orderId: orderId
  428. }
  429. })
  430. .then(res => {
  431. var payOrderId = "" + res.data.payOrderId;
  432. wx.hideLoading();
  433. wx.requestPayment({
  434. timeStamp: res.data.timeStamp,
  435. nonceStr: res.data.nonceStr,
  436. package: res.data.package,
  437. signType: (res.data.signType) ? res.data.signType : "MD5",
  438. paySign: res.data.paySign,
  439. success: res => {
  440. wx.showLoading({
  441. title: '订单正在处理中...',
  442. })
  443. setTimeout(function() {
  444. wx.hideLoading()
  445. }, 5000)
  446. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', '', that);
  447. if (res.errMsg == "requestPayment:ok") {
  448. setTimeout(function() {
  449. wx.hideLoading();
  450. }, 2000);
  451. /**
  452. * 用户支付成功以后跳转到券包列表
  453. */
  454. if (that.data.cardType == 100) {
  455. wx.setStorage({
  456. key: 'couponNum2',
  457. data: "couponNum2"
  458. })
  459. } else {
  460. wx.setStorage({
  461. key: 'couponNum',
  462. data: "couponNum"
  463. })
  464. }
  465. }
  466. },
  467. fail: res => {
  468. /**
  469. * 支付失败,需要更新订单的状态
  470. */
  471. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', 'fail', that);
  472. that.setData({
  473. showbutton: false
  474. })
  475. return;
  476. },
  477. complete: res => {}
  478. });
  479. /// End payment --------
  480. })
  481. .catch(err => {
  482. console.log(err);
  483. wx.showToast({
  484. title: err.message,
  485. icon: 'none',
  486. duration: 2000,
  487. mask: false
  488. });
  489. })
  490. } else {
  491. // 免费券
  492. that.payOrderUpdate(orderId, "0", 1, '', 'free', that);
  493. if (that.data.cardType == 100) {
  494. wx.setStorage({
  495. key: 'couponNum2',
  496. data: "couponNum2"
  497. })
  498. } else {
  499. wx.setStorage({
  500. key: 'couponNum',
  501. data: "couponNum"
  502. })
  503. }
  504. }
  505. }
  506. })
  507. },
  508. /**
  509. * 发起支付
  510. */
  511. orderFunc(e) {
  512. var that = this;
  513. that.setData({
  514. showButton: true
  515. })
  516. Http.get({
  517. url: config.api.checkPhoneStatus,
  518. data: {}
  519. })
  520. .then(res => {
  521. that.setData({
  522. showButton: false
  523. })
  524. if (typeof(res) != "undefined") {
  525. let orderId = "" + e.currentTarget.dataset.orderid;
  526. that.setData({
  527. orderId: orderId
  528. });
  529. if (e.currentTarget.dataset.saleprice / 100 > 0 || e.currentTarget.dataset.price / 100 > 0) {
  530. /**
  531. * 支付订单创建
  532. */
  533. Http.post({
  534. url: config.api.payOrderCreate,
  535. data: {
  536. orderId: orderId
  537. }
  538. })
  539. .then(res => {
  540. var payOrderId = "" + res.data.payOrderId;
  541. wx.hideLoading();
  542. wx.requestPayment({
  543. timeStamp: res.data.timeStamp,
  544. nonceStr: res.data.nonceStr,
  545. package: res.data.package,
  546. signType: (res.data.signType) ? res.data.signType : "MD5",
  547. paySign: res.data.paySign,
  548. success: res => {
  549. wx.showLoading({
  550. title: '订单正在处理中...',
  551. })
  552. setTimeout(function() {
  553. wx.hideLoading()
  554. }, 5000)
  555. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', '', that);
  556. if (res.errMsg == "requestPayment:ok") {
  557. setTimeout(function() {
  558. wx.hideLoading();
  559. }, 2000);
  560. /**
  561. * 用户支付成功以后跳转到券包列表
  562. */
  563. if (that.data.cardType == 100) {
  564. wx.setStorage({
  565. key: 'couponNum2',
  566. data: "couponNum2"
  567. })
  568. } else {
  569. wx.setStorage({
  570. key: 'couponNum',
  571. data: "couponNum"
  572. })
  573. }
  574. }
  575. },
  576. fail: res => {
  577. /**
  578. * 支付失败,需要更新订单的状态
  579. */
  580. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', 'fail', that);
  581. that.setData({
  582. showbutton: false
  583. })
  584. return;
  585. },
  586. complete: res => {}
  587. });
  588. /// End payment --------
  589. })
  590. .catch(err => {
  591. console.log(err);
  592. wx.showToast({
  593. title: err.message,
  594. icon: 'none',
  595. duration: 2000,
  596. mask: false
  597. });
  598. })
  599. } else {
  600. // 免费券
  601. that.payOrderUpdate(orderId, "0", 1, '', 'free', that);
  602. if (that.data.cardType == 100) {
  603. wx.setStorage({
  604. key: 'couponNum2',
  605. data: "couponNum2"
  606. })
  607. } else {
  608. wx.setStorage({
  609. key: 'couponNum',
  610. data: "couponNum"
  611. })
  612. }
  613. }
  614. }
  615. })
  616. .catch(err => {
  617. that.setData({
  618. showButton: true
  619. })
  620. if (err.code == 2011) {
  621. wx.showToast({
  622. title: "商户信息没找到",
  623. image: './../../../assets/images/fail.png',
  624. duration: 2000,
  625. mask: false
  626. });
  627. } else if (err.code == 2013) {
  628. wx.showToast({
  629. title: "商户信息禁用",
  630. image: './../../../assets/images/fail.png',
  631. duration: 2000,
  632. mask: false
  633. });
  634. } else if (err.code == 3000) {
  635. wx.showToast({
  636. title: "库存不足",
  637. image: './../../../assets/images/fail.png',
  638. duration: 2000,
  639. mask: false
  640. });
  641. } else if (err.code == 3001) {
  642. wx.showToast({
  643. title: "您已超过限购",
  644. image: './../../../assets/images/fail.png',
  645. duration: 2000,
  646. mask: false
  647. });
  648. } else if (err.code == 3002) {
  649. wx.showToast({
  650. title: "订单失败",
  651. image: './../../../assets/images/fail.png',
  652. duration: 2000,
  653. mask: false
  654. });
  655. } else if (err.code == 3003) {
  656. wx.showToast({
  657. title: "订单不存在",
  658. image: './../../../assets/images/fail.png',
  659. duration: 2000,
  660. mask: false
  661. });
  662. } else if (err.code == 3004) {
  663. wx.showToast({
  664. title: "订单不存在",
  665. image: './../../../assets/images/fail.png',
  666. duration: 2000,
  667. mask: false
  668. });
  669. } else if (err.code == 4003) {
  670. wx.showToast({
  671. title: "卡券已作废",
  672. image: './../../../assets/images/fail.png',
  673. duration: 2000,
  674. mask: false
  675. });
  676. } else if (err.code == 3012) {
  677. wx.showToast({
  678. title: "您已超过限购",
  679. image: './../../../assets/images/fail.png',
  680. duration: 2000,
  681. mask: false
  682. });
  683. }else if (err.code == 11005) {
  684. /**
  685. * 将值传到用户手机号授权的页面
  686. *
  687. */
  688. wx.redirectTo({
  689. url: "/pages/getphoneInfo/index?couponChannelId=" +
  690. that.data.couponChannelId +
  691. "&couponId=" +
  692. that.data.couponId
  693. });
  694. } else if (err.code == 11006) {
  695. // 用户手机已加密
  696. wx.redirectTo({
  697. url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  698. that.data.couponChannelId +
  699. "&couponId=" +
  700. that.data.couponId
  701. });
  702. } else {
  703. wx.showToast({
  704. title: err.message,
  705. icon: 'none',
  706. duration: 2000,
  707. mask: false
  708. });
  709. }
  710. })
  711. },
  712. onUnload: function() {
  713. let that = this;
  714. clearInterval(that.data.setInterval)
  715. },
  716. onHide: function() {
  717. let that = this;
  718. clearInterval(that.data.setInterval)
  719. },
  720. /**
  721. *
  722. * @param {朋友邀请砍价} e
  723. */
  724. barginAgain: function(e) {
  725. let that = this;
  726. let couponChannelId = e.currentTarget.dataset.couponchannelid;
  727. let couponId = e.currentTarget.dataset.couponid;
  728. wx.navigateTo({
  729. url: `/pages/coupon/detail/index?couponChannelId=${couponChannelId}&couponId=${couponId}`,
  730. })
  731. },
  732. /**
  733. * 刷新
  734. */
  735. onPullDownRefresh: function(e) {
  736. let that = this;
  737. console.log(app.globalData.wmorder);
  738. if (app.globalData.wmorder) {
  739. that.pressOrderDetail(app.globalData.wmorder);
  740. }
  741. },
  742. onShareAppMessage: function(options) {
  743. console.log(options)
  744. var that = this;
  745. console.log(that.data.data.id)
  746. var shareObj = {
  747. title: that.data.nickName + '邀请您帮砍' + that.data.data.title,
  748. path: `/pages/index/index?orderId=${that.data.data.id}&from=${"discount"}`,
  749. success: function(res) {
  750. if (res.errMsg == 'shareAppMessage:ok') {}
  751. },
  752. fail: function(error) {
  753. if (res.errMsg == 'shareAppMessage:fail cancel') {} else if (res.errMsg == 'shareAppMessage:fail') {}
  754. }
  755. };
  756. // 来自页面内的按钮的转发
  757. if (options.from == 'button') {
  758. var orderId = options.target.dataset.orderid;
  759. shareObj.path = `/pages/index/index?orderId=${orderId}&from=${"discount"}`;
  760. }
  761. // 返回shareObj
  762. return shareObj;
  763. },
  764. })
  765. function diffTime(startDate) {
  766. var diff = new Date().getTime() - startDate;
  767. var days = Math.floor(diff / (24 * 3600 * 1000));
  768. var leave1 = diff % (24 * 3600 * 1000);
  769. var hours = Math.floor(leave1 / (3600 * 1000));
  770. var leave2 = leave1 % (3600 * 1000);
  771. var minutes = Math.floor(leave2 / (60 * 1000));
  772. var leave3 = leave2 % (60 * 1000);
  773. var seconds = Math.round(leave3 / 1000);
  774. var returnStr = seconds + "秒";
  775. if (minutes > 0) {
  776. returnStr = minutes + "分" + returnStr;
  777. }
  778. if (hours > 0) {
  779. returnStr = hours + "小时" + returnStr;
  780. }
  781. if (days > 0) {
  782. returnStr = days + "天" + returnStr;
  783. }
  784. if (returnStr.indexOf('-1') == -1) {
  785. return returnStr;
  786. } else {
  787. return '1秒'
  788. }
  789. }
  790. function compare(property) {
  791. return function(a, b) {
  792. var value1 = a[property];
  793. var value2 = b[property];
  794. return value2 - value1;
  795. }
  796. }