C端小程序
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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