index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Add class to element
  3. * @param {HTMLElement} elm
  4. * @param {string} cls
  5. */
  6. export function addClass(ele, cls) {
  7. if (!hasClass(ele, cls)) ele.className += ' ' + cls
  8. }
  9. /**
  10. * Remove class from element
  11. * @param {HTMLElement} elm
  12. * @param {string} cls
  13. */
  14. export function removeClass(ele, cls) {
  15. if (hasClass(ele, cls)) {
  16. const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)')
  17. ele.className = ele.className.replace(reg, ' ')
  18. }
  19. }
  20. export function isLogin() {
  21. if (!localStorage.token || !parseInt(localStorage.uid)) {
  22. return false
  23. }
  24. return true
  25. }
  26. export function getMediaOptions(img, url) {
  27. return {
  28. // playbackRates: [0.5, 1.0, 1.5, 2.0], // 播放速度
  29. autoplay: false, // 如果true,浏览器准备好时开始回放。
  30. muted: false, // 默认情况下将会消除任何音频。
  31. loop: false, // 导致视频一结束就重新开始。
  32. preload: "true", // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频(如果浏览器支持)
  33. language: 'zh-CN',
  34. aspectRatio: '4:3', // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
  35. fluid: true, // 当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
  36. sources: [{
  37. src: url
  38. }],
  39. poster: img, // 你的封面地址
  40. notSupportedMessage: '无法播放媒体源', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
  41. playtimes: '',
  42. controlBar: {
  43. timeDivider: false,
  44. durationDisplay: false,
  45. remainingTimeDisplay: false,
  46. fullscreenToggle: true // 全屏按钮
  47. }
  48. }
  49. }
  50. export function parseTime(time, cFormat) {
  51. if (arguments.length === 0) {
  52. return null
  53. }
  54. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  55. let date
  56. if (typeof time === 'object') {
  57. date = time
  58. } else {
  59. if ((typeof time === 'string')) {
  60. if ((/^[0-9]+$/.test(time))) {
  61. // support "1548221490638"
  62. time = parseInt(time)
  63. } else {
  64. // support safari
  65. // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
  66. time = time.replace(new RegExp(/-/gm), '/')
  67. }
  68. }
  69. if ((typeof time === 'number') && (time.toString().length === 10)) {
  70. time = time * 1000
  71. }
  72. date = new Date(time)
  73. }
  74. const formatObj = {
  75. y: date.getFullYear(),
  76. m: date.getMonth() + 1,
  77. d: date.getDate(),
  78. h: date.getHours(),
  79. i: date.getMinutes(),
  80. s: date.getSeconds(),
  81. a: date.getDay()
  82. }
  83. const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
  84. const value = formatObj[key]
  85. // Note: getDay() returns 0 on Sunday
  86. if (key === 'a') {
  87. return ['日', '一', '二', '三', '四', '五', '六'][value]
  88. }
  89. return value.toString().padStart(2, '0')
  90. })
  91. return time_str
  92. }
  93. export function getPercent( item ){
  94. if( item.isFinish ) 100;
  95. let percent = parseInt(item.position * 10000 / item.duration) / 100;
  96. if( percent >= 100) return 99.99;
  97. return parseInt(item.position * 10000 / item.duration) / 100
  98. }