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.

762 lines
20 KiB

  1. // pages/spellGroup/mySpellGroup/index.js
  2. var config = require("../../../config/config.js");
  3. const Http = require("../../../utils/HttpBasics");
  4. const imgurl = require("../../../utils/imgurl");
  5. const utils = require("../../../utils/util.js")
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. fenxiang: imgurl.fenxiang.url,
  12. homeSelectedO:imgurl.homeSelectedO.url,
  13. isshowposter:false,//是否显示分享弹框
  14. teljpgUrl: imgurl.teljpg.url,
  15. wmhome: imgurl.wmhome.url,
  16. share01: imgurl.share01.url,
  17. close02: imgurl.close02.url,
  18. closedUrl: imgurl.closed.url,
  19. quesGouUrl: imgurl.ques_gou.url,
  20. headbgUrl: imgurl.headbg.url,
  21. quesBgUrl: imgurl.ques_bg.url,
  22. couponChannelId: '',
  23. couponId: '',
  24. data: null,
  25. spellData: null,
  26. canSpell: true,
  27. canBuyIf: true,
  28. clock: "00",
  29. day: "00",
  30. hour: "00",
  31. min: "00",
  32. sec: "00",
  33. detailPicture: [],
  34. coverPicture: [],
  35. swiperCurrent: 0,
  36. showTime: true,
  37. limitCondition: "",
  38. minLimit: 0,
  39. maxLimit: 0,
  40. spellGroupOrder:false,
  41. opacity: 0,
  42. queueData: null,
  43. zIndex: 11,
  44. display: "none",
  45. },
  46. /**
  47. * 生命周期函数--监听页面加载
  48. */
  49. onLoad: function(options) {
  50. this.setData({
  51. couponChannelId: options.couponChannelId,
  52. couponId: options.couponId
  53. })
  54. if (options && options.couponChannelId) {
  55. this.getDetail(options.couponChannelId);
  56. }
  57. if (options && options.couponId) {
  58. this.getOneSpell(options.couponId)
  59. }
  60. this.getUserInfo();
  61. },
  62. /**
  63. * 显示分享海报
  64. */
  65. showPoster: function() {
  66. //跳转到海报生成页
  67. wx.navigateTo({
  68. url: `/pages/canvas/index?couponChannelId=${this.data.data.id}`
  69. })
  70. },
  71. /**
  72. * 显示分享弹框
  73. */
  74. showshare: function() {
  75. this.setData({
  76. isshowposter: true,
  77. })
  78. },
  79. /**
  80. * 隐藏分享弹框
  81. */
  82. hidemodal: function() {
  83. this.setData({
  84. isshowposter: false,
  85. })
  86. },
  87. getUserInfo: function() {
  88. let that = this;
  89. // 获取用户信息
  90. Http.get({
  91. url: config.api.getScore,
  92. data: {}
  93. })
  94. .then(res => {
  95. console.log(res)
  96. that.setData({
  97. nickName: res.data.nickName,
  98. avatarUrl: res.data.avatarUrl
  99. })
  100. })
  101. },
  102. gotoIndex: function() {
  103. wx.switchTab({
  104. url: '/pages/main/index',
  105. })
  106. },
  107. /**
  108. * 拨打电话
  109. */
  110. phone: function(e) {
  111. let that = this;
  112. wx.makePhoneCall({
  113. phoneNumber: e.target.dataset.merchantlinkphone
  114. });
  115. },
  116. /**
  117. * 直接购买
  118. */
  119. gotoBuy(e) {
  120. let that = this;
  121. this.setData({
  122. canBuyIf: false,
  123. formId: e.detail.formId,
  124. queueData: null
  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. })
  349. that.getQuestions(0);
  350. },
  351. //参与别人的拼团
  352. gotoPartner() {
  353. wx.navigateTo({
  354. 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}`
  355. });
  356. },
  357. /**
  358. * 获取一个拼团信息
  359. */
  360. getOneSpell(couponId) {
  361. let that = this;
  362. Http.get({
  363. url: config.api.queryRemainOne,
  364. data: {
  365. couponId: couponId
  366. }
  367. }).then(res => {
  368. wx.stopPullDownRefresh();
  369. if (res.data) {
  370. that.countdown(res.data.expiredDate);
  371. that.setData({
  372. spellData: res.data
  373. });
  374. }
  375. });
  376. },
  377. /**
  378. * 获取券详情信息
  379. */
  380. getDetail(couponChannelId) {
  381. let that = this;
  382. Http.get({
  383. url: config.api.couponDetail,
  384. data: {
  385. couponChannelId: couponChannelId
  386. }
  387. }).then(res => {
  388. wx.stopPullDownRefresh();
  389. let data = res.data;
  390. if (res && res.data && res.data.detailPicture) {
  391. that.setData({
  392. detailPicture: JSON.parse(res.data.detailPicture)
  393. })
  394. }
  395. if (res && res.data && res.data.coverPicture) {
  396. that.setData({
  397. coverPicture: JSON.parse(res.data.coverPicture)
  398. })
  399. }
  400. if (res && res.data && res.data.conditions) {
  401. that.setData({
  402. limitCondition: JSON.parse(res.data.conditions).type
  403. })
  404. if (JSON.parse(res.data.conditions).type == 2 && (JSON.parse(res.data.conditions).min || JSON.parse(res.data.conditions).min == 0)) {
  405. that.setData({
  406. minLimit: JSON.parse(res.data.conditions).min
  407. })
  408. }
  409. if (JSON.parse(res.data.conditions).type == 2 && (JSON.parse(res.data.conditions).max) || JSON.parse(res.data.conditions).max == 0) {
  410. that.setData({
  411. maxLimit: JSON.parse(res.data.conditions).max
  412. })
  413. }
  414. }
  415. wx.setNavigationBarTitle({
  416. title: res.data.title
  417. })
  418. data.price = (data.price / 100).toFixed(2)
  419. data.salePrice = (data.salePrice / 100).toFixed(2);
  420. data.validStartDate = utils.formatTime(data.validStartDate, 'yyyy-MM-dd')
  421. data.validEndDate = utils.formatTime(data.validEndDate, 'yyyy-MM-dd')
  422. that.setData({
  423. data
  424. });
  425. });
  426. },
  427. /**
  428. * 去拼团
  429. */
  430. goToOrderGroup(orderId, orderGroupId, _this) {
  431. let that = this;
  432. // 支付成功
  433. Http.post({
  434. url: config.api.toOrderGroup,
  435. data: {
  436. id: orderGroupId,
  437. orderId,
  438. couponId: _this.data.data.couponId
  439. }
  440. })
  441. .then(res => {
  442. wx.navigateTo({
  443. url: `/pages/spellDetail/index?orderId=${orderId}&couponId=${_this.data.data.couponId}&orderGroupId=${res.data.orderGroupId}&couponChannelId=${_this.data.couponChannelId}`
  444. });
  445. })
  446. .catch(err => {
  447. console.log(err);
  448. })
  449. },
  450. /**
  451. * 支付订单更新
  452. */
  453. payOrderUpdate: (orderId, payOrderId, status, reason, _this, orderGroupId) => {
  454. console.log(orderGroupId, 7777)
  455. let that = this;
  456. // 支付成功
  457. Http.post({
  458. url: config.api.payOrderUpdate,
  459. data: {
  460. payOrderId: payOrderId,
  461. orderId: orderId,
  462. status: status,
  463. reason: reason
  464. }
  465. })
  466. .then(res => {
  467. wx.hideLoading()
  468. if (orderGroupId == undefined) {
  469. wx.navigateTo({
  470. url: '/pages/order/detail/index?orderId=' + res.data.id,
  471. })
  472. } else {
  473. _this.goToOrderGroup(orderId, res.data.orderGroupId, _this)
  474. }
  475. })
  476. .catch(err => {
  477. if (err.code != 12002) {
  478. setTimeout(function() {
  479. _this.payOrderUpdate(orderId, payOrderId, status, reason, _this, orderGroupId);
  480. }, 2000)
  481. }
  482. })
  483. },
  484. /**
  485. * 发起支付
  486. */
  487. orderFunc(orderGroupId) {
  488. let data = {
  489. couponChannelId: this.data.data.id,
  490. couponId: this.data.data.couponId,
  491. formId: this.data.formId
  492. }
  493. /**
  494. * 拼团订单
  495. */
  496. let that = this;
  497. if (orderGroupId == 0) {
  498. data.orderGroupId = orderGroupId,
  499. that.setData({
  500. isFromSpell:true
  501. })
  502. }else{
  503. that.setData({
  504. isFromSpell: false
  505. })
  506. }
  507. Http.get({
  508. url: config.api.checkPhoneStatus,
  509. data: {}
  510. })
  511. .then(res => {
  512. /**
  513. * orderSave 下单
  514. */
  515. return Http.post({
  516. url: config.api.orderSave,
  517. data: data
  518. });
  519. })
  520. .catch(err => {
  521. console.log(err);
  522. that.setData({
  523. showbutton: false,
  524. showbutton1: false,
  525. canSpell: true,
  526. canBuyIf: true
  527. })
  528. if (err.code == 2011) {
  529. wx.showToast({
  530. title: "商户信息没找到",
  531. image: './../../../assets/images/fail.png',
  532. duration: 2000,
  533. mask: false
  534. });
  535. } else if (err.code == 2013) {
  536. wx.showToast({
  537. title: "商户信息禁用",
  538. image: './../../../assets/images/fail.png',
  539. duration: 2000,
  540. mask: false
  541. });
  542. } else if (err.code == 3000) {
  543. wx.showToast({
  544. title: "库存不足",
  545. image: './../../../assets/images/fail.png',
  546. duration: 2000,
  547. mask: false
  548. });
  549. } else if (err.code == 3001) {
  550. wx.showToast({
  551. title: "您已超过限购",
  552. image: './../../../assets/images/fail.png',
  553. duration: 2000,
  554. mask: false
  555. });
  556. } else if (err.code == 3002) {
  557. wx.showToast({
  558. title: "订单失败",
  559. image: './../../../assets/images/fail.png',
  560. duration: 2000,
  561. mask: false
  562. });
  563. } else if (err.code == 3003) {
  564. wx.showToast({
  565. title: "订单不存在",
  566. image: './../../../assets/images/fail.png',
  567. duration: 2000,
  568. mask: false
  569. });
  570. } else if (err.code == 3004) {
  571. wx.showToast({
  572. title: "订单不存在",
  573. image: './../../../assets/images/fail.png',
  574. duration: 2000,
  575. mask: false
  576. });
  577. } else if (err.code == 4003) {
  578. wx.showToast({
  579. title: "卡券已作废",
  580. image: './../../../assets/images/fail.png',
  581. duration: 2000,
  582. mask: false
  583. });
  584. } else if (err.code == 3012) {
  585. // wx.showToast({
  586. // title: err.message,
  587. // icon: "none"
  588. // })
  589. that.getUnPaidOrder(that.data.data.couponId);
  590. } else if (err.code == 11005) {
  591. /**
  592. * 将值传到用户手机号授权的页面
  593. *
  594. */
  595. wx.redirectTo({
  596. url: "/pages/getphoneInfo/index?path=spell&couponChannelId=" +
  597. that.data.couponChannelId + '&couponId=' + that.data.couponId
  598. });
  599. } else if (err.code == 11006) {
  600. // 用户手机已加密
  601. wx.redirectTo({
  602. url: "/pages/phoneinput/phoneinput?path=spell&couponChannelId=" +
  603. that.data.couponChannelId + '&couponId=' + that.data.couponId
  604. });
  605. } else {
  606. wx.showToast({
  607. title: err.message,
  608. icon: 'none',
  609. duration: 2000,
  610. mask: false
  611. });
  612. }
  613. })
  614. .then(res => {
  615. console.log(res)
  616. if (typeof(res) != "undefined") {
  617. let orderId = "" + res.data.id;
  618. that.setData({
  619. orderId: orderId,
  620. canSpell: false,
  621. canBuyIf: true
  622. });
  623. // 支付金额不为0
  624. /**
  625. * 支付订单创建
  626. */
  627. Http.post({
  628. url: config.api.payOrderCreate,
  629. data: {
  630. orderId: orderId
  631. }
  632. })
  633. .then(res => {
  634. var payOrderId = "" + res.data.payOrderId;
  635. wx.hideLoading();
  636. wx.requestPayment({
  637. timeStamp: res.data.timeStamp,
  638. nonceStr: res.data.nonceStr,
  639. package: res.data.package,
  640. signType: (res.data.signType) ? res.data.signType : "MD5",
  641. paySign: res.data.paySign,
  642. success: res => {
  643. that.setData({
  644. canSpell: false
  645. })
  646. wx.showLoading({
  647. title: '订单正在处理中...',
  648. })
  649. setTimeout(function() {
  650. wx.hideLoading()
  651. }, 5000)
  652. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', that, orderGroupId);
  653. if (res.errMsg == "requestPayment:ok") {
  654. setTimeout(function() {
  655. wx.hideLoading();
  656. }, 2000);
  657. }
  658. },
  659. fail: res => {
  660. /**
  661. * 支付失败,需要更新订单的状态
  662. */
  663. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', that, orderGroupId);
  664. that.setData({
  665. showbutton: false,
  666. canSpell: true,
  667. canBuyIf: true
  668. })
  669. return;
  670. },
  671. complete: res => {}
  672. });
  673. /// End payment --------
  674. })
  675. .catch(err => {
  676. that.setData({
  677. canSpell: true,
  678. canBuyIf: true
  679. })
  680. wx.showToast({
  681. title: err.message,
  682. icon: 'none',
  683. duration: 2000,
  684. mask: false
  685. });
  686. })
  687. }
  688. })
  689. },
  690. /**
  691. * 生命周期函数--监听页面显示
  692. */
  693. onShow: function() {
  694. this.setData({
  695. canSpell: true,
  696. canBuyIf: true,
  697. showTime: true
  698. })
  699. if (this.data.spellData != null) {
  700. this.countdown()
  701. }
  702. },
  703. /**
  704. * 生命周期函数--监听页面隐藏
  705. */
  706. onHide: function() {
  707. this.setData({
  708. showTime: false
  709. })
  710. },
  711. /**
  712. * 生命周期函数--监听页面卸载
  713. */
  714. onUnload: function() {
  715. this.setData({
  716. showTime: false
  717. })
  718. },
  719. /**
  720. * 页面相关事件处理函数--监听用户下拉动作
  721. */
  722. onPullDownRefresh: function(e) {
  723. let that = this;
  724. that.getDetail(that.data.couponChannelId);
  725. if (that.data.couponId) {
  726. that.getOneSpell(that.data.couponId)
  727. }
  728. },
  729. onShareAppMessage: function (options) {
  730. console.log(options)
  731. var that = this;
  732. var shareObj = {
  733. title: that.data.data.title,
  734. path: `/pages/index/index?couponChannelId=${that.data.couponChannelId}&spellGroup=spellGroup`,
  735. success: function (res) {
  736. if (res.errMsg == 'shareAppMessage:ok') { }
  737. },
  738. fail: function (error) {
  739. if (res.errMsg == 'shareAppMessage:fail cancel') { } else if (res.errMsg == 'shareAppMessage:fail') { }
  740. }
  741. };
  742. // 来自页面内的按钮的转发
  743. if (options.from == 'button') {
  744. var eData = options.target.dataset.id;
  745. shareObj.path = `/pages/index/index?couponChannelId=${eData}&spellGroup=spellGroup&frommd=JC`;
  746. }
  747. // 返回shareObj
  748. return shareObj;
  749. },
  750. })