common.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import Cookies from 'js-cookie'
  2. import { getStorageItem } from '@/utils/storage'
  3. export default {
  4. data () {
  5. return {
  6. }
  7. },
  8. computed: {
  9. // 网页高度
  10. bodyWidth () {
  11. return document.body.clientWidth
  12. },
  13. // 网页宽度
  14. bodyHeight () {
  15. return document.body.clientHeight
  16. },
  17. },
  18. created () {
  19. },
  20. mounted () {
  21. },
  22. destroyed () {
  23. },
  24. methods: {
  25. setCookies (key, val, option) {
  26. if (option == null) {
  27. option = { expires: 15 }
  28. }
  29. Cookies.set(key, val, option)
  30. },
  31. goBack () {
  32. this.$router.go(-1)
  33. },
  34. refresh () {
  35. this.$router.go(0)
  36. },
  37. parseString (object) {
  38. if (typeof object === 'undefined' || object == null) {
  39. return ''
  40. }
  41. if (typeof object === 'number') {
  42. return object.toString()
  43. }
  44. if (typeof object === 'boolean') {
  45. return object.toString()
  46. }
  47. if (typeof object === 'object') {
  48. return JSON.stringify(object)
  49. }
  50. return ''
  51. },
  52. isBlank (val) {
  53. if (typeof val === 'undefined') {
  54. return true
  55. }
  56. if (val == null || val === '') {
  57. return true
  58. }
  59. return false
  60. },
  61. // 封装定制删除数组中的值
  62. contains (a, obj) {
  63. let i = a.length
  64. while (i--) {
  65. if (a[i] === obj) {
  66. return i
  67. }
  68. }
  69. return false
  70. },
  71. //获取url后边参数
  72. getUrlKey: function (name) {
  73. return (
  74. decodeURIComponent(
  75. (new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(
  76. /\+/g,
  77. '%20'
  78. )
  79. ) || null
  80. )
  81. },
  82. /**
  83. *
  84. */
  85. resetForm (data) {
  86. let formKeys = Object.keys(data)
  87. for (let k of formKeys) {
  88. data[k] = null
  89. }
  90. },
  91. sortArray (propertyName) {
  92. return function (object1, object2) {
  93. let value1 = object1[propertyName];
  94. let value2 = object2[propertyName];
  95. if (value1 < value2) {
  96. return -1;
  97. } else if (value1 > value2) {
  98. return 1;
  99. } else {
  100. return 0;
  101. }
  102. }
  103. },
  104. // 获取对象类型
  105. getObjectType (obj) {
  106. let toString = Object.prototype.toString
  107. let map = {
  108. '[object Boolean]': 'boolean',
  109. '[object Number]': 'number',
  110. '[object String]': 'string',
  111. '[object Function]': 'function',
  112. '[object Array]': 'array',
  113. '[object Date]': 'date',
  114. '[object RegExp]': 'regExp',
  115. '[object Undefined]': 'undefined',
  116. '[object Null]': 'null',
  117. '[object Object]': 'object',
  118. }
  119. if (obj instanceof Element) {
  120. return 'element'
  121. }
  122. return map[toString.call(obj)]
  123. },
  124. isNumber (obj) {
  125. return this.getObjectType(obj) == 'number'
  126. },
  127. isString (obj) {
  128. return this.getObjectType(obj) == 'string'
  129. },
  130. isArray (obj) {
  131. return this.getObjectType(obj) == 'array'
  132. },
  133. hasOwn (obj, key) {
  134. return Object.prototype.hasOwnProperty.call(obj, key)
  135. },
  136. isNotBlank (val) {
  137. return !this.isBlank(val)
  138. },
  139. isBlank (val) {
  140. if (this.isNull(val)) {
  141. return true
  142. }
  143. if (typeof val === 'string') {
  144. return val.trim() == ''
  145. }
  146. if (typeof val === 'object') {
  147. for (let key in val) {
  148. return false
  149. }
  150. return true
  151. }
  152. return false
  153. },
  154. isNotNull (val) {
  155. return !this.isNull(val)
  156. },
  157. isNull (val) {
  158. // 特殊判断
  159. if (val && parseInt(val) === 0) return false
  160. const list = ['$parent']
  161. if (val instanceof Date || typeof val === 'boolean' || typeof val === 'number') return false
  162. if (val instanceof Array) {
  163. if (val.length === 0) return true
  164. } else if (val instanceof Object) {
  165. val = this.deepClone(val)
  166. list.forEach((ele) => {
  167. delete val[ele]
  168. })
  169. for (let o in val) {
  170. return false
  171. }
  172. return true
  173. } else {
  174. if (val === 'null' || val == null || val === 'undefined' || val === undefined || val === '') {
  175. return true
  176. }
  177. return false
  178. }
  179. return false
  180. },
  181. // 对象深拷贝
  182. deepClone (data) {
  183. let type = this.getObjectType(data)
  184. let obj
  185. if (type === 'array') {
  186. obj = []
  187. } else if (type === 'object') {
  188. obj = {}
  189. } else {
  190. // 不再具有下一层次
  191. return data
  192. }
  193. if (type === 'array') {
  194. for (let i = 0, len = data.length; i < len; i++) {
  195. data[i] = (() => {
  196. if (data[i] === 0) {
  197. return data[i]
  198. }
  199. return data[i]
  200. })()
  201. if (data[i]) {
  202. delete data[i].$parent
  203. }
  204. obj.push(this.deepClone(data[i]))
  205. }
  206. } else if (type === 'object') {
  207. for (let key in data) {
  208. if (data) {
  209. delete data.$parent
  210. }
  211. obj[key] = this.deepClone(data[key])
  212. }
  213. }
  214. return obj
  215. },
  216. // 合并json
  217. mergeObject () {
  218. let target = arguments[0] || {}
  219. let deep = false
  220. let arr = Array.prototype.slice.call(arguments)
  221. let i = 1
  222. let options, src, key, copy
  223. let isArray = false
  224. if (typeof target === 'boolean') {
  225. deep = target
  226. i++
  227. target = arguments[1]
  228. }
  229. for (; i < arr.length; i++) {
  230. // 循环传入的对象数组
  231. if ((options = arr[i]) != null) {
  232. // 如果当前值不是null,如果是null不做处理
  233. for (key in options) {
  234. // for in循环对象中key
  235. copy = options[key]
  236. src = target[key]
  237. // 如果对象中value值任然是一个引用类型
  238. if (deep && (toString.call(copy) === '[object Object]' || (isArray = toString.call(copy) == '[object Array]'))) {
  239. if (isArray) {
  240. // 如果引用类型是数组
  241. // 如果目标对象target存在当前key,且数据类型是数组,那就还原此值,如果不是就定义成一个空数组;
  242. src = toString.call(src) === '[object Array]' ? src : []
  243. } else {
  244. // 如果目标对象target存在当前key,且数据类型是对象,那就还原此值,如果不是就定义成一个空对象;
  245. src = toString.call(src) === '[object Object]' ? src : {}
  246. }
  247. // 引用类型就再次调用extend,递归,直到此时copy是一个基本类型的值。
  248. target[key] = this.mergeObject(deep, src, copy)
  249. } else if (copy !== undefined && copy !== src) {
  250. // 如果这个值是基本值类型,且不是undefined
  251. target[key] = copy
  252. }
  253. }
  254. }
  255. }
  256. return target
  257. },
  258. // 获取dom在屏幕中的top和left
  259. getDomTopLeftById (id) {
  260. let dom = document.getElementById(id)
  261. let top = 0
  262. let left = 0
  263. if (dom != null) {
  264. top = dom.getBoundingClientRect().top
  265. left = dom.getBoundingClientRect().left
  266. }
  267. return { top: top, left: left }
  268. },
  269. objToOne (obj) {
  270. let tmpData = {}
  271. for (let index in obj) {
  272. if (typeof obj[index] == 'object') {
  273. let resObj = this.objToOne(obj[index])
  274. Object.assign(tmpData, resObj) // 这里使用对象合并
  275. } else {
  276. tmpData[index] = obj[index]
  277. }
  278. }
  279. return tmpData
  280. },
  281. urlEncode (val) {
  282. return encodeURIComponent(val)
  283. },
  284. urlDecode (val) {
  285. return decodeURIComponent(val)
  286. },
  287. urlEncodeObject (obj, ingoreFields) {
  288. if (toString.call(obj) != '[object Object]') {
  289. return obj
  290. }
  291. let result = {}
  292. for (let key in obj) {
  293. if (this.isBlank(obj[key])) {
  294. continue
  295. }
  296. if (ingoreFields != null && ingoreFields.indexOf(key) >= 0) {
  297. result[key] = obj[key]
  298. } else {
  299. result[key] = this.urlEncode(obj[key])
  300. }
  301. }
  302. return result
  303. },
  304. // 根据数据字典,查询指定字典dict指定值code的,返回整个dictItem{id, text, extend}
  305. getDictItemByCode (dict, code) {
  306. let dicts = getStorageItem('gaeaDict')
  307. if (!dicts.hasOwnProperty(dict)) {
  308. return null
  309. }
  310. let dictItems = dicts[dict]
  311. for (let i = 0; i < dictItems.length; i++) {
  312. let dictItem = dictItems[i]
  313. if (typeof (code) == 'number') {
  314. code = code.toString()
  315. }
  316. if (dictItem['id'].toString() == code) {
  317. return dictItem
  318. }
  319. }
  320. return null
  321. },
  322. // 根据数据字典,查询指定字典dict指定值code的dictItem.text
  323. getDictLabelByCode (dict, code) {
  324. let dictItem = this.getDictItemByCode(dict, code)
  325. if (dictItem != null) {
  326. return dictItem['text']
  327. } else {
  328. return ''
  329. }
  330. },
  331. // 根据数据字典,查询指定字典dict指定值code的dictItem.extend
  332. getDictExtendByCode (dict, code) {
  333. let dictItem = this.getDictItemByCode(dict, code)
  334. if (dictItem == null) {
  335. return null
  336. }
  337. let extend = dictItem['extend']
  338. if (extend == null || extend.trim() == 'null') {
  339. return null
  340. }
  341. return dictItem['extend']
  342. },
  343. getSettingByName(settingName) {
  344. let gaeaSetting = JSON.parse(localStorage.getItem('gaeaDict'))
  345. if (gaeaSetting[settingName] != null) {
  346. return gaeaSetting[settingName]
  347. } else {
  348. console.error('没有找到系统参数' + settingName + ',请与后端联系')
  349. return null
  350. }
  351. },
  352. }
  353. }