ruoyi.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. import {idCard} from '@/utils/regular'
  2. /**
  3. * 通用js方法封装处理
  4. * Copyright (c) 2019 ruoyi
  5. */
  6. // 日期格式化
  7. export function parseTime(time, pattern) {
  8. if (arguments.length === 0 || !time) {
  9. return null
  10. }
  11. const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  12. let date
  13. if (typeof time === 'object') {
  14. date = time
  15. } else {
  16. if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
  17. time = parseInt(time)
  18. } else if (typeof time === 'string') {
  19. time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
  20. }
  21. if ((typeof time === 'number') && (time.toString().length === 10)) {
  22. time = time * 1000
  23. }
  24. date = new Date(time)
  25. }
  26. const formatObj = {
  27. y: date.getFullYear(),
  28. m: date.getMonth() + 1,
  29. d: date.getDate(),
  30. h: date.getHours(),
  31. i: date.getMinutes(),
  32. s: date.getSeconds(),
  33. a: date.getDay()
  34. }
  35. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  36. let value = formatObj[key]
  37. // Note: getDay() returns 0 on Sunday
  38. if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
  39. if (result.length > 0 && value < 10) {
  40. value = '0' + value
  41. }
  42. return value || 0
  43. })
  44. return time_str
  45. }
  46. // 表单重置
  47. export function resetForm(refName) {
  48. if (this.$refs[refName]) {
  49. this.$refs[refName].resetFields();
  50. }
  51. }
  52. // 添加日期范围
  53. export function addDateRange(params, dateRange, propName) {
  54. let search = params;
  55. search.params = typeof (search.params) === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {};
  56. dateRange = Array.isArray(dateRange) ? dateRange : [];
  57. if (typeof (propName) === 'undefined') {
  58. search.params['beginTime'] = dateRange[0];
  59. search.params['endTime'] = dateRange[1];
  60. } else {
  61. search.params['begin' + propName] = dateRange[0];
  62. search.params['end' + propName] = dateRange[1];
  63. }
  64. return search;
  65. }
  66. // 回显数据字典
  67. export function selectDictLabel(datas, value) {
  68. if (value === undefined) {
  69. return "";
  70. }
  71. var actions = [];
  72. Object.keys(datas).some((key) => {
  73. if (datas[key].value == ('' + value)) {
  74. actions.push(datas[key].label);
  75. return true;
  76. }
  77. })
  78. if (actions.length === 0) {
  79. actions.push(value);
  80. }
  81. return actions.join('');
  82. }
  83. // 回显数据字典(字符串数组)
  84. export function selectDictLabels(datas, value, separator) {
  85. if (value === undefined) {
  86. return "";
  87. }
  88. var actions = [];
  89. var currentSeparator = undefined === separator ? "," : separator;
  90. var temp = value.split(currentSeparator);
  91. Object.keys(value.split(currentSeparator)).some((val) => {
  92. var match = false;
  93. Object.keys(datas).some((key) => {
  94. if (datas[key].value == ('' + temp[val])) {
  95. actions.push(datas[key].label + currentSeparator);
  96. match = true;
  97. }
  98. })
  99. if (!match) {
  100. actions.push(temp[val] + currentSeparator);
  101. }
  102. })
  103. return actions.join('').substring(0, actions.join('').length - 1);
  104. }
  105. // 字符串格式化(%s )
  106. export function sprintf(str) {
  107. var args = arguments, flag = true, i = 1;
  108. str = str.replace(/%s/g, function () {
  109. var arg = args[i++];
  110. if (typeof arg === 'undefined') {
  111. flag = false;
  112. return '';
  113. }
  114. return arg;
  115. });
  116. return flag ? str : '';
  117. }
  118. // 转换字符串,undefined,null等转化为""
  119. export function parseStrEmpty(str) {
  120. if (!str || str == "undefined" || str == "null") {
  121. return "";
  122. }
  123. return str;
  124. }
  125. // 数据合并
  126. export function mergeRecursive(source, target) {
  127. for (var p in target) {
  128. try {
  129. if (target[p].constructor == Object) {
  130. source[p] = mergeRecursive(source[p], target[p]);
  131. } else {
  132. source[p] = target[p];
  133. }
  134. } catch (e) {
  135. source[p] = target[p];
  136. }
  137. }
  138. return source;
  139. };
  140. /**
  141. * 构造树型结构数据
  142. * @param {*} data 数据源
  143. * @param {*} id id字段 默认 'id'
  144. * @param {*} parentId 父节点字段 默认 'parentId'
  145. * @param {*} children 孩子节点字段 默认 'children'
  146. */
  147. export function handleTree(data, id, parentId, children) {
  148. let config = {
  149. id: id || 'id',
  150. parentId: parentId || 'parentId',
  151. childrenList: children || 'children'
  152. };
  153. var childrenListMap = {};
  154. var nodeIds = {};
  155. var tree = [];
  156. for (let d of data) {
  157. let parentId = d[config.parentId];
  158. if (childrenListMap[parentId] == null) {
  159. childrenListMap[parentId] = [];
  160. }
  161. nodeIds[d[config.id]] = d;
  162. childrenListMap[parentId].push(d);
  163. }
  164. for (let d of data) {
  165. let parentId = d[config.parentId];
  166. if (nodeIds[parentId] == null) {
  167. tree.push(d);
  168. }
  169. }
  170. for (let t of tree) {
  171. adaptToChildrenList(t);
  172. }
  173. function adaptToChildrenList(o) {
  174. if (childrenListMap[o[config.id]] !== null) {
  175. o[config.childrenList] = childrenListMap[o[config.id]];
  176. }
  177. if (o[config.childrenList]) {
  178. for (let c of o[config.childrenList]) {
  179. adaptToChildrenList(c);
  180. }
  181. }
  182. }
  183. return tree;
  184. }
  185. /**
  186. * 参数处理
  187. * @param {*} params 参数
  188. */
  189. export function tansParams(params) {
  190. let result = ''
  191. for (const propName of Object.keys(params)) {
  192. const value = params[propName];
  193. var part = encodeURIComponent(propName) + "=";
  194. if (value !== null && value !== "" && typeof (value) !== "undefined") {
  195. if (typeof value === 'object') {
  196. for (const key of Object.keys(value)) {
  197. if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
  198. let params = propName + '[' + key + ']';
  199. var subPart = encodeURIComponent(params) + "=";
  200. result += subPart + encodeURIComponent(value[key]) + "&";
  201. }
  202. }
  203. } else {
  204. result += part + encodeURIComponent(value) + "&";
  205. }
  206. }
  207. }
  208. return result
  209. }
  210. // 验证是否为blob格式
  211. export async function blobValidate(data) {
  212. try {
  213. const text = await data.text();
  214. JSON.parse(text);
  215. return false;
  216. } catch (error) {
  217. return true;
  218. }
  219. }
  220. /**
  221. * 特殊时间格式处理 例如20200329
  222. * @param {*} data 数据源
  223. * @param {*} type 类型 默认 'date'
  224. * @param {*} theme 转换的格式 默认 0
  225. */
  226. let themeData=[
  227. ['-','-'],
  228. ['年','月','日'],
  229. ]
  230. export function special(data,type='date',theme=0){
  231. let time="";
  232. switch (type) {
  233. case 'date':
  234. if(data&&data.length>=8)
  235. {
  236. time=data.slice(0,data.length-4)+themeData[theme][0]+data.slice(-4,-2)+themeData[theme][1]+data.slice(-2)+(themeData[theme][2]?themeData[theme][2]:'')
  237. }
  238. break;
  239. case 'time':
  240. if(data&&data.length>=14)
  241. {
  242. time=data.slice(0,data.length-10)+themeData[theme][0]+data.slice(-10,-8)+themeData[theme][1]+data.slice(-8,-6)+(themeData[theme][2]?themeData[theme][2]:'')
  243. time+=" "+data.slice(-6,-4)+':'+data.slice(-4,-2)+':'+data.slice(-2)
  244. }
  245. break;
  246. default:break;
  247. }
  248. return time;
  249. }
  250. /**
  251. * 删除数据处理
  252. * @param {*} data 数据源
  253. * @param {*} name 需要显示的字段名
  254. * @param {*} title 需要显示字段的名称
  255. * @param {*} index 单个删除的顺序号
  256. */
  257. export function changeDelData(data,name,title,index){
  258. return "顺序号【"+(index)+"】"+title+"【"+data[name]+"】";
  259. }
  260. /**
  261. * 两个对象比对
  262. * @param {*} object 数据源1
  263. * @param {*} other 数据源2
  264. * @param {*} def 默认需要穿的参数
  265. * 以数据源1作为根本
  266. */
  267. export function comparisonObject(object,other,def){
  268. let diff = {};
  269. let vChildren;
  270. Object.keys(object).forEach(key=>{
  271. if (typeof object[key] === "object" && typeof other[key] === "object" && object[key] && other[key]) {
  272. vChildren = comparisonObject(object[key], other[key],def);
  273. if (vChildren) {
  274. diff[key] = vChildren;
  275. }
  276. } else if (object[key] !== other[key]) {
  277. diff[key] = (object[key]==null?'':object[key]);
  278. if(def&&object[def])
  279. {
  280. diff[def]=object[def]
  281. }
  282. }
  283. })
  284. if (Object.keys(diff).length===0)
  285. {
  286. return null;
  287. }
  288. return diff;
  289. }
  290. /**
  291. * 两个对象比对
  292. * @param {*} data 全部数据
  293. * @param {*} id 需要替换数据的id
  294. * @param {*} value 需要替换的数据
  295. */
  296. export function dataReplacement(data,id,value){
  297. return data.map((item)=>{
  298. if(item.id==id)
  299. {
  300. return {...item,...value}
  301. }
  302. return item
  303. })
  304. }
  305. /**
  306. * 对象的深度拷贝
  307. */
  308. export function deepCopy(data) {
  309. return JSON.parse(JSON.stringify(data))
  310. }
  311. /*
  312. * 检验身份证的方法
  313. */
  314. export function valid(rules, zjhmKey, zjlx = '01'){
  315. console.log(rules);
  316. console.log(zjhmKey);
  317. console.log(zjlx);
  318. if (zjlx !== '01') {
  319. rules[zjhmKey] = [
  320. { required: true, message: '证件号码不能为空', trigger: 'blur' },
  321. { max: 18, message: '证件号码不能超过18个字符', trigger: 'blur' }
  322. ];
  323. }else {
  324. rules[zjhmKey]= [
  325. { required: true, message: '证件号码不能为空', trigger: 'blur' },
  326. { max: 18, message: '证件号码不能超过18个字符', trigger: 'blur' },
  327. { validator: idCard, trigger: 'blur' }
  328. ];
  329. }
  330. }
  331. export function checkPassword(str){
  332. const SPEC_CHARACTERS = " !\"#$%&'()*+,-./:;<=>?@\\]\\[^_`{|}~";
  333. // 纯字母
  334. const character = /[a-zA-Z]{1,}$/;
  335. // 纯数字
  336. const numberic = /[0-9]{1,}$/;
  337. // 字母和数字
  338. const number_and_character = /((^[a-zA-Z]{1,}[0-9]{1,}[a-zA-Z0-9]*)+)|((^[0-9]{1,}[a-zA-Z]{1,}[a-zA-Z0-9]*)+)$/;
  339. let isLegal = false;
  340. let hasSpecChar = false;
  341. const charArray = str.split("");
  342. charArray.forEach(item=>{
  343. if (SPEC_CHARACTERS.indexOf(item) != -1) {
  344. hasSpecChar = true;
  345. // 替换此字符串
  346. str = str.replace(item, ' ');
  347. }
  348. })
  349. const excSpecCharStr = str.replace(" ", "");
  350. const isPureNum = numberic.test(excSpecCharStr);
  351. const isPureChar = character.test(excSpecCharStr);
  352. const isNumAndChar = number_and_character.test(excSpecCharStr);
  353. isLegal = ((isPureNum && hasSpecChar)
  354. || (isPureChar && hasSpecChar) || isNumAndChar && hasSpecChar) || isNumAndChar;
  355. return isLegal;
  356. }