C端小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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