ruoyi.js 6.1 KB

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