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.

688 lines
18 KiB

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