当前位置: 首页 > news >正文

前端全局工具函数utils.js/正则(持续更新)

1. 接口返回提示

// 接口返回提示requestCodeTips(code, msg) {// code错误码,msg提示信息let errorrMessage = ''switch (Number(code)) {case 400:errorrMessage = '错误请求'break;case 401:errorrMessage = '未授权,请重新登录'break;case 403:errorrMessage = '拒绝访问'break;case 404:errorrMessage = '请求错误,未找到该资源'break;case 405:errorrMessage = '请求方法未允许'break;case 408:errorrMessage = '请求超时'break;case 500:errorrMessage = '服务器端出错啦'break;case 501:errorrMessage = '网络未实现'break;case 502:errorrMessage = '网络错误'break;case 503:errorrMessage = '服务不可用'break;case 504:errorrMessage = '网络超时'break;case 505:errorrMessage = 'http版本不支持该请求'break;default:errorrMessage = '连接错误'}this.toast(0, `${code}-${errorrMessage}-${msg?msg:''}`)},

2.全局文本/加载提示(uniapp/wxapp)

data: {msg: null, // 消息提示time: 300, // 时间hideLoadTimes: null, // 清除加载中的定时器showToastTimes: null, // 提示框的定时器},
/*** *  提示信息* state: 类型 (0 提示框  1 showLoading  2 hideLoading )* title: 标题* duration: 时间* icon: 图标*/async toast(state = 0, title = '', duration = 3000, icon = "none") {let errMsg = this.data.msg ? this.data.msg.errMsg : nullif (state == 0) { //// 隐藏加载中的定时器还在 if (this.data.hideLoadTimes || errMsg == 'showLoading:ok') {// 延后显示clearTimeout(this.data.showToastTimes)this.data.showToastTimes = setTimeout(async () => {this.data.msg = await uni.showToast({icon,title,duration,});this.data.showToastTimes = null}, this.data.time + 200)} else { // 隐藏加载中的定时器不能存在this.data.msg = await uni.showToast({icon,title,duration,});}} else if (state == 1) {this.data.time = this.data.time >= 900 ? 900 : this.data.time + 300this.data.msg = await uni.showLoading({title,mask: true});} else if (state == 2) {clearTimeout(this.data.hideLoadTimes)this.data.hideLoadTimes = setTimeout(async () => {this.data.msg = await uni.hideLoading();this.data.time = 300this.data.msg = null //this.data.hideLoadTimes = null}, this.data.time)}},

3.生成随机字符串

// 生成随机串randomString(len) {len = len || 32;var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/var maxPos = $chars.length;var pwd = '';for (let i = 0; i < len; i++) {pwd += $chars.charAt(Math.floor(Math.random() * maxPos));}// console.log("pwd", pwd)return pwd;},

4.获取年月日

// 获取当前年月日getDate(type) {// year 获取年// YMD 获取年月日if (type == "year") {let date = new Date();return date.getFullYear() //获取完整的年份(4位)}let date = new Date();let sign2 = ":";let year = date.getFullYear() // 年let month = date.getMonth() + 1; // 月let day = date.getDate(); // 日 let hour = date.getHours(); // 时let minutes = date.getMinutes(); // 分let seconds = date.getSeconds() //秒let weekArr = ['星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];let week = weekArr[date.getDay()]; //getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)// 给一位数的数据前面加 “0”if (month >= 1 && month <= 9) {month = "0" + month;}if (day >= 0 && day <= 9) {day = "0" + day;}if (hour >= 0 && hour <= 9) {hour = "0" + hour;}if (minutes >= 0 && minutes <= 9) {minutes = "0" + minutes;}if (seconds >= 0 && seconds <= 9) {seconds = "0" + seconds;}if (type == "YMD") { // 获取年月日// let date = new Date();return year + "-" + month + "-" + day}// console.log("---", year + "-" + month + "-" + day + " " + hour + sign2 + minutes + sign2 + seconds)return year + "-" + month + "-" + day + " " + hour + sign2 + minutes + sign2 + seconds;// // 需要自取,自行配置// let date = new Date();// date.getYear(); //获取当前年份(2位)// date.getFullYear(); //获取完整的年份(4位)// date.getMonth(); //获取当前月份(0-11,0代表1月)// date.getDate(); //获取当前日(1-31)// date.getDay(); //获取当前星期X(0-6,0代表星期天)// date.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)// date.getHours(); //获取当前小时数(0-23)// date.getMinutes(); //获取当前分钟数(0-59)// date.getSeconds(); //获取当前秒数(0-59)// date.getMilliseconds(); //获取当前毫秒数(0-999)// date.toLocaleDateString(); //获取当前日期// let mytime = date.toLocaleTimeString(); //获取当前时间// date.toLocaleString(); //获取日期与时间// // 获取当前月份// let nowMonth = date.getMonth() + 1;// // 获取当前是几号// let strDate = date.getDate();// // 添加分隔符“-”// let seperator = "-";// // 对月份进行处理,1-9月在前面添加一个“0”// if (nowMonth >= 1 && nowMonth <= 9) {// 	nowMonth = "0" + nowMonth;// }// // 对月份进行处理,1-9号在前面添加一个“0”// if (strDate >= 0 && strDate <= 9) {// 	strDate = "0" + strDate;// }// // 最后拼接字符串,得到一个格式为(yyyy-MM-dd)的日期// let nowDate = date.getFullYear() + seperator + nowMonth + seperator + strDate;// // 获取的是前一天日期// let time = (new Date).getTime() - 24 * 60 * 60 * 1000;// let yesday = new Date(time); // 获取的是前一天日期// console.log("mytime--",mytime)// console.log("nowDate",nowDate)// console.log("time",time)// console.log("yesday",yesday)// return yesday},

4.经纬度距离计算

// 经纬度距离计算/*** 计算地球上任意两点(经纬度)距离** @param long1 第一点经度* @param lat1  第一点纬度* @param long2 第二点经度* @param lat2  第二点纬度* @return 返回距离 单位:米*/distanceByLongNLat(long1, lat1, long2, lat2) {let a, b, R;R = 6378137; //地球半径lat1 = lat1 * Math.PI / 180.0;lat2 = lat2 * Math.PI / 180.0;a = lat1 - lat2;b = (long1 - long2) * Math.PI / 180.0;let d;let sa2, sb2;sa2 = Math.sin(a / 2.0);sb2 = Math.sin(b / 2.0);d = 2 * R * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1) * Math.cos(lat2) * sb2 * sb2));return d;},

5.文件格式

// 附件 格式getFileFormat(value) {return value == "mp4" || value == "flv" || value == "f4v" || value == "webm" ||value == "m4v" || value == "MOV" || value == "3gp" || value == "3g2" || value == "avi" ||value == "rmvb" ? value : "I"},

6.正则相关

1.手机号判断

// 判断是不是手机号isMobile(data) {return data && data.length == 11 &&/^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(17[0-9]{1})|(18[0-9]{1})|(19[0-9]{1})|)+\d{8})$/.test(data)},

2.手机号加星

// 手机号加星mobileAddStar(data) {let str = data && typeof data == 'string' ? data.substr(0, 3) + '****' + data.substr(7) : ''return str},

3. 1-6位数字,支持两位小数

// 1-6位数字,支持两位小数
// /^(?!(\d{7}|[0-9]{1,6}\.\d{3,}))(?=.*[1-9])\d{0,6}(\.\d{1,2})?$/

4.金额校验(大于等于零的两位小数或整数  不能超过亿)

// 金额校验  大于等于零的两位小数或整数  不能超过亿amountVerify(val, tit) {const str = /^([1-9]\d*|0)(\.\d{1,2})?$/if (!str.test(val)) {this.toast(0, `${tit}只能是大于等于零的两位小数或整数字!`)return false}let arr = (val + "").split('.')if ((arr[0] + "").length > 9) { // 长度超过限制this.toast(0, `${tit}位数超过限制,不能超过亿!`)return false}return true},

5.年份校验 (大于零的整数  不能超过100)

// 年份校验  大于零的整数  不能超过100yearVerify(val, tit) {const str = /^([1-9]\d*|0)?$/if (!str.test(val)) {this.toast(0, `${tit}只能是大于等于零的整数字!`)return false}let arr = (val + "").split('.')if ((arr[0] + "").length > 3 || (arr[0] * 1) > 999) { // 长度超过限制this.toast(0, `${tit}位数超过限制,不能大于999!`)return false}return true},

6.银行卡号校验

//校验银行卡号,银行卡校验规则(Luhn算法)validateBankNo(value) {// 银行账号是16-19位// 验证m-n位的数字:^\d{m,n}$if (value == "" || !this.isPositiveInteger(value)) {this.toast(0, `请输入正确的银行卡号,只能是16-19位的数字!`)return false;}const str = /^\d{16,19}$/if (!str.test(value)) {this.toast(0, `银行卡号只能是16-19位的数字!`)return false}return trueif (value == "" || !this.isPositiveInteger(value)) return false;var wei = [],sumOdd = 0,sumEven = 0,length = value.length;for (var j = 0; j < length; j++) {wei[j] = parseInt(value.substring(length - j - 1, length - j)); // 从最末一位开始提取,每一位上的数值}for (var i = 0; i < length / 2; i++) {sumOdd += wei[2 * i];if (wei[2 * i + 1] * 2 > 9) wei[2 * i + 1] = wei[2 * i + 1] * 2 - 9;else wei[2 * i + 1] *= 2;sumEven += wei[2 * i + 1];}return (sumOdd + sumEven) % 10 == 0;},

7.正整数

//正整数isPositiveInteger(value) {return /^[1-9][0-9]*$/.test(value);},

8.汉字带点校验

// 汉字带点校验isStarChinese(value) {return /^[\u4E00-\u9FA5]{2,8}([\u25CF\u00B7][\u4E00-\u9FA5]{2,8})*$/.test(value)},

9.纯汉字校验

// 纯汉字校验isChinese(value) {return /^([\u4e00-\u9fa5]{2,8})$/gi.test(value)},

10.带生僻字

// 带生僻字isChineseTwo(value) {return /^[\u9FA6-\u9FCB\u3400-\u4DB5\u4E00-\u9FA5]{2,5}([\u25CF\u00B7][\u9FA6-\u9FCB\u3400-\u4DB5\u4E00-\u9FA5]{2,5})*$/gi.test(value);},

http://www.lryc.cn/news/190285.html

相关文章:

  • 如何基于先进视频技术,构建互联网视频监控安全管理平台解决方案
  • 【React native】navigation 状态重置
  • 2023全国大学生软件测试大赛开发者测试练习题99分答案(ScapegoatTree2023)
  • Centos8 openjdk升级
  • 开启深度学习之门—《深度学习》
  • 优先调节阀位,条件调节阀位
  • oracle入门笔记六
  • 腾讯云优惠券种类、领取方法及使用教程分享
  • JavaScript使用类-模态窗口
  • 【轻松玩转MacOS】外部设备篇
  • location rewrite
  • XLSX.utils.sheet_to_json()解析excel,给空的单元格赋值为空字符串
  • 安装Docker(Linux:CentOS)
  • 2310月问题描述
  • y _hat[ [ 0, 1], y ]语法——pytorch张量花式索引
  • 高级岗位面试问题
  • 区块链游戏的开发框架
  • Windows Nginx 服务器部署(保姆级)
  • 常用的Linux命令及其用法
  • linux总结
  • java - 设计模式 - 状态模式
  • c/c++--编译指令(预处理之后) #pragma
  • 黑马JVM总结(三十二)
  • 接口自动化测试框架【reudom】
  • 【数据库问题】删除数据库失败,提示:there is 1 other session using the database
  • 【技术干货】如何快速创建商用照明 OEM APP?
  • 阿里云ModelScope 是一个“模型即服务”(MaaS)平台
  • Nodejs内置模块process
  • Vue2 修改了数组哪些方法,为什么
  • 均值滤波算法及例程