mutil.jsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * @Author: Johnhong9527
  3. * @Date: 2019-05-26 12:23:53
  4. * @Last Modified by: Johnhong9527
  5. * @Last Modified time: 2019-05-26 16:25:00
  6. */
  7. import { message } from 'antd';
  8. export default class MUtil {
  9. // 前往登录页面
  10. doLogin() {
  11. window.location.href = '/login?redirect=' + encodeURIComponent(window.location.pathname);
  12. }
  13. // 获取跳转前的path name
  14. getUrlParams(name) {
  15. let queryString = window.location.search.split('?')[1] || '',
  16. reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)'),
  17. result = queryString.match(reg);
  18. return result ? decodeURIComponent(result[2]) : null;
  19. }
  20. // 错误提示
  21. errorTips(errMsg) {
  22. message.error(`${errMsg || '好像哪里不对劲~'}`);
  23. }
  24. // 设置 本地缓存
  25. setStorage(name, data) {
  26. const typeDate = typeof data;
  27. if (typeDate === 'object') {
  28. window.localStorage.setItem(name, JSON.stringify(data));
  29. } else if (['number', 'string', 'boolean'].indexOf(typeDate) > -1) {
  30. window.localStorage.setItem(name, data);
  31. } else {
  32. this.errorTips('该数据类型不能用于本地存储');
  33. }
  34. }
  35. // 获取 缓存数据
  36. getStorage(name) {
  37. const data = window.localStorage.getItem(name);
  38. if (data) {
  39. return JSON.parse(data);
  40. } else {
  41. return '';
  42. }
  43. }
  44. // 删除 缓存数据
  45. removeStorage(name) {
  46. window.localStorage.removeItem(name);
  47. }
  48. }