C端小程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

562 líneas
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. that.setData({
  308. isFromSpell:true
  309. })
  310. }else{
  311. that.setData({
  312. isFromSpell: false
  313. })
  314. }
  315. Http.get({
  316. url: config.api.checkPhoneStatus,
  317. data: {}
  318. })
  319. .then(res => {
  320. /**
  321. * orderSave 下单
  322. */
  323. return Http.post({
  324. url: config.api.orderSave,
  325. data: data
  326. });
  327. })
  328. .catch(err => {
  329. console.log(err);
  330. that.setData({
  331. showbutton: false,
  332. showbutton1: false,
  333. canSpell: true,
  334. canBuyIf: true
  335. })
  336. if (err.code == 2011) {
  337. wx.showToast({
  338. title: "商户信息没找到",
  339. image: './../../../assets/images/fail.png',
  340. duration: 2000,
  341. mask: false
  342. });
  343. } else if (err.code == 2013) {
  344. wx.showToast({
  345. title: "商户信息禁用",
  346. image: './../../../assets/images/fail.png',
  347. duration: 2000,
  348. mask: false
  349. });
  350. } else if (err.code == 3000) {
  351. wx.showToast({
  352. title: "库存不足",
  353. image: './../../../assets/images/fail.png',
  354. duration: 2000,
  355. mask: false
  356. });
  357. } else if (err.code == 3001) {
  358. wx.showToast({
  359. title: "您已超过限购",
  360. image: './../../../assets/images/fail.png',
  361. duration: 2000,
  362. mask: false
  363. });
  364. } else if (err.code == 3002) {
  365. wx.showToast({
  366. title: "订单失败",
  367. image: './../../../assets/images/fail.png',
  368. duration: 2000,
  369. mask: false
  370. });
  371. } else if (err.code == 3003) {
  372. wx.showToast({
  373. title: "订单不存在",
  374. image: './../../../assets/images/fail.png',
  375. duration: 2000,
  376. mask: false
  377. });
  378. } else if (err.code == 3004) {
  379. wx.showToast({
  380. title: "订单不存在",
  381. image: './../../../assets/images/fail.png',
  382. duration: 2000,
  383. mask: false
  384. });
  385. } else if (err.code == 4003) {
  386. wx.showToast({
  387. title: "卡券已作废",
  388. image: './../../../assets/images/fail.png',
  389. duration: 2000,
  390. mask: false
  391. });
  392. } else if (err.code == 3012) {
  393. wx.showToast({
  394. title: err.message,
  395. icon: "none"
  396. })
  397. } else if (err.code == 11005) {
  398. /**
  399. * 将值传到用户手机号授权的页面
  400. *
  401. */
  402. wx.redirectTo({
  403. url: "/pages/getphoneInfo/index?path=spell&couponChannelId=" +
  404. that.data.couponChannelId + '&couponId=' + that.data.couponId
  405. });
  406. } else if (err.code == 11006) {
  407. // 用户手机已加密
  408. wx.redirectTo({
  409. url: "/pages/phoneinput/phoneinput?path=spell&couponChannelId=" +
  410. that.data.couponChannelId + '&couponId=' + that.data.couponId
  411. });
  412. } else {
  413. wx.showToast({
  414. title: err.message,
  415. icon: 'none',
  416. duration: 2000,
  417. mask: false
  418. });
  419. }
  420. })
  421. .then(res => {
  422. console.log(res)
  423. if (typeof(res) != "undefined") {
  424. let orderId = "" + res.data.id;
  425. that.setData({
  426. orderId: orderId,
  427. canSpell: false,
  428. canBuyIf: true
  429. });
  430. // 支付金额不为0
  431. /**
  432. * 支付订单创建
  433. */
  434. Http.post({
  435. url: config.api.payOrderCreate,
  436. data: {
  437. orderId: orderId
  438. }
  439. })
  440. .then(res => {
  441. var payOrderId = "" + res.data.payOrderId;
  442. wx.hideLoading();
  443. wx.requestPayment({
  444. timeStamp: res.data.timeStamp,
  445. nonceStr: res.data.nonceStr,
  446. package: res.data.package,
  447. signType: (res.data.signType) ? res.data.signType : "MD5",
  448. paySign: res.data.paySign,
  449. success: res => {
  450. that.setData({
  451. canSpell: false
  452. })
  453. wx.showLoading({
  454. title: '订单正在处理中...',
  455. })
  456. setTimeout(function() {
  457. wx.hideLoading()
  458. }, 5000)
  459. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', that, orderGroupId);
  460. if (res.errMsg == "requestPayment:ok") {
  461. setTimeout(function() {
  462. wx.hideLoading();
  463. }, 2000);
  464. }
  465. },
  466. fail: res => {
  467. /**
  468. * 支付失败,需要更新订单的状态
  469. */
  470. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', that, orderGroupId);
  471. that.setData({
  472. showbutton: false,
  473. canSpell: true,
  474. canBuyIf: true
  475. })
  476. return;
  477. },
  478. complete: res => {}
  479. });
  480. /// End payment --------
  481. })
  482. .catch(err => {
  483. that.setData({
  484. canSpell: true,
  485. canBuyIf: true
  486. })
  487. wx.showToast({
  488. title: err.message,
  489. icon: 'none',
  490. duration: 2000,
  491. mask: false
  492. });
  493. })
  494. }
  495. })
  496. },
  497. /**
  498. * 生命周期函数--监听页面显示
  499. */
  500. onShow: function() {
  501. this.setData({
  502. canSpell: true,
  503. canBuyIf: true,
  504. showTime: true
  505. })
  506. if (this.data.spellData != null) {
  507. this.countdown()
  508. }
  509. },
  510. /**
  511. * 生命周期函数--监听页面隐藏
  512. */
  513. onHide: function() {
  514. this.setData({
  515. showTime: false
  516. })
  517. },
  518. /**
  519. * 生命周期函数--监听页面卸载
  520. */
  521. onUnload: function() {
  522. this.setData({
  523. showTime: false
  524. })
  525. },
  526. /**
  527. * 页面相关事件处理函数--监听用户下拉动作
  528. */
  529. onPullDownRefresh: function(e) {
  530. let that = this;
  531. that.getDetail(that.data.couponChannelId);
  532. that.getOneSpell(that.data.couponId)
  533. },
  534. onShareAppMessage: function (options) {
  535. console.log(options)
  536. var that = this;
  537. var shareObj = {
  538. title: that.data.data.title,
  539. path: `/pages/index/index?couponChannelId=${that.data.couponChannelId}&spellGroup=spellGroup`,
  540. success: function (res) {
  541. if (res.errMsg == 'shareAppMessage:ok') { }
  542. },
  543. fail: function (error) {
  544. if (res.errMsg == 'shareAppMessage:fail cancel') { } else if (res.errMsg == 'shareAppMessage:fail') { }
  545. }
  546. };
  547. // 来自页面内的按钮的转发
  548. if (options.from == 'button') {
  549. var eData = options.target.dataset.id;
  550. shareObj.path = `/pages/index/index?couponChannelId=${eData}&spellGroup=spellGroup&frommd=JC`;
  551. }
  552. // 返回shareObj
  553. return shareObj;
  554. },
  555. })