C端小程序
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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