common.js 10 KB

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