华西海圻ELN前端工程
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
6.2 KiB

  1. /**
  2. * 通用js方法封装处理
  3. * Copyright (c) 2019 ruoyi
  4. */
  5. // 日期格式化
  6. export function parseTime(time, pattern) {
  7. if (arguments.length === 0 || !time) {
  8. return null
  9. }
  10. const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  11. let date
  12. if (typeof time === 'object') {
  13. date = time
  14. } else {
  15. if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
  16. time = parseInt(time)
  17. } else if (typeof time === 'string') {
  18. time = time
  19. .replace(new RegExp(/-/gm), '/')
  20. .replace('T', ' ')
  21. .replace(new RegExp(/\.[\d]{3}/gm), '')
  22. }
  23. if (typeof time === 'number' && time.toString().length === 10) {
  24. time = time * 1000
  25. }
  26. date = new Date(time)
  27. }
  28. const formatObj = {
  29. y: date.getFullYear(),
  30. m: date.getMonth() + 1,
  31. d: date.getDate(),
  32. h: date.getHours(),
  33. i: date.getMinutes(),
  34. s: date.getSeconds(),
  35. a: date.getDay()
  36. }
  37. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  38. let value = formatObj[key]
  39. // Note: getDay() returns 0 on Sunday
  40. if (key === 'a') {
  41. return ['日', '一', '二', '三', '四', '五', '六'][value]
  42. }
  43. if (result.length > 0 && value < 10) {
  44. value = '0' + value
  45. }
  46. return value || 0
  47. })
  48. return time_str
  49. }
  50. // 表单重置
  51. export function resetForm(refName) {
  52. if (this.$refs[refName]) {
  53. this.$refs[refName].resetFields()
  54. }
  55. }
  56. // 表单清除验证
  57. export function clearFormValidate(refName) {
  58. setTimeout(() => {
  59. if (this.$refs[refName]) {
  60. this.$refs[refName].clearValidate()
  61. }
  62. }, 100)
  63. // if (this.$refs[refName]) {
  64. // this.$refs[refName].clearValidate()
  65. // }
  66. // else {
  67. // setTimeout(() => {
  68. // this.clearFormValidate(refName)
  69. // }, 100)
  70. // }
  71. }
  72. // 添加日期范围
  73. export function addDateRange(params, dateRange, propName) {
  74. let search = params
  75. search.params =
  76. typeof search.params === 'object' &&
  77. search.params !== null &&
  78. !Array.isArray(search.params)
  79. ? search.params
  80. : {}
  81. dateRange = Array.isArray(dateRange) ? dateRange : []
  82. if (typeof propName === 'undefined') {
  83. search.params['beginTime'] = dateRange[0]
  84. search.params['endTime'] = dateRange[1]
  85. } else {
  86. search.params['begin' + propName] = dateRange[0]
  87. search.params['end' + propName] = dateRange[1]
  88. }
  89. return search
  90. }
  91. // 回显数据字典
  92. export function selectDictLabel(datas, value) {
  93. if (value === undefined) {
  94. return ''
  95. }
  96. var actions = []
  97. Object.keys(datas).some((key) => {
  98. if (datas[key].value == '' + value) {
  99. actions.push(datas[key].label)
  100. return true
  101. }
  102. })
  103. if (actions.length === 0) {
  104. actions.push(value)
  105. }
  106. return actions.join('')
  107. }
  108. // 回显数据字典(字符串、数组)
  109. export function selectDictLabels(datas, value, separator) {
  110. if (value === undefined || value.length === 0) {
  111. return ''
  112. }
  113. if (Array.isArray(value)) {
  114. value = value.join(',')
  115. }
  116. var actions = []
  117. var currentSeparator = undefined === separator ? ',' : separator
  118. var temp = value.split(currentSeparator)
  119. Object.keys(value.split(currentSeparator)).some((val) => {
  120. var match = false
  121. Object.keys(datas).some((key) => {
  122. if (datas[key].value == '' + temp[val]) {
  123. actions.push(datas[key].label + currentSeparator)
  124. match = true
  125. }
  126. })
  127. if (!match) {
  128. actions.push(temp[val] + currentSeparator)
  129. }
  130. })
  131. return actions.join('').substring(0, actions.join('').length - 1)
  132. }
  133. // 字符串格式化(%s )
  134. export function sprintf(str) {
  135. var args = arguments,
  136. flag = true,
  137. i = 1
  138. str = str.replace(/%s/g, function () {
  139. var arg = args[i++]
  140. if (typeof arg === 'undefined') {
  141. flag = false
  142. return ''
  143. }
  144. return arg
  145. })
  146. return flag ? str : ''
  147. }
  148. // 转换字符串,undefined,null等转化为""
  149. export function parseStrEmpty(str) {
  150. if (!str || str == 'undefined' || str == 'null') {
  151. return ''
  152. }
  153. return str
  154. }
  155. // 数据合并
  156. export function mergeRecursive(source, target) {
  157. for (var p in target) {
  158. try {
  159. if (target[p].constructor == Object) {
  160. source[p] = mergeRecursive(source[p], target[p])
  161. } else {
  162. source[p] = target[p]
  163. }
  164. } catch (e) {
  165. source[p] = target[p]
  166. }
  167. }
  168. return source
  169. }
  170. /**
  171. * 构造树型结构数据
  172. * @param {*} data 数据源
  173. * @param {*} id id字段 默认 'id'
  174. * @param {*} parentId 父节点字段 默认 'parentId'
  175. * @param {*} children 孩子节点字段 默认 'children'
  176. */
  177. export function handleTree(data, id, parentId, children) {
  178. let config = {
  179. id: id || 'id',
  180. parentId: parentId || 'parentId',
  181. childrenList: children || 'children'
  182. }
  183. var childrenListMap = {}
  184. var tree = []
  185. for (let d of data) {
  186. let id = d[config.id]
  187. childrenListMap[id] = d
  188. if (!d[config.childrenList]) {
  189. d[config.childrenList] = []
  190. }
  191. }
  192. for (let d of data) {
  193. let parentId = d[config.parentId]
  194. let parentObj = childrenListMap[parentId]
  195. if (!parentObj) {
  196. tree.push(d)
  197. } else {
  198. parentObj[config.childrenList].push(d)
  199. }
  200. }
  201. return tree
  202. }
  203. /**
  204. * 参数处理
  205. * @param {*} params 参数
  206. */
  207. export function tansParams(params) {
  208. let result = ''
  209. for (const propName of Object.keys(params)) {
  210. const value = params[propName]
  211. var part = encodeURIComponent(propName) + '='
  212. if (value !== null && value !== '' && typeof value !== 'undefined') {
  213. if (typeof value === 'object') {
  214. for (const key of Object.keys(value)) {
  215. if (
  216. value[key] !== null &&
  217. value[key] !== '' &&
  218. typeof value[key] !== 'undefined'
  219. ) {
  220. let params = propName + '[' + key + ']'
  221. var subPart = encodeURIComponent(params) + '='
  222. result += subPart + encodeURIComponent(value[key]) + '&'
  223. }
  224. }
  225. } else {
  226. result += part + encodeURIComponent(value) + '&'
  227. }
  228. }
  229. }
  230. return result
  231. }
  232. // 返回项目路径
  233. export function getNormalPath(p) {
  234. if (p.length === 0 || !p || p == 'undefined') {
  235. return p
  236. }
  237. let res = p.replace('//', '/')
  238. if (res[res.length - 1] === '/') {
  239. return res.slice(0, res.length - 1)
  240. }
  241. return res
  242. }
  243. // 验证是否为blob格式
  244. export function blobValidate(data) {
  245. return data.type !== 'application/json'
  246. }