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.

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