C端小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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