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.

589 regels
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. if (total_micro_second > 0) {
  195. that.setData({
  196. clock: obj,
  197. day: obj.a1,
  198. hour: obj.b1,
  199. min: obj.c1,
  200. sec: obj.d1,
  201. })
  202. } else {
  203. that.setData({
  204. clock: "00",
  205. day: "00",
  206. hour: "00",
  207. min: "00",
  208. sec: "00",
  209. })
  210. //如果倒计时结束,需要重新查询一下券的状态
  211. // 给getDetail一个标识
  212. }
  213. total_micro_second -= 1000;
  214. },
  215. countdown: function (end_time) {
  216. let that = this;
  217. that.setIntervalTime(end_time);
  218. that.data.setInterval = setInterval(function () {
  219. that.setIntervalTime(end_time);
  220. }, 1000)
  221. },
  222. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  223. dateformat(micro_second) {
  224. // 总秒数
  225. var second = Math.floor(micro_second / 1000);
  226. // 天数
  227. var day = Math.floor(second / 3600 / 24) < 10 ? "0" + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
  228. // 小时
  229. var hr = Math.floor(second / 3600 % 24) < 10 ? "0" + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
  230. // 分钟
  231. var min = Math.floor(second / 60 % 60) < 10 ? "0" + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
  232. // 秒
  233. var sec = Math.floor(second % 60) < 10 ? "0" + Math.floor(second % 60) : Math.floor(second % 60);
  234. // return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  235. return {
  236. a1: day,
  237. b1: hr,
  238. c1: min,
  239. d1: sec
  240. }
  241. },
  242. payOrderUpdate: (orderId, payOrderId, status, reason, type, _this) => {
  243. let that = this;
  244. console.log(that)
  245. console.log(_this)
  246. // 支付成功
  247. Http.post({
  248. url: config.api.payOrderUpdate,
  249. data: {
  250. payOrderId: payOrderId,
  251. orderId: orderId,
  252. status: status,
  253. reason: reason
  254. }
  255. })
  256. .then(res => {
  257. wx.hideLoading();
  258. _this.setData({
  259. display: "none!important",
  260. })
  261. // 有价券
  262. if (!type && type != 'free') {
  263. wx.navigateTo({
  264. url: `/pages/order/detail/index?orderId=${
  265. orderId
  266. }`
  267. });
  268. } else if (type == 'free') {
  269. wx.navigateTo({
  270. url: `/pages/order/detail/index?orderId=${
  271. orderId
  272. }`
  273. });
  274. }
  275. })
  276. .catch(err => {
  277. console.log(err);
  278. if (!type) {
  279. setTimeout(function() {
  280. _this.payOrderUpdate(orderId, payOrderId, status, reason, type, _this);
  281. }, 2000)
  282. }
  283. })
  284. },
  285. /**
  286. * 发起支付
  287. */
  288. orderFunc(e) {
  289. var that = this;
  290. that.setData({
  291. showButton:true
  292. })
  293. Http.get({
  294. url: config.api.checkPhoneStatus,
  295. data: {}
  296. })
  297. .then(res => {
  298. that.setData({
  299. showButton: false
  300. })
  301. if (typeof(res) != "undefined") {
  302. let orderId = "" + e.currentTarget.dataset.orderid;
  303. that.setData({
  304. orderId: orderId
  305. });
  306. if (e.currentTarget.dataset.saleprice / 100 > 0 || e.currentTarget.dataset.price / 100 > 0) {
  307. /**
  308. * 支付订单创建
  309. */
  310. Http.post({
  311. url: config.api.payOrderCreate,
  312. data: {
  313. orderId: orderId
  314. }
  315. })
  316. .then(res => {
  317. var payOrderId = "" + res.data.payOrderId;
  318. wx.hideLoading();
  319. wx.requestPayment({
  320. timeStamp: res.data.timeStamp,
  321. nonceStr: res.data.nonceStr,
  322. package: res.data.package,
  323. signType: (res.data.signType) ? res.data.signType : "MD5",
  324. paySign: res.data.paySign,
  325. success: res => {
  326. wx.showLoading({
  327. title: '订单正在处理中...',
  328. })
  329. setTimeout(function() {
  330. wx.hideLoading()
  331. }, 5000)
  332. that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', '', that);
  333. if (res.errMsg == "requestPayment:ok") {
  334. setTimeout(function() {
  335. wx.hideLoading();
  336. }, 2000);
  337. /**
  338. * 用户支付成功以后跳转到券包列表
  339. */
  340. if (that.data.cardType == 100) {
  341. wx.setStorage({
  342. key: 'couponNum2',
  343. data: "couponNum2"
  344. })
  345. } else {
  346. wx.setStorage({
  347. key: 'couponNum',
  348. data: "couponNum"
  349. })
  350. }
  351. }
  352. },
  353. fail: res => {
  354. /**
  355. * 支付失败,需要更新订单的状态
  356. */
  357. that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', 'fail', that);
  358. that.setData({
  359. showbutton: false
  360. })
  361. return;
  362. },
  363. complete: res => {}
  364. });
  365. /// End payment --------
  366. })
  367. .catch(err => {
  368. console.log(err);
  369. wx.showToast({
  370. title: err.message,
  371. icon: 'none',
  372. duration: 2000,
  373. mask: false
  374. });
  375. })
  376. } else {
  377. // 免费券
  378. that.payOrderUpdate(orderId, "0", 1, '', 'free',that);
  379. if (that.data.cardType == 100) {
  380. wx.setStorage({
  381. key: 'couponNum2',
  382. data: "couponNum2"
  383. })
  384. } else {
  385. wx.setStorage({
  386. key: 'couponNum',
  387. data: "couponNum"
  388. })
  389. }
  390. }
  391. }
  392. })
  393. .catch(err => {
  394. that.setData({
  395. showButton: true
  396. })
  397. if (err.code == 2011) {
  398. wx.showToast({
  399. title: "商户信息没找到",
  400. image: './../../../assets/images/fail.png',
  401. duration: 2000,
  402. mask: false
  403. });
  404. } else if (err.code == 2013) {
  405. wx.showToast({
  406. title: "商户信息禁用",
  407. image: './../../../assets/images/fail.png',
  408. duration: 2000,
  409. mask: false
  410. });
  411. } else if (err.code == 3000) {
  412. wx.showToast({
  413. title: "库存不足",
  414. image: './../../../assets/images/fail.png',
  415. duration: 2000,
  416. mask: false
  417. });
  418. } else if (err.code == 3001) {
  419. wx.showToast({
  420. title: "超过限购条件",
  421. image: './../../../assets/images/fail.png',
  422. duration: 2000,
  423. mask: false
  424. });
  425. } else if (err.code == 3002) {
  426. wx.showToast({
  427. title: "订单失败",
  428. image: './../../../assets/images/fail.png',
  429. duration: 2000,
  430. mask: false
  431. });
  432. } else if (err.code == 3003) {
  433. wx.showToast({
  434. title: "订单不存在",
  435. image: './../../../assets/images/fail.png',
  436. duration: 2000,
  437. mask: false
  438. });
  439. } else if (err.code == 3004) {
  440. wx.showToast({
  441. title: "订单不存在",
  442. image: './../../../assets/images/fail.png',
  443. duration: 2000,
  444. mask: false
  445. });
  446. } else if (err.code == 4003) {
  447. wx.showToast({
  448. title: "卡券已作废",
  449. image: './../../../assets/images/fail.png',
  450. duration: 2000,
  451. mask: false
  452. });
  453. } else if (err.code == 11005) {
  454. /**
  455. * 将值传到用户手机号授权的页面
  456. *
  457. */
  458. wx.redirectTo({
  459. url: "/pages/getphoneInfo/index?couponChannelId=" +
  460. that.data.couponChannelId +
  461. "&couponId=" +
  462. that.data.couponId
  463. });
  464. } else if (err.code == 11006) {
  465. // 用户手机已加密
  466. wx.redirectTo({
  467. url: "/pages/phoneinput/phoneinput?couponChannelId=" +
  468. that.data.couponChannelId +
  469. "&couponId=" +
  470. that.data.couponId
  471. });
  472. } else {
  473. wx.showToast({
  474. title: err.message,
  475. icon: 'none',
  476. duration: 2000,
  477. mask: false
  478. });
  479. }
  480. })
  481. },
  482. onUnload: function () {
  483. let that = this;
  484. clearInterval(that.data.setInterval)
  485. },
  486. onHide: function () {
  487. let that = this;
  488. clearInterval(that.data.setInterval)
  489. },
  490. /**
  491. *
  492. * @param {朋友邀请砍价} e
  493. */
  494. barginAgain: function(e) {
  495. let that = this;
  496. let couponChannelId = e.currentTarget.dataset.couponchannelid;
  497. let couponId = e.currentTarget.dataset.couponid;
  498. wx.navigateTo({
  499. url: `/pages/coupon/detail/index?couponChannelId=${couponChannelId}&couponId=${couponId}`,
  500. })
  501. },
  502. /**
  503. * 刷新
  504. */
  505. onPullDownRefresh: function (e) {
  506. let that = this;
  507. if (that.data.orderId || that.data.orderId && that.data.from == 'discount') {
  508. if (that.data.from) {
  509. that.setData({
  510. orderId: that.data.orderId,
  511. from: "discount"
  512. })
  513. } else {
  514. that.setData({
  515. orderId: that.data.orderId
  516. })
  517. }
  518. that.pressOrderDetail(that.data.orderId);
  519. }
  520. },
  521. onShareAppMessage: function(options) {
  522. console.log(options)
  523. var that = this;
  524. console.log(that.data.data.id)
  525. var shareObj = {
  526. title: that.data.nickName + '邀请您帮砍' + that.data.data.title,
  527. path: `/pages/index/index?orderId=${that.data.data.id}&from=${"discount"}`,
  528. success: function(res) {
  529. if (res.errMsg == 'shareAppMessage:ok') {}
  530. },
  531. fail: function(error) {
  532. if (res.errMsg == 'shareAppMessage:fail cancel') {} else if (res.errMsg == 'shareAppMessage:fail') {}
  533. }
  534. };
  535. // 来自页面内的按钮的转发
  536. if (options.from == 'button') {
  537. var orderId = options.target.dataset.orderid;
  538. shareObj.path = `/pages/index/index?orderId=${orderId}&from=${"discount"}`;
  539. }
  540. // 返回shareObj
  541. return shareObj;
  542. },
  543. })
  544. function diffTime(startDate) {
  545. var diff = new Date().getTime() - startDate;
  546. var days = Math.floor(diff / (24 * 3600 * 1000));
  547. var leave1 = diff % (24 * 3600 * 1000);
  548. var hours = Math.floor(leave1 / (3600 * 1000));
  549. var leave2 = leave1 % (3600 * 1000);
  550. var minutes = Math.floor(leave2 / (60 * 1000));
  551. var leave3 = leave2 % (60 * 1000);
  552. var seconds = Math.round(leave3 / 1000);
  553. var returnStr = seconds + "秒";
  554. if (minutes > 0) {
  555. returnStr = minutes + "分" + returnStr;
  556. }
  557. if (hours > 0) {
  558. returnStr = hours + "小时" + returnStr;
  559. }
  560. if (days > 0) {
  561. returnStr = days + "天" + returnStr;
  562. }
  563. if (returnStr.indexOf('-1')==-1){
  564. return returnStr;
  565. }else{
  566. return '1秒'
  567. }
  568. }
  569. function compare(property) {
  570. return function (a, b) {
  571. var value1 = a[property];
  572. var value2 = b[property];
  573. return value2 - value1;
  574. }
  575. }