- // 接口返回提示
- 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:''}`)
- },
- 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 : null
-
- if (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 + 300
- this.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 = 300
- this.data.msg = null //
- this.data.hideLoadTimes = null
- }, this.data.time)
- }
- },
- // 生成随机串
- 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
- },
- // 经纬度距离计算
- /**
- * 计算地球上任意两点(经纬度)距离
- *
- * @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;
- },
- // 附件 格式
- 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"
- },
- // 判断是不是手机号
- 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)
- },
- // 手机号加星
- mobileAddStar(data) {
- let str = data && typeof data == 'string' ? data.substr(0, 3) + '****' + data.substr(7) : ''
- return str
- },
// 1-6位数字,支持两位小数
// /^(?!(\d{7}|[0-9]{1,6}\.\d{3,}))(?=.*[1-9])\d{0,6}(\.\d{1,2})?$/
- // 金额校验 大于等于零的两位小数或整数 不能超过亿
- 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
- },
- // 年份校验 大于零的整数 不能超过100
- yearVerify(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
- },
- //校验银行卡号,银行卡校验规则(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 true
-
- if (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;
- },
- //正整数
- isPositiveInteger(value) {
- return /^[1-9][0-9]*$/.test(value);
- },
- // 汉字带点校验
- isStarChinese(value) {
- return /^[\u4E00-\u9FA5]{2,8}([\u25CF\u00B7][\u4E00-\u9FA5]{2,8})*$/.test(value)
- },
- // 纯汉字校验
- isChinese(value) {
- return /^([\u4e00-\u9fa5]{2,8})$/gi.test(value)
- },
- // 带生僻字
- isChineseTwo(value) {
- return /^[\u9FA6-\u9FCB\u3400-\u4DB5\u4E00-\u9FA5]{2,5}([\u25CF\u00B7][\u9FA6-\u9FCB\u3400-\u4DB5\u4E00-\u9FA5]{2,5})*$/gi
- .test(value);
- },