utils.js 10.7 KB
Newer Older
钟是志's avatar
钟是志 committed
1 2 3 4
/***
 * 一站式正在使用此文件
 * 请谨慎使用
 * */
钟是志's avatar
钟是志 committed
5 6 7 8
import React from 'react';
import moment from 'moment';
import { Icon, message, notification } from 'antd';
import { getOneStopActiveMenus, getOnestopKey } from '../../Services';
9 10 11 12 13
let messageTime = new Date().getTime() - 3000;

/**
 * 校验 开始时间必须在结束时间之前的函数
 * */
钟是志's avatar
钟是志 committed
14
export function checkDate(endTime = '2019-01-01', startTime = '2018-12-31') {
15 16
  return moment(endTime)
    .isAfter(moment(startTime));
17 18 19 20 21 22
}

/**
 * 去除字符串中的所有html 标签
 * */
export function matchReg(str) {
23
  let reg = /<\/?.+?\/?>/g;
24 25
  return str.replace(reg, '')
    .replace(/&nbsp;/g, ' ');
26 27 28
}

export function htmlFormat(str) {
钟是志's avatar
钟是志 committed
29 30
  if (typeof str !== 'string') {
    return '';
31
  }
32
  const newTxt = str.replace(/\s+([^<>]+)(?=<)/g, function (match) {
钟是志's avatar
钟是志 committed
33
    return match.replace(/\s/g, '&nbsp;');
34 35
  });
  return newTxt;
36 37 38
}

export function countSpecialField(filedSpanBig, nameSpanBig) {
39 40 41 42
  let style = {};
  if (document.body.clientWidth > 1400) {
    if (filedSpanBig === 5) {
      // 当设置一行排列5个字段时 自定义宽度为20%
钟是志's avatar
钟是志 committed
43
      style = { width: '20%' };
44 45 46
    }
    if (filedSpanBig === 1 && nameSpanBig === 2) {
      // 当一行显示一个字段 然后名字又特别长时 使用这个width
钟是志's avatar
钟是志 committed
47
      style = { width: '6%' };
48 49 50
    }
  }
  return style;
51
}
52 53 54 55 56

/**
 * 深拷贝函数
 * */
export function deepCopy(obj, parent = null) {
57 58 59
  if (React.isValidElement(obj)) {
    return React.cloneElement(obj);
  }
60
  if (moment.isMoment(obj)) {
61 62
    return obj.clone(obj);
  }
钟是志's avatar
钟是志 committed
63
  if (['boolean', 'string', 'number'].indexOf(typeof obj) > -1 || !obj) {
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    return obj;
  }
  let result;
  if (obj.constructor === Array) {
    result = [];
  } else {
    result = {};
  }
  let keys = Object.keys(obj),
    key = null,
    temp = null,
    _parent = parent;
  // 该字段有父级则需要追溯该字段的父级
  while (_parent) {
    // 如果该字段引用了它的父级则为循环引用
    if (_parent.originalParent === obj) {
      // 循环引用直接返回同级的新对象
      return _parent.currentParent;
    }
    _parent = _parent.parent;
  }
  for (let i = 0; i < keys.length; i++) {
    key = keys[i];
    temp = obj[key];
    // 如果字段的值也是一个对象
钟是志's avatar
钟是志 committed
89
    if (temp && typeof temp === 'object') {
90 91 92 93
      // 递归执行深拷贝 将同级的待拷贝对象与新对象传递给 parent 方便追溯循环引用
      result[key] = deepCopy(temp, {
        originalParent: obj,
        currentParent: result,
钟是志's avatar
钟是志 committed
94
        parent: parent,
95 96 97 98 99 100
      });
    } else {
      result[key] = temp;
    }
  }
  return result;
101 102 103 104 105 106 107 108 109 110
}

/**
 * 获取表单元素中每个元素的值,
 * @param type
 * @param e
 * @param other
 * @returns {*|boolean}
 */
export function getFormElemValue(type, e, other) {
111
  let value = e;
112

113
  switch (type) {
钟是志's avatar
钟是志 committed
114
    case 'input':
115 116
      value = e.target.value;
      break;
钟是志's avatar
钟是志 committed
117
    case 'checkbox':
118 119
      value = e.target.checked;
      break;
钟是志's avatar
钟是志 committed
120
    case 'textarea':
121 122
      value = e.target.value;
      break;
钟是志's avatar
钟是志 committed
123
    case 'buttonUpload':
124 125
      value = e.url;
      break;
钟是志's avatar
钟是志 committed
126 127
    case 'upload':
      value = Array.isArray(e) ? e.join(',') : '';
128 129 130 131
      break;
    default:
      break;
  }
132

133
  return value;
134 135 136 137 138 139
}

/**
 * 生成随机字符串
 * */
export function randomStr() {
140 141 142
  return Math.random()
    .toString(36)
    .substr(2);
143 144 145
}

export function isJSON(str) {
钟是志's avatar
钟是志 committed
146
  if (typeof str == 'string') {
147 148
    try {
      JSON.parse(str);
钟是志's avatar
钟是志 committed
149
      if (typeof JSON.parse(str) === 'number') {
150 151 152 153 154 155 156
        return false;
      }
      return true;
    } catch (e) {
      return false;
    }
  }
157 158
}

