C端小程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

578 行
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.showModal({
  394. title: '提示',
  395. content: '您有未支付订单,请先进行支付',
  396. confirmText: "去支付",
  397. success: function (res) {
  398. console.log(res.confirm)
  399. if (res.confirm) {
  400. console.log(that.data.isFromSpell)
  401. if (that.data.isFromSpell){
  402. wx.navigateTo({
  403. url: '/pages/spellGroup/spellGroup?from=myspellList',
  404. })
  405. }else{
  406. wx.navigateTo({
  407. url: '/pages/order/index/index?id=all',
  408. })
  409. }
  410. }
  411. }
  412. })
  413. } else if (err.code == 11005) {
  414. /**
  415. * 将值传到用户手机号授权的页面
  416. *
  417. */
  418. wx.redirectTo({
  419. url: "/pages/getphoneInfo/index?path=spell&couponChannelId=" +
  420. that.data.couponChannelId + '&couponId=' + that.data.couponId
  421. });
  422. } else if (err.code == 11006) {
  423. // 用户手机已加密
  424. wx.redirectTo({
  425. url: "/pages/phoneinput/phoneinput?path=spell&couponChannelId=" +
  426. that.data.couponChannelId + '&couponId=' + that.data.couponId
  427. });
  428. } else {
  429. wx.showToast({
  430. title: err.message,
  431. icon: 'none',
  432. duration: 2000,
  433. mask: false
  434. });
  435. }
  436. })
  437. .then(res => {
  438. console.log(res)
  439. if (typeof(res) != "undefined") {
  440. let orderId = "" + res.data.id;
  441. that.setData({
  442. orderId: orderId,
  443. canSpell: false,
  444. canBuyIf: true
  445. });
  446. // 支付金额不为0
  447. /**
  448. * 支付订单创建
  449. */
  450. Http.post({
  451. url: config.api.payOrderCreate,
  452. data: {
  453. orderId: orderId
  454. }
  455. })
  456. .then(res => {
  457. var payOrderId = "" + res.data.payOrderId;
  458. wx.hideLoading();
  459. wx.requestPayment({
  460. timeStamp: res.data.timeStamp,
  461. nonceStr: res.data.nonceStr,
  462. package: res.data.package,
  463. signType: (res.data.signType) ? res.data.signType : "MD5",
  464. paySign: res.data.paySign,
  465. success: res => {
  466. that.setData({
  467. canSpell: false
  468. })
  469. wx.showLoading({
  470. title: '订单正在处理中...',
  471. })
  472. setTimeout(function() {
  473. wx.hideLoading()
  474. }, 5000)
  475. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', that, orderGroupId);
  476. if (res.errMsg == "requestPayment:ok") {
  477. setTimeout(function() {
  478. wx.hideLoading();
  479. }, 2000);
  480. }
  481. },
  482. fail: res => {
  483. /**
  484. * 支付失败,需要更新订单的状态
  485. */
  486. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', that, orderGroupId);
  487. that.setData({
  488. showbutton: false,
  489. canSpell: true,
  490. canBuyIf: true
  491. })
  492. return;
  493. },
  494. complete: res => {}
  495. });
  496. /// End payment --------
  497. })
  498. .catch(err => {
  499. that.setData({
  500. canSpell: true,
  501. canBuyIf: true
  502. })
  503. wx.showToast({
  504. title: err.message,
  505. icon: 'none',
  506. duration: 2000,
  507. mask: false
  508. });
  509. })
  510. }
  511. })
  512. },
  513. /**
  514. * 生命周期函数--监听页面显示
  515. */
  516. onShow: function() {
  517. this.setData({
  518. canSpell: true,
  519. canBuyIf: true,
  520. showTime: true
  521. })
  522. if (this.data.spellData != null) {
  523. this.countdown()
  524. }
  525. },
  526. /**
  527. * 生命周期函数--监听页面隐藏
  528. */
  529. onHide: function() {
  530. this.setData({
  531. showTime: false
  532. })
  533. },
  534. /**
  535. * 生命周期函数--监听页面卸载
  536. */
  537. onUnload: function() {
  538. this.setData({
  539. showTime: false
  540. })
  541. },
  542. /**
  543. * 页面相关事件处理函数--监听用户下拉动作
  544. */
  545. onPullDownRefresh: function(e) {
  546. let that = this;
  547. that.getDetail(that.data.couponChannelId);
  548. that.getOneSpell(that.data.couponId)
  549. },
  550. onShareAppMessage: function (options) {
  551. console.log(options)
  552. var that = this;
  553. var shareObj = {
  554. title: that.data.data.title,
  555. path: `/pages/index/index?couponChannelId=${that.data.couponChannelId}&spellGroup=spellGroup`,
  556. success: function (res) {
  557. if (res.errMsg == 'shareAppMessage:ok') { }
  558. },
  559. fail: function (error) {
  560. if (res.errMsg == 'shareAppMessage:fail cancel') { } else if (res.errMsg == 'shareAppMessage:fail') { }
  561. }
  562. };
  563. // 来自页面内的按钮的转发
  564. if (options.from == 'button') {
  565. var eData = options.target.dataset.id;
  566. shareObj.path = `/pages/index/index?couponChannelId=${eData}&spellGroup=spellGroup&frommd=JC`;
  567. }
  568. // 返回shareObj
  569. return shareObj;
  570. },
  571. })