C端小程序
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

768 rader
20 KiB

  1. // pages/spellGroup/mySpellGroup/index.js
  2. const navigationBarHeight = (getApp().statusBarHeight + 50) + 'px'
  3. var config = require("../../../config/config.js");
  4. const Http = require("../../../utils/HttpBasics");
  5. const imgurl = require("../../../utils/imgurl");
  6. const utils = require("../../../utils/util.js")
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. navigationBarHeight,
  13. fenxiang: imgurl.fenxiang.url,
  14. homeSelectedO:imgurl.homeSelectedO.url,
  15. isshowposter:false,//是否显示分享弹框
  16. teljpgUrl: imgurl.teljpg.url,
  17. wmhome: imgurl.wmhome.url,
  18. share01: imgurl.share01.url,
  19. close02: imgurl.close02.url,
  20. closedUrl: imgurl.closed.url,
  21. quesGouUrl: imgurl.ques_gou.url,
  22. headbgUrl: imgurl.headbg.url,
  23. quesBgUrl: imgurl.ques_bg.url,
  24. couponChannelId: '',
  25. couponId: '',
  26. data: null,
  27. spellData: null,
  28. canSpell: true,
  29. canBuyIf: true,
  30. clock: "00",
  31. day: "00",
  32. hour: "00",
  33. min: "00",
  34. sec: "00",
  35. detailPicture: [],
  36. coverPicture: [],
  37. swiperCurrent: 0,
  38. showTime: true,
  39. limitCondition: "",
  40. minLimit: 0,
  41. maxLimit: 0,
  42. spellGroupOrder:false,
  43. opacity: 0,
  44. queueData: null,
  45. zIndex: 11,
  46. display: "none",
  47. orderGroupId:''
  48. },
  49. /**
  50. * 生命周期函数--监听页面加载
  51. */
  52. onLoad: function(options) {
  53. this.setData({
  54. couponChannelId: options.couponChannelId,
  55. couponId: options.couponId
  56. })
  57. if (options && options.couponChannelId) {
  58. this.getDetail(options.couponChannelId);
  59. }
  60. if (options && options.couponId) {
  61. this.getOneSpell(options.couponId)
  62. }
  63. this.getUserInfo();
  64. },
  65. /**
  66. * 显示分享海报
  67. */
  68. showPoster: function() {
  69. //跳转到海报生成页
  70. wx.navigateTo({
  71. url: `/pages/canvas/index?couponChannelId=${this.data.data.id}`
  72. })
  73. },
  74. /**
  75. * 显示分享弹框
  76. */
  77. showshare: function() {
  78. this.setData({
  79. isshowposter: true,
  80. })
  81. },
  82. /**
  83. * 隐藏分享弹框
  84. */
  85. hidemodal: function() {
  86. this.setData({
  87. isshowposter: false,
  88. })
  89. },
  90. getUserInfo: function() {
  91. let that = this;
  92. // 获取用户信息
  93. Http.get({
  94. url: config.api.getScore,
  95. data: {}
  96. })
  97. .then(res => {
  98. console.log(res)
  99. that.setData({
  100. nickName: res.data.nickName,
  101. avatarUrl: res.data.avatarUrl
  102. })
  103. })
  104. },
  105. gotoIndex: function() {
  106. wx.switchTab({
  107. url: '/pages/main/index',
  108. })
  109. },
  110. /**
  111. * 拨打电话
  112. */
  113. phone: function(e) {
  114. let that = this;
  115. wx.makePhoneCall({
  116. phoneNumber: e.target.dataset.merchantlinkphone
  117. });
  118. },
  119. /**
  120. * 直接购买
  121. */
  122. gotoBuy(e) {
  123. let that = this;
  124. this.setData({
  125. canBuyIf: false,
  126. formId: e.detail.formId,
  127. queueData: null,
  128. orderGroupId:''
  129. })
  130. that.getQuestions();
  131. },
  132. getQuestions(type){
  133. let that = this;
  134. Http.get({
  135. url: config.api.getQuestion,
  136. data: {
  137. couponType: JSON.stringify(that.data.data.type)
  138. }
  139. })
  140. .then(res => {
  141. wx.hideLoading();
  142. if (res.data == undefined) {
  143. // 立即购买
  144. that.orderFunc(type);
  145. } else if (res.data) {
  146. var animation = wx.createAnimation({});
  147. animation.translate((that.data.widthScreen - that.data.widthScreen), 0).scale(1).opacity(1).step({
  148. duration: 500
  149. })
  150. that.setData({
  151. queueData: animation.export(),
  152. zIndex: 9,
  153. opacity: 1,
  154. display: "block",
  155. questionnaire: JSON.parse(res.data.content),
  156. questionId: res.data.id
  157. });
  158. }
  159. })
  160. .catch(err => {
  161. wx.hideLoading();
  162. wx.showToast({
  163. title: err.message,
  164. icon: 'none',
  165. duration: 2000,
  166. mask: false
  167. });
  168. })
  169. },
  170. /**
  171. * 点击提交问题单选
  172. */
  173. formSubmit: function (e) {
  174. console.log(e);
  175. let that = this;
  176. /**
  177. * 多选
  178. */
  179. if (e.currentTarget.dataset.flags == 'multi') {
  180. if (that.data.anwserId.length == 0) {
  181. var answserIs = null
  182. } else {
  183. var answserIs = that.data.anwserId.join(",");
  184. }
  185. } else if (e.currentTarget.dataset.flags == 'single') {
  186. var answserIs = e.detail.value;
  187. }
  188. console.log(answserIs)
  189. console.log(e.currentTarget.dataset.questionid)
  190. Http.post({
  191. url: config.api.answerQuestion,
  192. data: {
  193. answer: answserIs,
  194. questionId: e.currentTarget.dataset.questionid
  195. }
  196. })
  197. .then(res => {
  198. that.closeQuestion();
  199. })
  200. .catch(err => {
  201. wx.showToast({
  202. title: err.message,
  203. icon: 'none',
  204. duration: 2000,
  205. mask: false
  206. });
  207. })
  208. },
  209. /**
  210. * 多选
  211. */
  212. checkboxChange: function (e) {
  213. console.log(e)
  214. this.setData({
  215. anwserId: e.detail.value
  216. })
  217. },
  218. closeQuestion: function () {
  219. let that = this;
  220. that.setData({
  221. display: "none",
  222. zIndex: 11,
  223. opacity: 0
  224. })
  225. setTimeout(function () {
  226. that.orderFunc();
  227. }, 500)
  228. },
  229. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  230. dateformat(micro_second) {
  231. // 总秒数
  232. var second = Math.floor(micro_second / 1000);
  233. // 天数
  234. var day = Math.floor(second / 3600 / 24) < 10 ? "0" + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
  235. // 小时
  236. var hr = Math.floor(second / 3600 % 24) < 10 ? "0" + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
  237. // 分钟
  238. var min = Math.floor(second / 60 % 60) < 10 ? "0" + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
  239. // 秒
  240. var sec = Math.floor(second % 60) < 10 ? "0" + Math.floor(second % 60) : Math.floor(second % 60);
  241. // return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  242. return {
  243. a1: day,
  244. b1: hr,
  245. c1: min,
  246. d1: sec
  247. }
  248. },
  249. countdown(time) {
  250. let that = this;
  251. var EndTime = time;
  252. if (that.data.spellData != null) {
  253. EndTime = that.data.spellData.expiredDate;
  254. }
  255. var NowTime = new Date().getTime();
  256. var total_micro_second = EndTime - NowTime || [];
  257. // 渲染倒计时时钟
  258. let obj = that.dateformat(total_micro_second);
  259. if (total_micro_second > 0) {
  260. that.setData({
  261. clock: obj,
  262. day: obj.a1,
  263. hour: obj.b1,
  264. min: obj.c1,
  265. sec: obj.d1,
  266. })
  267. } else {
  268. that.setData({
  269. clock: "00",
  270. day: "00",
  271. hour: "00",
  272. min: "00",
  273. sec: "00",
  274. })
  275. }
  276. if (that.data.showTime) {
  277. setTimeout(function() {
  278. total_micro_second -= 1000;
  279. that.countdown();
  280. }, 1000)
  281. }
  282. },
  283. // 获得未支付的订单
  284. getUnPaidOrder(couponId) {
  285. let that = this;
  286. Http.get({
  287. url: config.api.getUnPaidOrder,
  288. data: {
  289. couponId: couponId
  290. }
  291. }).then(res => {
  292. that.setData({
  293. disOrderNumber: res.data.orderNumber,
  294. couponChannelId: res.data.couponChannelId,
  295. orderGroupId: res.data.orderGroupId
  296. })
  297. // 拼团订单
  298. if (res.data.payment/100 != Number(that.data.data.price)) {
  299. that.setData({
  300. spellGroupOrder: true
  301. })
  302. } else {
  303. // 原价购买订单
  304. that.setData({
  305. spellGroupOrder: false
  306. })
  307. }
  308. if (that.data.spellGroupOrder) {
  309. wx.showModal({
  310. title: '提示',
  311. content: "您有未支付拼团订单,请先进行支付",
  312. confirmText: "去支付",
  313. success: function (res) {
  314. if (res.confirm) {
  315. wx.navigateTo({
  316. url: `/pages/spellGroup/paySpellGroup/index?orderId=${that.data.disOrderNumber}&couponChannelId=${that.data.couponChannelId}&couponId=${couponId}&orderGroupId={that.data.orderGroupId}`,
  317. })
  318. }
  319. }
  320. })
  321. } else {
  322. wx.showModal({
  323. title: '提示',
  324. content: '您有未支付订单,请先进行支付',
  325. confirmText: "去支付",
  326. success: function (res) {
  327. if (res.confirm) {
  328. wx.navigateTo({
  329. url: `/pages/order/detail/index?orderId=${that.data.disOrderNumber}`,
  330. })
  331. }
  332. }
  333. })
  334. }
  335. }).catch(error => {
  336. console.log(error)
  337. wx.showToast({
  338. title: error.message,
  339. icon:"none",
  340. duration:3000
  341. })
  342. })
  343. },
  344. /**
  345. * 发起拼团
  346. */
  347. gotoSpell(e) {
  348. let that = this;
  349. that.setData({
  350. canSpell: false,
  351. formId: e.detail.formId,
  352. orderGroupId:0
  353. })
  354. that.getQuestions(0);
  355. },
  356. //参与别人的拼团
  357. gotoPartner() {
  358. wx.navigateTo({
  359. url: `/pages/joinFrDpell/index?orderId=${this.data.spellData.orderId}&couponId=${this.data.spellData.couponId}&orderGroupId=${this.data.spellData.orderGroupId}&couponChannelId=${this.data.couponChannelId}&avatarUrl=${this.data.spellData.avatarUrl}&nickName=${this.data.spellData.nickName}`
  360. });
  361. },
  362. /**
  363. * 获取一个拼团信息
  364. */
  365. getOneSpell(couponId) {
  366. let that = this;
  367. Http.get({
  368. url: config.api.queryRemainOne,
  369. data: {
  370. couponId: couponId
  371. }
  372. }).then(res => {
  373. wx.stopPullDownRefresh();
  374. if (res.data) {
  375. that.countdown(res.data.expiredDate);
  376. that.setData({
  377. spellData: res.data
  378. });
  379. }
  380. });
  381. },
  382. /**
  383. * 获取券详情信息
  384. */
  385. getDetail(couponChannelId) {
  386. let that = this;
  387. Http.get({
  388. url: config.api.couponDetail,
  389. data: {
  390. couponChannelId: couponChannelId
  391. }
  392. }).then(res => {
  393. wx.stopPullDownRefresh();
  394. let data = res.data;
  395. if (res && res.data && res.data.detailPicture) {
  396. that.setData({
  397. detailPicture: JSON.parse(res.data.detailPicture)
  398. })
  399. }
  400. if (res && res.data && res.data.coverPicture) {
  401. that.setData({
  402. coverPicture: JSON.parse(res.data.coverPicture)
  403. })
  404. }
  405. if (res && res.data && res.data.conditions) {
  406. that.setData({
  407. limitCondition: JSON.parse(res.data.conditions).type
  408. })
  409. if (JSON.parse(res.data.conditions).type == 2 && (JSON.parse(res.data.conditions).min || JSON.parse(res.data.conditions).min == 0)) {
  410. that.setData({
  411. minLimit: JSON.parse(res.data.conditions).min
  412. })
  413. }
  414. if (JSON.parse(res.data.conditions).type == 2 && (JSON.parse(res.data.conditions).max) || JSON.parse(res.data.conditions).max == 0) {
  415. that.setData({
  416. maxLimit: JSON.parse(res.data.conditions).max
  417. })
  418. }
  419. }
  420. wx.setNavigationBarTitle({
  421. title: res.data.title
  422. })
  423. data.price = (data.price / 100).toFixed(2)
  424. data.salePrice = (data.salePrice / 100).toFixed(2);
  425. data.validStartDate = utils.formatTime(data.validStartDate, 'yyyy-MM-dd')
  426. data.validEndDate = utils.formatTime(data.validEndDate, 'yyyy-MM-dd')
  427. that.setData({
  428. data
  429. });
  430. });
  431. },
  432. /**
  433. * 去拼团
  434. */
  435. goToOrderGroup(orderId, orderGroupId, _this) {
  436. let that = this;
  437. // 支付成功
  438. Http.post({
  439. url: config.api.toOrderGroup,
  440. data: {
  441. id: orderGroupId,
  442. orderId,
  443. couponId: _this.data.data.couponId
  444. }
  445. })
  446. .then(res => {
  447. wx.navigateTo({
  448. url: `/pages/spellDetail/index?orderId=${orderId}&couponId=${_this.data.data.couponId}&orderGroupId=${res.data.orderGroupId}&couponChannelId=${_this.data.couponChannelId}`
  449. });
  450. })
  451. .catch(err => {
  452. console.log(err);
  453. })
  454. },
  455. /**
  456. * 支付订单更新
  457. */
  458. payOrderUpdate: (orderId, payOrderId, status, reason, _this, orderGroupId) => {
  459. console.log(orderGroupId, 7777)
  460. let that = this;
  461. // 支付成功
  462. Http.post({
  463. url: config.api.payOrderUpdate,
  464. data: {
  465. payOrderId: payOrderId,
  466. orderId: orderId,
  467. status: status,
  468. reason: reason
  469. }
  470. })
  471. .then(res => {
  472. wx.hideLoading()
  473. if (orderGroupId == undefined) {
  474. wx.navigateTo({
  475. url: '/pages/order/detail/index?orderId=' + res.data.id,
  476. })
  477. } else {
  478. _this.goToOrderGroup(orderId, res.data.orderGroupId, _this)
  479. }
  480. })
  481. .catch(err => {
  482. if (err.code != 12002) {
  483. setTimeout(function() {
  484. _this.payOrderUpdate(orderId, payOrderId, status, reason, _this, orderGroupId);
  485. }, 2000)
  486. }
  487. })
  488. },
  489. /**
  490. * 发起支付
  491. */
  492. orderFunc() {
  493. let orderGroupId = this.data.orderGroupId;
  494. let data = {
  495. couponChannelId: this.data.data.id,
  496. couponId: this.data.data.couponId,
  497. formId: this.data.formId
  498. }
  499. /**
  500. * 拼团订单
  501. */
  502. let that = this;
  503. if (this.data.orderGroupId == 0) {
  504. data.orderGroupId = this.data.orderGroupId,
  505. that.setData({
  506. isFromSpell:true
  507. })
  508. }else{
  509. that.setData({
  510. isFromSpell: false
  511. })
  512. }
  513. Http.get({
  514. url: config.api.checkPhoneStatus,
  515. data: {}
  516. })
  517. .then(res => {
  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. canSpell: true,
  532. canBuyIf: true
  533. })
  534. if (err.code == 2011) {
  535. wx.showToast({
  536. title: "商户信息没找到",
  537. image: './../../../assets/images/fail.png',
  538. duration: 2000,
  539. mask: false
  540. });
  541. } else if (err.code == 2013) {
  542. wx.showToast({
  543. title: "商户信息禁用",
  544. image: './../../../assets/images/fail.png',
  545. duration: 2000,
  546. mask: false
  547. });
  548. } else if (err.code == 3000) {
  549. wx.showToast({
  550. title: "库存不足",
  551. image: './../../../assets/images/fail.png',
  552. duration: 2000,
  553. mask: false
  554. });
  555. } else if (err.code == 3001) {
  556. wx.showToast({
  557. title: "您已超过限购",
  558. image: './../../../assets/images/fail.png',
  559. duration: 2000,
  560. mask: false
  561. });
  562. } else if (err.code == 3002) {
  563. wx.showToast({
  564. title: "订单失败",
  565. image: './../../../assets/images/fail.png',
  566. duration: 2000,
  567. mask: false
  568. });
  569. } else if (err.code == 3003) {
  570. wx.showToast({
  571. title: "订单不存在",
  572. image: './../../../assets/images/fail.png',
  573. duration: 2000,
  574. mask: false
  575. });
  576. } else if (err.code == 3004) {
  577. wx.showToast({
  578. title: "订单不存在",
  579. image: './../../../assets/images/fail.png',
  580. duration: 2000,
  581. mask: false
  582. });
  583. } else if (err.code == 4003) {
  584. wx.showToast({
  585. title: "卡券已作废",
  586. image: './../../../assets/images/fail.png',
  587. duration: 2000,
  588. mask: false
  589. });
  590. } else if (err.code == 3012) {
  591. // wx.showToast({
  592. // title: err.message,
  593. // icon: "none"
  594. // })
  595. that.getUnPaidOrder(that.data.data.couponId);
  596. } else if (err.code == 11005) {
  597. /**
  598. * 将值传到用户手机号授权的页面
  599. *
  600. */
  601. wx.redirectTo({
  602. url: "/pages/getphoneInfo/index?path=spell&couponChannelId=" +
  603. that.data.couponChannelId + '&couponId=' + that.data.couponId
  604. });
  605. } else if (err.code == 11006) {
  606. // 用户手机已加密
  607. wx.redirectTo({
  608. url: "/pages/phoneinput/phoneinput?path=spell&couponChannelId=" +
  609. that.data.couponChannelId + '&couponId=' + that.data.couponId
  610. });
  611. } else {
  612. wx.showToast({
  613. title: err.message,
  614. icon: 'none',
  615. duration: 2000,
  616. mask: false
  617. });
  618. }
  619. })
  620. .then(res => {
  621. console.log(res)
  622. if (typeof(res) != "undefined") {
  623. let orderId = "" + res.data.id;
  624. that.setData({
  625. orderId: orderId,
  626. canSpell: false,
  627. canBuyIf: true
  628. });
  629. // 支付金额不为0
  630. /**
  631. * 支付订单创建
  632. */
  633. Http.post({
  634. url: config.api.payOrderCreate,
  635. data: {
  636. orderId: orderId
  637. }
  638. })
  639. .then(res => {
  640. var payOrderId = "" + res.data.payOrderId;
  641. wx.hideLoading();
  642. wx.requestPayment({
  643. timeStamp: res.data.timeStamp,
  644. nonceStr: res.data.nonceStr,
  645. package: res.data.package,
  646. signType: (res.data.signType) ? res.data.signType : "MD5",
  647. paySign: res.data.paySign,
  648. success: res => {
  649. that.setData({
  650. canSpell: false
  651. })
  652. wx.showLoading({
  653. title: '订单正在处理中...',
  654. })
  655. setTimeout(function() {
  656. wx.hideLoading()
  657. }, 5000)
  658. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', that, orderGroupId);
  659. if (res.errMsg == "requestPayment:ok") {
  660. setTimeout(function() {
  661. wx.hideLoading();
  662. }, 2000);
  663. }
  664. },
  665. fail: res => {
  666. /**
  667. * 支付失败,需要更新订单的状态
  668. */
  669. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', that, orderGroupId);
  670. that.setData({
  671. showbutton: false,
  672. canSpell: true,
  673. canBuyIf: true
  674. })
  675. return;
  676. },
  677. complete: res => {}
  678. });
  679. /// End payment --------
  680. })
  681. .catch(err => {
  682. that.setData({
  683. canSpell: true,
  684. canBuyIf: true
  685. })
  686. wx.showToast({
  687. title: err.message,
  688. icon: 'none',
  689. duration: 2000,
  690. mask: false
  691. });
  692. })
  693. }
  694. })
  695. },
  696. /**
  697. * 生命周期函数--监听页面显示
  698. */
  699. onShow: function() {
  700. this.setData({
  701. canSpell: true,
  702. canBuyIf: true,
  703. showTime: true
  704. })
  705. if (this.data.spellData != null) {
  706. this.countdown()
  707. }
  708. },
  709. /**
  710. * 生命周期函数--监听页面隐藏
  711. */
  712. onHide: function() {
  713. this.setData({
  714. showTime: false
  715. })
  716. },
  717. /**
  718. * 生命周期函数--监听页面卸载
  719. */
  720. onUnload: function() {
  721. this.setData({
  722. showTime: false
  723. })
  724. },
  725. /**
  726. * 页面相关事件处理函数--监听用户下拉动作
  727. */
  728. onPullDownRefresh: function(e) {
  729. let that = this;
  730. that.getDetail(that.data.couponChannelId);
  731. if (that.data.couponId) {
  732. that.getOneSpell(that.data.couponId)
  733. }
  734. },
  735. onShareAppMessage: function (options) {
  736. console.log(options)
  737. var that = this;
  738. var shareObj = {
  739. title: that.data.data.title,
  740. path: `/pages/index/index?couponChannelId=${that.data.couponChannelId}&spellGroup=spellGroup`,
  741. success: function (res) {
  742. if (res.errMsg == 'shareAppMessage:ok') { }
  743. },
  744. fail: function (error) {
  745. if (res.errMsg == 'shareAppMessage:fail cancel') { } else if (res.errMsg == 'shareAppMessage:fail') { }
  746. }
  747. };
  748. // 来自页面内的按钮的转发
  749. if (options.from == 'button') {
  750. var eData = options.target.dataset.id;
  751. shareObj.path = `/pages/index/index?couponChannelId=${eData}&spellGroup=spellGroup&frommd=JC`;
  752. }
  753. // 返回shareObj
  754. return shareObj;
  755. },
  756. })