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.

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