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.

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