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.

496 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.showToast({
  347. title: "您有未支付订单",
  348. image: './../../../assets/images/fail.png',
  349. duration: 2000,
  350. mask: false
  351. });
  352. } else if (err.code == 11005) {
  353. /**
  354. * 将值传到用户手机号授权的页面
  355. *
  356. */
  357. wx.redirectTo({
  358. url: "/pages/getphoneInfo/index?path=spell&couponChannelId=" +
  359. that.data.couponChannelId + '&couponId=' + that.data.couponId
  360. });
  361. } else if (err.code == 11006) {
  362. // 用户手机已加密
  363. wx.redirectTo({
  364. url: "/pages/phoneinput/phoneinput?path=spell&couponChannelId=" +
  365. that.data.couponChannelId + '&couponId=' + that.data.couponId
  366. });
  367. } else {
  368. wx.showToast({
  369. title: err.message,
  370. icon: 'none',
  371. duration: 2000,
  372. mask: false
  373. });
  374. }
  375. })
  376. .then(res => {
  377. console.log(res)
  378. if (typeof(res) != "undefined") {
  379. let orderId = "" + res.data.id;
  380. that.setData({
  381. orderId: orderId,
  382. canSpell: false,
  383. canBuyIf: true
  384. });
  385. // 支付金额不为0
  386. /**
  387. * 支付订单创建
  388. */
  389. Http.post({
  390. url: config.api.payOrderCreate,
  391. data: {
  392. orderId: orderId
  393. }
  394. })
  395. .then(res => {
  396. var payOrderId = "" + res.data.payOrderId;
  397. wx.hideLoading();
  398. wx.requestPayment({
  399. timeStamp: res.data.timeStamp,
  400. nonceStr: res.data.nonceStr,
  401. package: res.data.package,
  402. signType: (res.data.signType) ? res.data.signType : "MD5",
  403. paySign: res.data.paySign,
  404. success: res => {
  405. that.setData({
  406. canSpell: false
  407. })
  408. wx.showLoading({
  409. title: '订单正在处理中...',
  410. })
  411. setTimeout(function() {
  412. wx.hideLoading()
  413. }, 5000)
  414. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', that, orderGroupId);
  415. if (res.errMsg == "requestPayment:ok") {
  416. setTimeout(function() {
  417. wx.hideLoading();
  418. }, 2000);
  419. }
  420. },
  421. fail: res => {
  422. /**
  423. * 支付失败,需要更新订单的状态
  424. */
  425. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', that, orderGroupId);
  426. that.setData({
  427. showbutton: false,
  428. canSpell: true,
  429. canBuyIf: true
  430. })
  431. return;
  432. },
  433. complete: res => {}
  434. });
  435. /// End payment --------
  436. })
  437. .catch(err => {
  438. that.setData({
  439. canSpell: true,
  440. canBuyIf: true
  441. })
  442. wx.showToast({
  443. title: err.message,
  444. icon: 'none',
  445. duration: 2000,
  446. mask: false
  447. });
  448. })
  449. }
  450. })
  451. },
  452. /**
  453. * 生命周期函数--监听页面显示
  454. */
  455. onShow: function() {
  456. this.setData({
  457. canSpell: true,
  458. canBuyIf: true,
  459. showTime: true
  460. })
  461. if (this.data.spellData != null) {
  462. this.countdown()
  463. }
  464. },
  465. /**
  466. * 生命周期函数--监听页面隐藏
  467. */
  468. onHide: function() {
  469. this.setData({
  470. showTime: false
  471. })
  472. },
  473. /**
  474. * 生命周期函数--监听页面卸载
  475. */
  476. onUnload: function() {
  477. this.setData({
  478. showTime: false
  479. })
  480. },
  481. /**
  482. * 页面相关事件处理函数--监听用户下拉动作
  483. */
  484. onPullDownRefresh: function(e) {
  485. let that = this;
  486. that.getDetail(that.data.couponChannelId);
  487. that.getOneSpell(that.data.couponId)
  488. },
  489. })