util.js 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import Ajax from './ajax'
  2. import api from './api'
  3. const toolClass = {
  4. dateFormat: (timestamp, fmt) => {
  5. if (!fmt) {
  6. fmt = 'yyyy-MM-dd hh:mm'
  7. }
  8. let date = new Date(timestamp)
  9. var o = {
  10. 'M+': date.getMonth() + 1,
  11. 'd+': date.getDate(),
  12. 'h+': date.getHours(),
  13. 'm+': date.getMinutes(),
  14. 's+': date.getSeconds(),
  15. 'q+': Math.floor((date.getMonth() + 3) / 3),
  16. 'S': date.getMilliseconds()
  17. }
  18. if (/(y+)/.test(fmt)) {
  19. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  20. }
  21. for (var k in o) {
  22. if (new RegExp('(' + k + ')').test(fmt)) {
  23. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  24. }
  25. }
  26. return fmt
  27. },
  28. ReplaceOrg (url) {
  29. return url.replace(':org', 'MQ')
  30. },
  31. upload (item) {
  32. return new Promise((resolve, reject) => {
  33. Ajax({
  34. ...api.file.image,
  35. data: { file: item.file },
  36. }).then((res) => {
  37. let result = {
  38. result: res
  39. }
  40. item.onSuccess(result, item.file)
  41. console.log(result)
  42. resolve(result)
  43. }).catch(reject)
  44. })
  45. }
  46. }
  47. export function formatTimeBySeconds (ms) {
  48. let hs = ms / 3600000 | 0
  49. let min = (ms - (hs * 3600000)) / 60000 | 0
  50. let sec = (ms - (hs * 3600000) - (min * 60000)) / 1000 | 0
  51. hs = hs < 10 ? '0' + hs : hs
  52. min = min < 10 ? '0' + min : min
  53. sec = sec < 10 ? '0' + sec : sec
  54. return hs > 0 ? `${hs}:${min}:${sec}` : `${min}:${sec}`
  55. }
  56. export default toolClass