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.

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