C端小程序
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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