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.

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