C端小程序
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

570 rader
15 KiB

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