1
0

tiegu.js 5.5 KB

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