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

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