C端小程序
Não pode escolher mais do que 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.

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