C端小程序
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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