C端小程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

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