华西海圻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.

253 lines
6.0 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 resetFormTime(refName) {
  58. setTimeout(() => {
  59. if (this.$refs[refName]) {
  60. this.$refs[refName].resetFields()
  61. }
  62. }, 1000)
  63. }
  64. // 添加日期范围
  65. export function addDateRange(params, dateRange, propName) {
  66. let search = params
  67. search.params =
  68. typeof search.params === 'object' &&
  69. search.params !== null &&
  70. !Array.isArray(search.params)
  71. ? search.params
  72. : {}
  73. dateRange = Array.isArray(dateRange) ? dateRange : []
  74. if (typeof propName === 'undefined') {
  75. search.params['beginTime'] = dateRange[0]
  76. search.params['endTime'] = dateRange[1]
  77. } else {
  78. search.params['begin' + propName] = dateRange[0]
  79. search.params['end' + propName] = dateRange[1]
  80. }
  81. return search
  82. }
  83. // 回显数据字典
  84. export function selectDictLabel(datas, value) {
  85. if (value === undefined) {
  86. return ''
  87. }
  88. var actions = []
  89. Object.keys(datas).some((key) => {
  90. if (datas[key].value == '' + value) {
  91. actions.push(datas[key].label)
  92. return true
  93. }
  94. })
  95. if (actions.length === 0) {
  96. actions.push(value)
  97. }
  98. return actions.join('')
  99. }
  100. // 回显数据字典(字符串、数组)
  101. export function selectDictLabels(datas, value, separator) {
  102. if (value === undefined || value.length === 0) {
  103. return ''
  104. }
  105. if (Array.isArray(value)) {
  106. value = value.join(',')
  107. }
  108. var actions = []
  109. var currentSeparator = undefined === separator ? ',' : separator
  110. var temp = value.split(currentSeparator)
  111. Object.keys(value.split(currentSeparator)).some((val) => {
  112. var match = false
  113. Object.keys(datas).some((key) => {
  114. if (datas[key].value == '' + temp[val]) {
  115. actions.push(datas[key].label + currentSeparator)
  116. match = true
  117. }
  118. })
  119. if (!match) {
  120. actions.push(temp[val] + currentSeparator)
  121. }
  122. })
  123. return actions.join('').substring(0, actions.join('').length - 1)
  124. }
  125. // 字符串格式化(%s )
  126. export function sprintf(str) {
  127. var args = arguments,
  128. flag = true,
  129. i = 1
  130. str = str.replace(/%s/g, function () {
  131. var arg = args[i++]
  132. if (typeof arg === 'undefined') {
  133. flag = false
  134. return ''
  135. }
  136. return arg
  137. })
  138. return flag ? str : ''
  139. }
  140. // 转换字符串,undefined,null等转化为""
  141. export function parseStrEmpty(str) {
  142. if (!str || str == 'undefined' || str == 'null') {
  143. return ''
  144. }
  145. return str
  146. }
  147. // 数据合并
  148. export function mergeRecursive(source, target) {
  149. for (var p in target) {
  150. try {
  151. if (target[p].constructor == Object) {
  152. source[p] = mergeRecursive(source[p], target[p])
  153. } else {
  154. source[p] = target[p]
  155. }
  156. } catch (e) {
  157. source[p] = target[p]
  158. }
  159. }
  160. return source
  161. }
  162. /**
  163. * 构造树型结构数据
  164. * @param {*} data 数据源
  165. * @param {*} id id字段 默认 'id'
  166. * @param {*} parentId 父节点字段 默认 'parentId'
  167. * @param {*} children 孩子节点字段 默认 'children'
  168. */
  169. export function handleTree(data, id, parentId, children) {
  170. let config = {
  171. id: id || 'id',
  172. parentId: parentId || 'parentId',
  173. childrenList: children || 'children'
  174. }
  175. var childrenListMap = {}
  176. var tree = []
  177. for (let d of data) {
  178. let id = d[config.id]
  179. childrenListMap[id] = d
  180. if (!d[config.childrenList]) {
  181. d[config.childrenList] = []
  182. }
  183. }
  184. for (let d of data) {
  185. let parentId = d[config.parentId]
  186. let parentObj = childrenListMap[parentId]
  187. if (!parentObj) {
  188. tree.push(d)
  189. } else {
  190. parentObj[config.childrenList].push(d)
  191. }
  192. }
  193. return tree
  194. }
  195. /**
  196. * 参数处理
  197. * @param {*} params 参数
  198. */
  199. export function tansParams(params) {
  200. let result = ''
  201. for (const propName of Object.keys(params)) {
  202. const value = params[propName]
  203. var part = encodeURIComponent(propName) + '='
  204. if (value !== null && value !== '' && typeof value !== 'undefined') {
  205. if (typeof value === 'object') {
  206. for (const key of Object.keys(value)) {
  207. if (
  208. value[key] !== null &&
  209. value[key] !== '' &&
  210. typeof value[key] !== 'undefined'
  211. ) {
  212. let params = propName + '[' + key + ']'
  213. var subPart = encodeURIComponent(params) + '='
  214. result += subPart + encodeURIComponent(value[key]) + '&'
  215. }
  216. }
  217. } else {
  218. result += part + encodeURIComponent(value) + '&'
  219. }
  220. }
  221. }
  222. return result
  223. }
  224. // 返回项目路径
  225. export function getNormalPath(p) {
  226. if (p.length === 0 || !p || p == 'undefined') {
  227. return p
  228. }
  229. let res = p.replace('//', '/')
  230. if (res[res.length - 1] === '/') {
  231. return res.slice(0, res.length - 1)
  232. }
  233. return res
  234. }
  235. // 验证是否为blob格式
  236. export function blobValidate(data) {
  237. return data.type !== 'application/json'
  238. }