C端小程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

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