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.

821 líneas
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. 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. let param={
  87. id: this.data.cardData.couponOrderId,
  88. couponId: this.data.cardData.couponChannelId,
  89. cUserId: this.data.cardData.cuserId
  90. }
  91. Http.get({
  92. url: config.api.queryCardStatus,
  93. data: param
  94. })
  95. .then(res => {
  96. console.log(res,333333333333)
  97. })
  98. .catch(err => {
  99. that.setData({
  100. isReceived: true,
  101. statusText: err.message
  102. })
  103. })
  104. },
  105. receiveCard(){
  106. let that=this;
  107. let param = {
  108. id: this.data.cardData.couponOrderId,
  109. cUserId: this.data.cardData.cuserId
  110. }
  111. this.setData({
  112. receivedDisabled:true
  113. })
  114. Http.post({
  115. url: config.api.cardAccept,
  116. data: param
  117. })
  118. .then(res => {
  119. console.log(res, 333333333333)
  120. wx.showToast({
  121. title: '领取成功!',
  122. icon: 'none',
  123. duration: 2000,
  124. mask: false
  125. });
  126. wx.navigateTo({
  127. url: '/pages/complete/index?couponOrderId=' + this.data.cardData.couponOrderId,
  128. })
  129. })
  130. .catch(err => {
  131. wx.showToast({
  132. title: err.message,
  133. icon: 'none',
  134. duration: 2000,
  135. mask: false
  136. });
  137. that.setData({
  138. receivedDisabled: false
  139. })
  140. })
  141. },
  142. closeAlert(){
  143. this.setData({
  144. showCardOffer:false
  145. })
  146. },
  147. goback: function () {
  148. wx.switchTab({
  149. url: '/pages/login/index',
  150. })
  151. },
  152. /**
  153. * 点击提交问题单选
  154. */
  155. formSubmit: function (e) {
  156. console.log(e);
  157. let that = this;
  158. /**
  159. * 多选
  160. */
  161. if (e.currentTarget.dataset.flags == 'multi') {
  162. if (that.data.anwserId.length == 0) {
  163. var answserIs = null
  164. } else {
  165. var answserIs = that.data.anwserId.join(",");
  166. }
  167. }
  168. else if (e.currentTarget.dataset.flags == 'single') {
  169. var answserIs = e.detail.value;
  170. }
  171. console.log(answserIs)
  172. console.log(e.currentTarget.dataset.questionid)
  173. Http.post({
  174. url: config.api.answerQuestion,
  175. data: {
  176. answer: answserIs,
  177. questionId: e.currentTarget.dataset.questionid
  178. }
  179. })
  180. .then(res => {
  181. that.closeQuestion();
  182. })
  183. .catch(err => {
  184. wx.showToast({
  185. title: err.message,
  186. icon: 'none',
  187. duration: 2000,
  188. mask: false
  189. });
  190. })
  191. },
  192. /**
  193. * 多选
  194. */
  195. checkboxChange: function (e) {
  196. console.log(e)
  197. this.setData({
  198. anwserId: e.detail.value
  199. })
  200. },
  201. closeQuestion: function () {
  202. let that = this;
  203. that.setData({
  204. display: "none",
  205. zIndex: 11,
  206. opacity: 0
  207. })
  208. setTimeout(function () {
  209. that.orderFunc();
  210. }, 500)
  211. },
  212. /**
  213. * gotopay
  214. */
  215. gotopay: function (e) {
  216. let that = this;
  217. var discount = e.currentTarget.dataset.discount;
  218. that.setData({
  219. queueData: null,
  220. showbutton: true
  221. })
  222. Http.get({
  223. url: config.api.getQuestion,
  224. data: {
  225. couponType: JSON.stringify(that.data.data.type)
  226. }
  227. })
  228. .then(res => {
  229. console.log(res);
  230. if (res.data == undefined) {
  231. that.orderFunc(discount);
  232. that.setData({
  233. flag: false
  234. })
  235. } else if (res.data) {
  236. var animation = wx.createAnimation({});
  237. animation.translate((that.data.widthScreen - that.data.widthScreen), 0).scale(1).opacity(1).step({
  238. duration: 500
  239. })
  240. that.setData({
  241. queueData: animation.export(),
  242. zIndex: 9,
  243. opacity: 1,
  244. display: "block",
  245. questionnaire: JSON.parse(res.data.content),
  246. questionId: res.data.id
  247. });
  248. }
  249. })
  250. .catch(err => {
  251. wx.showToast({
  252. title: err.message,
  253. icon: 'none',
  254. duration: 2000,
  255. mask: false
  256. });
  257. })
  258. },
  259. countdown(end_time) {
  260. let that = this;
  261. var EndTime = end_time;
  262. var NowTime = new Date().getTime();
  263. var total_micro_second = EndTime - NowTime || [];
  264. // 渲染倒计时时钟
  265. let obj = that.dateformat(total_micro_second);
  266. if (total_micro_second > 0) {
  267. that.setData({
  268. clock: obj,
  269. day: obj.a1,
  270. hour: obj.b1,
  271. min: obj.c1,
  272. sec: obj.d1,
  273. })
  274. } else {
  275. that.setData({
  276. clock: "00",
  277. day: "00",
  278. hour: "00",
  279. min: "00",
  280. sec: "00",
  281. })
  282. }
  283. setTimeout(function () {
  284. total_micro_second -= 1000;
  285. that.countdown(end_time);
  286. }, 1000)
  287. },
  288. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  289. dateformat(micro_second) {
  290. // 总秒数
  291. var second = Math.floor(micro_second / 1000);
  292. // 天数
  293. var day = Math.floor(second / 3600 / 24) < 10 ? "0" + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
  294. // 小时
  295. var hr = Math.floor(second / 3600 % 24) < 10 ? "0" + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
  296. // 分钟
  297. var min = Math.floor(second / 60 % 60) < 10 ? "0" + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
  298. // 秒
  299. var sec = Math.floor(second % 60) < 10 ? "0" + Math.floor(second % 60) : Math.floor(second % 60);
  300. // return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  301. return {
  302. a1: day,
  303. b1: hr,
  304. c1: min,
  305. d1: sec
  306. }
  307. },
  308. /**
  309. * 获取用户信息
  310. */
  311. onLoad(options) {
  312. let that = this;
  313. wx.showLoading({
  314. title: "加载中..."
  315. });
  316. /**
  317. * 转赠判断
  318. */
  319. if (options.cuserId){
  320. this.setData({
  321. showCardOffer:true,
  322. cardData:options
  323. })
  324. console.log(options,22222222222)
  325. this.getUserInfo()
  326. }
  327. that.setData({
  328. couponChannelId: options.couponChannelId,
  329. couponId: options.couponId,
  330. title: that.data.data.title ? that.data.data.title : '',
  331. });
  332. if (options.cardType) {
  333. that.setData({
  334. cardType: options.cardType
  335. })
  336. }
  337. var parmer = {
  338. url: config.api.couponDetail,
  339. data: {
  340. couponChannelId: options.couponChannelId
  341. }
  342. };
  343. Http.get(parmer)
  344. .then(res => {
  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. }`
  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 + "&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,
  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 + "&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,
  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. "&couponId=" +
  595. that.data.couponId
  596. });
  597. } else if (err.code == 11006) {
  598. // 用户手机已加密
  599. wx.redirectTo({
  600. url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  601. that.data.couponChannelId +
  602. "&couponId=" +
  603. that.data.couponId
  604. });
  605. } else {
  606. wx.showToast({
  607. title: err.message,
  608. icon: 'none',
  609. duration: 2000,
  610. mask: false
  611. });
  612. }
  613. })
  614. .then(res => {
  615. console.log(res)
  616. if (discount != 'discount') {
  617. if (typeof (res) != "undefined") {
  618. let orderId = "" + res.data.id;
  619. that.setData({
  620. orderId: orderId
  621. });
  622. if (res.data.payment > 0) {
  623. // 支付金额不为0
  624. /**
  625. * 支付订单创建
  626. */
  627. Http.post({
  628. url: config.api.payOrderCreate,
  629. data: {
  630. orderId: orderId
  631. }
  632. })
  633. .then(res => {
  634. var payOrderId = "" + res.data.payOrderId;
  635. wx.hideLoading();
  636. wx.requestPayment({
  637. timeStamp: res.data.timeStamp,
  638. nonceStr: res.data.nonceStr,
  639. package: res.data.package,
  640. signType: (res.data.signType) ? res.data.signType : "MD5",
  641. paySign: res.data.paySign,
  642. success: res => {
  643. wx.showLoading({
  644. title: '订单正在处理中...',
  645. })
  646. setTimeout(function () {
  647. wx.hideLoading()
  648. }, 5000)
  649. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', '', that);
  650. if (res.errMsg == "requestPayment:ok") {
  651. setTimeout(function () {
  652. wx.hideLoading();
  653. }, 2000);
  654. /**
  655. * 用户支付成功以后跳转到券包列表
  656. */
  657. if (that.data.cardType == 100) {
  658. wx.setStorage({
  659. key: 'couponNum2',
  660. data: "couponNum2"
  661. })
  662. } else if(that.data.data.type!=5){
  663. wx.setStorage({
  664. key: 'couponNum',
  665. data: "couponNum"
  666. })
  667. }
  668. }
  669. },
  670. fail: res => {
  671. /**
  672. * 支付失败,需要更新订单的状态
  673. */
  674. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', 'fail', that);
  675. that.setData({
  676. showbutton: false
  677. })
  678. return;
  679. },
  680. complete: res => {}
  681. });
  682. /// End payment --------
  683. })
  684. .catch(err => {
  685. wx.showToast({
  686. title: err.message,
  687. icon: 'none',
  688. duration: 2000,
  689. mask: false
  690. });
  691. })
  692. } else {
  693. console.log(that.data.type)
  694. // 免费券
  695. that.payOrderUpdate(orderId, "0", 1, '', 'free');
  696. if (that.data.cardType == 100) {
  697. wx.setStorage({
  698. key: 'couponNum2',
  699. data: "couponNum2"
  700. })
  701. } else if (that.data.data.type != "5"){
  702. wx.setStorage({
  703. key: 'couponNum',
  704. data: "couponNum"
  705. })
  706. }
  707. }
  708. }
  709. } else {
  710. if(res){
  711. wx.navigateTo({
  712. url: `/pages/bargain/bargainDatail/bargainDatail?orderId=${res.data.id}`,
  713. })
  714. }
  715. }
  716. })
  717. },
  718. onShow() {
  719. this.setData({
  720. showbutton: false
  721. })
  722. },
  723. onShareAppMessage: function (options) {
  724. var that = this;
  725. var shareObj = {
  726. title: that.data.data.title,
  727. path: `/pages/index/index?couponChannelId=${that.data.couponChannelId}&couponId=${that.data.couponId}`,
  728. success: function (res) {
  729. if (res.errMsg == 'shareAppMessage:ok') {}
  730. },
  731. fail: function (error) {
  732. if (res.errMsg == 'shareAppMessage:fail cancel') {} else if (res.errMsg == 'shareAppMessage:fail') {}
  733. }
  734. };
  735. // 来自页面内的按钮的转发
  736. if (options.from == 'button') {
  737. var eData = options.target.dataset.id;
  738. var couponId = options.target.dataset.couponid;
  739. shareObj.path = `/pages/index/index?couponChannelId=${eData}&couponId=${couponId}`;
  740. }
  741. // 返回shareObj
  742. return shareObj;
  743. },
  744. /**
  745. * 用户登录
  746. */
  747. userLogin: function (couponChannelId, couponId, orderId) {
  748. var that = this;
  749. // 登录
  750. wx.login({
  751. success: ({
  752. code
  753. }) => {
  754. wx.getSystemInfo({
  755. success: function (res) {
  756. that.setData({
  757. systemInfo: JSON.stringify(res)
  758. })
  759. }
  760. })
  761. var usrdata = {
  762. appId: config.weapp.AppId,
  763. code: code,
  764. sceneAddress: app.globalData.sceneAddress,
  765. scene: that.data.scene,
  766. systemInfo: that.data.systemInfo
  767. };
  768. if (app.globalData.locationInfo) {
  769. usrdata = {
  770. appId: config.weapp.AppId,
  771. code: code,
  772. sceneAddress: app.globalData.sceneAddress,
  773. latitude: "" + app.globalData.locationInfo.latitude,
  774. longitude: "" + app.globalData.locationInfo.longitude,
  775. scene: that.data.scene,
  776. systemInfo: that.data.systemInfo
  777. };
  778. }
  779. Http.post({
  780. url: config.api.login,
  781. data: usrdata
  782. })
  783. .then(res => {
  784. that.setData({
  785. showPages: true
  786. })
  787. app.globalData.token = res.data.token;
  788. Http.setToken(res.data.token);
  789. if (couponChannelId && couponId || orderId) {
  790. that.checkuerstatus(couponChannelId, couponId, orderId);
  791. } else {
  792. that.checkuerstatus();
  793. }
  794. })
  795. .catch(err => {
  796. wx.showModal({
  797. title: '提示',
  798. showCancel: false,
  799. content: '登录失败,请重新尝试',
  800. success: function (res) {
  801. if (res.cancel) {
  802. //点击取消,默认隐藏弹框
  803. } else {
  804. //点击确定
  805. wx.reLaunch({
  806. url: '/pages/index/index',
  807. })
  808. }
  809. }
  810. })
  811. });
  812. }
  813. });
  814. },
  815. });