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.

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