1、获取URL参数

说明:根据参数名从url中获取对应参数值

export function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var param = location.href;
    if(location.href.indexOf("?") > 0) {
        param = location.href.split("?")[1];
    }
    var r = param.match(reg);
    if(r != null)
        return decodeURIComponent(r[2]);smkVersion
    return null;
}

引用方法

import {getQueryString} from "@/assets/js/util.js"
getQueryString("param1")  

2、生成流水号

说明:24位数 - 时间(YYYYMMDDhhmmss)+随机数10位

export function getSerialNo() {
    var day = parseTime(new Date(),'{y}{m}{d}{h}{i}{s}');
    var len = 10;
    var chars = "0123456789";
    var maxPos = chars.length;
    var rand = "";
    for (var i = 0; i < len; i++) {
        rand += chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return day + rand;
}

3、生成uuid

说明:随机18位数,常用于前端生成唯一用户标识或请求号

export const getUuid = () => {
    const s = [];
    const hexDigits = "0123456789abcdef";
    for (let i = 0; i < 22; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4"; 
    s[21] = hexDigits.substr((s[18] & 0x3) | 0x8, 1); 
    s[3] = s[8] = s[13] = s[18] = "";
    const uuid = s.join("");
    return uuid;
}

4、isAndroid - 是否安卓环境

export const isAndroid = () => {
    const u = navigator.userAgent;
    const isAdr = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
    return isAdr;
}

5、isIos - 是否ios环境

export const isIos = () =>{
    const isiOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    return isiOS
}

6、isHarmony - 是否鸿蒙环境

export const isHarmony = () => {
    let isHarmony = false
    const ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf('platform/harmony') > 0) {
        isHarmony = true;
    }
    return isHarmony;
}

7、isWechat - 是否微信环境

export const isWechat = () => {
    let isWechat = false
    const ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf('micromessenger') > 0) { //微信内部
        isWechat = true;
    }
    return isWechat;
}

8、isWechatApp - 是否微信小程序环境

export const isWechatApp = () => {
    let isWechatApp = false
    const ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf('micromessenger') > 0 && ua.indexOf('miniprogram')>0 ) {
        isWechatApp = true;
    }
    return isWechatApp;
}
//true - 是微信小程序环境;false - 非微信小程序环境

9、setDocumentTitle - 重写页面title

export const setDocumentTitle = (title)=>{
    if (window.document.title === title) {
        return
    }
    document.title = title
}

10、系统版本号新老对比

function compareVersion(version1, version2) {
  const v1 = version1.split('.');
  const v2 = version2.split('.');
   
  const length = Math.max(v1.length, v2.length);
   
  for (let i = 0; i < length; i++) {
    const num1 = parseInt(v1[i] || 0);
    const num2 = parseInt(v2[i] || 0);
     
    if (num1 > num2) {
      return 1;
    } else if (num1 < num2) {
      return -1;
    }
  }
   
  return 0;
}

// 示例用法
    const smkVersion = navigator.userAgent.split("smkVersion/")[1].split(';')[0];
    const assignVersion = "6.7.1";
    const result = compareVersion(smkVersion, assignVersion);
 
 
    if (result === 1) {
      console.log(`${smkVersion} 大于 ${assignVersion}`);
    } else if (result === -1) {
      console.log(`${smkVersion} 小于 ${assignVersion}`);
    } else {
      console.log(`${smkVersion} 等于 ${assignVersion}`);
    }
      }
    }

11、getUrlByDeleteQuerys - 删除链接中的某些参数

function getUrlByDeleteQuerys(deleteArr, href) {
  if(!href)href = location.href
  let arr = href.split('?')
  let route = arr[0]
  if(arr.length == 1)return route
   
  const queryReplace = (query, str) => {
    var reg = new RegExp(str + "\=[^\&]+?\&|[\&|\?]" + str + "\=[^\&]+$")
    return query.replace(reg, '')
  }
   
  let locationSearch = arr[1]
  for(let deleteStr of deleteArr) {
    locationSearch = queryReplace(locationSearch, deleteStr)
  }
  if(locationSearch)locationSearch = '?' + locationSearch
  return route + locationSearch
}

// 示例用法,登录后删除无用参数
let backUrl = getUrlByDeleteQuerys(['code', 'openid', 'wxUnionid', 'isRegister']);
history.replaceState({}, '', backUrl)

 

posted on 2025-04-29 16:33  小海豚Dolphins  阅读(14)  评论(0)    收藏  举报