C端小程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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