util.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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: {
  30. Authorization: wx.getStorageSync('token') || undefined,
  31. ...(data.header || {})
  32. },
  33. timeout: data.timeout || 60000,
  34. method: (data.method || 'get').toUpperCase(),
  35. responseType: data.responseType || 'text',
  36. enableCache: data.enableCache || false,
  37. success(res) {
  38. if (res.statusCode !== 200) {
  39. wx.showToast({
  40. title: '请求失败',
  41. icon: 'none'
  42. })
  43. return
  44. }
  45. resolve(res.data)
  46. },
  47. fail(err) {
  48. reject(err)
  49. },
  50. complete() {
  51. t && clearTimeout(t);
  52. wx.hideLoading();
  53. }
  54. })
  55. })
  56. }
  57. export const ele_height = (e, rWidth) => {
  58. const {
  59. detail: {
  60. width,
  61. height
  62. }
  63. } = e;
  64. const h = rWidth / width * height;
  65. return h
  66. }