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.

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