自用uni-app前端工具类,还是蛮好用的
判断是开发环境还是运行环境
// 在main.js中判断是生产环境还是开发环境,根据不同的环境配置请求的url
let development = process.env.NODE_ENV == 'development' ? true : false;
if (development){
uni.setStorageSync("serverurl","http://192.168.0.99:801/"); //开发模式
}else{
uni.setStorageSync("serverurl","https://www.sn58.cn/"); //编译模式
}
const root ='http://127.0.0.1:801/' //API根路径
//简化uni.request
const get = (url, data = '', method = 'GET') => {
return new Promise(resolve => {
uni.request({
url: root + url,
method: method,
data: data,
success(res) {
resolve(res.data)
},
fail(res) {
console.log(res.data)
}
})
})
}
//获取上一页实例
const prePage = () => {
let pages = getCurrentPages();
let prePage = pages[pages.length - 2];
// #ifdef H5
return prePage;
// #endif
return prePage.$vm;
}
//简化uni.showtoast
const msg = (title, duration = 1500, mask = false, icon = 'none') => {
//统一提示方便全局修改
if (Boolean(title) === false) {
return;
}
uni.showToast({
title,
duration,
mask,
icon
});
}
//简化uni.loading
const showLoading = (title, mask = true, icon = 'none') => {
//统一提示方便全局修改
uni.showLoading({
title: title,
mask:true
})
}
//简化uni.showmodel
const msgbox = (content,title="均维软件") => {
//统一提示方便全局修改
if (Boolean(title) === false) {
return;
}
uni.showModal({
title: title,
content: content,
showCancel:false
})
}
//简化uni.navigateTo
const jump=(url)=>{
uni.navigateTo({
url:url
})
}
//判断是否登录,否则跳转到登录页
const isLogin = (hasLogin) => {
if (!hasLogin) {
uni.showModal({
content: '请先登陆',
confirmText: '去登陆',
success(res) {
res.confirm && uni.navigateTo({
url: '/pages/login/login/login'
})
}
})
return;
}
}
//格式化时间
const formatTime = (timeStamp) => {
let currentTime = Date.parse(new Date()) / 1000;
if (timeStamp > currentTime) {
let time = timeStamp - currentTime;
let day = parseInt(time / 86400);
time = time % 86400;
let hour = parseInt(time / 3600);
time = time % 3600;
let minute = parseInt(time / 60);
time = time % 60;
let second = time;
return {
day,
hour,
minute,
second
}
}else{console.log('error')}
}
const rad=e=> {
let PI = Math.PI;
return e * PI / 180
}
const getDistance=(lat1, lng1, lat2, lng2)=> {
let radLat1 = rad(lat1);
let radLat2 = rad(lat2);
let a = radLat1 - radLat2;
let b = rad(lng1) - rad(lng2);
let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(
b / 2), 2)));
s = s * 6378.137;
// EARTH_RADIUS;
s = Math.round(s * 10000) / 10000;
return s;
};
const msgarray=(objarray,valField,isback=true,title="均维软件")=> {
if (Boolean(title) === false) {
//return;
}
if (objarray.length==0) {
uni.showModal({
title: title,
//cancelText: "返回上一页", // 取消按钮的文字
// confirmText: "关闭", // 确认按钮的文字
cancelText:"返回上页",
content: "没有数据",
showCancel:isback,
success:function(res){
console.log(res);
if (!res.confirm){
//uni.$emit("isok",true);
uni.navigateBack()
}
}
})
return;
}
let arr =[]
console.log(objarray)
objarray.forEach((row,index)=>{
if(row[valField]!='[]'){
arr.push(index+1+"."+row[valField])
}
})
if (arr.length==0) {
uni.showModal({
title: title,
//cancelText: "返回上一页", // 取消按钮的文字
// confirmText: "关闭", // 确认按钮的文字
cancelText:"返回上页",
content: "没有数据",
showCancel:isback,
success:function(res){
console.log(res);
if (!res.confirm){
//uni.$emit("isok",true);
uni.navigateBack()
}
}
})
return;
}
//return arr.join()
var content=arr.join("\n").replace("\r\r","\n") //微信摸拟器不会换行,真机会换行
uni.showModal({
title: title,
//cancelText: "返回上一页", // 取消按钮的文字
// confirmText: "关闭", // 确认按钮的文字
cancelText:"返回上页",
content:content,
showCancel:isback,
success:function(res){
console.log(res);
if (!res.confirm){
//uni.$emit("isok",true);
uni.navigateBack()
}
}
})
}
/**
* 获取 近**天,前天,昨天,今天,明天,后天,未来**天等通用方法
* ..............and so on
* -15 ----- 近15天
* -7 ----- 近7天
* -3 ----- 近3天
* -2 ----- 前天
* -1 ----- 昨天
* 0 ------------------------- 今天
* 1 ----- 明天
* 2 ----- 后天
* 3 ----- 未来3天
* 7 ----- 未来7天
* 15 ----- 未来15天
* ..............and so on
* @param {Object} count
*/
const timeFormat=(count=0)=> {
// 实例化开始日期
const startDate = new Date();
// 以毫秒设置 Date 对象
if(count > 2){
startDate.setTime(startDate.getTime()); // 大于2,设置起始时间为今天
}else if(count < -2){
startDate.setTime(startDate.getTime() + (24 * 60 * 60 * 1000) * (count + 1));
}else{
startDate.setTime(startDate.getTime() + (24 * 60 * 60 * 1000) * count);
}
// 获取开始年份
const startY = startDate.getFullYear();
// 获取开始月份
const startM = startDate.getMonth() + 1 < 10 ? '0' + (startDate.getMonth() + 1) : startDate.getMonth() + 1;
// 获取开始日
const startD = startDate.getDate() < 10 ? '0' + startDate.getDate() : startDate.getDate();
// 拼接 最终开始时间
const startTime = `${startY}-${startM}-${startD} 00:00:00`;
// 实例化结束日期
const endDate = new Date();
// 以毫秒设置 Date 对象
if(count > 2){
endDate.setTime(endDate.getTime() + (24 * 60 * 60 * 1000) * (count - 1));
}else if(count < -2){
endDate.setTime(endDate.getTime()); // 小于-2,设置结束时间为今天
}else{
endDate.setTime(endDate.getTime() + (24 * 60 * 60 * 1000) * count);
}
// 获取结束年份
const endY = endDate.getFullYear();
// 获取结束月份
const endM = endDate.getMonth() + 1 < 10 ? '0' + (endDate.getMonth() + 1) : endDate.getMonth() + 1;
// 获取结束日
const endD = endDate.getDate() < 10 ? '0' + endDate.getDate() : endDate.getDate();
// 拼接 最终结束时间
const endTime = `${endY}-${endM}-${endD} 23:59:59`;
// 返回 开始 至 结束 日期 数组
return [startTime, endTime];
}
/**
* 获取 近**天,前天,昨天,今天,明天,后天,未来**天等通用方法
* ..............and so on
* -15 ----- 近15天
* -7 ----- 近7天
* -3 ----- 近3天
* -2 ----- 前天
* -1 ----- 昨天
* 0 ------------------------- 今天
* 1 ----- 明天
* 2 ----- 后天
* 3 ----- 未来3天
* 7 ----- 未来7天
* 15 ----- 未来15天
* ..............and so on
* @param {Object} count
*/
const dateFormat=(count=0)=> {
// 实例化开始日期
const startDate = new Date();
// 以毫秒设置 Date 对象
if(count > 2){
startDate.setTime(startDate.getTime()); // 大于2,设置起始时间为今天
}else if(count < -2){
startDate.setTime(startDate.getTime() + (24 * 60 * 60 * 1000) * (count + 1));
}else{
startDate.setTime(startDate.getTime() + (24 * 60 * 60 * 1000) * count);
}
// 获取开始年份
const startY = startDate.getFullYear();
// 获取开始月份
const startM = startDate.getMonth() + 1 < 10 ? '0' + (startDate.getMonth() + 1) : startDate.getMonth() + 1;
// 获取开始日
const startD = startDate.getDate() < 10 ? '0' + startDate.getDate() : startDate.getDate();
// 拼接 最终开始时间
const startTime = `${startY}-${startM}-${startD}`;
// 实例化结束日期
const endDate = new Date();
// 以毫秒设置 Date 对象
if(count > 2){
endDate.setTime(endDate.getTime() + (24 * 60 * 60 * 1000) * (count - 1));
}else if(count < -2){
endDate.setTime(endDate.getTime()); // 小于-2,设置结束时间为今天
}else{
endDate.setTime(endDate.getTime() + (24 * 60 * 60 * 1000) * count);
}
// 获取结束年份
const endY = endDate.getFullYear();
// 获取结束月份
const endM = endDate.getMonth() + 1 < 10 ? '0' + (endDate.getMonth() + 1) : endDate.getMonth() + 1;
// 获取结束日
const endD = endDate.getDate() < 10 ? '0' + endDate.getDate() : endDate.getDate();
// 拼接 最终结束时间
const endTime = `${endY}-${endM}-${endD}`;
// 返回 开始 至 结束 日期 数组
return [startTime, endTime];
}
export default {
root,
get,
prePage,
msg,
msgbox,
jump,
isLogin,
formatTime,
getDistance,
msgarray,
timeFormat,
dateFormat
}
猫猫的心里话
加菲猫的VFP|狐友会社群接收投稿啦
加菲猫的VFP,用VFP不局限VFP,用VFP混合一切。无论是VFP,还是JS,还是C,只要能混合起来,都可以发表。
商业模式,销售技巧、需求规划、产品设计的知识通通可以发表。
暂定千字50元红包,,优秀的文章红包更大,一经发表,红包到手。
如何帮助使用VFP的人?
用VFP的人,有专业的,有非专业了,很多人其实是小白,问出的问题是小白,如果问题不对,我们引导他们问正确的问题。无论如何请不要嘲笑他们说帮助都不看,这么简单的问题都不会,嘲笑别人不行,而无法提出建设性答案,是很low的。
我们无论工作需要,还是有自己的软件,都是是需要真正的知识,如何让更多人学习真正的VFP知识呢,只需要点赞,在看,能转发朋友圈就更好了。
加菲猫的vfp倡导用"VFP极简混合开发,少写代码、快速出活,用VFP,但不局限于VFP,各种语言混合开发"。
我已经带领一百多名会员成功掌到VFP的黑科技,进入了移动互联网时代,接下来我们要进入物联网领域。
2023年狐友会社群会员继续招募中
社群会员获取的权益有:
祺佑三层开发框架商业版(猫框),终身免费升级,终身技术支持。
开放的录播课程有:
微信小程序,微信公众号开发,H5 APP开发,Extjs BS开发,VFP面向对象进阶,VFP中间层开发。
源码类资源有:
支付组件源码,短信源码,权限组件源码,一些完整系统的源码。这个可以单独出售的,需要的可以联系我。
会员也可以实现群内资源对接,可以接分包,合作等各项商业或技术业务