C端小程序
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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