C端小程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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