C端小程序
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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