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.

823 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. console.log(obj,111)
  324. if (total_micro_second > 0) {
  325. that.setData({
  326. clock02: obj,
  327. day02: obj.a1,
  328. hour02: obj.b1,
  329. min02: obj.c1,
  330. sec02: obj.d1,
  331. })
  332. } else {
  333. that.setData({
  334. clock02: "00",
  335. day02: "00",
  336. hour02: "00",
  337. min02: "00",
  338. sec02: "00",
  339. showbutton1: false
  340. })
  341. }
  342. setTimeout(function () {
  343. total_micro_second -= 1000;
  344. that.countdown02(end_time);
  345. }, 1000)
  346. },
  347. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  348. dateformat(micro_second) {
  349. // 总秒数
  350. var second = Math.floor(micro_second / 1000);
  351. // 天数
  352. var day = Math.floor(second / 3600 / 24) < 10 ? "0" + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
  353. // 小时
  354. var hr = Math.floor(second / 3600 % 24) < 10 ? "0" + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
  355. // 分钟
  356. var min = Math.floor(second / 60 % 60) < 10 ? "0" + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
  357. // 秒
  358. var sec = Math.floor(second % 60) < 10 ? "0" + Math.floor(second % 60) : Math.floor(second % 60);
  359. // return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  360. return {
  361. a1: day,
  362. b1: hr,
  363. c1: min,
  364. d1: sec
  365. }
  366. },
  367. /**
  368. * 获取用户信息
  369. */
  370. onLoad(options) {
  371. let that = this;
  372. wx.showLoading({
  373. title: "加载中..."
  374. });
  375. /**
  376. * 转赠判断
  377. */
  378. if (options.cuserId){
  379. this.setData({
  380. showCardOffer:true,
  381. cardData:options
  382. })
  383. this.getUserInfo()
  384. }
  385. that.setData({
  386. couponChannelId: options.couponChannelId,
  387. title: that.data.data.title ? that.data.data.title : '',
  388. });
  389. if (options.cardType) {
  390. that.setData({
  391. cardType: options.cardType
  392. })
  393. }
  394. var parmer = {
  395. url: config.api.couponDetail,
  396. data: {
  397. couponChannelId: options.couponChannelId
  398. }
  399. };
  400. Http.get(parmer)
  401. .then(res => {
  402. that.setData({
  403. couponId: res.data.couponId
  404. })
  405. if (res.code == 200) {
  406. that.setData({
  407. showPage: true
  408. })
  409. }
  410. if (res.data.type == 100) {
  411. wx.setNavigationBarTitle({
  412. title: "卡详情"
  413. })
  414. }
  415. var EndTime = res.data.validStartDate;
  416. var NowTime = new Date().getTime();
  417. if (EndTime - NowTime>0){
  418. that.setData({
  419. showbutton1:true
  420. })
  421. }
  422. that.countdown02(res.data.validStartDate)
  423. if (res.data.endTime) {
  424. that.countdown(res.data.endTime);
  425. //当前时间与优惠券下架时间做计算
  426. var endTime = util.formatTime(res.data.endTime, "yyyy-MM-dd hh:mm:ss");
  427. if (util.timechuo(endTime).indexOf('-') == 0) {
  428. that.setData({
  429. endtime: "活动已结束",
  430. });
  431. } else {
  432. that.setData({
  433. endtime: util.timechuo(endTime)
  434. });
  435. }
  436. }
  437. wx.hideLoading();
  438. that.setData({
  439. data: res.data
  440. });
  441. if (that.data.data.merchantVoList && that.data.data.merchantVoList.length <= 4) {
  442. that.setData({
  443. height: that.data.data.merchantVoList.length * 140 + 'rpx',
  444. showMore: false
  445. })
  446. } else if (that.data.data.merchantVoList && that.data.data.merchantVoList.length > 4) {
  447. that.setData({
  448. height: 4 * 140 + 'rpx'
  449. })
  450. }
  451. console.log(that.data.height)
  452. if (res.data.validType == 1) {
  453. that.setData({
  454. validStartDate: util.formatTime(res.data.validStartDate, "yyyy-MM-dd"),
  455. validEndDate: util.formatTime(res.data.validEndDate, "yyyy-MM-dd"),
  456. });
  457. } else {
  458. if (res.data.validDays) {
  459. that.setData({
  460. validDays: res.data.validDays
  461. });
  462. }
  463. }
  464. }).catch(err => {
  465. wx.showToast({
  466. title: err.message,
  467. icon: 'none',
  468. duration: 2000,
  469. mask: false
  470. });
  471. })
  472. },
  473. /**
  474. * 邀请好友继续砍价
  475. */
  476. inviteFriend: function () {
  477. let that = this;
  478. that.orderFunc("discount");
  479. that.setData({
  480. showbutton1:true
  481. })
  482. },
  483. /**
  484. * 支付订单更新
  485. */
  486. payOrderUpdate: (orderId, payOrderId, status, reason, type, _this) => {
  487. let that = this;
  488. // 支付成功
  489. Http.post({
  490. url: config.api.payOrderUpdate,
  491. data: {
  492. payOrderId: payOrderId,
  493. orderId: orderId,
  494. status: status,
  495. reason: reason
  496. }
  497. })
  498. .then(res => {
  499. wx.hideLoading()
  500. // 有价券
  501. if (!type && type != 'free') {
  502. wx.navigateTo({
  503. url: `/pages/order/detail/index?orderId=${
  504. orderId
  505. }&cardIf=true`
  506. });
  507. } else if (type == 'free') {
  508. wx.navigateTo({
  509. url: `/pages/order/detail/index?orderId=${
  510. orderId
  511. }`
  512. });
  513. }
  514. })
  515. .catch(err => {
  516. console.log(err);
  517. if (!type) {
  518. setTimeout(function () {
  519. _this.payOrderUpdate(orderId, payOrderId, status, reason, type, _this);
  520. }, 2000)
  521. }
  522. })
  523. },
  524. /**
  525. *
  526. */
  527. checkPhoneStatus: function () {
  528. let that = this;
  529. Http.post({
  530. url: config.api.checkPhoneStatus,
  531. data: {}
  532. })
  533. .then(res => {
  534. console.log(666,'授权成功!')
  535. that.receiveCard()
  536. })
  537. .catch(err => {
  538. if (err.code == 11005) {
  539. // 用户手机未授权
  540. /**
  541. * 将值传到用户手机号授权的页面
  542. *
  543. */
  544. wx.redirectTo({
  545. 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,
  546. });
  547. } else if (err.code == 11006) {
  548. // 用户手机已加密
  549. wx.redirectTo({
  550. 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,
  551. });
  552. } else {
  553. wx.showToast({
  554. title: err.message,
  555. icon: 'none',
  556. duration: 2000,
  557. mask: false
  558. });
  559. }
  560. })
  561. },
  562. /**
  563. * 发起支付
  564. */
  565. orderFunc(discount) {
  566. console.log(discount)
  567. var that = this;
  568. Http.post({
  569. url: config.api.checkPhoneStatus,
  570. data: {}
  571. })
  572. .then(res => {
  573. if (discount == 'discount') {
  574. var data = {
  575. couponChannelId: "" + that.data.couponChannelId,
  576. couponId: "" + that.data.couponId,
  577. press: true
  578. }
  579. } else if (discount == 'discount1'){
  580. var data = {
  581. couponChannelId: "" + that.data.couponChannelId,
  582. couponId: "" + that.data.couponId,
  583. press: false
  584. }
  585. }else if (that.data.couponChannelId == null) {
  586. var data = {
  587. couponId: "" + that.data.couponId
  588. };
  589. } else {
  590. var data = {
  591. couponChannelId: "" + that.data.couponChannelId,
  592. couponId: "" + that.data.couponId,
  593. }
  594. }
  595. /**
  596. * orderSave 下单
  597. */
  598. return Http.post({
  599. url: config.api.orderSave,
  600. data: data
  601. });
  602. })
  603. .catch(err => {
  604. console.log(err);
  605. that.setData({
  606. showbutton: false,
  607. showbutton1:false
  608. })
  609. if (err.code == 2011) {
  610. wx.showToast({
  611. title: "商户信息没找到",
  612. image: './../../../assets/images/fail.png',
  613. duration: 2000,
  614. mask: false
  615. });
  616. } else if (err.code == 2013) {
  617. wx.showToast({
  618. title: "商户信息禁用",
  619. image: './../../../assets/images/fail.png',
  620. duration: 2000,
  621. mask: false
  622. });
  623. } else if (err.code == 3000) {
  624. wx.showToast({
  625. title: "库存不足",
  626. image: './../../../assets/images/fail.png',
  627. duration: 2000,
  628. mask: false
  629. });
  630. } else if (err.code == 3001) {
  631. wx.showToast({
  632. title: "超过限购条件",
  633. image: './../../../assets/images/fail.png',
  634. duration: 2000,
  635. mask: false
  636. });
  637. } else if (err.code == 3002) {
  638. wx.showToast({
  639. title: "订单失败",
  640. image: './../../../assets/images/fail.png',
  641. duration: 2000,
  642. mask: false
  643. });
  644. } else if (err.code == 3003) {
  645. wx.showToast({
  646. title: "订单不存在",
  647. image: './../../../assets/images/fail.png',
  648. duration: 2000,
  649. mask: false
  650. });
  651. } else if (err.code == 3004) {
  652. wx.showToast({
  653. title: "订单不存在",
  654. image: './../../../assets/images/fail.png',
  655. duration: 2000,
  656. mask: false
  657. });
  658. } else if (err.code == 4003) {
  659. wx.showToast({
  660. title: "卡券已作废",
  661. image: './../../../assets/images/fail.png',
  662. duration: 2000,
  663. mask: false
  664. });
  665. } else if (err.code == 11005) {
  666. /**
  667. * 将值传到用户手机号授权的页面
  668. *
  669. */
  670. wx.redirectTo({
  671. url: "/pages/getphoneInfo/index?couponChannelId=" +
  672. that.data.couponChannelId
  673. });
  674. } else if (err.code == 11006) {
  675. // 用户手机已加密
  676. wx.redirectTo({
  677. url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  678. that.data.couponChannelId
  679. });
  680. } else {
  681. wx.showToast({
  682. title: err.message,
  683. icon: 'none',
  684. duration: 2000,
  685. mask: false
  686. });
  687. }
  688. })
  689. .then(res => {
  690. console.log(res)
  691. if (discount != 'discount') {
  692. if (typeof (res) != "undefined") {
  693. let orderId = "" + res.data.id;
  694. that.setData({
  695. orderId: orderId
  696. });
  697. if (res.data.payment > 0) {
  698. // 支付金额不为0
  699. /**
  700. * 支付订单创建
  701. */
  702. Http.post({
  703. url: config.api.payOrderCreate,
  704. data: {
  705. orderId: orderId
  706. }
  707. })
  708. .then(res => {
  709. var payOrderId = "" + res.data.payOrderId;
  710. wx.hideLoading();
  711. wx.requestPayment({
  712. timeStamp: res.data.timeStamp,
  713. nonceStr: res.data.nonceStr,
  714. package: res.data.package,
  715. signType: (res.data.signType) ? res.data.signType : "MD5",
  716. paySign: res.data.paySign,
  717. success: res => {
  718. wx.showLoading({
  719. title: '订单正在处理中...',
  720. })
  721. setTimeout(function () {
  722. wx.hideLoading()
  723. }, 5000)
  724. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', '', that);
  725. if (res.errMsg == "requestPayment:ok") {
  726. setTimeout(function () {
  727. wx.hideLoading();
  728. }, 2000);
  729. /**
  730. * 用户支付成功以后跳转到券包列表
  731. */
  732. if (that.data.cardType == 100) {
  733. wx.setStorage({
  734. key: 'couponNum2',
  735. data: "couponNum2"
  736. })
  737. } else if(that.data.data.type!=5){
  738. wx.setStorage({
  739. key: 'couponNum',
  740. data: "couponNum"
  741. })
  742. }
  743. }
  744. },
  745. fail: res => {
  746. /**
  747. * 支付失败,需要更新订单的状态
  748. */
  749. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', 'fail', that);
  750. that.setData({
  751. showbutton: false
  752. })
  753. return;
  754. },
  755. complete: res => {}
  756. });
  757. /// End payment --------
  758. })
  759. .catch(err => {
  760. wx.showToast({
  761. title: err.message,
  762. icon: 'none',
  763. duration: 2000,
  764. mask: false
  765. });
  766. })
  767. } else {
  768. console.log(that.data.type)
  769. // 免费券
  770. that.payOrderUpdate(orderId, "0", 1, '', 'free');
  771. if (that.data.cardType == 100) {
  772. wx.setStorage({
  773. key: 'couponNum2',
  774. data: "couponNum2"
  775. })
  776. } else if (that.data.data.type != "5"){
  777. wx.setStorage({
  778. key: 'couponNum',
  779. data: "couponNum"
  780. })
  781. }
  782. }
  783. }
  784. } else {
  785. if(res){
  786. wx.navigateTo({
  787. url: `/pages/bargain/bargainDatail/bargainDatail?orderId=${res.data.id}`,
  788. })
  789. }
  790. }
  791. })
  792. },
  793. onShow() {
  794. this.setData({
  795. showbutton: false
  796. })
  797. console.log(this.data.showMore)
  798. },
  799. onShareAppMessage: function (options) {
  800. var that = this;
  801. var shareObj = {
  802. title: that.data.data.title,
  803. path: `/pages/index/index?couponChannelId=${that.data.couponChannelId}`,
  804. success: function (res) {
  805. if (res.errMsg == 'shareAppMessage:ok') {}
  806. },
  807. fail: function (error) {
  808. if (res.errMsg == 'shareAppMessage:fail cancel') {} else if (res.errMsg == 'shareAppMessage:fail') {}
  809. }
  810. };
  811. // 来自页面内的按钮的转发
  812. if (options.from == 'button') {
  813. var eData = options.target.dataset.id;
  814. shareObj.path = `/pages/index/index?couponChannelId=${eData}`;
  815. }
  816. // 返回shareObj
  817. return shareObj;
  818. },
  819. });