core.js 6.2 KB

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