util.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import Ajax from './ajax'
  2. import api from './api'
  3. // import { Message } from 'element-ui'
  4. // import store from '../store'
  5. const toolClass = {
  6. dateFormat: (timestamp, fmt) => {
  7. if (timestamp === '0001-01-01T00:00:00Z') {
  8. return ''
  9. }
  10. if (!fmt) {
  11. fmt = 'yyyy-MM-dd hh:mm'
  12. }
  13. let date = new Date(timestamp)
  14. var o = {
  15. 'M+': date.getMonth() + 1,
  16. 'd+': date.getDate(),
  17. 'h+': date.getHours(),
  18. 'm+': date.getMinutes(),
  19. 's+': date.getSeconds(),
  20. 'q+': Math.floor((date.getMonth() + 3) / 3),
  21. 'S': date.getMilliseconds()
  22. }
  23. if (/(y+)/.test(fmt)) {
  24. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  25. }
  26. for (var k in o) {
  27. if (new RegExp('(' + k + ')').test(fmt)) {
  28. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  29. }
  30. }
  31. return fmt
  32. },
  33. ReplaceOrg (url) {
  34. return url.replace(':org', 'MQ')
  35. },
  36. upload (item) {
  37. return new Promise((resolve, reject) => {
  38. Ajax({
  39. ...api.file.image,
  40. data: { file: item.file },
  41. }).then((res) => {
  42. let result = {
  43. result: res
  44. }
  45. if (item.onSuccess) {
  46. item.onSuccess(result, item.file)
  47. }
  48. // console.log(result)
  49. resolve(result)
  50. }).catch(reject)
  51. })
  52. },
  53. beforeUpload (file) {
  54. return true
  55. // return new Promise((resolve, reject) => {
  56. // let w = store.state.app.picSize.w
  57. // let h = store.state.app.picSize.h
  58. // if ('image'.indexOf(file.type) > -1) {
  59. // Message({
  60. // type: 'error',
  61. // message: `请上传${w}像素*${h}像素尺寸的图片`
  62. // })
  63. // reject(new Error('error'))
  64. // }
  65. // var reader = new FileReader()
  66. // reader.onload = function (e) {
  67. // var data = e.target.result
  68. // // 加载图片获取图片真实宽度和高度
  69. // var image = new Image()
  70. // image.onload = function () {
  71. // var width = image.width
  72. // var height = image.height
  73. // console.log(width)
  74. // console.log(height)
  75. // if (width === w && height === h) {
  76. // resolve()
  77. // } else {
  78. // Message({
  79. // type: 'error',
  80. // message: `请上传${w}像素*${h}像素尺寸的图片`
  81. // })
  82. // reject(new Error('error'))
  83. // }
  84. // }
  85. // image.src = data
  86. // }
  87. // reader.readAsDataURL(file)
  88. // })
  89. }
  90. }
  91. export function formatTimeBySeconds (ms) {
  92. let hs = ms / 3600000 | 0
  93. let min = (ms - (hs * 3600000)) / 60000 | 0
  94. let sec = (ms - (hs * 3600000) - (min * 60000)) / 1000 | 0
  95. hs = hs < 10 ? '0' + hs : hs
  96. min = min < 10 ? '0' + min : min
  97. sec = sec < 10 ? '0' + sec : sec
  98. return hs > 0 ? `${hs}:${min}:${sec}` : `${min}:${sec}`
  99. }
  100. export default toolClass