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.

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