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.

645 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. })
  181. // 拼团订单
  182. if (res.data.payment/100 != Number(that.data.data.price)) {
  183. that.setData({
  184. spellGroupOrder: true
  185. })
  186. } else {
  187. // 原价购买订单
  188. that.setData({
  189. spellGroupOrder: false
  190. })
  191. }
  192. if (that.data.spellGroupOrder) {
  193. wx.showModal({
  194. title: '提示',
  195. content: "您有未支付拼团订单,请先进行支付",
  196. confirmText: "去支付",
  197. success: function (res) {
  198. if (res.confirm) {
  199. wx.navigateTo({
  200. url: `/pages/bargain/bargainDatail/bargainDatail?orderId=${that.data.disOrderNumber}`,
  201. })
  202. }
  203. }
  204. })
  205. } else {
  206. wx.showModal({
  207. title: '提示',
  208. content: '您有未支付订单,请先进行支付',
  209. confirmText: "去支付",
  210. success: function (res) {
  211. if (res.confirm) {
  212. wx.navigateTo({
  213. url: `/pages/order/detail/index?orderId=${that.data.disOrderNumber}`,
  214. })
  215. }
  216. }
  217. })
  218. }
  219. }).catch(error => {
  220. console.log(error)
  221. wx.showToast({
  222. title: error.message,
  223. icon:"none",
  224. duration:3000
  225. })
  226. })
  227. },
  228. /**
  229. * 发起拼团
  230. */
  231. gotoSpell(e) {
  232. this.setData({
  233. canSpell: false,
  234. formId: e.detail.formId
  235. })
  236. this.orderFunc(0)
  237. },
  238. //参与别人的拼团
  239. gotoPartner() {
  240. wx.navigateTo({
  241. 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}`
  242. });
  243. },
  244. /**
  245. * 获取一个拼团信息
  246. */
  247. getOneSpell(couponId) {
  248. let that = this;
  249. Http.get({
  250. url: config.api.queryRemainOne,
  251. data: {
  252. couponId: couponId
  253. }
  254. }).then(res => {
  255. wx.stopPullDownRefresh();
  256. if (res.data) {
  257. that.countdown(res.data.expiredDate);
  258. that.setData({
  259. spellData: res.data
  260. });
  261. }
  262. });
  263. },
  264. /**
  265. * 获取券详情信息
  266. */
  267. getDetail(couponChannelId) {
  268. let that = this;
  269. Http.get({
  270. url: config.api.couponDetail,
  271. data: {
  272. couponChannelId: couponChannelId
  273. }
  274. }).then(res => {
  275. wx.stopPullDownRefresh();
  276. let data = res.data;
  277. if (res && res.data && res.data.detailPicture) {
  278. that.setData({
  279. detailPicture: JSON.parse(res.data.detailPicture)
  280. })
  281. }
  282. if (res && res.data && res.data.coverPicture) {
  283. that.setData({
  284. coverPicture: JSON.parse(res.data.coverPicture)
  285. })
  286. }
  287. if (res && res.data && res.data.conditions) {
  288. that.setData({
  289. limitCondition: JSON.parse(res.data.conditions).type
  290. })
  291. if (JSON.parse(res.data.conditions).type == 2 && (JSON.parse(res.data.conditions).min || JSON.parse(res.data.conditions).min == 0)) {
  292. that.setData({
  293. minLimit: JSON.parse(res.data.conditions).min
  294. })
  295. }
  296. if (JSON.parse(res.data.conditions).type == 2 && (JSON.parse(res.data.conditions).max) || JSON.parse(res.data.conditions).max == 0) {
  297. that.setData({
  298. maxLimit: JSON.parse(res.data.conditions).max
  299. })
  300. }
  301. }
  302. wx.setNavigationBarTitle({
  303. title: res.data.title
  304. })
  305. data.price = (data.price / 100).toFixed(2)
  306. data.salePrice = (data.salePrice / 100).toFixed(2);
  307. data.validStartDate = utils.formatTime(data.validStartDate, 'yyyy-MM-dd')
  308. data.validEndDate = utils.formatTime(data.validEndDate, 'yyyy-MM-dd')
  309. that.setData({
  310. data
  311. });
  312. });
  313. },
  314. /**
  315. * 去拼团
  316. */
  317. goToOrderGroup(orderId, orderGroupId, _this) {
  318. let that = this;
  319. // 支付成功
  320. Http.post({
  321. url: config.api.toOrderGroup,
  322. data: {
  323. id: orderGroupId,
  324. orderId,
  325. couponId: _this.data.data.couponId
  326. }
  327. })
  328. .then(res => {
  329. wx.navigateTo({
  330. url: `/pages/spellDetail/index?orderId=${orderId}&couponId=${_this.data.data.couponId}&orderGroupId=${res.data.orderGroupId}&couponChannelId=${_this.data.couponChannelId}`
  331. });
  332. })
  333. .catch(err => {
  334. console.log(err);
  335. })
  336. },
  337. /**
  338. * 支付订单更新
  339. */
  340. payOrderUpdate: (orderId, payOrderId, status, reason, _this, orderGroupId) => {
  341. console.log(orderGroupId, 7777)
  342. let that = this;
  343. // 支付成功
  344. Http.post({
  345. url: config.api.payOrderUpdate,
  346. data: {
  347. payOrderId: payOrderId,
  348. orderId: orderId,
  349. status: status,
  350. reason: reason
  351. }
  352. })
  353. .then(res => {
  354. wx.hideLoading()
  355. if (orderGroupId == undefined) {
  356. wx.navigateTo({
  357. url: '/pages/order/detail/index?orderId=' + res.data.id,
  358. })
  359. } else {
  360. _this.goToOrderGroup(orderId, res.data.orderGroupId, _this)
  361. }
  362. })
  363. .catch(err => {
  364. if (err.code != 12002) {
  365. setTimeout(function() {
  366. _this.payOrderUpdate(orderId, payOrderId, status, reason, _this, orderGroupId);
  367. }, 2000)
  368. }
  369. })
  370. },
  371. /**
  372. * 发起支付
  373. */
  374. orderFunc(orderGroupId) {
  375. let data = {
  376. couponChannelId: this.data.data.id,
  377. couponId: this.data.data.couponId,
  378. formId: this.data.formId
  379. }
  380. /**
  381. * 拼团订单
  382. */
  383. let that = this;
  384. if (orderGroupId == 0) {
  385. data.orderGroupId = orderGroupId,
  386. that.setData({
  387. isFromSpell:true
  388. })
  389. }else{
  390. that.setData({
  391. isFromSpell: false
  392. })
  393. }
  394. Http.get({
  395. url: config.api.checkPhoneStatus,
  396. data: {}
  397. })
  398. .then(res => {
  399. /**
  400. * orderSave 下单
  401. */
  402. return Http.post({
  403. url: config.api.orderSave,
  404. data: data
  405. });
  406. })
  407. .catch(err => {
  408. console.log(err);
  409. that.setData({
  410. showbutton: false,
  411. showbutton1: false,
  412. canSpell: true,
  413. canBuyIf: true
  414. })
  415. if (err.code == 2011) {
  416. wx.showToast({
  417. title: "商户信息没找到",
  418. image: './../../../assets/images/fail.png',
  419. duration: 2000,
  420. mask: false
  421. });
  422. } else if (err.code == 2013) {
  423. wx.showToast({
  424. title: "商户信息禁用",
  425. image: './../../../assets/images/fail.png',
  426. duration: 2000,
  427. mask: false
  428. });
  429. } else if (err.code == 3000) {
  430. wx.showToast({
  431. title: "库存不足",
  432. image: './../../../assets/images/fail.png',
  433. duration: 2000,
  434. mask: false
  435. });
  436. } else if (err.code == 3001) {
  437. wx.showToast({
  438. title: "您已超过限购",
  439. image: './../../../assets/images/fail.png',
  440. duration: 2000,
  441. mask: false
  442. });
  443. } else if (err.code == 3002) {
  444. wx.showToast({
  445. title: "订单失败",
  446. image: './../../../assets/images/fail.png',
  447. duration: 2000,
  448. mask: false
  449. });
  450. } else if (err.code == 3003) {
  451. wx.showToast({
  452. title: "订单不存在",
  453. image: './../../../assets/images/fail.png',
  454. duration: 2000,
  455. mask: false
  456. });
  457. } else if (err.code == 3004) {
  458. wx.showToast({
  459. title: "订单不存在",
  460. image: './../../../assets/images/fail.png',
  461. duration: 2000,
  462. mask: false
  463. });
  464. } else if (err.code == 4003) {
  465. wx.showToast({
  466. title: "卡券已作废",
  467. image: './../../../assets/images/fail.png',
  468. duration: 2000,
  469. mask: false
  470. });
  471. } else if (err.code == 3012) {
  472. // wx.showToast({
  473. // title: err.message,
  474. // icon: "none"
  475. // })
  476. that.getUnPaidOrder(that.data.data.couponId);
  477. } else if (err.code == 11005) {
  478. /**
  479. * 将值传到用户手机号授权的页面
  480. *
  481. */
  482. wx.redirectTo({
  483. url: "/pages/getphoneInfo/index?path=spell&couponChannelId=" +
  484. that.data.couponChannelId + '&couponId=' + that.data.couponId
  485. });
  486. } else if (err.code == 11006) {
  487. // 用户手机已加密
  488. wx.redirectTo({
  489. url: "/pages/phoneinput/phoneinput?path=spell&couponChannelId=" +
  490. that.data.couponChannelId + '&couponId=' + that.data.couponId
  491. });
  492. } else {
  493. wx.showToast({
  494. title: err.message,
  495. icon: 'none',
  496. duration: 2000,
  497. mask: false
  498. });
  499. }
  500. })
  501. .then(res => {
  502. console.log(res)
  503. if (typeof(res) != "undefined") {
  504. let orderId = "" + res.data.id;
  505. that.setData({
  506. orderId: orderId,
  507. canSpell: false,
  508. canBuyIf: true
  509. });
  510. // 支付金额不为0
  511. /**
  512. * 支付订单创建
  513. */
  514. Http.post({
  515. url: config.api.payOrderCreate,
  516. data: {
  517. orderId: orderId
  518. }
  519. })
  520. .then(res => {
  521. var payOrderId = "" + res.data.payOrderId;
  522. wx.hideLoading();
  523. wx.requestPayment({
  524. timeStamp: res.data.timeStamp,
  525. nonceStr: res.data.nonceStr,
  526. package: res.data.package,
  527. signType: (res.data.signType) ? res.data.signType : "MD5",
  528. paySign: res.data.paySign,
  529. success: res => {
  530. that.setData({
  531. canSpell: false
  532. })
  533. wx.showLoading({
  534. title: '订单正在处理中...',
  535. })
  536. setTimeout(function() {
  537. wx.hideLoading()
  538. }, 5000)
  539. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', that, orderGroupId);
  540. if (res.errMsg == "requestPayment:ok") {
  541. setTimeout(function() {
  542. wx.hideLoading();
  543. }, 2000);
  544. }
  545. },
  546. fail: res => {
  547. /**
  548. * 支付失败,需要更新订单的状态
  549. */
  550. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', that, orderGroupId);
  551. that.setData({
  552. showbutton: false,
  553. canSpell: true,
  554. canBuyIf: true
  555. })
  556. return;
  557. },
  558. complete: res => {}
  559. });
  560. /// End payment --------
  561. })
  562. .catch(err => {
  563. that.setData({
  564. canSpell: true,
  565. canBuyIf: true
  566. })
  567. wx.showToast({
  568. title: err.message,
  569. icon: 'none',
  570. duration: 2000,
  571. mask: false
  572. });
  573. })
  574. }
  575. })
  576. },
  577. /**
  578. * 生命周期函数--监听页面显示
  579. */
  580. onShow: function() {
  581. this.setData({
  582. canSpell: true,
  583. canBuyIf: true,
  584. showTime: true
  585. })
  586. if (this.data.spellData != null) {
  587. this.countdown()
  588. }
  589. },
  590. /**
  591. * 生命周期函数--监听页面隐藏
  592. */
  593. onHide: function() {
  594. this.setData({
  595. showTime: false
  596. })
  597. },
  598. /**
  599. * 生命周期函数--监听页面卸载
  600. */
  601. onUnload: function() {
  602. this.setData({
  603. showTime: false
  604. })
  605. },
  606. /**
  607. * 页面相关事件处理函数--监听用户下拉动作
  608. */
  609. onPullDownRefresh: function(e) {
  610. let that = this;
  611. that.getDetail(that.data.couponChannelId);
  612. if (that.data.couponId) {
  613. that.getOneSpell(that.data.couponId)
  614. }
  615. },
  616. onShareAppMessage: function (options) {
  617. console.log(options)
  618. var that = this;
  619. var shareObj = {
  620. title: that.data.data.title,
  621. path: `/pages/index/index?couponChannelId=${that.data.couponChannelId}&spellGroup=spellGroup`,
  622. success: function (res) {
  623. if (res.errMsg == 'shareAppMessage:ok') { }
  624. },
  625. fail: function (error) {
  626. if (res.errMsg == 'shareAppMessage:fail cancel') { } else if (res.errMsg == 'shareAppMessage:fail') { }
  627. }
  628. };
  629. // 来自页面内的按钮的转发
  630. if (options.from == 'button') {
  631. var eData = options.target.dataset.id;
  632. shareObj.path = `/pages/index/index?couponChannelId=${eData}&spellGroup=spellGroup&frommd=JC`;
  633. }
  634. // 返回shareObj
  635. return shareObj;
  636. },
  637. })