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.

767 lines
20 KiB

  1. // pages/spellGroup/mySpellGroup/index.js
  2. const navigationBarHeight = (getApp().statusBarHeight + 44) + '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(orderGroupId) {
  493. let data = {
  494. couponChannelId: this.data.data.id,
  495. couponId: this.data.data.couponId,
  496. formId: this.data.formId
  497. }
  498. /**
  499. * 拼团订单
  500. */
  501. let that = this;
  502. if (this.data.orderGroupId == 0) {
  503. data.orderGroupId = this.data.orderGroupId,
  504. that.setData({
  505. isFromSpell:true
  506. })
  507. }else{
  508. that.setData({
  509. isFromSpell: false
  510. })
  511. }
  512. Http.get({
  513. url: config.api.checkPhoneStatus,
  514. data: {}
  515. })
  516. .then(res => {
  517. /**
  518. * orderSave 下单
  519. */
  520. return Http.post({
  521. url: config.api.orderSave,
  522. data: data
  523. });
  524. })
  525. .catch(err => {
  526. console.log(err);
  527. that.setData({
  528. showbutton: false,
  529. showbutton1: false,
  530. canSpell: true,
  531. canBuyIf: true
  532. })
  533. if (err.code == 2011) {
  534. wx.showToast({
  535. title: "商户信息没找到",
  536. image: './../../../assets/images/fail.png',
  537. duration: 2000,
  538. mask: false
  539. });
  540. } else if (err.code == 2013) {
  541. wx.showToast({
  542. title: "商户信息禁用",
  543. image: './../../../assets/images/fail.png',
  544. duration: 2000,
  545. mask: false
  546. });
  547. } else if (err.code == 3000) {
  548. wx.showToast({
  549. title: "库存不足",
  550. image: './../../../assets/images/fail.png',
  551. duration: 2000,
  552. mask: false
  553. });
  554. } else if (err.code == 3001) {
  555. wx.showToast({
  556. title: "您已超过限购",
  557. image: './../../../assets/images/fail.png',
  558. duration: 2000,
  559. mask: false
  560. });
  561. } else if (err.code == 3002) {
  562. wx.showToast({
  563. title: "订单失败",
  564. image: './../../../assets/images/fail.png',
  565. duration: 2000,
  566. mask: false
  567. });
  568. } else if (err.code == 3003) {
  569. wx.showToast({
  570. title: "订单不存在",
  571. image: './../../../assets/images/fail.png',
  572. duration: 2000,
  573. mask: false
  574. });
  575. } else if (err.code == 3004) {
  576. wx.showToast({
  577. title: "订单不存在",
  578. image: './../../../assets/images/fail.png',
  579. duration: 2000,
  580. mask: false
  581. });
  582. } else if (err.code == 4003) {
  583. wx.showToast({
  584. title: "卡券已作废",
  585. image: './../../../assets/images/fail.png',
  586. duration: 2000,
  587. mask: false
  588. });
  589. } else if (err.code == 3012) {
  590. // wx.showToast({
  591. // title: err.message,
  592. // icon: "none"
  593. // })
  594. that.getUnPaidOrder(that.data.data.couponId);
  595. } else if (err.code == 11005) {
  596. /**
  597. * 将值传到用户手机号授权的页面
  598. *
  599. */
  600. wx.redirectTo({
  601. url: "/pages/getphoneInfo/index?path=spell&couponChannelId=" +
  602. that.data.couponChannelId + '&couponId=' + that.data.couponId
  603. });
  604. } else if (err.code == 11006) {
  605. // 用户手机已加密
  606. wx.redirectTo({
  607. url: "/pages/phoneinput/phoneinput?path=spell&couponChannelId=" +
  608. that.data.couponChannelId + '&couponId=' + that.data.couponId
  609. });
  610. } else {
  611. wx.showToast({
  612. title: err.message,
  613. icon: 'none',
  614. duration: 2000,
  615. mask: false
  616. });
  617. }
  618. })
  619. .then(res => {
  620. console.log(res)
  621. if (typeof(res) != "undefined") {
  622. let orderId = "" + res.data.id;
  623. that.setData({
  624. orderId: orderId,
  625. canSpell: false,
  626. canBuyIf: true
  627. });
  628. // 支付金额不为0
  629. /**
  630. * 支付订单创建
  631. */
  632. Http.post({
  633. url: config.api.payOrderCreate,
  634. data: {
  635. orderId: orderId
  636. }
  637. })
  638. .then(res => {
  639. var payOrderId = "" + res.data.payOrderId;
  640. wx.hideLoading();
  641. wx.requestPayment({
  642. timeStamp: res.data.timeStamp,
  643. nonceStr: res.data.nonceStr,
  644. package: res.data.package,
  645. signType: (res.data.signType) ? res.data.signType : "MD5",
  646. paySign: res.data.paySign,
  647. success: res => {
  648. that.setData({
  649. canSpell: false
  650. })
  651. wx.showLoading({
  652. title: '订单正在处理中...',
  653. })
  654. setTimeout(function() {
  655. wx.hideLoading()
  656. }, 5000)
  657. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', that, orderGroupId);
  658. if (res.errMsg == "requestPayment:ok") {
  659. setTimeout(function() {
  660. wx.hideLoading();
  661. }, 2000);
  662. }
  663. },
  664. fail: res => {
  665. /**
  666. * 支付失败,需要更新订单的状态
  667. */
  668. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', that, orderGroupId);
  669. that.setData({
  670. showbutton: false,
  671. canSpell: true,
  672. canBuyIf: true
  673. })
  674. return;
  675. },
  676. complete: res => {}
  677. });
  678. /// End payment --------
  679. })
  680. .catch(err => {
  681. that.setData({
  682. canSpell: true,
  683. canBuyIf: true
  684. })
  685. wx.showToast({
  686. title: err.message,
  687. icon: 'none',
  688. duration: 2000,
  689. mask: false
  690. });
  691. })
  692. }
  693. })
  694. },
  695. /**
  696. * 生命周期函数--监听页面显示
  697. */
  698. onShow: function() {
  699. this.setData({
  700. canSpell: true,
  701. canBuyIf: true,
  702. showTime: true
  703. })
  704. if (this.data.spellData != null) {
  705. this.countdown()
  706. }
  707. },
  708. /**
  709. * 生命周期函数--监听页面隐藏
  710. */
  711. onHide: function() {
  712. this.setData({
  713. showTime: false
  714. })
  715. },
  716. /**
  717. * 生命周期函数--监听页面卸载
  718. */
  719. onUnload: function() {
  720. this.setData({
  721. showTime: false
  722. })
  723. },
  724. /**
  725. * 页面相关事件处理函数--监听用户下拉动作
  726. */
  727. onPullDownRefresh: function(e) {
  728. let that = this;
  729. that.getDetail(that.data.couponChannelId);
  730. if (that.data.couponId) {
  731. that.getOneSpell(that.data.couponId)
  732. }
  733. },
  734. onShareAppMessage: function (options) {
  735. console.log(options)
  736. var that = this;
  737. var shareObj = {
  738. title: that.data.data.title,
  739. path: `/pages/index/index?couponChannelId=${that.data.couponChannelId}&spellGroup=spellGroup`,
  740. success: function (res) {
  741. if (res.errMsg == 'shareAppMessage:ok') { }
  742. },
  743. fail: function (error) {
  744. if (res.errMsg == 'shareAppMessage:fail cancel') { } else if (res.errMsg == 'shareAppMessage:fail') { }
  745. }
  746. };
  747. // 来自页面内的按钮的转发
  748. if (options.from == 'button') {
  749. var eData = options.target.dataset.id;
  750. shareObj.path = `/pages/index/index?couponChannelId=${eData}&spellGroup=spellGroup&frommd=JC`;
  751. }
  752. // 返回shareObj
  753. return shareObj;
  754. },
  755. })