235 lines
4.5 KiB
JavaScript
235 lines
4.5 KiB
JavaScript
/**
|
||
* 工具类
|
||
*/
|
||
var utils = {
|
||
/**
|
||
* scene解码
|
||
*/
|
||
scene_decode(e) {
|
||
if (e === undefined)
|
||
return {};
|
||
let scene = decodeURIComponent(e),
|
||
params = scene.split(','),
|
||
data = {};
|
||
for (let i in params) {
|
||
var val = params[i].split(':');
|
||
val.length > 0 && val[0] && (data[val[0]] = val[1] || null)
|
||
}
|
||
return data;
|
||
},
|
||
|
||
/**
|
||
* 格式化日期格式 (用于兼容ios Date对象)
|
||
*/
|
||
format_date(time) {
|
||
// 将xxxx-xx-xx的时间格式,转换为 xxxx/xx/xx的格式
|
||
return time.replace(/\-/g, "/");
|
||
},
|
||
|
||
/**
|
||
* 格式化详情内容,去除图片之间的间隙,图片宽度最大100%
|
||
*/
|
||
format_content(str) {
|
||
return str.replace(/\<img/gi, '<img style="display:block; margin:0 auto; max-width:100%;" ');
|
||
},
|
||
|
||
/**
|
||
* 对象转URL
|
||
*/
|
||
urlEncode(data) {
|
||
var _result = [];
|
||
for (var key in data) {
|
||
var value = data[key];
|
||
if (value.constructor == Array) {
|
||
value.forEach(_value => {
|
||
_result.push(key + "=" + _value);
|
||
});
|
||
} else {
|
||
_result.push(key + '=' + value);
|
||
}
|
||
}
|
||
return _result.join('&');
|
||
},
|
||
|
||
/**
|
||
* 遍历对象
|
||
*/
|
||
objForEach(obj, callback) {
|
||
Object.keys(obj).forEach((key) => {
|
||
callback(obj[key], key);
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 是否在数组内
|
||
*/
|
||
inArray(search, array) {
|
||
for (var i in array) {
|
||
if (array[i] == search) {
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
},
|
||
|
||
/**
|
||
* 判断是否为正整数
|
||
*/
|
||
isPositiveInteger(value) {
|
||
return /(^[0-9]\d*$)/.test(value);
|
||
},
|
||
|
||
/**
|
||
* 获取场景值(scene)
|
||
*/
|
||
getSceneData(query) {
|
||
return query.scene ? this.scene_decode(query.scene) : {};
|
||
},
|
||
// 判断是否为身份证
|
||
isVail(value) {
|
||
|
||
if (!/^\d{17}(\d|x)$/i.test(value)) {
|
||
console.log('长度不对')
|
||
return false;
|
||
}
|
||
return true
|
||
},
|
||
// 判断是否为手机号
|
||
isPoneAvailable(pone) {
|
||
var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
|
||
if (!myreg.test(pone)) {
|
||
return false;
|
||
} else {
|
||
return true;
|
||
}
|
||
},
|
||
// 判断是否为座机或者手机号
|
||
isTelAvailable(tel) {
|
||
var myreg = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;
|
||
var myregs = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
|
||
var flag = false;
|
||
if (myreg.test(tel)) {
|
||
flag = true;
|
||
}
|
||
if (myregs.test(tel)) {
|
||
flag = true;
|
||
}
|
||
if(flag){
|
||
return true
|
||
}else{
|
||
return false
|
||
}
|
||
},
|
||
// 判断是否为电子邮箱
|
||
isMail(mail) {
|
||
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
|
||
if (filter.test(mail)){
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
},
|
||
// 判断是否为数字
|
||
isNum(num) {
|
||
var filter = /^[0-9]*$/;
|
||
if (filter.test(num)){
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
},
|
||
/*
|
||
数字转中文
|
||
*/
|
||
numberToChinese(num,type) {
|
||
/* const chineseNums = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
|
||
const chineseUnits = ['', '十', '百', '千'];
|
||
|
||
if (num === 0) {
|
||
return chineseNums[0];
|
||
}
|
||
|
||
let chineseStr = ''; */
|
||
/* let unitIndex = 0;
|
||
|
||
while (num > 0) {
|
||
const digit = num % 10;
|
||
if (digit !== 0) {
|
||
chineseStr = chineseNums[digit] + chineseUnits[unitIndex] + chineseStr;
|
||
} else if (chineseStr.charAt(0) !== chineseNums[0]) {
|
||
chineseStr = chineseNums[0] + chineseStr;
|
||
}
|
||
|
||
num = Math.floor(num / 10);
|
||
unitIndex++;
|
||
} */
|
||
// console.log(self,'self')
|
||
let chineseStr = "";
|
||
/* 季度 */
|
||
if(type == 'quarter'){
|
||
if(num == 1){
|
||
chineseStr = 'S_C_36'
|
||
}else if(num == 2){
|
||
chineseStr = 'S_C_37'
|
||
}else if(num == 3){
|
||
chineseStr = 'S_C_38'
|
||
}else if(num == 4){
|
||
chineseStr = 'S_C_39'
|
||
}
|
||
}else if(type == 'year'){
|
||
if(num == 1){
|
||
chineseStr = 'S_C_46'
|
||
}else if(num == 2){
|
||
chineseStr = 'S_C_47'
|
||
}else if(num == 3){
|
||
chineseStr = 'S_C_48'
|
||
}else if(num == 4){
|
||
chineseStr = 'S_C_49'
|
||
}else if(num == 5){
|
||
chineseStr = 'S_C_50'
|
||
}else if(num == 6){
|
||
chineseStr = 'S_C_51'
|
||
}else if(num == 7){
|
||
chineseStr = 'S_C_52'
|
||
}else if(num == 8){
|
||
chineseStr = 'S_C_53'
|
||
}else if(num == 9){
|
||
chineseStr = 'S_C_54'
|
||
}else if(num == 10){
|
||
chineseStr = 'S_C_55'
|
||
}else if(num == 11){
|
||
chineseStr = 'S_C_56'
|
||
}else if(num == 12){
|
||
chineseStr = 'S_C_57'
|
||
}
|
||
}else{
|
||
/* 月 */
|
||
if(num == 1){
|
||
chineseStr = 'S_C_11'
|
||
}else if(num == 2){
|
||
chineseStr = 'S_C_12'
|
||
}else if(num == 3){
|
||
chineseStr = 'S_C_13'
|
||
}else if(num == 4){
|
||
chineseStr = 'S_C_14'
|
||
}
|
||
}
|
||
|
||
|
||
return chineseStr;
|
||
},
|
||
/*
|
||
补零
|
||
*/
|
||
fillZ0(value){
|
||
if(!value){
|
||
return value
|
||
}
|
||
if(value.toString().length > 1){
|
||
return value
|
||
}
|
||
return `0${value}`
|
||
},
|
||
};
|
||
export default utils;
|