C端小程序
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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