util.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import {
  2. base
  3. } from "../config/index"
  4. export const formatTime = date => {
  5. const year = date.getFullYear()
  6. const month = date.getMonth() + 1
  7. const day = date.getDate()
  8. const hour = date.getHours()
  9. const minute = date.getMinutes()
  10. const second = date.getSeconds()
  11. return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  12. }
  13. const formatNumber = n => {
  14. n = n.toString()
  15. return n[1] ? n : `0${n}`
  16. }
  17. export const ajax = data => {
  18. return new Promise((resolve, reject) => {
  19. const t = setTimeout(() => {
  20. clearTimeout(t);
  21. wx.showLoading({
  22. title: '加载中...',
  23. mask: true
  24. })
  25. }, 200);
  26. wx.request({
  27. url: base[data.urlType || 'apiurl'] + (data.api || ''),
  28. data: data.data,
  29. header: data.header,
  30. timeout: data.timeout || 60000,
  31. method: (data.method || 'get').toUpperCase(),
  32. responseType: data.responseType || 'text',
  33. enableCache: data.enableCache || false,
  34. success(res) {
  35. if (res.statusCode !== 200) {
  36. wx.showToast({
  37. title: '请求失败',
  38. icon: 'none'
  39. })
  40. return
  41. }
  42. resolve(res.data)
  43. },
  44. fail(err) {
  45. reject(err)
  46. },
  47. complete() {
  48. t && clearTimeout(t);
  49. wx.hideLoading();
  50. }
  51. })
  52. })
  53. }
  54. export const ele_height = (e, rWidth) => {
  55. const {
  56. detail: {
  57. width,
  58. height
  59. }
  60. } = e;
  61. const h = rWidth / width * height;
  62. return h
  63. }