C端小程序
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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