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.

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