钟是志's avatar
钟是志 committed
159
export function checkMustHaveValue(configFileds, data) {
160 161 162 163 164 165
  for (let item of configFileds) {
    if (!data[item.key] && data[item.key] !== false && data[item.key] !== 0) {
      return false;
    }
  }
  return true;
166 167 168
}

export function mustHaveValue(configFields, data) {
169 170 171 172 173 174 175 176 177 178 179 180
  for (let item of configFields) {
    if (!item.required) continue;
    if (Array.isArray(data[item.key]) && data[item.key].length < 1) {
      message.warning(`${item.name}是必填项请填写`);
      return false;
    }
    if (!data[item.key] && data[item.key] !== false && data[item.key] !== 0) {
      message.warning(`${item.name}是必填项请填写`);
      return false;
    }
  }
  return true;
181 182 183 184 185 186 187
}

/**
 * 判断数组中是否有重复元素
 * 通过数组排序,比较临近元素
 * */
export function isRepeat(ary) {
188 189 190 191 192 193 194 195 196 197 198
  if (ary.length <= 1) {
    return false;
  }
  let nary = ary.sort();
  for (let i = 0; i < ary.length - 1; i++) {
    if (nary[i] === nary[i + 1]) {
      return nary[i];
      // alert("数组重复内容:" + nary[i]);
    }
  }
  return false;
199 200 201
}

export function displayRender(label) {
202 203 204
  if (label && label.length) {
    return label[label.length - 1];
  } else {
钟是志's avatar
钟是志 committed
205
    return '';
206
  }
207 208 209
}

export function isEmptyValue(value) {
钟是志's avatar
钟是志 committed
210
  return typeof value === 'undefined' || value === null || value === '';
211 212 213 214
}

// 全局的通知消息组件
export function controlNotification(props) {
215 216 217 218 219 220 221
  const nowTime = new Date().getTime();
  if (nowTime - messageTime < 3000) {
    return;
  }
  messageTime = nowTime;
  notification.info({
    ...props,
222
    icon: <Icon type="info-circle" style={{ color: '#fa8c16' }}/>,
223 224
  });
  return true;
225
}
226

钟是志's avatar
钟是志 committed
227
export function setOneStopConfig(value) {
钟是志's avatar
钟是志 committed
228
  if (typeof value !== 'string') {
229 230
    value = JSON.stringify(value);
  }
钟是志's avatar
钟是志 committed
231
  localStorage.setItem('oneStopConfig', value);
232 233
}

钟是志's avatar
钟是志 committed
234
export function getOneStopConfig(key) {
钟是志's avatar
钟是志 committed
235
  let configList = localStorage.getItem('oneStopConfig');
236 237
  if (configList && isJSON(configList)) {
    let data = JSON.parse(configList);
钟是志's avatar
钟是志 committed
238 239 240
    if (data && typeof data === 'object') {
      if (typeof data === 'undefined') {
        return '';
241 242 243 244 245 246
      }
      return data[key] || false;
    }
  } else {
    return getOnestopKey(key);
  }
247
}
钟是志's avatar
钟是志 committed
248

249 250 251 252 253 254 255
export function setOneStopActiveMenusConfig(value) {
  window.oneStopActiveMenusConfig = value;
}

export function getOneStopActiveMenusConfig(key) {

  if (window.oneStopActiveMenusConfig && typeof window.oneStopActiveMenusConfig === 'object') {
钟是志's avatar
钟是志 committed
256
    return window.oneStopActiveMenusConfig[key] || false;
257 258 259 260 261 262
  } else {
    return getOneStopActiveMenus(key);
  }
}


263
export function diGuiTree(treeData = [], i = 0) {
264 265 266 267 268 269 270 271 272 273
  // for (let item of treeData) {
  //   if(i === 2){
  //     item.selectable = true;
  //   }
  //   if (item.children && item.children.length) {
  //     i += 1;
  //     diGuiTree(item.children, i);
  //   }
  // }
  return treeData;
钟是志's avatar
钟是志 committed
274
}
钟是志's avatar
钟是志 committed
275 276


钟是志's avatar
钟是志 committed
277
export { downloadFile } from './downloadFile';
278

279
// 校验密码是否符合 包含数字 字母 和特殊字符 解决 中医大的安全漏洞
280
export default function CheckPassWord(password = '', length = 12) {
281
  // console.log(password);
282
  if (!password || password.length < length) {
283
    // message.warning("密码过于简单, 请输入不小于8位的密码 且必须包含数字和字母!");
284
    // console.log('位数不够');
285 286
    return false;
  }
287
  let cRegex = new RegExp(/.*[\u4e00-\u9fa5]+.*$/);
288
  if (cRegex.test(password)) {
289 290 291
    message.warning('密码中不能包含中文字符!');
    return false;
  }
292

293
  let pwdRegex = new RegExp(`(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{${length},30}`);
294
  if (!pwdRegex.test(password)) {
钟是志's avatar
钟是志 committed
295
    // alert("您的密码复杂度太低(密码中必须包含字母、数字、特殊字符),请及时修改密码!");
296 297
    return false;
  }
298
  return true;
299
}
300

