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.

820 lines
21 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. app.globalData.previewFlag = false
  378. let that = this;
  379. that.setData({
  380. canSpell: false,
  381. formId: e.detail.formId,
  382. orderGroupId: 0
  383. })
  384. that.getQuestions(0);
  385. },
  386. //参与别人的拼团
  387. gotoPartner() {
  388. wx.navigateTo({
  389. 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}`
  390. });
  391. },
  392. /**
  393. * 获取一个拼团信息
  394. */
  395. getOneSpell(couponId) {
  396. let that = this;
  397. Http.get({
  398. url: config.api.queryRemainOne,
  399. data: {
  400. couponId: couponId
  401. }
  402. }).then(res => {
  403. wx.stopPullDownRefresh();
  404. if (res.data) {
  405. that.countdown(res.data.expiredDate);
  406. that.setData({
  407. spellData: res.data
  408. });
  409. }
  410. });
  411. },
  412. /**
  413. * 获取券详情信息
  414. */
  415. getDetail(couponChannelId) {
  416. let that = this;
  417. Http.get({
  418. url: config.api.couponDetail,
  419. data: {
  420. couponChannelId: couponChannelId
  421. }
  422. }).then(res => {
  423. wx.stopPullDownRefresh();
  424. let data = res.data;
  425. if (res && res.data && res.data.detailPicture) {
  426. that.setData({
  427. detailPicture: JSON.parse(res.data.detailPicture)
  428. })
  429. }
  430. if (res && res.data && res.data.coverPicture) {
  431. that.setData({
  432. coverPicture: JSON.parse(res.data.coverPicture)
  433. })
  434. }
  435. if (res && res.data && res.data.conditions) {
  436. that.setData({
  437. limitCondition: JSON.parse(res.data.conditions).type
  438. })
  439. if (JSON.parse(res.data.conditions).type == 2 && (JSON.parse(res.data.conditions).min || JSON.parse(res.data.conditions).min == 0)) {
  440. that.setData({
  441. minLimit: JSON.parse(res.data.conditions).min
  442. })
  443. }
  444. if (JSON.parse(res.data.conditions).type == 2 && (JSON.parse(res.data.conditions).max) || JSON.parse(res.data.conditions).max == 0) {
  445. that.setData({
  446. maxLimit: JSON.parse(res.data.conditions).max
  447. })
  448. }
  449. }
  450. data.price = (data.price / 100).toFixed(2)
  451. data.salePrice = (data.salePrice / 100).toFixed(2);
  452. data.validStartDate = utils.formatTime(data.validStartDate, 'yyyy-MM-dd')
  453. data.validEndDate = utils.formatTime(data.validEndDate, 'yyyy-MM-dd')
  454. that.setData({
  455. data,
  456. merchantVoList: data.merchantVoList
  457. });
  458. });
  459. },
  460. /**
  461. * 去拼团
  462. */
  463. goToOrderGroup(orderId, orderGroupId, _this) {
  464. let that = this;
  465. // 支付成功
  466. Http.post({
  467. url: config.api.toOrderGroup,
  468. data: {
  469. id: orderGroupId,
  470. orderId,
  471. couponId: _this.data.data.couponId
  472. }
  473. })
  474. .then(res => {
  475. wx.navigateTo({
  476. url: `/pages/spellDetail/index?orderId=${orderId}&couponId=${_this.data.data.couponId}&orderGroupId=${res.data.orderGroupId}&couponChannelId=${_this.data.couponChannelId}`
  477. });
  478. })
  479. .catch(err => {
  480. console.log(err);
  481. })
  482. },
  483. /**
  484. * 支付订单更新
  485. */
  486. payOrderUpdate: (orderId, payOrderId, status, reason, _this, orderGroupId) => {
  487. console.log(orderGroupId, 7777)
  488. let that = this;
  489. // 支付成功
  490. Http.post({
  491. url: config.api.payOrderUpdate,
  492. data: {
  493. payOrderId: payOrderId,
  494. orderId: orderId,
  495. status: status,
  496. reason: reason
  497. }
  498. })
  499. .then(res => {
  500. wx.hideLoading()
  501. if (orderGroupId !== 0) {
  502. wx.navigateTo({
  503. url: '/pages/order/detail/index?orderId=' + res.data.id,
  504. })
  505. } else {
  506. _this.goToOrderGroup(orderId, res.data.orderGroupId, _this)
  507. }
  508. })
  509. .catch(err => {
  510. if (err.code != 12002) {
  511. setTimeout(function () {
  512. _this.payOrderUpdate(orderId, payOrderId, status, reason, _this, orderGroupId);
  513. }, 2000)
  514. }
  515. })
  516. },
  517. /**
  518. * 发起支付
  519. */
  520. orderFunc() {
  521. let that = this;
  522. let orderGroupId = this.data.orderGroupId;
  523. let data = {
  524. couponChannelId: this.data.data.id,
  525. couponId: this.data.data.couponId,
  526. formId: this.data.formId
  527. }
  528. /**
  529. * 拼团订单
  530. */
  531. if (this.data.orderGroupId == 0) {
  532. data.orderGroupId = this.data.orderGroupId,
  533. that.setData({
  534. isFromSpell: true
  535. })
  536. } else {
  537. that.setData({
  538. isFromSpell: false
  539. })
  540. }
  541. Http.get({
  542. url: config.api.checkUserStatus,
  543. data: {
  544. token: app.globalData.token
  545. }
  546. }).then(res => {
  547. Http.get({
  548. url: config.api.checkPhoneStatus,
  549. data: {}
  550. }).then(res => {
  551. /**
  552. * orderSave 下单
  553. */
  554. return Http.post({
  555. url: config.api.orderSave,
  556. data: data
  557. });
  558. }).catch(err => {
  559. console.log(err);
  560. that.setData({
  561. showbutton: false,
  562. showbutton1: false,
  563. canSpell: true,
  564. canBuyIf: true
  565. })
  566. if (err.code == 2011) {
  567. wx.showToast({
  568. title: "商户信息没找到",
  569. image: './../../../assets/images/fail.png',
  570. duration: 2000,
  571. mask: false
  572. });
  573. } else if (err.code == 2013) {
  574. wx.showToast({
  575. title: "商户信息禁用",
  576. image: './../../../assets/images/fail.png',
  577. duration: 2000,
  578. mask: false
  579. });
  580. } else if (err.code == 3000) {
  581. wx.showToast({
  582. title: "库存不足",
  583. image: './../../../assets/images/fail.png',
  584. duration: 2000,
  585. mask: false
  586. });
  587. } else if (err.code == 3001) {
  588. wx.showToast({
  589. title: "您已超过限购",
  590. image: './../../../assets/images/fail.png',
  591. duration: 2000,
  592. mask: false
  593. });
  594. } else if (err.code == 3002) {
  595. wx.showToast({
  596. title: "订单失败",
  597. image: './../../../assets/images/fail.png',
  598. duration: 2000,
  599. mask: false
  600. });
  601. } else if (err.code == 3003) {
  602. wx.showToast({
  603. title: "订单不存在",
  604. image: './../../../assets/images/fail.png',
  605. duration: 2000,
  606. mask: false
  607. });
  608. } else if (err.code == 3004) {
  609. wx.showToast({
  610. title: "订单不存在",
  611. image: './../../../assets/images/fail.png',
  612. duration: 2000,
  613. mask: false
  614. });
  615. } else if (err.code == 4003) {
  616. wx.showToast({
  617. title: "卡券已作废",
  618. image: './../../../assets/images/fail.png',
  619. duration: 2000,
  620. mask: false
  621. });
  622. } else if (err.code == 3012) {
  623. // wx.showToast({
  624. // title: err.message,
  625. // icon: "none"
  626. // })
  627. that.getUnPaidOrder(that.data.data.couponId);
  628. } else if (err.code == 11005) {
  629. /**
  630. * 将值传到用户手机号授权的页面
  631. *
  632. */
  633. wx.redirectTo({
  634. url: `/pages/getphoneInfo/index?path=spell&couponChannelId=${that.data.couponChannelId}&couponId=${that.data.data.couponId}`
  635. });
  636. } else if (err.code == 11006) {
  637. // 用户手机已加密
  638. wx.redirectTo({
  639. url: `/pages/phoneinput/phoneinput?path=spell&couponChannelId=${that.data.couponChannelId}&couponId=${that.data.data.couponId}`
  640. });
  641. } else {
  642. wx.showToast({
  643. title: err.message,
  644. icon: 'none',
  645. duration: 2000,
  646. mask: false
  647. });
  648. }
  649. }).then(res => {
  650. console.log(res)
  651. if (typeof (res) != "undefined") {
  652. let orderId = "" + res.data.id;
  653. that.setData({
  654. orderId: orderId,
  655. canSpell: false,
  656. canBuyIf: true
  657. });
  658. // 支付金额不为0
  659. /**
  660. * 支付订单创建
  661. */
  662. Http.post({
  663. url: config.api.payOrderCreate,
  664. data: {
  665. orderId: orderId
  666. }
  667. }).then(res => {
  668. var payOrderId = "" + res.data.payOrderId;
  669. wx.hideLoading();
  670. wx.requestPayment({
  671. timeStamp: res.data.timeStamp,
  672. nonceStr: res.data.nonceStr,
  673. package: res.data.package,
  674. signType: (res.data.signType) ? res.data.signType : "MD5",
  675. paySign: res.data.paySign,
  676. success: res => {
  677. that.setData({
  678. canSpell: false
  679. })
  680. wx.showLoading({
  681. title: '订单正在处理中...',
  682. })
  683. // setTimeout(function() {
  684. // wx.hideLoading()
  685. // }, 5000)
  686. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', that, orderGroupId);
  687. if (res.errMsg == "requestPayment:ok") {
  688. setTimeout(function () {
  689. wx.hideLoading();
  690. }, 2000);
  691. }
  692. },
  693. fail: res => {
  694. /**
  695. * 支付失败,需要更新订单的状态
  696. */
  697. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', that, orderGroupId);
  698. that.setData({
  699. showbutton: false,
  700. canSpell: true,
  701. canBuyIf: true
  702. })
  703. return;
  704. },
  705. complete: res => { }
  706. });
  707. /// End payment --------
  708. })
  709. .catch(err => {
  710. that.setData({
  711. canSpell: true,
  712. canBuyIf: true
  713. })
  714. wx.showToast({
  715. title: err.message,
  716. icon: 'none',
  717. duration: 2000,
  718. mask: false
  719. });
  720. })
  721. }
  722. })
  723. })
  724. .catch(err => {
  725. console.log(err)
  726. if (err.code == 11004) {
  727. wx.redirectTo({
  728. url: `/pages/getuserinfo/index?fromflag=spellcoupondetail&couponChannelIdflag=${that.data.couponChannelId}`,
  729. })
  730. } else {
  731. wx.showToast({
  732. title: err.message,
  733. icon: "none",
  734. duration: 2500
  735. })
  736. }
  737. })
  738. },
  739. /**
  740. * 生命周期函数--监听页面显示
  741. */
  742. onShow: function () {
  743. this.setData({
  744. canSpell: true,
  745. canBuyIf: true,
  746. showTime: true
  747. })
  748. if (this.data.spellData != null) {
  749. this.countdown()
  750. }
  751. },
  752. /**
  753. * 生命周期函数--监听页面隐藏
  754. */
  755. onHide: function () {
  756. this.setData({
  757. showTime: false
  758. })
  759. },
  760. /**
  761. * 生命周期函数--监听页面卸载
  762. */
  763. onUnload: function () {
  764. this.setData({
  765. showTime: false
  766. })
  767. },
  768. /**
  769. * 页面相关事件处理函数--监听用户下拉动作
  770. */
  771. onPullDownRefresh: function (e) {
  772. let that = this;
  773. console.log(this.data.couponId, 222)
  774. that.getDetail(that.data.couponChannelId);
  775. if (that.data.couponId) {
  776. that.getOneSpell(that.data.couponId)
  777. }
  778. },
  779. onShareAppMessage: function (options) {
  780. app.globalData.previewFlag = true
  781. console.log(options)
  782. var that = this;
  783. var shareObj = {
  784. title: that.data.data.title,
  785. path: `/pages/index/index?couponChannelId=${that.data.couponChannelId}&couponId=${that.data.data.couponId}&spellGroup=spellGroup`,
  786. success: function (res) {
  787. if (res.errMsg == 'shareAppMessage:ok') { }
  788. },
  789. fail: function (error) {
  790. if (res.errMsg == 'shareAppMessage:fail cancel') { } else if (res.errMsg == 'shareAppMessage:fail') { }
  791. }
  792. };
  793. // 来自页面内的按钮的转发
  794. if (options.from == 'button') {
  795. var eData = options.target.dataset.id;
  796. shareObj.path = `/pages/index/index?couponChannelId=${eData}&couponId=${that.data.data.couponId}&spellGroup=spellGroup&frommd=JC`;
  797. }
  798. // 返回shareObj
  799. return shareObj;
  800. },
  801. })