C端小程序
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

807 righe
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. data: {
  8. fenxiangUrl: imgurl.fenxiang.url,
  9. clockUrl: imgurl.clock.url,
  10. teljpgUrl: imgurl.teljpg.url,
  11. closedUrl: imgurl.closed.url,
  12. quesGouUrl: imgurl.ques_gou.url,
  13. headbgUrl: imgurl.headbg.url,
  14. quesBgUrl: imgurl.ques_bg.url,
  15. close02: imgurl.close02.url,
  16. hidden: "hidden",
  17. height: "",
  18. more: "点击查看更多",
  19. showMore: true,
  20. data: {
  21. title: null
  22. },
  23. showPage: false,
  24. questions1: null,
  25. questions2: null,
  26. carList: [],
  27. couponChannelId: null,
  28. orderId: "",
  29. hour: null,
  30. day: null,
  31. month: null,
  32. year: null,
  33. minute: "",
  34. tempFilePaths: null,
  35. userInfo: {},
  36. hasUserInfo: false,
  37. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  38. id: null,
  39. result: [],
  40. end_time: null,
  41. checked: false,
  42. clock: "已经截止",
  43. questionnaire: {},
  44. questionId: null,
  45. widthScreen: null,
  46. moveData: null,
  47. rotateData: null,
  48. alphaData: null,
  49. scaleData: null,
  50. skewData: null,
  51. matrixData: null,
  52. opacity: 0,
  53. queueData: null,
  54. zIndex: 11,
  55. display: "none",
  56. showbutton: false,
  57. cardData: null,
  58. showCardOffer: false,
  59. isSamePeople: true,
  60. statusText: '',
  61. isReceived: false,
  62. receivedDisabled: false,
  63. clock: "00",
  64. day: "00",
  65. hour: "00",
  66. min: "00",
  67. sec: "00",
  68. countdown: ""
  69. },
  70. phone: function (e) {
  71. let that = this;
  72. wx.makePhoneCall({
  73. phoneNumber: e.target.dataset.merchantlinkphone
  74. });
  75. },
  76. // 点击查看更多
  77. more: function () {
  78. let that = this;
  79. if (that.data.more == '点击查看更多') {
  80. this.setData({
  81. hidden: "",
  82. height: that.data.data.merchantVoList.length * 140 + 'rpx',
  83. more: "点击收起",
  84. showMore: true
  85. })
  86. }
  87. else {
  88. this.setData({
  89. hidden: "hidden",
  90. height: 4 * 140 + 'rpx',
  91. more: "点击查看更多",
  92. showMore: true
  93. })
  94. }
  95. },
  96. //获取当前登录用户信息
  97. getUserInfo: function () {
  98. let that = this;
  99. // 获取用户信息
  100. Http.get({
  101. url: config.api.getScore,
  102. data: {}
  103. })
  104. .then(res => {
  105. console.log(res)
  106. that.getQueryCardStatus()
  107. if (that.data.cardData.cuserId != res.data.id) {
  108. console.log(2222, 6666)
  109. that.setData({
  110. isSamePeople: false
  111. })
  112. }
  113. })
  114. },
  115. //获取卡转赠状态
  116. getQueryCardStatus() {
  117. let that = this;
  118. let param = {
  119. id: this.data.cardData.couponOrderId,
  120. cUserId: this.data.cardData.cuserId,
  121. updateDate: util.formatTime(Number(that.data.cardData.updateDate), "yyyy-MM-dd hh:mm:ss")
  122. }
  123. Http.get({
  124. url: config.api.queryCardStatus,
  125. data: param
  126. })
  127. .then(res => {
  128. console.log(res, 333333333333)
  129. })
  130. .catch(err => {
  131. that.setData({
  132. isReceived: true,
  133. statusText: err.message
  134. })
  135. })
  136. },
  137. receiveCard() {
  138. let that = this;
  139. let param = {
  140. id: this.data.cardData.couponOrderId,
  141. cUserId: this.data.cardData.cuserId,
  142. updateDate: util.formatTime(Number(this.data.cardData.updateDate), "yyyy-MM-dd hh:mm:ss")
  143. }
  144. this.setData({
  145. receivedDisabled: true
  146. })
  147. Http.post({
  148. url: config.api.cardAccept,
  149. data: param
  150. })
  151. .then(res => {
  152. wx.showToast({
  153. title: '领取成功!',
  154. icon: 'none',
  155. duration: 2000,
  156. mask: false
  157. });
  158. wx.reLaunch({
  159. url: '/pages/complete/index?couponOrderId=' + this.data.cardData.couponOrderId,
  160. })
  161. })
  162. .catch(err => {
  163. wx.showToast({
  164. title: err.message,
  165. icon: 'none',
  166. duration: 2000,
  167. mask: false
  168. });
  169. that.setData({
  170. receivedDisabled: false
  171. })
  172. })
  173. },
  174. closeAlert() {
  175. this.setData({
  176. showCardOffer: false
  177. })
  178. },
  179. goback: function () {
  180. wx.switchTab({
  181. url: '/pages/main/index',
  182. })
  183. },
  184. /**
  185. * 点击提交问题单选
  186. */
  187. formSubmit: function (e) {
  188. console.log(e);
  189. let that = this;
  190. /**
  191. * 多选
  192. */
  193. if (e.currentTarget.dataset.flags == 'multi') {
  194. if (that.data.anwserId.length == 0) {
  195. var answserIs = null
  196. } else {
  197. var answserIs = that.data.anwserId.join(",");
  198. }
  199. }
  200. else if (e.currentTarget.dataset.flags == 'single') {
  201. var answserIs = e.detail.value;
  202. }
  203. console.log(answserIs)
  204. console.log(e.currentTarget.dataset.questionid)
  205. Http.post({
  206. url: config.api.answerQuestion,
  207. data: {
  208. answer: answserIs,
  209. questionId: e.currentTarget.dataset.questionid
  210. }
  211. })
  212. .then(res => {
  213. that.closeQuestion();
  214. })
  215. .catch(err => {
  216. wx.showToast({
  217. title: err.message,
  218. icon: 'none',
  219. duration: 2000,
  220. mask: false
  221. });
  222. })
  223. },
  224. /**
  225. * 多选
  226. */
  227. checkboxChange: function (e) {
  228. console.log(e)
  229. this.setData({
  230. anwserId: e.detail.value
  231. })
  232. },
  233. closeQuestion: function () {
  234. let that = this;
  235. that.setData({
  236. display: "none",
  237. zIndex: 11,
  238. opacity: 0
  239. })
  240. setTimeout(function () {
  241. that.orderFunc();
  242. }, 500)
  243. },
  244. /**
  245. * gotopay
  246. */
  247. gotopay: function (e) {
  248. let that = this;
  249. var discount = e.currentTarget.dataset.discount;
  250. that.setData({
  251. queueData: null,
  252. showbutton: true
  253. })
  254. Http.get({
  255. url: config.api.getQuestion,
  256. data: {
  257. couponType: JSON.stringify(that.data.data.type)
  258. }
  259. })
  260. .then(res => {
  261. console.log(res);
  262. if (res.data == undefined) {
  263. that.orderFunc(discount);
  264. that.setData({
  265. flag: false
  266. })
  267. } else if (res.data) {
  268. var animation = wx.createAnimation({});
  269. animation.translate((that.data.widthScreen - that.data.widthScreen), 0).scale(1).opacity(1).step({
  270. duration: 500
  271. })
  272. that.setData({
  273. queueData: animation.export(),
  274. zIndex: 9,
  275. opacity: 1,
  276. display: "block",
  277. questionnaire: JSON.parse(res.data.content),
  278. questionId: res.data.id
  279. });
  280. }
  281. })
  282. .catch(err => {
  283. wx.showToast({
  284. title: err.message,
  285. icon: 'none',
  286. duration: 2000,
  287. mask: false
  288. });
  289. })
  290. },
  291. setIntervalTime(end_time) {
  292. let that = this;
  293. var EndTime = end_time;
  294. var NowTime = new Date().getTime();
  295. var total_micro_second = EndTime - NowTime || [];
  296. // 渲染倒计时时钟
  297. let obj = that.dateformat(total_micro_second);
  298. console.log(obj)
  299. if (total_micro_second > 0) {
  300. that.setData({
  301. clock: obj,
  302. day: obj.a1,
  303. hour: obj.b1,
  304. min: obj.c1,
  305. sec: obj.d1,
  306. })
  307. } else {
  308. that.setData({
  309. clock: "00",
  310. day: "00",
  311. hour: "00",
  312. min: "00",
  313. sec: "00",
  314. })
  315. }
  316. total_micro_second -= 1000;
  317. console.log(obj);
  318. },
  319. countdown: function (end_time) {
  320. let that = this;
  321. that.setIntervalTime(end_time);
  322. that.data.setInterval = setInterval(function () {
  323. that.setIntervalTime(end_time);
  324. }, 1000)
  325. },
  326. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  327. dateformat(micro_second) {
  328. // 总秒数
  329. var second = Math.floor(micro_second / 1000);
  330. // 天数
  331. var day = Math.floor(second / 3600 / 24) < 10 ? "0" + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
  332. // 小时
  333. var hr = Math.floor(second / 3600 % 24) < 10 ? "0" + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
  334. // 分钟
  335. var min = Math.floor(second / 60 % 60) < 10 ? "0" + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
  336. // 秒
  337. var sec = Math.floor(second % 60) < 10 ? "0" + Math.floor(second % 60) : Math.floor(second % 60);
  338. // return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  339. return {
  340. a1: day,
  341. b1: hr,
  342. c1: min,
  343. d1: sec
  344. }
  345. },
  346. /**
  347. * 获取用户信息
  348. */
  349. onLoad(options) {
  350. let that = this;
  351. wx.showLoading({
  352. title: "加载中..."
  353. });
  354. /**
  355. * 转赠判断
  356. */
  357. if (options.cuserId) {
  358. this.setData({
  359. showCardOffer: true,
  360. cardData: options
  361. })
  362. this.getUserInfo()
  363. }
  364. that.setData({
  365. couponChannelId: options.couponChannelId,
  366. title: that.data.data.title ? that.data.data.title : '',
  367. });
  368. if (options.cardType) {
  369. that.setData({
  370. cardType: options.cardType
  371. })
  372. }
  373. var parmer = {
  374. url: config.api.couponDetail,
  375. data: {
  376. couponChannelId: options.couponChannelId
  377. }
  378. };
  379. Http.get(parmer)
  380. .then(res => {
  381. that.setData({
  382. couponId: res.data.couponId
  383. })
  384. if (res.code == 200) {
  385. that.setData({
  386. showPage: true
  387. })
  388. }
  389. if (res.data.type == 100) {
  390. wx.setNavigationBarTitle({
  391. title: "卡详情"
  392. })
  393. }
  394. var EndTime = res.data.validStartDate;
  395. var NowTime = new Date().getTime();
  396. if (EndTime - NowTime > 0) {
  397. that.setData({
  398. showbutton1: true
  399. })
  400. }
  401. // that.countdown02(res.data.validStartDate)
  402. if (res.data.endTime) {
  403. that.countdown(res.data.endTime);
  404. //当前时间与优惠券下架时间做计算
  405. var endTime = util.formatTime(res.data.endTime, "yyyy-MM-dd hh:mm:ss");
  406. if (util.timechuo(endTime).indexOf('-') == 0) {
  407. that.setData({
  408. endtime: "活动已结束",
  409. });
  410. } else {
  411. that.setData({
  412. endtime: util.timechuo(endTime)
  413. });
  414. }
  415. }
  416. wx.hideLoading();
  417. that.setData({
  418. data: res.data
  419. });
  420. if (that.data.data.merchantVoList && that.data.data.merchantVoList.length <= 4) {
  421. that.setData({
  422. height: that.data.data.merchantVoList.length * 140 + 'rpx',
  423. showMore: false
  424. })
  425. } else if (that.data.data.merchantVoList && that.data.data.merchantVoList.length > 4) {
  426. that.setData({
  427. height: 4 * 140 + 'rpx'
  428. })
  429. }
  430. console.log(that.data.height)
  431. if (res.data.validType == 1) {
  432. that.setData({
  433. validStartDate: util.formatTime(res.data.validStartDate, "yyyy-MM-dd"),
  434. validEndDate: util.formatTime(res.data.validEndDate, "yyyy-MM-dd"),
  435. });
  436. } else {
  437. if (res.data.validDays) {
  438. that.setData({
  439. validDays: res.data.validDays
  440. });
  441. }
  442. }
  443. }).catch(err => {
  444. wx.showToast({
  445. title: err.message,
  446. icon: 'none',
  447. duration: 2000,
  448. mask: false
  449. });
  450. })
  451. },
  452. /**
  453. * 邀请好友继续砍价
  454. */
  455. inviteFriend: function () {
  456. let that = this;
  457. that.orderFunc("discount");
  458. that.setData({
  459. showbutton1: true
  460. })
  461. },
  462. /**
  463. * 支付订单更新
  464. */
  465. payOrderUpdate: (orderId, payOrderId, status, reason, type, _this) => {
  466. let that = this;
  467. // 支付成功
  468. Http.post({
  469. url: config.api.payOrderUpdate,
  470. data: {
  471. payOrderId: payOrderId,
  472. orderId: orderId,
  473. status: status,
  474. reason: reason
  475. }
  476. })
  477. .then(res => {
  478. wx.hideLoading()
  479. // 有价券
  480. if (!type && type != 'free') {
  481. wx.navigateTo({
  482. url: `/pages/order/detail/index?orderId=${
  483. orderId
  484. }&cardIf=true`
  485. });
  486. } else if (type == 'free') {
  487. wx.navigateTo({
  488. url: `/pages/order/detail/index?orderId=${
  489. orderId
  490. }`
  491. });
  492. }
  493. })
  494. .catch(err => {
  495. console.log(err);
  496. if (!type) {
  497. setTimeout(function () {
  498. _this.payOrderUpdate(orderId, payOrderId, status, reason, type, _this);
  499. }, 2000)
  500. }
  501. })
  502. },
  503. checkPhoneStatus: function () {
  504. let that = this;
  505. Http.post({
  506. url: config.api.checkPhoneStatus,
  507. data: {}
  508. })
  509. .then(res => {
  510. console.log(666, '授权成功!')
  511. that.receiveCard()
  512. })
  513. .catch(err => {
  514. if (err.code == 11005) {
  515. // 用户手机未授权
  516. /**
  517. * 将值传到用户手机号授权的页面
  518. *
  519. */
  520. wx.redirectTo({
  521. url: "/pages/getphoneInfo/index?path=index&cuserId=" + that.data.cardData.cuserId + "&couponChannelId=" + that.data.cardData.couponChannelId + "&userName=" + that.data.cardData.userName + "&coverImg=" + that.data.cardData.coverImg + "&avatarUrl=" + that.data.cardData.avatarUrl + "&couponOrderId=" + that.data.cardData.couponOrderId + "&updateDate=" + that.data.cardData.updateDate,
  522. });
  523. } else if (err.code == 11006) {
  524. // 用户手机已加密
  525. wx.redirectTo({
  526. url: "/pages/phoneinput/phoneinput?path=index&cuserId=" + that.data.cardData.cuserId + "&couponChannelId=" + that.data.cardData.couponChannelId + "&userName=" + that.data.cardData.userName + "&coverImg=" + that.data.cardData.coverImg + "&avatarUrl=" + that.data.cardData.avatarUrl + "&couponOrderId=" + that.data.cardData.couponOrderId + "&updateDate=" + that.data.cardData.updateDate,
  527. });
  528. } else {
  529. wx.showToast({
  530. title: err.message,
  531. icon: 'none',
  532. duration: 2000,
  533. mask: false
  534. });
  535. }
  536. })
  537. },
  538. /**
  539. * 发起支付
  540. */
  541. orderFunc(discount) {
  542. console.log(discount)
  543. var that = this;
  544. Http.post({
  545. url: config.api.checkPhoneStatus,
  546. data: {}
  547. })
  548. .then(res => {
  549. if (discount == 'discount') {
  550. var data = {
  551. couponChannelId: "" + that.data.couponChannelId,
  552. couponId: "" + that.data.couponId,
  553. press: true
  554. }
  555. } else if (discount == 'discount1') {
  556. var data = {
  557. couponChannelId: "" + that.data.couponChannelId,
  558. couponId: "" + that.data.couponId,
  559. press: false
  560. }
  561. } else if (that.data.couponChannelId == null) {
  562. var data = {
  563. couponId: "" + that.data.couponId
  564. };
  565. } else {
  566. var data = {
  567. couponChannelId: "" + that.data.couponChannelId,
  568. couponId: "" + that.data.couponId,
  569. }
  570. }
  571. /**
  572. * orderSave 下单
  573. */
  574. return Http.post({
  575. url: config.api.orderSave,
  576. data: data
  577. });
  578. })
  579. .catch(err => {
  580. console.log(err);
  581. that.setData({
  582. showbutton: false,
  583. showbutton1: false
  584. })
  585. if (err.code == 2011) {
  586. wx.showToast({
  587. title: "商户信息没找到",
  588. image: './../../../assets/images/fail.png',
  589. duration: 2000,
  590. mask: false
  591. });
  592. } else if (err.code == 2013) {
  593. wx.showToast({
  594. title: "商户信息禁用",
  595. image: './../../../assets/images/fail.png',
  596. duration: 2000,
  597. mask: false
  598. });
  599. } else if (err.code == 3000) {
  600. wx.showToast({
  601. title: "库存不足",
  602. image: './../../../assets/images/fail.png',
  603. duration: 2000,
  604. mask: false
  605. });
  606. } else if (err.code == 3001) {
  607. wx.showToast({
  608. title: "超过限购条件",
  609. image: './../../../assets/images/fail.png',
  610. duration: 2000,
  611. mask: false
  612. });
  613. } else if (err.code == 3002) {
  614. wx.showToast({
  615. title: "订单失败",
  616. image: './../../../assets/images/fail.png',
  617. duration: 2000,
  618. mask: false
  619. });
  620. } else if (err.code == 3003) {
  621. wx.showToast({
  622. title: "订单不存在",
  623. image: './../../../assets/images/fail.png',
  624. duration: 2000,
  625. mask: false
  626. });
  627. } else if (err.code == 3004) {
  628. wx.showToast({
  629. title: "订单不存在",
  630. image: './../../../assets/images/fail.png',
  631. duration: 2000,
  632. mask: false
  633. });
  634. } else if (err.code == 4003) {
  635. wx.showToast({
  636. title: "卡券已作废",
  637. image: './../../../assets/images/fail.png',
  638. duration: 2000,
  639. mask: false
  640. });
  641. } else if (err.code == 11005) {
  642. /**
  643. * 将值传到用户手机号授权的页面
  644. *
  645. */
  646. wx.redirectTo({
  647. url: "/pages/getphoneInfo/index?couponChannelId=" +
  648. that.data.couponChannelId
  649. });
  650. } else if (err.code == 11006) {
  651. // 用户手机已加密
  652. wx.redirectTo({
  653. url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  654. that.data.couponChannelId
  655. });
  656. } else {
  657. wx.showToast({
  658. title: err.message,
  659. icon: 'none',
  660. duration: 2000,
  661. mask: false
  662. });
  663. }
  664. })
  665. .then(res => {
  666. console.log(res)
  667. if (discount != 'discount') {
  668. if (typeof (res) != "undefined") {
  669. let orderId = "" + res.data.id;
  670. that.setData({
  671. orderId: orderId
  672. });
  673. if (res.data.payment > 0) {
  674. // 支付金额不为0
  675. /**
  676. * 支付订单创建
  677. */
  678. Http.post({
  679. url: config.api.payOrderCreate,
  680. data: {
  681. orderId: orderId
  682. }
  683. })
  684. .then(res => {
  685. var payOrderId = "" + res.data.payOrderId;
  686. wx.hideLoading();
  687. wx.requestPayment({
  688. timeStamp: res.data.timeStamp,
  689. nonceStr: res.data.nonceStr,
  690. package: res.data.package,
  691. signType: (res.data.signType) ? res.data.signType : "MD5",
  692. paySign: res.data.paySign,
  693. success: res => {
  694. wx.showLoading({
  695. title: '订单正在处理中...',
  696. })
  697. setTimeout(function () {
  698. wx.hideLoading()
  699. }, 5000)
  700. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', '', that);
  701. if (res.errMsg == "requestPayment:ok") {
  702. setTimeout(function () {
  703. wx.hideLoading();
  704. }, 2000);
  705. /**
  706. * 用户支付成功以后跳转到券包列表
  707. */
  708. if (that.data.cardType == 100) {
  709. wx.setStorage({
  710. key: 'couponNum2',
  711. data: "couponNum2"
  712. })
  713. } else if (that.data.data.type != 5) {
  714. wx.setStorage({
  715. key: 'couponNum',
  716. data: "couponNum"
  717. })
  718. }
  719. }
  720. },
  721. fail: res => {
  722. /**
  723. * 支付失败,需要更新订单的状态
  724. */
  725. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', 'fail', that);
  726. that.setData({
  727. showbutton: false
  728. })
  729. return;
  730. },
  731. complete: res => { }
  732. });
  733. /// End payment --------
  734. })
  735. .catch(err => {
  736. wx.showToast({
  737. title: err.message,
  738. icon: 'none',
  739. duration: 2000,
  740. mask: false
  741. });
  742. })
  743. } else {
  744. console.log(that.data.type)
  745. // 免费券
  746. that.payOrderUpdate(orderId, "0", 1, '', 'free');
  747. if (that.data.cardType == 100) {
  748. wx.setStorage({
  749. key: 'couponNum2',
  750. data: "couponNum2"
  751. })
  752. } else if (that.data.data.type != "5") {
  753. wx.setStorage({
  754. key: 'couponNum',
  755. data: "couponNum"
  756. })
  757. }
  758. }
  759. }
  760. } else {
  761. if (res) {
  762. wx.navigateTo({
  763. url: `/pages/bargain/bargainDatail/bargainDatail?orderId=${res.data.id}`,
  764. })
  765. }
  766. }
  767. })
  768. },
  769. onShow() {
  770. this.setData({
  771. showbutton: false
  772. })
  773. console.log(this.data.showMore)
  774. },
  775. onShareAppMessage: function (options) {
  776. var that = this;
  777. var shareObj = {
  778. title: that.data.data.title,
  779. path: `/pages/index/index?couponChannelId=${that.data.couponChannelId}`,
  780. success: function (res) {
  781. if (res.errMsg == 'shareAppMessage:ok') { }
  782. },
  783. fail: function (error) {
  784. if (res.errMsg == 'shareAppMessage:fail cancel') { } else if (res.errMsg == 'shareAppMessage:fail') { }
  785. }
  786. };
  787. // 来自页面内的按钮的转发
  788. if (options.from == 'button') {
  789. var eData = options.target.dataset.id;
  790. shareObj.path = `/pages/index/index?couponChannelId=${eData}`;
  791. }
  792. // 返回shareObj
  793. return shareObj;
  794. },
  795. onUnload: function () {
  796. let that = this;
  797. console.log("onUnload")
  798. clearInterval(that.data.setInterval);
  799. },
  800. onHide: function () {
  801. let that = this;
  802. console.log("onHide");
  803. clearInterval(that.data.setInterval);
  804. }
  805. });