You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
4.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 公共方法
*
*/
import moment from 'moment';
import { lang } from 'moment';
import { ROOTPATH } from "@/common/config.js"
const base = {
imgHost:(img)=>{
return ROOTPATH + '/wx-icon/' + img
},
base64ToFile:(dataurl, filename = 'file') => {
let arr = dataurl.split(',')
let mime = arr[0].match(/:(.*?);/)[1]
let suffix = mime.split('/')[1]
let bstr = atob(arr[1])
let n = bstr.length
let u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], `${filename}.${suffix}`, {
type: mime
})
},
toast : (msg, time, callback) => {
uni.showToast({
icon: "none",
title: msg,
duration: time || 2000,
success: function(res) {
if (callback && typeof(callback) == 'function') {
console.log(callback)
callback()
}
}
})
},
// 转换日期为日月
formatDateToMonthDay : (dateString) => {
const month = moment(dateString).format('MM')
const day = moment(dateString).format('DD')
return `${month}${day}`;
},
// 比较当前时间是否在活动期间 在true 不在false
isCurrentTimeBetween : (startTime, endTime) => {
let that = this
const now = moment().valueOf()
const start = moment(startTime).valueOf();
const end = moment(endTime).valueOf();
return now < start || now > end;
},
// 验证是否填写的是中文
isOnlyChinese : (str) => {
const chineseRegex = /^[\u4e00-\u9fa5]+$/;
return chineseRegex.test(str);
},
// 验证是否为空
isNull : (p) => {
return p == '' || p == undefined || p == null || p == 'undefined' || p == 'null';
},
// 验证手机号
isMobile : (phoneNumber) => {
return /^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(phoneNumber)
},
isMail (val) {
return /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(val)
},
// 整数
isInteger(str) {
// 正则表达式,用于匹配整数,包括带正号的情况
const regex = /^[+-]?\d+$/;
// 测试字符串是否匹配正则表达式
return regex.test(str);
},
// 数字
isNumber(str) {
// 正则表达式,用于匹配数字
const regex = /^-?\d+(\.\d+)?$/;
// 测试字符串是否匹配正则表达式
return regex.test(str);
},
// 分享
shareInfo : () => {
return{
title:"苏州科技商学院",
imageUrl:"/static/share.jpg"
}
},
// 检查是否空对象
isEmptyObject : (obj) => {
// 检查传入参数是否为一个对象
if (typeof obj !== 'object' || obj === null) {
throw new Error('Input must be an object');
}
// 使用Object.keys()方法获取对象的属性键数组并检查其长度是否为0
return Object.keys(obj).length === 0;
},
deepCopy(data) {
//string,number,bool,null,undefined,symbol
//object,array,date
if (data && typeof data === "object") {
//针对函数的拷贝
if (typeof data === "function") {
let tempFunc = data.bind(null);
tempFunc.prototype = this.deepCopy(data.prototype);
return tempFunc;
}
switch (Object.prototype.toString.call(data)) {
case "[object String]":
return data.toString();
case "[object Number]":
return Number(data.toString());
case "[object Boolean]":
return Boolean(data.toString());
case "[object Date]":
return new Date(data.getTime());
case "[object Array]":
let arr = [];
for (let i = 0; i < data.length; i++) {
arr[i] = this.deepCopy(data[i]);
}
return arr;
//js自带对象或用户自定义类实例
case "[object Object]":
let obj = {};
for (let key in data) {
//会遍历原型链上的属性方法可以用hasOwnProperty来控制 obj.hasOwnProperty(prop)
obj[key] = this.deepCopy(data[key]);
}
return obj;
}
} else {
//string,number,bool,null,undefined,symbol
return data;
}
},
// 按顺序把接口返回值 赋给 form
requestToForm(requestObj,form){
// 获取a对象中属性的顺序和值
let aKeys = Object.keys(form);
let aValues = aKeys.map(key => form[key]);
// 遍历b对象将属性等于a中属性值的对象深拷贝给a中的相应属性
for (let i = 0; i < aKeys.length; i++) {
let key = aKeys[i];
let value = aValues[i];
if (requestObj[key] === value) {
// 深拷贝对象
Object.assign(form, { [key]: this.deepCopy(requestObj[key]) });
}
}
// 遍历b对象将b中独有的属性按照a的顺序插入到a中
for (let key in requestObj) {
if (!form[key]) {
// 深拷贝属性值
Object.assign(form, { [key]: this.deepCopy(requestObj[key]) });
}
}
return form
}
}
export {
base
}