C端小程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

590 строки
16 KiB

  1. var config = require("../../../config/config.js");
  2. var app = getApp();
  3. const Http = require("../../../utils/HttpBasics");
  4. const util = require("../../../utils/util");
  5. const imgurl = require("../../../utils/imgurl");
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. bannerUrl: imgurl.banner.url,
  12. data: null,
  13. from: null,
  14. day: null,
  15. hour: null,
  16. min: null,
  17. sec: null,
  18. showPage: false,
  19. discountStatus:null,
  20. disabled:false,
  21. display:"block!important",
  22. showButton: false
  23. },
  24. /**
  25. * 生命周期函数--监听页面加载
  26. */
  27. onLoad: function(options) {
  28. let that = this;
  29. console.log(options)
  30. if (options.orderId || options.orderId && options.from == 'discount') {
  31. if (options.from) {
  32. that.setData({
  33. orderId: options.orderId,
  34. from: "discount"
  35. })
  36. } else {
  37. that.setData({
  38. orderId: options.orderId
  39. })
  40. }
  41. that.pressOrderDetail(options.orderId);
  42. }
  43. this.getUserInfo()
  44. },
  45. getUserInfo: function () {
  46. let that = this;
  47. // 获取用户信息
  48. Http.get({
  49. url: config.api.getScore,
  50. data: {}
  51. })
  52. .then(res => {
  53. console.log(res)
  54. that.setData({
  55. nickName: res.data.nickName,
  56. avatarUrl: res.data.avatarUrl
  57. })
  58. })
  59. },
  60. onShow: function () {
  61. let that = this;
  62. console.log("--------------------------------onShow-------------------------------")
  63. var todayDate = new Date().getTime();
  64. that.setData({
  65. todayDate: todayDate,
  66. showButton:false
  67. })
  68. if(that.data.pressEndDate){
  69. that.countdown(that.data.pressEndDate)
  70. }
  71. },
  72. goback:function(){
  73. wx.switchTab({
  74. url: '/pages/main/index',
  75. })
  76. },
  77. /**
  78. * 更多砍价商品
  79. */
  80. gotomore: function() {
  81. wx.navigateTo({
  82. url: '/pages/bargain/bargain',
  83. })
  84. },
  85. /**
  86. * @param {砍价状态(1:我发起的砍价,2:未参与的砍价, 3:已参与的砍价)} orderId
  87. */
  88. getPressOrderStatus: function(orderId) {
  89. let that = this;
  90. Http.get({
  91. url: config.api.getPressOrderStatus,
  92. data: {
  93. orderId: orderId
  94. }
  95. })
  96. .then(res => {
  97. console.log(res)
  98. console.log("--------------------------------status------------------------------------------")
  99. if (res.data.status == 1) {
  100. that.setData({
  101. discountStatus: 1
  102. })
  103. } else if (res.data.status == 2) {
  104. that.setData({
  105. discountStatus: 2
  106. })
  107. } else if (res.data.status == 3) {
  108. that.setData({
  109. discountStatus: 3
  110. })
  111. }
  112. wx.stopPullDownRefresh();
  113. })
  114. .catch(err => {
  115. console.log(err)
  116. wx.stopPullDownRefresh();
  117. wx.showToast({
  118. title: err.errMsg,
  119. icon: "none"
  120. })
  121. })
  122. },
  123. helpDiscount: function() {
  124. let that = this;
  125. that.pressOrderJoin(that.data.orderId);
  126. },
  127. //参与砍价
  128. pressOrderJoin: function(orderId) {
  129. let that = this;
  130. Http.post({
  131. url: config.api.pressOrderJoin,
  132. data: {
  133. orderId: orderId
  134. }
  135. })
  136. .then(res => {
  137. console.log(res);
  138. // id 是订单号
  139. that.pressOrderDetail(orderId);
  140. })
  141. .catch(err => {
  142. console.log(err)
  143. wx.showToast({
  144. title: err.message,
  145. icon: "none"
  146. })
  147. })
  148. },
  149. // 订单详情
  150. pressOrderDetail: function(orderId) {
  151. let that = this;
  152. Http.get({
  153. url: config.api.pressOrderDetail,
  154. data: {
  155. orderId: orderId
  156. }
  157. })
  158. .then(res => {
  159. console.log(res);
  160. res.data.orderPressList.map(file => {
  161. file.createDate = diffTime(file.createDate)
  162. })
  163. that.countdown(res.data.pressEndDate);
  164. that.setData({
  165. pressEndDate: res.data.pressEndDate
  166. })
  167. res.data.orderPressList.map(file=>{
  168. compare(file.first);
  169. })
  170. console.log(res.data.orderPressList.sort(compare('first')))
  171. that.setData({
  172. data: res.data,
  173. already: (res.data.price / 100 - res.data.salePrice / 100 - res.data.pressCurrentValue / 100).toFixed(2),
  174. remain: (res.data.pressCurrentValue / 100).toFixed(2),
  175. showPage: true
  176. })
  177. that.getPressOrderStatus(orderId);
  178. })
  179. .catch(err => {
  180. wx.showToast({
  181. title: err.message,
  182. icon: "none"
  183. })
  184. wx.stopPullDownRefresh();
  185. })
  186. },
  187. setIntervalTime(end_time) {
  188. let that = this;
  189. var EndTime = end_time;
  190. var NowTime = new Date().getTime();
  191. var total_micro_second = EndTime - NowTime || [];
  192. // 渲染倒计时时钟
  193. let obj = that.dateformat(total_micro_second);
  194. console.log(obj);
  195. if (total_micro_second > 0) {
  196. that.setData({
  197. clock: obj,
  198. day: obj.a1,
  199. hour: obj.b1,
  200. min: obj.c1,
  201. sec: obj.d1,
  202. })
  203. } else {
  204. that.setData({
  205. clock: "00",
  206. day: "00",
  207. hour: "00",
  208. min: "00",
  209. sec: "00",
  210. })
  211. //如果倒计时结束,需要重新查询一下券的状态
  212. // 给getDetail一个标识
  213. }
  214. total_micro_second -= 1000;
  215. },
  216. countdown: function (end_time) {
  217. let that = this;
  218. that.setIntervalTime(end_time);
  219. that.data.setInterval = setInterval(function () {
  220. that.setIntervalTime(end_time);
  221. }, 1000)
  222. },
  223. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  224. dateformat(micro_second) {
  225. // 总秒数
  226. var second = Math.floor(micro_second / 1000);
  227. // 天数
  228. var day = Math.floor(second / 3600 / 24) < 10 ? "0" + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
  229. // 小时
  230. var hr = Math.floor(second / 3600 % 24) < 10 ? "0" + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
  231. // 分钟
  232. var min = Math.floor(second / 60 % 60) < 10 ? "0" + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
  233. // 秒
  234. var sec = Math.floor(second % 60) < 10 ? "0" + Math.floor(second % 60) : Math.floor(second % 60);
  235. // return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  236. return {
  237. a1: day,
  238. b1: hr,
  239. c1: min,
  240. d1: sec
  241. }
  242. },
  243. payOrderUpdate: (orderId, payOrderId, status, reason, type, _this) => {
  244. let that = this;
  245. console.log(that)
  246. console.log(_this)
  247. // 支付成功
  248. Http.post({
  249. url: config.api.payOrderUpdate,
  250. data: {
  251. payOrderId: payOrderId,
  252. orderId: orderId,
  253. status: status,
  254. reason: reason
  255. }
  256. })
  257. .then(res => {
  258. wx.hideLoading();
  259. _this.setData({
  260. display: "none!important",
  261. })
  262. // 有价券
  263. if (!type && type != 'free') {
  264. wx.navigateTo({
  265. url: `/pages/order/detail/index?orderId=${
  266. orderId
  267. }`
  268. });
  269. } else if (type == 'free') {
  270. wx.navigateTo({
  271. url: `/pages/order/detail/index?orderId=${
  272. orderId
  273. }`
  274. });
  275. }
  276. })
  277. .catch(err => {
  278. console.log(err);
  279. if (!type) {
  280. setTimeout(function() {
  281. _this.payOrderUpdate(orderId, payOrderId, status, reason, type, _this);
  282. }, 2000)
  283. }
  284. })
  285. },
  286. /**
  287. * 发起支付
  288. */
  289. orderFunc(e) {
  290. var that = this;
  291. that.setData({
  292. showButton:true
  293. })
  294. Http.post({
  295. url: config.api.checkPhoneStatus,
  296. data: {}
  297. })
  298. .then(res => {
  299. that.setData({
  300. showButton: false
  301. })
  302. if (typeof(res) != "undefined") {
  303. let orderId = "" + e.currentTarget.dataset.orderid;
  304. that.setData({
  305. orderId: orderId
  306. });
  307. if (e.currentTarget.dataset.saleprice / 100 > 0 || e.currentTarget.dataset.price / 100 > 0) {
  308. /**
  309. * 支付订单创建
  310. */
  311. Http.post({
  312. url: config.api.payOrderCreate,
  313. data: {
  314. orderId: orderId
  315. }
  316. })
  317. .then(res => {
  318. var payOrderId = "" + res.data.payOrderId;
  319. wx.hideLoading();
  320. wx.requestPayment({
  321. timeStamp: res.data.timeStamp,
  322. nonceStr: res.data.nonceStr,
  323. package: res.data.package,
  324. signType: (res.data.signType) ? res.data.signType : "MD5",
  325. paySign: res.data.paySign,
  326. success: res => {
  327. wx.showLoading({
  328. title: '订单正在处理中...',
  329. })
  330. setTimeout(function() {
  331. wx.hideLoading()
  332. }, 5000)
  333. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', '', that);
  334. if (res.errMsg == "requestPayment:ok") {
  335. setTimeout(function() {
  336. wx.hideLoading();
  337. }, 2000);
  338. /**
  339. * 用户支付成功以后跳转到券包列表
  340. */
  341. if (that.data.cardType == 100) {
  342. wx.setStorage({
  343. key: 'couponNum2',
  344. data: "couponNum2"
  345. })
  346. } else {
  347. wx.setStorage({
  348. key: 'couponNum',
  349. data: "couponNum"
  350. })
  351. }
  352. }
  353. },
  354. fail: res => {
  355. /**
  356. * 支付失败,需要更新订单的状态
  357. */
  358. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', 'fail', that);
  359. that.setData({
  360. showbutton: false
  361. })
  362. return;
  363. },
  364. complete: res => {}
  365. });
  366. /// End payment --------
  367. })
  368. .catch(err => {
  369. console.log(err);
  370. wx.showToast({
  371. title: err.message,
  372. icon: 'none',
  373. duration: 2000,
  374. mask: false
  375. });
  376. })
  377. } else {
  378. // 免费券
  379. that.payOrderUpdate(orderId, "0", 1, '', 'free',that);
  380. if (that.data.cardType == 100) {
  381. wx.setStorage({
  382. key: 'couponNum2',
  383. data: "couponNum2"
  384. })
  385. } else {
  386. wx.setStorage({
  387. key: 'couponNum',
  388. data: "couponNum"
  389. })
  390. }
  391. }
  392. }
  393. })
  394. .catch(err => {
  395. that.setData({
  396. showButton: true
  397. })
  398. if (err.code == 2011) {
  399. wx.showToast({
  400. title: "商户信息没找到",
  401. image: './../../../assets/images/fail.png',
  402. duration: 2000,
  403. mask: false
  404. });
  405. } else if (err.code == 2013) {
  406. wx.showToast({
  407. title: "商户信息禁用",
  408. image: './../../../assets/images/fail.png',
  409. duration: 2000,
  410. mask: false
  411. });
  412. } else if (err.code == 3000) {
  413. wx.showToast({
  414. title: "库存不足",
  415. image: './../../../assets/images/fail.png',
  416. duration: 2000,
  417. mask: false
  418. });
  419. } else if (err.code == 3001) {
  420. wx.showToast({
  421. title: "超过限购条件",
  422. image: './../../../assets/images/fail.png',
  423. duration: 2000,
  424. mask: false
  425. });
  426. } else if (err.code == 3002) {
  427. wx.showToast({
  428. title: "订单失败",
  429. image: './../../../assets/images/fail.png',
  430. duration: 2000,
  431. mask: false
  432. });
  433. } else if (err.code == 3003) {
  434. wx.showToast({
  435. title: "订单不存在",
  436. image: './../../../assets/images/fail.png',
  437. duration: 2000,
  438. mask: false
  439. });
  440. } else if (err.code == 3004) {
  441. wx.showToast({
  442. title: "订单不存在",
  443. image: './../../../assets/images/fail.png',
  444. duration: 2000,
  445. mask: false
  446. });
  447. } else if (err.code == 4003) {
  448. wx.showToast({
  449. title: "卡券已作废",
  450. image: './../../../assets/images/fail.png',
  451. duration: 2000,
  452. mask: false
  453. });
  454. } else if (err.code == 11005) {
  455. /**
  456. * 将值传到用户手机号授权的页面
  457. *
  458. */
  459. wx.redirectTo({
  460. url: "/pages/getphoneInfo/index?couponChannelId=" +
  461. that.data.couponChannelId +
  462. "&couponId=" +
  463. that.data.couponId
  464. });
  465. } else if (err.code == 11006) {
  466. // 用户手机已加密
  467. wx.redirectTo({
  468. url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  469. that.data.couponChannelId +
  470. "&couponId=" +
  471. that.data.couponId
  472. });
  473. } else {
  474. wx.showToast({
  475. title: err.message,
  476. icon: 'none',
  477. duration: 2000,
  478. mask: false
  479. });
  480. }
  481. })
  482. },
  483. onUnload: function () {
  484. let that = this;
  485. clearInterval(that.data.setInterval)
  486. },
  487. onHide: function () {
  488. let that = this;
  489. clearInterval(that.data.setInterval)
  490. },
  491. /**
  492. *
  493. * @param {朋友邀请砍价} e
  494. */
  495. barginAgain: function(e) {
  496. let that = this;
  497. let couponChannelId = e.currentTarget.dataset.couponchannelid;
  498. let couponId = e.currentTarget.dataset.couponid;
  499. wx.navigateTo({
  500. url: `/pages/coupon/detail/index?couponChannelId=${couponChannelId}&couponId=${couponId}`,
  501. })
  502. },
  503. /**
  504. * 刷新
  505. */
  506. onPullDownRefresh: function (e) {
  507. let that = this;
  508. if (that.data.orderId || that.data.orderId && that.data.from == 'discount') {
  509. if (that.data.from) {
  510. that.setData({
  511. orderId: that.data.orderId,
  512. from: "discount"
  513. })
  514. } else {
  515. that.setData({
  516. orderId: that.data.orderId
  517. })
  518. }
  519. that.pressOrderDetail(that.data.orderId);
  520. }
  521. },
  522. onShareAppMessage: function(options) {
  523. console.log(options)
  524. var that = this;
  525. console.log(that.data.data.id)
  526. var shareObj = {
  527. title: that.data.nickName + '邀请您帮砍' + that.data.data.title,
  528. path: `/pages/index/index?orderId=${that.data.data.id}&from=${"discount"}`,
  529. success: function(res) {
  530. if (res.errMsg == 'shareAppMessage:ok') {}
  531. },
  532. fail: function(error) {
  533. if (res.errMsg == 'shareAppMessage:fail cancel') {} else if (res.errMsg == 'shareAppMessage:fail') {}
  534. }
  535. };
  536. // 来自页面内的按钮的转发
  537. if (options.from == 'button') {
  538. var orderId = options.target.dataset.orderid;
  539. shareObj.path = `/pages/index/index?orderId=${orderId}&from=${"discount"}`;
  540. }
  541. // 返回shareObj
  542. return shareObj;
  543. },
  544. })
  545. function diffTime(startDate) {
  546. var diff = new Date().getTime() - startDate;
  547. var days = Math.floor(diff / (24 * 3600 * 1000));
  548. var leave1 = diff % (24 * 3600 * 1000);
  549. var hours = Math.floor(leave1 / (3600 * 1000));
  550. var leave2 = leave1 % (3600 * 1000);
  551. var minutes = Math.floor(leave2 / (60 * 1000));
  552. var leave3 = leave2 % (60 * 1000);
  553. var seconds = Math.round(leave3 / 1000);
  554. var returnStr = seconds + "秒";
  555. if (minutes > 0) {
  556. returnStr = minutes + "分" + returnStr;
  557. }
  558. if (hours > 0) {
  559. returnStr = hours + "小时" + returnStr;
  560. }
  561. if (days > 0) {
  562. returnStr = days + "天" + returnStr;
  563. }
  564. if (returnStr.indexOf('-1')==-1){
  565. return returnStr;
  566. }else{
  567. return '1秒'
  568. }
  569. }
  570. function compare(property) {
  571. return function (a, b) {
  572. var value1 = a[property];
  573. var value2 = b[property];
  574. return value2 - value1;
  575. }
  576. }