public.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. export function judgmentNetworkReturnAddress() {
  2. /*判断是否是内网IP*/
  3. // 获取当前页面url
  4. var curPageUrl = window.location.href;
  5. // console.log('curPageUrl-0 '+curPageUrl);
  6. var reg1 = /(http|ftp|https|www):\/\//g;//去掉前缀
  7. curPageUrl =curPageUrl.replace(reg1,'');
  8. // console.log('curPageUrl-1 '+curPageUrl);
  9. var reg2 = /\:+/g;//替换冒号为一点
  10. curPageUrl =curPageUrl.replace(reg2,'.');
  11. // console.log('curPageUrl-2 '+curPageUrl);
  12. curPageUrl = curPageUrl.split('.');//通过一点来划分数组
  13. // console.log(curPageUrl);
  14. var ipAddress = curPageUrl[0]+'.'+curPageUrl[1]+'.'+curPageUrl[2]+'.'+curPageUrl[3];
  15. var isInnerIp = false;//默认给定IP不是内网IP
  16. var ipNum = getIpNum(ipAddress);
  17. /**
  18. * 私有IP:A类 10.0.0.0 -10.255.255.255
  19. * B类 172.16.0.0 -172.31.255.255
  20. * C类 192.168.0.0 -192.168.255.255
  21. * D类 127.0.0.0 -127.255.255.255(环回地址)
  22. **/
  23. var aBegin = getIpNum("10.0.0.0");
  24. var aEnd = getIpNum("10.255.255.255");
  25. var bBegin = getIpNum("172.16.0.0");
  26. var bEnd = getIpNum("172.31.255.255");
  27. var cBegin = getIpNum("192.168.0.0");
  28. var cEnd = getIpNum("192.168.255.255");
  29. var dBegin = getIpNum("127.0.0.0");
  30. var dEnd = getIpNum("127.255.255.255");
  31. isInnerIp = isInner(ipNum,aBegin,aEnd) || isInner(ipNum,bBegin,bEnd) || isInner(ipNum,cBegin,cEnd) || isInner(ipNum,dBegin,dEnd);
  32. // console.log('是否是内网:'+isInnerIp);
  33. return isInnerIp?process.env.VUE_APP_BASE_LOCAL_API:process.env.VUE_APP_BASE_API;
  34. /*获取IP数*/
  35. function getIpNum(ipAddress) {
  36. var ip = ipAddress.split(".");
  37. var a = parseInt(ip[0]);
  38. var b = parseInt(ip[1]);
  39. var c = parseInt(ip[2]);
  40. var d = parseInt(ip[3]);
  41. var ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
  42. return ipNum;
  43. }
  44. function isInner(userIp,begin,end){
  45. return (userIp>=begin) && (userIp<=end);
  46. }
  47. }
  48. /**
  49. * 参数处理
  50. * @param {*} params 参数
  51. */
  52. export function tansParams(params) {
  53. let result = ''
  54. for (const propName of Object.keys(params)) {
  55. const value = params[propName];
  56. var part = encodeURIComponent(propName) + "=";
  57. if (value !== null && typeof (value) !== "undefined") {
  58. if (typeof value === 'object') {
  59. for (const key of Object.keys(value)) {
  60. if (value[key] !== null && typeof (value[key]) !== 'undefined') {
  61. let params = propName + '[' + key + ']';
  62. var subPart = encodeURIComponent(params) + "=";
  63. result += subPart + encodeURIComponent(value[key]) + "&";
  64. }
  65. }
  66. } else {
  67. result += part + encodeURIComponent(value) + "&";
  68. }
  69. }
  70. }
  71. return result
  72. }
  73. // 日期格式化
  74. export function parseTime(time, pattern) {
  75. if (arguments.length === 0 || !time) {
  76. return null
  77. }
  78. if(time.indexOf('T')!== -1){
  79. let newTime = time.split('T')
  80. time = newTime[0] + ' ' + newTime[1]
  81. }
  82. const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  83. let date
  84. if (typeof time === 'object') {
  85. date = time
  86. } else {
  87. if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
  88. time = parseInt(time)
  89. } else if (typeof time === 'string') {
  90. time = time.replace(new RegExp(/-/gm), '/');
  91. }
  92. if ((typeof time === 'number') && (time.toString().length === 10)) {
  93. time = time * 1000
  94. }
  95. date = new Date(time)
  96. }
  97. const formatObj = {
  98. y: date.getFullYear(),
  99. m: date.getMonth() + 1,
  100. d: date.getDate(),
  101. h: date.getHours(),
  102. i: date.getMinutes(),
  103. s: date.getSeconds(),
  104. a: date.getDay()
  105. }
  106. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  107. let value = formatObj[key]
  108. // Note: getDay() returns 0 on Sunday
  109. if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
  110. if (result.length > 0 && value < 10) {
  111. value = '0' + value
  112. }
  113. return value || 0
  114. })
  115. return time_str
  116. }