index.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import Vue from 'vue'
  2. import router from '@/router'
  3. import store from '@/store'
  4. import $http from './httpRequest'
  5. /**
  6. * 是否有权限
  7. * @param {*} key
  8. */
  9. export function hasPermission (key) {
  10. return JSON.parse(sessionStorage.getItem('permissions') || '[]').indexOf(key) !== -1 || false
  11. }
  12. /**
  13. * 树形数据转换
  14. * @param {*} data list数据
  15. * @param {*} id 主键ID
  16. * @param {*} pid 上级ID
  17. * @param childrenKey 子list数据的key
  18. */
  19. export function treeDataTranslate (data, id = 'id', pid = 'parentId', childrenKey = 'childNodes') {
  20. let res = []
  21. let temp = {}
  22. for (let i = 0; i < data.length; i++) {
  23. temp[data[i][id]] = data[i]
  24. }
  25. for (let k = 0; k < data.length; k++) {
  26. if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
  27. if (!temp[data[k][pid]][childrenKey]) {
  28. temp[data[k][pid]][childrenKey] = []
  29. }
  30. if (!temp[data[k][pid]]['_level']) {
  31. temp[data[k][pid]]['_level'] = 1
  32. }
  33. data[k]['_level'] = temp[data[k][pid]]._level + 1
  34. temp[data[k][pid]][childrenKey].push(data[k])
  35. } else {
  36. res.push(data[k])
  37. }
  38. }
  39. return res
  40. }
  41. /**
  42. * 清除登录信息
  43. */
  44. export function clearLoginInfo () {
  45. Vue.cookie.delete('token')
  46. store.commit('resetStore')
  47. router.options.isAddDynamicMenuRoutes = false
  48. }
  49. /**
  50. * 表单对象赋值:
  51. * 对目标对象存在且源对象同样存在的属性,全部覆盖;
  52. * 目标对象不存在但是源对象存在的属性, 全部丢弃;
  53. * 目标对象存在但是源对象不存在的属性,如果是字符串赋值为空串,其余类型赋值为undefined
  54. */
  55. export function recover (target, source) {
  56. if (target === undefined || target === null) { throw new TypeError('Cannot convert first argument to object') }
  57. var to = Object(target)
  58. if (source === undefined || source === null) { return to }
  59. var keysArray = Object.keys(Object(target))
  60. for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
  61. var nextKey = keysArray[nextIndex]
  62. var desc = Object.getOwnPropertyDescriptor(target, nextKey)
  63. if (desc !== undefined && desc.enumerable) {
  64. if (to.hasOwnProperty(nextKey)) {
  65. if (to[nextKey] instanceof Array) {
  66. to[nextKey] = source[nextKey]
  67. } else if (to[nextKey] instanceof Object) {
  68. recover(to[nextKey], source[nextKey])
  69. } else if (source[nextKey] !== undefined) {
  70. to[nextKey] = source[nextKey]
  71. } else if (typeof (to[nextKey]) === 'string') {
  72. to[nextKey] = ''
  73. } else {
  74. to[nextKey] = undefined
  75. }
  76. }
  77. }
  78. }
  79. return to
  80. }
  81. /**
  82. * 表单对象赋值:
  83. * 对目标对象存在且源对象同样存在的属性,全部覆盖;
  84. * 目标对象不存在但是源对象存在的属性, 全部丢弃;
  85. * 目标对象存在但是源对象不存在的属性,保留目标对象的属性不做处理
  86. */
  87. export function recoverNotNull (target, source) {
  88. if (target === undefined || target === null) { throw new TypeError('Cannot convert first argument to object') }
  89. var to = Object(target)
  90. if (source === undefined || source === null) { return to }
  91. var keysArray = Object.keys(Object(target))
  92. for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
  93. var nextKey = keysArray[nextIndex]
  94. var desc = Object.getOwnPropertyDescriptor(target, nextKey)
  95. if (desc !== undefined && desc.enumerable) {
  96. if (to.hasOwnProperty(nextKey)) {
  97. if (to[nextKey] instanceof Array) {
  98. to[nextKey] = source[nextKey]
  99. } else if (to[nextKey] instanceof Object) {
  100. recover(to[nextKey], source[nextKey])
  101. } else if (source[nextKey] !== undefined) {
  102. to[nextKey] = source[nextKey]
  103. }
  104. }
  105. }
  106. }
  107. return to
  108. }
  109. export function download (url) {
  110. $http({
  111. method: 'get',
  112. url: url,
  113. responseType: 'blob'
  114. }).then(response => {
  115. if (!response) {
  116. return
  117. }
  118. let link = document.createElement('a')
  119. link.href = window.URL.createObjectURL(new Blob([response.data]))
  120. link.target = '_blank'
  121. let filename = response.headers['content-disposition']
  122. link.download = decodeURI(filename)
  123. document.body.appendChild(link)
  124. link.click()
  125. document.body.removeChild(link)
  126. // eslint-disable-next-line handle-callback-err
  127. }).catch((error) => {
  128. })
  129. }
  130. export function escapeHTML (a) {
  131. a = '' + a
  132. return a.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&apos;')
  133. }
  134. /**
  135. * @function unescapeHTML 还原html脚本 < > & " '
  136. * @param a -
  137. * 字符串
  138. */
  139. export function unescapeHTML (a) {
  140. a = '' + a
  141. return a.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&').replace(/&quot;/g, '"').replace(/&apos;/g, "'")
  142. }
  143. export function printLogo () {
  144. console.info(
  145. '%c欢迎使用%cJEEPLUS',
  146. 'color: #ffffff; background: #000000; padding:5px 10px 5px 10px;font-size:40px;border-radius:12px 0 0 12px;', 'color: #000000; background: #FE9A00; padding:5px 10px;font-size:40px;border-radius:0 12px 12px 0;')
  147. }
  148. /**
  149. * 对象深拷贝
  150. */
  151. export function deepClone (data) {
  152. var type = getObjType(data)
  153. var obj
  154. if (type === 'array') {
  155. obj = []
  156. } else if (type === 'object') {
  157. obj = {}
  158. } else {
  159. // 不再具有下一层次
  160. return data
  161. }
  162. if (type === 'array') {
  163. for (var i = 0, len = data.length; i < len; i++) {
  164. data[i] = (function () {
  165. if (data[i] === 0) {
  166. return data[i]
  167. }
  168. return data[i]
  169. }())
  170. delete data[i].$parent
  171. obj.push(deepClone(data[i]))
  172. }
  173. } else if (type === 'object') {
  174. for (var key in data) {
  175. delete data.$parent
  176. obj[key] = deepClone(data[key])
  177. }
  178. }
  179. return obj
  180. };
  181. export function getObjType (obj) {
  182. var toString = Object.prototype.toString
  183. var map = {
  184. '[object Boolean]': 'boolean',
  185. '[object Number]': 'number',
  186. '[object String]': 'string',
  187. '[object Function]': 'function',
  188. '[object Array]': 'array',
  189. '[object Date]': 'date',
  190. '[object RegExp]': 'regExp',
  191. '[object Undefined]': 'undefined',
  192. '[object Null]': 'null',
  193. '[object Object]': 'object'
  194. }
  195. if (obj instanceof Element) {
  196. return 'element'
  197. }
  198. return map[toString.call(obj)]
  199. };
  200. export function validatenull (val) {
  201. // 特殊判断
  202. if (val && parseInt(val) === 0) return false
  203. var list = ['$parent']
  204. if (typeof val === 'boolean') {
  205. return false
  206. }
  207. if (typeof val === 'number') {
  208. return false
  209. }
  210. if (val instanceof Array) {
  211. if (val.length === 0) return true
  212. } else if (val instanceof Object) {
  213. val = (0, deepClone)(val)
  214. list.forEach(function (ele) {
  215. delete val[ele]
  216. })
  217. if (JSON.stringify(val) === '{}') return true
  218. } else {
  219. if (val === 'null' || val == null || val === 'undefined' || val === undefined || val === '') {
  220. return true
  221. }
  222. return false
  223. }
  224. return false
  225. }
  226. function handleImageAdded (file, Editor, cursorLocation, resetUploader) {
  227. // An example of using FormData
  228. // NOTE: Your key could be different such as:
  229. // formData.append('file', file)
  230. var formData = new FormData()
  231. formData.append('file', file)
  232. $http({
  233. url: '/sys/file/webupload/upload?uploadPath=/vueEditor',
  234. method: 'POST',
  235. data: formData,
  236. headers: { 'Content-Type': 'multipart/form-data' }
  237. })
  238. .then(result => {
  239. let url = result.data.url // Get url from response
  240. Editor.insertEmbed(cursorLocation, 'image', url)
  241. resetUploader()
  242. })
  243. .catch(err => {
  244. console.log(err)
  245. })
  246. }
  247. function hashCode (str) {
  248. var hash = 0
  249. if (str.length === 0) return hash
  250. for (let i = 0; i < str.length; i++) {
  251. let char = str.charCodeAt(i)
  252. hash = ((hash << 5) - hash) + char
  253. hash = hash & hash // Convert to 32bit integer
  254. }
  255. return hash
  256. }
  257. export default {escapeHTML, hashCode, unescapeHTML, handleImageAdded, download, recover, recoverNotNull, hasPermission, treeDataTranslate, printLogo, deepClone, validatenull}