C端小程序
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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