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.

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