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.

525 lines
14 KiB

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