index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. import { parseTime } from './ruoyi'
  2. import user from '../store/modules/user'
  3. /**
  4. * 表格时间格式化
  5. */
  6. export function formatDate(cellValue) {
  7. if (cellValue == null || cellValue == "") return "";
  8. var date = new Date(cellValue)
  9. var year = date.getFullYear()
  10. var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  11. var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  12. var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  13. var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  14. var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  15. return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
  16. }
  17. /**
  18. * @param {number} time
  19. * @param {string} option
  20. * @returns {string}
  21. */
  22. export function formatTime(time, option) {
  23. if (('' + time).length === 10) {
  24. time = parseInt(time) * 1000
  25. } else {
  26. time = +time
  27. }
  28. const d = new Date(time)
  29. const now = Date.now()
  30. const diff = (now - d) / 1000
  31. if (diff < 30) {
  32. return '刚刚'
  33. } else if (diff < 3600) {
  34. // less 1 hour
  35. return Math.ceil(diff / 60) + '分钟前'
  36. } else if (diff < 3600 * 24) {
  37. return Math.ceil(diff / 3600) + '小时前'
  38. } else if (diff < 3600 * 24 * 2) {
  39. return '1天前'
  40. }
  41. if (option) {
  42. return parseTime(time, option)
  43. } else {
  44. return (
  45. d.getMonth() +
  46. 1 +
  47. '月' +
  48. d.getDate() +
  49. '日' +
  50. d.getHours() +
  51. '时' +
  52. d.getMinutes() +
  53. '分'
  54. )
  55. }
  56. }
  57. /**
  58. * @param {string} url
  59. * @returns {Object}
  60. */
  61. export function getQueryObject(url) {
  62. url = url == null ? window.location.href : url
  63. const search = url.substring(url.lastIndexOf('?') + 1)
  64. const obj = {}
  65. const reg = /([^?&=]+)=([^?&=]*)/g
  66. search.replace(reg, (rs, $1, $2) => {
  67. const name = decodeURIComponent($1)
  68. let val = decodeURIComponent($2)
  69. val = String(val)
  70. obj[name] = val
  71. return rs
  72. })
  73. return obj
  74. }
  75. /**
  76. * @param {string} input value
  77. * @returns {number} output value
  78. */
  79. export function byteLength(str) {
  80. // returns the byte length of an utf8 string
  81. let s = str.length
  82. for (var i = str.length - 1; i >= 0; i--) {
  83. const code = str.charCodeAt(i)
  84. if (code > 0x7f && code <= 0x7ff) s++
  85. else if (code > 0x7ff && code <= 0xffff) s += 2
  86. if (code >= 0xDC00 && code <= 0xDFFF) i--
  87. }
  88. return s
  89. }
  90. /**
  91. * @param {Array} actual
  92. * @returns {Array}
  93. */
  94. export function cleanArray(actual) {
  95. const newArray = []
  96. for (let i = 0; i < actual.length; i++) {
  97. if (actual[i]) {
  98. newArray.push(actual[i])
  99. }
  100. }
  101. return newArray
  102. }
  103. /**
  104. * @param {Object} json
  105. * @returns {Array}
  106. */
  107. export function param(json) {
  108. if (!json) return ''
  109. return cleanArray(
  110. Object.keys(json).map(key => {
  111. if (json[key] === undefined) return ''
  112. return encodeURIComponent(key) + '=' + encodeURIComponent(json[key])
  113. })
  114. ).join('&')
  115. }
  116. /**
  117. * @param {string} url
  118. * @returns {Object}
  119. */
  120. export function param2Obj(url) {
  121. const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  122. if (!search) {
  123. return {}
  124. }
  125. const obj = {}
  126. const searchArr = search.split('&')
  127. searchArr.forEach(v => {
  128. const index = v.indexOf('=')
  129. if (index !== -1) {
  130. const name = v.substring(0, index)
  131. const val = v.substring(index + 1, v.length)
  132. obj[name] = val
  133. }
  134. })
  135. return obj
  136. }
  137. /**
  138. * @param {string} val
  139. * @returns {string}
  140. */
  141. export function html2Text(val) {
  142. const div = document.createElement('div')
  143. div.innerHTML = val
  144. return div.textContent || div.innerText
  145. }
  146. /**
  147. * Merges two objects, giving the last one precedence
  148. * @param {Object} target
  149. * @param {(Object|Array)} source
  150. * @returns {Object}
  151. */
  152. export function objectMerge(target, source) {
  153. if (typeof target !== 'object') {
  154. target = {}
  155. }
  156. if (Array.isArray(source)) {
  157. return source.slice()
  158. }
  159. Object.keys(source).forEach(property => {
  160. const sourceProperty = source[property]
  161. if (typeof sourceProperty === 'object') {
  162. target[property] = objectMerge(target[property], sourceProperty)
  163. } else {
  164. target[property] = sourceProperty
  165. }
  166. })
  167. return target
  168. }
  169. /**
  170. * @param {HTMLElement} element
  171. * @param {string} className
  172. */
  173. export function toggleClass(element, className) {
  174. if (!element || !className) {
  175. return
  176. }
  177. let classString = element.className
  178. const nameIndex = classString.indexOf(className)
  179. if (nameIndex === -1) {
  180. classString += '' + className
  181. } else {
  182. classString =
  183. classString.substr(0, nameIndex) +
  184. classString.substr(nameIndex + className.length)
  185. }
  186. element.className = classString
  187. }
  188. /**
  189. * @param {string} type
  190. * @returns {Date}
  191. */
  192. export function getTime(type) {
  193. if (type === 'start') {
  194. return new Date().getTime() - 3600 * 1000 * 24 * 90
  195. } else {
  196. return new Date(new Date().toDateString())
  197. }
  198. }
  199. /**
  200. * @param {Function} func
  201. * @param {number} wait
  202. * @param {boolean} immediate
  203. * @return {*}
  204. */
  205. export function debounce(func, wait, immediate) {
  206. let timeout, args, context, timestamp, result
  207. const later = function() {
  208. // 据上一次触发时间间隔
  209. const last = +new Date() - timestamp
  210. // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
  211. if (last < wait && last > 0) {
  212. timeout = setTimeout(later, wait - last)
  213. } else {
  214. timeout = null
  215. // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
  216. if (!immediate) {
  217. result = func.apply(context, args)
  218. if (!timeout) context = args = null
  219. }
  220. }
  221. }
  222. return function(...args) {
  223. context = this
  224. timestamp = +new Date()
  225. const callNow = immediate && !timeout
  226. // 如果延时不存在,重新设定延时
  227. if (!timeout) timeout = setTimeout(later, wait)
  228. if (callNow) {
  229. result = func.apply(context, args)
  230. context = args = null
  231. }
  232. return result
  233. }
  234. }
  235. /**
  236. * This is just a simple version of deep copy
  237. * Has a lot of edge cases bug
  238. * If you want to use a perfect deep copy, use lodash's _.cloneDeep
  239. * @param {Object} source
  240. * @returns {Object}
  241. */
  242. export function deepClone(source) {
  243. if (!source && typeof source !== 'object') {
  244. throw new Error('error arguments', 'deepClone')
  245. }
  246. const targetObj = source.constructor === Array ? [] : {}
  247. Object.keys(source).forEach(keys => {
  248. if (source[keys] && typeof source[keys] === 'object') {
  249. targetObj[keys] = deepClone(source[keys])
  250. } else {
  251. targetObj[keys] = source[keys]
  252. }
  253. })
  254. return targetObj
  255. }
  256. /**
  257. * @param {Array} arr
  258. * @returns {Array}
  259. */
  260. export function uniqueArr(arr) {
  261. return Array.from(new Set(arr))
  262. }
  263. /**
  264. * @returns {string}
  265. */
  266. export function createUniqueString() {
  267. const timestamp = +new Date() + ''
  268. const randomNum = parseInt((1 + Math.random()) * 65536) + ''
  269. return (+(randomNum + timestamp)).toString(32)
  270. }
  271. /**
  272. * Check if an element has a class
  273. * @param {HTMLElement} elm
  274. * @param {string} cls
  275. * @returns {boolean}
  276. */
  277. export function hasClass(ele, cls) {
  278. return !!ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'))
  279. }
  280. /**
  281. * Add class to element
  282. * @param {HTMLElement} elm
  283. * @param {string} cls
  284. */
  285. export function addClass(ele, cls) {
  286. if (!hasClass(ele, cls)) ele.className += ' ' + cls
  287. }
  288. /**
  289. * Remove class from element
  290. * @param {HTMLElement} elm
  291. * @param {string} cls
  292. */
  293. export function removeClass(ele, cls) {
  294. if (hasClass(ele, cls)) {
  295. const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)')
  296. ele.className = ele.className.replace(reg, ' ')
  297. }
  298. }
  299. export function makeMap(str, expectsLowerCase) {
  300. const map = Object.create(null)
  301. const list = str.split(',')
  302. for (let i = 0; i < list.length; i++) {
  303. map[list[i]] = true
  304. }
  305. return expectsLowerCase
  306. ? val => map[val.toLowerCase()]
  307. : val => map[val]
  308. }
  309. export const exportDefault = 'export default '
  310. export const beautifierConf = {
  311. html: {
  312. indent_size: '2',
  313. indent_char: ' ',
  314. max_preserve_newlines: '-1',
  315. preserve_newlines: false,
  316. keep_array_indentation: false,
  317. break_chained_methods: false,
  318. indent_scripts: 'separate',
  319. brace_style: 'end-expand',
  320. space_before_conditional: true,
  321. unescape_strings: false,
  322. jslint_happy: false,
  323. end_with_newline: true,
  324. wrap_line_length: '110',
  325. indent_inner_html: true,
  326. comma_first: false,
  327. e4x: true,
  328. indent_empty_lines: true
  329. },
  330. js: {
  331. indent_size: '2',
  332. indent_char: ' ',
  333. max_preserve_newlines: '-1',
  334. preserve_newlines: false,
  335. keep_array_indentation: false,
  336. break_chained_methods: false,
  337. indent_scripts: 'normal',
  338. brace_style: 'end-expand',
  339. space_before_conditional: true,
  340. unescape_strings: false,
  341. jslint_happy: true,
  342. end_with_newline: true,
  343. wrap_line_length: '110',
  344. indent_inner_html: true,
  345. comma_first: false,
  346. e4x: true,
  347. indent_empty_lines: true
  348. }
  349. }
  350. // 首字母大小
  351. export function titleCase(str) {
  352. return str.replace(/( |^)[a-z]/g, L => L.toUpperCase())
  353. }
  354. // 下划转驼峰
  355. export function camelCase(str) {
  356. return str.replace(/-[a-z]/g, str1 => str1.substr(-1).toUpperCase())
  357. }
  358. export function isNumberStr(str) {
  359. return /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g.test(str)
  360. }
  361. /* 操作权限验证
  362. * str 为权限字符 用于匹配操作角色的字符
  363. * str === performEvacuation 应急疏散操作权限
  364. * str === checkGentle 安全检查
  365. */
  366. export function controlsRestrictVerify(str) {
  367. if(str){
  368. let controlsRestrict = localStorage.getItem('controlsRestrict');
  369. return controlsRestrict.indexOf(str) != -1;
  370. }else{
  371. return false
  372. }
  373. }
  374. /* 硬件控制与视频监控权限验证(校级管理员/院级管理员/实验室管理员)
  375. subAdminId 传入的实验室管理员ID
  376. schoolId 校级管理员身份ID
  377. collegeId 院级管理员身份ID
  378. userInfo.position 当前用户身份ID (系统用户为NULL)
  379. userInfo.userId 当前用户userID
  380. */
  381. export function itoOrVideoLimits(subAdminId) {
  382. console.log('subAdminId',subAdminId)
  383. let userInfo = JSON.parse(localStorage.getItem('user'));
  384. let schoolId = process.env.VUE_APP_SCHOOL_ID;
  385. let collegeId = process.env.VUE_APP_COLLEGE_ID;
  386. if(userInfo.position == null){
  387. //如果是管理员(系统用户)
  388. return true
  389. }else if(userInfo.position == schoolId || userInfo.position == collegeId){
  390. //如果是校级管理员/院级管理员
  391. return true
  392. }else if(userInfo.userId == subAdminId){
  393. //如果当前用户userId 与 管理员ID相等
  394. return true
  395. }else{
  396. return false
  397. }
  398. }