C端小程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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