301 302 303 304 305

/**
 *
 * 检查文本格式是否正确
 * */
306
export function checkInputType(data, type) {
307 308
  switch (type) {
    case 'phone':
309
      if (!(/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(data))) {
310 311 312 313 314
        message.warning('手机号码格式错误!');
        return false;
      }
      break;
    case 'email':
315
      if (!(/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(data))) {
316 317 318 319 320
        message.warning('邮箱格式错误!');
        return false;
      }
      break;
    case 'idCard':
321
      if (!(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(data))) {
322 323 324 325 326 327 328 329 330 331 332
        message.warning('身份证号码格式错误!');
        return false;
      }
      break;
    default:
      break;
  }

  return true;
}

钟是志's avatar
钟是志 committed
333 334

export { getHeaders } from './getHeaders';
335

336

钟是志's avatar
钟是志 committed
337
export function getIsA_Ba() { // 判断当前环境是不是阿坝学校 然后做定制需求. 主要用于定制开发
338
  return window.specialImportantSystemConfig?.schoolName && window.specialImportantSystemConfig?.schoolName.indexOf('阿坝') > -1;
钟是志's avatar
钟是志 committed
339 340 341
}

export function getIsBei_Dian() {  // 判断当前环境是不是北电科学校 然后做定制需求. 主要用于定制开发
342
  return window.specialImportantSystemConfig?.schoolName && window.specialImportantSystemConfig?.schoolName.indexOf('北京电子科技') > -1;
343
}
344

钟是志's avatar
钟是志 committed
345
export function getIsGui_Jian() {  // 判断当前环境是不是贵建 然后做定制需求. 主要用于定制开发
346
  return window.specialImportantSystemConfig?.schoolName && window.specialImportantSystemConfig?.schoolName.indexOf('贵州建设职业') > -1;
347 348
}

349

350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
// (function (_0x49c6e2, _0x5afabe) {
//   const _0x125f6a = _0x3342,
//     _0x2c1408 = _0x49c6e2();
//   while (!![]) {
//     try {
//       const _0x53c0bd = -parseInt(_0x125f6a(0x190)) / 0x1 * (-parseInt(_0x125f6a(0x19b)) / 0x2) + parseInt(_0x125f6a(0x19c)) / 0x3 * (-parseInt(_0x125f6a(0x194)) / 0x4) + -parseInt(_0x125f6a(0x18e)) / 0x5 * (parseInt(_0x125f6a(0x18f)) / 0x6) + -parseInt(_0x125f6a(0x197)) / 0x7 * (-parseInt(_0x125f6a(0x191)) / 0x8) + -parseInt(_0x125f6a(0x19a)) / 0x9 + -parseInt(_0x125f6a(0x198)) / 0xa + parseInt(_0x125f6a(0x192)) / 0xb * (parseInt(_0x125f6a(0x19d)) / 0xc);
//       if (_0x53c0bd === _0x5afabe) break; else _0x2c1408['push'](_0x2c1408['shift']());
//     } catch (_0x47358a) {
//       _0x2c1408['push'](_0x2c1408['shift']());
//     }
//   }
// }(_0x1b15, 0x55be2));
//
// function _0x1b15() {
//   const _0x282d1 = ['serviceCurrentDate', 'bearer\x20', '1673sgmbsL', '2312160uyJpgu', 'getTime', '1780569IrbRgk', '24Kigyjl', '9HzRNWg', '408YAMsHI', 'log', '1268455sEkXMX', '6HkuEHp', '12443zxYqpr', '12856jCmFlr', '162118klnJKs', 'typeString', '708ajTqoU'];
//   _0x1b15 = function () {
//     return _0x282d1;
//   };
//   return _0x1b15();
// }
//
// function _0x3342(_0xdcd98d, _0x28a347) {
//   const _0x1b15b7 = _0x1b15();
//   return _0x3342 = function (_0x334243, _0x3607d2) {
//     _0x334243 = _0x334243 - 0x18e;
//     let _0x35962a = _0x1b15b7[_0x334243];
//     return _0x35962a;
//   }, _0x3342(_0xdcd98d, _0x28a347);
// }
//
// export function getHeadersRemix(_0x202751 = '') {
//   const _0x549aa2 = _0x3342,
//     _0x4e39f8 = getToken(),
//     _0x1a0607 = getCurrentUser(),
//     _0xa4505 = window[_0x549aa2(0x195)] || new Date()[_0x549aa2(0x199)](),
//     _0x4437ff = _0x4e39f8 + _0x1a0607['xgUserId'] + _0x1a0607[_0x549aa2(0x193)] + _0xa4505;
//   return console[_0x549aa2(0x19e)](Md5(_0x4437ff)), {
//     'headers': {
//       'Authorization': _0x549aa2(0x196) + _0x4e39f8,
//       'awc_auth': Md5(_0x4437ff),
//       'awc_timestamp': _0xa4505
//     }
//   };
// }