11
This commit is contained in:
parent
1d7fbd9f5b
commit
a5d5e3557a
288
utils/index.js
288
utils/index.js
@ -1,17 +1,17 @@
|
|||||||
// 环境配置
|
// 环境配置
|
||||||
const environments = {
|
const environments = {
|
||||||
development: {
|
development: {
|
||||||
apiUrl: 'https://test.hshuishang.com',
|
apiUrl: "https://test.hshuishang.com",
|
||||||
picUrl: 'https://test.hshuishang.com',
|
picUrl: "https://test.hshuishang.com",
|
||||||
aliyunOssUrl: 'https://wechat-img-file.oss-cn-beijing.aliyuncs.com',
|
aliyunOssUrl: "https://wechat-img-file.oss-cn-beijing.aliyuncs.com",
|
||||||
staticUrl: 'https://wechat-img-file.oss-cn-beijing.aliyuncs.com'
|
staticUrl: "https://static.hshuishang.com",
|
||||||
},
|
},
|
||||||
production: {
|
production: {
|
||||||
apiUrl: 'https://api.hshuishang.com',
|
apiUrl: "https://api.hshuishang.com",
|
||||||
picUrl: 'https://api.hshuishang.com',
|
picUrl: "https://api.hshuishang.com",
|
||||||
aliyunOssUrl: 'https://wechat-img-file.oss-cn-beijing.aliyuncs.com',
|
aliyunOssUrl: "https://wechat-img-file.oss-cn-beijing.aliyuncs.com",
|
||||||
staticUrl: 'http://static.hshuishang.com'
|
staticUrl: "https://static.hshuishang.com",
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
// 判断当前环境
|
// 判断当前环境
|
||||||
@ -22,35 +22,35 @@ const getCurrentEnvironment = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 2. 微信小程序环境判断
|
// 2. 微信小程序环境判断
|
||||||
if (typeof wx !== 'undefined' && wx.getAccountInfoSync) {
|
if (typeof wx !== "undefined" && wx.getAccountInfoSync) {
|
||||||
try {
|
try {
|
||||||
const accountInfo = wx.getAccountInfoSync();
|
const accountInfo = wx.getAccountInfoSync();
|
||||||
const envVersion = accountInfo.miniProgram.envVersion;
|
const envVersion = accountInfo.miniProgram.envVersion;
|
||||||
// 根据微信小程序环境返回对应环境标识
|
// 根据微信小程序环境返回对应环境标识
|
||||||
if (envVersion === 'release') {
|
if (envVersion === "release") {
|
||||||
return 'production'; // 正式版
|
return "production"; // 正式版
|
||||||
} else if (envVersion === 'trial') {
|
} else if (envVersion === "trial") {
|
||||||
return 'development'; // 体验版
|
return "development"; // 体验版
|
||||||
} else if (envVersion === 'develop') {
|
} else if (envVersion === "develop") {
|
||||||
return 'development'; // 开发版
|
return "development"; // 开发版
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn('获取小程序环境信息失败:', e);
|
console.warn("获取小程序环境信息失败:", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 通过全局配置判断(例如Vercel等平台的环境变量)
|
// 3. 通过全局配置判断(例如Vercel等平台的环境变量)
|
||||||
if (typeof global !== 'undefined' && global.env) {
|
if (typeof global !== "undefined" && global.env) {
|
||||||
return global.env;
|
return global.env;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. 检查是否有全局的uni对象,可能是uni-app环境
|
// 4. 检查是否有全局的uni对象,可能是uni-app环境
|
||||||
if (typeof uni !== 'undefined') {
|
if (typeof uni !== "undefined") {
|
||||||
// 可以根据实际情况添加更多uni-app环境的判断逻辑
|
// 可以根据实际情况添加更多uni-app环境的判断逻辑
|
||||||
}
|
}
|
||||||
|
|
||||||
// 默认返回生产环境,避免线上环境使用开发地址
|
// 默认返回生产环境,避免线上环境使用开发地址
|
||||||
return 'production';
|
return "production";
|
||||||
};
|
};
|
||||||
|
|
||||||
// 获取当前环境配置
|
// 获取当前环境配置
|
||||||
@ -72,8 +72,10 @@ export const processImageUrl = (url) => {
|
|||||||
|
|
||||||
// 如果URL包含阿里云OSS地址,则根据环境替换
|
// 如果URL包含阿里云OSS地址,则根据环境替换
|
||||||
if (url.includes(aliyunOssUrl)) {
|
if (url.includes(aliyunOssUrl)) {
|
||||||
if (currentEnv === 'production') {
|
if (currentEnv === "production") {
|
||||||
return url.replace(aliyunOssUrl, staticUrl);
|
url = url.replace(aliyunOssUrl, staticUrl);
|
||||||
|
|
||||||
|
return url;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,28 +89,28 @@ export const processImageUrl = (url) => {
|
|||||||
* @param {Object} options - 配置选项对象
|
* @param {Object} options - 配置选项对象
|
||||||
* @param {Boolean} options.isLogin - 是否需要校验登录态,默认为 true
|
* @param {Boolean} options.isLogin - 是否需要校验登录态,默认为 true
|
||||||
* @param {Boolean} options.forceReplace - 是否强制替换当前页面,解决webview数量限制问题,默认为 false
|
* @param {Boolean} options.forceReplace - 是否强制替换当前页面,解决webview数量限制问题,默认为 false
|
||||||
*/
|
*/
|
||||||
export const NavgateTo = (path, options = {}) => {
|
export const NavgateTo = (path, options = {}) => {
|
||||||
// 首页不需要登录验证
|
// 首页不需要登录验证
|
||||||
if (path === '/pages/index/index') {
|
if (path === "/pages/index/index") {
|
||||||
uni.navigateTo({ url: path });
|
uni.navigateTo({ url: path });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const { isLogin = true, forceReplace = false } = options;
|
const { isLogin = true, forceReplace = false } = options;
|
||||||
const ctoken = uni.getStorageSync('ctoken');
|
const ctoken = uni.getStorageSync("ctoken");
|
||||||
|
|
||||||
// 登录校验
|
// 登录校验
|
||||||
if (isLogin && !ctoken) {
|
if (isLogin && !ctoken) {
|
||||||
uni.redirectTo({ url: '/pages/login/login' })
|
uni.redirectTo({ url: "/pages/login/login" });
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 返回上一页
|
// 返回上一页
|
||||||
if (path == '1') {
|
if (path == "1") {
|
||||||
uni.navigateBack({
|
uni.navigateBack({
|
||||||
delta: 1
|
delta: 1,
|
||||||
})
|
});
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 选择合适的跳转方式
|
// 选择合适的跳转方式
|
||||||
@ -116,13 +118,13 @@ export const NavgateTo = (path, options = {}) => {
|
|||||||
url: path,
|
url: path,
|
||||||
fail: (err) => {
|
fail: (err) => {
|
||||||
// 处理webview数量限制错误
|
// 处理webview数量限制错误
|
||||||
if (err.errMsg && err.errMsg.includes('webview count limit')) {
|
if (err.errMsg && err.errMsg.includes("webview count limit")) {
|
||||||
console.warn('检测到webview数量限制,自动切换为redirectTo模式');
|
console.warn("检测到webview数量限制,自动切换为redirectTo模式");
|
||||||
uni.redirectTo({ url: path });
|
uni.redirectTo({ url: path });
|
||||||
} else {
|
} else {
|
||||||
console.error('页面跳转失败:', err);
|
console.error("页面跳转失败:", err);
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
// 如果强制替换,使用redirectTo
|
// 如果强制替换,使用redirectTo
|
||||||
@ -131,7 +133,7 @@ export const NavgateTo = (path, options = {}) => {
|
|||||||
} else {
|
} else {
|
||||||
uni.navigateTo(navigateOptions);
|
uni.navigateTo(navigateOptions);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 封装请求方法
|
* 封装请求方法
|
||||||
@ -142,66 +144,68 @@ export const NavgateTo = (path, options = {}) => {
|
|||||||
* @param {Boolean} options.silent - 是否显示loading,默认为 true
|
* @param {Boolean} options.silent - 是否显示loading,默认为 true
|
||||||
* @param {Boolean} options.nested - 是否平铺接口返回参数,默认为 false
|
* @param {Boolean} options.nested - 是否平铺接口返回参数,默认为 false
|
||||||
* @returns {Promise} 返回一个Promise对象
|
* @returns {Promise} 返回一个Promise对象
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
export const request = (url, method = "POST", data = {}, options = {}) => {
|
||||||
export const request = (url, method = 'POST', data = {}, options = {}) => {
|
|
||||||
const { silent = true, nested = false } = options;
|
const { silent = true, nested = false } = options;
|
||||||
|
|
||||||
let ctoken = null;
|
let ctoken = null;
|
||||||
if (options.token) {
|
if (options.token) {
|
||||||
ctoken = options.token;
|
ctoken = options.token;
|
||||||
} else {
|
} else {
|
||||||
ctoken = uni.getStorageSync('ctoken'); // 后续接口强依赖强校验该字段
|
ctoken = uni.getStorageSync("ctoken"); // 后续接口强依赖强校验该字段
|
||||||
}
|
}
|
||||||
|
|
||||||
if (silent) {
|
if (silent) {
|
||||||
uni.showLoading({
|
uni.showLoading({
|
||||||
title: '加载中',
|
title: "加载中",
|
||||||
mask: true
|
mask: true,
|
||||||
})
|
});
|
||||||
};
|
|
||||||
let params = {
|
|
||||||
user_id: uni.getStorageSync('userId'),
|
|
||||||
...data,
|
|
||||||
}
|
}
|
||||||
|
let params = {
|
||||||
|
user_id: uni.getStorageSync("userId"),
|
||||||
|
...data,
|
||||||
|
};
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
uni.request({
|
uni.request({
|
||||||
url: RequsetUrl + url,
|
url: RequsetUrl + url,
|
||||||
method: method,
|
method: method,
|
||||||
data: params,
|
data: params,
|
||||||
header: {
|
header: {
|
||||||
'Content-Type': 'application/json',
|
"Content-Type": "application/json",
|
||||||
'Authorization': ctoken,
|
Authorization: ctoken,
|
||||||
// ...header,
|
// ...header,
|
||||||
},
|
},
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
// console.log('请求成功,接口返参', res);
|
// console.log('请求成功,接口返参', res);
|
||||||
if (res.statusCode == 401 || (res.statusCode == 500 && res.data.msg == "效验令牌失败")) {
|
if (
|
||||||
uni.removeStorageSync('ctoken');
|
res.statusCode == 401 ||
|
||||||
uni.removeStorageSync('userId');
|
(res.statusCode == 500 && res.data.msg == "效验令牌失败")
|
||||||
uni.removeStorageSync('is_worker');
|
) {
|
||||||
|
uni.removeStorageSync("ctoken");
|
||||||
|
uni.removeStorageSync("userId");
|
||||||
|
uni.removeStorageSync("is_worker");
|
||||||
uni.showModal({
|
uni.showModal({
|
||||||
title: '提示',
|
title: "提示",
|
||||||
content: '请登录后查看',
|
content: "请登录后查看",
|
||||||
confirmText: "去登陆",
|
confirmText: "去登陆",
|
||||||
complete: (res) => {
|
complete: (res) => {
|
||||||
if (res.cancel) {
|
if (res.cancel) {
|
||||||
uni.hideLoading();
|
uni.hideLoading();
|
||||||
uni.redirectTo({
|
uni.redirectTo({
|
||||||
url: '/pages/index/index'
|
url: "/pages/index/index",
|
||||||
});
|
});
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (res.confirm) {
|
if (res.confirm) {
|
||||||
uni.redirectTo({
|
uni.redirectTo({
|
||||||
url: '/pages/login/login',
|
url: "/pages/login/login",
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
})
|
});
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
if (res.statusCode === 200) {
|
if (res.statusCode === 200) {
|
||||||
if (silent) {
|
if (silent) {
|
||||||
@ -210,22 +214,22 @@ export const request = (url, method = 'POST', data = {}, options = {}) => {
|
|||||||
if (options?.nested) {
|
if (options?.nested) {
|
||||||
let data = {
|
let data = {
|
||||||
...res.data,
|
...res.data,
|
||||||
}
|
};
|
||||||
resolve(data); // 请求成功
|
resolve(data); // 请求成功
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
resolve(res.data.data); // 请求成功
|
resolve(res.data.data); // 请求成功
|
||||||
} else {
|
} else {
|
||||||
console.log('走到这列');
|
console.log("走到这列");
|
||||||
uni.hideLoading();
|
uni.hideLoading();
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: res.data.msg || '请求失败',
|
title: res.data.msg || "请求失败",
|
||||||
icon: 'none'
|
icon: "none",
|
||||||
})
|
});
|
||||||
|
|
||||||
reject({
|
reject({
|
||||||
code: res.statusCode,
|
code: res.statusCode,
|
||||||
message: res.data.msg || '请求失败',
|
message: res.data.msg || "请求失败",
|
||||||
data: res.data,
|
data: res.data,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -233,35 +237,36 @@ export const request = (url, method = 'POST', data = {}, options = {}) => {
|
|||||||
fail: (err) => {
|
fail: (err) => {
|
||||||
uni.hideLoading();
|
uni.hideLoading();
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: res.data.msg || '请求失败',
|
title: res.data.msg || "请求失败",
|
||||||
icon: 'none'
|
icon: "none",
|
||||||
})
|
});
|
||||||
reject({
|
reject({
|
||||||
code: -1,
|
code: -1,
|
||||||
message: '接口异常,请稍后重试',
|
message: "接口异常,请稍后重试",
|
||||||
error: err,
|
error: err,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验手机号是否合法
|
* 校验手机号是否合法
|
||||||
* @param {string} option 请求方法
|
* @param {string} option 请求方法
|
||||||
* @returns {Boolean} 手机号是否正确
|
* @returns {Boolean} 手机号是否正确
|
||||||
*/
|
*/
|
||||||
export const isPhone = (option) => {
|
export const isPhone = (option) => {
|
||||||
if (option.length != 11) {
|
if (option.length != 11) {
|
||||||
return false
|
return false;
|
||||||
}
|
}
|
||||||
if (!(/^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/.test(option))) {
|
if (
|
||||||
return false
|
!/^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/.test(option)
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
} else {
|
} else {
|
||||||
return true
|
return true;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 精确的浮点数运算
|
* 精确的浮点数运算
|
||||||
@ -269,12 +274,12 @@ export const isPhone = (option) => {
|
|||||||
* @param {number} num2 - 第二个数字
|
* @param {number} num2 - 第二个数字
|
||||||
* @param {string} operator - 运算符,支持 '+', '-', '*', '/', '+='
|
* @param {string} operator - 运算符,支持 '+', '-', '*', '/', '+='
|
||||||
* @returns {number} - 运算结果
|
* @returns {number} - 运算结果
|
||||||
*/
|
*/
|
||||||
export const floatCalculate = (num1, num2, operator) => {
|
export const floatCalculate = (num1, num2, operator) => {
|
||||||
// 获取小数位数
|
// 获取小数位数
|
||||||
function getPrecision(num) {
|
function getPrecision(num) {
|
||||||
const str = num.toString();
|
const str = num.toString();
|
||||||
const decimalIndex = str.indexOf('.');
|
const decimalIndex = str.indexOf(".");
|
||||||
return decimalIndex === -1 ? 0 : str.length - decimalIndex - 1;
|
return decimalIndex === -1 ? 0 : str.length - decimalIndex - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -291,45 +296,45 @@ export const floatCalculate = (num1, num2, operator) => {
|
|||||||
// 根据运算符进行计算
|
// 根据运算符进行计算
|
||||||
let result;
|
let result;
|
||||||
switch (operator) {
|
switch (operator) {
|
||||||
case '+':
|
case "+":
|
||||||
result = (intNum1 + intNum2) / factor;
|
result = (intNum1 + intNum2) / factor;
|
||||||
break;
|
break;
|
||||||
case '-':
|
case "-":
|
||||||
result = (intNum1 - intNum2) / factor;
|
result = (intNum1 - intNum2) / factor;
|
||||||
break;
|
break;
|
||||||
case '*':
|
case "*":
|
||||||
result = (intNum1 * intNum2) / (factor * factor);
|
result = (intNum1 * intNum2) / (factor * factor);
|
||||||
break;
|
break;
|
||||||
case '/':
|
case "/":
|
||||||
result = intNum1 / intNum2;
|
result = intNum1 / intNum2;
|
||||||
break;
|
break;
|
||||||
case '+=':
|
case "+=":
|
||||||
result = (intNum1 + intNum2) / factor;
|
result = (intNum1 + intNum2) / factor;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error('不支持的运算符');
|
throw new Error("不支持的运算符");
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 图片上传
|
* 图片上传
|
||||||
* @param {string} filename - 图片上传地址
|
* @param {string} filename - 图片上传地址
|
||||||
* @param {Function} fn - 接口回调函数
|
* @param {Function} fn - 接口回调函数
|
||||||
*/
|
*/
|
||||||
export const upload = (filename, fn) => {
|
export const upload = (filename, fn) => {
|
||||||
uni.showLoading({
|
uni.showLoading({
|
||||||
title: '上传中',
|
title: "上传中",
|
||||||
mask: true
|
mask: true,
|
||||||
})
|
});
|
||||||
|
|
||||||
uni.uploadFile({
|
uni.uploadFile({
|
||||||
url: RequsetUrl + '/api/v1/public/upload-image',
|
url: RequsetUrl + "/api/v1/public/upload-image",
|
||||||
filePath: filename,
|
filePath: filename,
|
||||||
name: 'image',
|
name: "image",
|
||||||
formData: {
|
formData: {
|
||||||
'uid': uni.getStorageSync('uid'),
|
uid: uni.getStorageSync("uid"),
|
||||||
},
|
},
|
||||||
success: (f) => {
|
success: (f) => {
|
||||||
uni.hideLoading();
|
uni.hideLoading();
|
||||||
@ -339,31 +344,30 @@ export const upload = (filename, fn) => {
|
|||||||
uni.hideLoading();
|
uni.hideLoading();
|
||||||
console.log(res);
|
console.log(res);
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: '上传文件失败',
|
title: "上传文件失败",
|
||||||
icon: 'none'
|
icon: "none",
|
||||||
})
|
});
|
||||||
},
|
},
|
||||||
complete: () => { }
|
complete: () => {},
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 视频上传
|
* 视频上传
|
||||||
* @param {string} filename - 图片上传地址
|
* @param {string} filename - 图片上传地址
|
||||||
* @param {Function} fn - 接口回调函数
|
* @param {Function} fn - 接口回调函数
|
||||||
*/
|
*/
|
||||||
export const uploadVideo = (filename, fn) => {
|
export const uploadVideo = (filename, fn) => {
|
||||||
uni.showLoading({
|
uni.showLoading({
|
||||||
title: '上传中',
|
title: "上传中",
|
||||||
mask: true
|
mask: true,
|
||||||
})
|
});
|
||||||
uni.uploadFile({
|
uni.uploadFile({
|
||||||
url: RequsetUrl + '/api/v1/public/upload-video',
|
url: RequsetUrl + "/api/v1/public/upload-video",
|
||||||
filePath: filename,
|
filePath: filename,
|
||||||
name: 'file',
|
name: "file",
|
||||||
formData: {
|
formData: {
|
||||||
'uid': uni.getStorageSync('uid'),
|
uid: uni.getStorageSync("uid"),
|
||||||
},
|
},
|
||||||
success: (f) => {
|
success: (f) => {
|
||||||
uni.hideLoading();
|
uni.hideLoading();
|
||||||
@ -373,13 +377,13 @@ export const uploadVideo = (filename, fn) => {
|
|||||||
uni.hideLoading();
|
uni.hideLoading();
|
||||||
console.log(res);
|
console.log(res);
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: '上传文件失败',
|
title: "上传文件失败",
|
||||||
icon: 'none'
|
icon: "none",
|
||||||
})
|
});
|
||||||
},
|
},
|
||||||
complete: () => { }
|
complete: () => {},
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算两地之间的距离
|
* 计算两地之间的距离
|
||||||
@ -388,7 +392,7 @@ export const uploadVideo = (filename, fn) => {
|
|||||||
* @param {string} lat2 第二个点的纬度
|
* @param {string} lat2 第二个点的纬度
|
||||||
* @param {string} lon2 第二个点的经度
|
* @param {string} lon2 第二个点的经度
|
||||||
* @returns {number} 距离
|
* @returns {number} 距离
|
||||||
*/
|
*/
|
||||||
export const calculateDistance = (lat1, lon1, lat2, lon2) => {
|
export const calculateDistance = (lat1, lon1, lat2, lon2) => {
|
||||||
// 将经纬度转换为弧度
|
// 将经纬度转换为弧度
|
||||||
const toRad = (value) => (value * Math.PI) / 180;
|
const toRad = (value) => (value * Math.PI) / 180;
|
||||||
@ -398,18 +402,20 @@ export const calculateDistance = (lat1, lon1, lat2, lon2) => {
|
|||||||
const dLon = toRad(lon2 - lon1);
|
const dLon = toRad(lon2 - lon1);
|
||||||
const a =
|
const a =
|
||||||
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
||||||
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
|
Math.cos(toRad(lat1)) *
|
||||||
Math.sin(dLon / 2) * Math.sin(dLon / 2);
|
Math.cos(toRad(lat2)) *
|
||||||
|
Math.sin(dLon / 2) *
|
||||||
|
Math.sin(dLon / 2);
|
||||||
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||||
const distance = R * c; // 最终距离(单位:千米)
|
const distance = R * c; // 最终距离(单位:千米)
|
||||||
return distance;
|
return distance;
|
||||||
}
|
};
|
||||||
//
|
//
|
||||||
export const menuButtonInfo = () => {
|
export const menuButtonInfo = () => {
|
||||||
const systemInfo = uni.getSystemInfoSync();
|
const systemInfo = uni.getSystemInfoSync();
|
||||||
const platform = systemInfo.platform;
|
const platform = systemInfo.platform;
|
||||||
|
|
||||||
if (platform === 'ios') {
|
if (platform === "ios") {
|
||||||
// TODO: ios待测试
|
// TODO: ios待测试
|
||||||
return {
|
return {
|
||||||
height: systemInfo.statusBarHeight,
|
height: systemInfo.statusBarHeight,
|
||||||
@ -417,50 +423,50 @@ export const menuButtonInfo = () => {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (platform === 'android') {
|
if (platform === "android") {
|
||||||
return {
|
return {
|
||||||
height: systemInfo.statusBarHeight,
|
height: systemInfo.statusBarHeight,
|
||||||
top: 44, // 自定义导航栏默认高度
|
top: 44, // 自定义导航栏默认高度
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (systemInfo.uniPlatform === 'mp-weixin') {
|
if (systemInfo.uniPlatform === "mp-weixin") {
|
||||||
// 微信小程序、支付宝小程序等,平台标识以 'mp-' 开头
|
// 微信小程序、支付宝小程序等,平台标识以 'mp-' 开头
|
||||||
return uni.getMenuButtonBoundingClientRect()
|
return uni.getMenuButtonBoundingClientRect();
|
||||||
} else {
|
} else {
|
||||||
return 'Unknown';
|
return "Unknown";
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取服务供应商
|
* 获取服务供应商
|
||||||
* @param {string} opt - 服务类型
|
* @param {string} opt - 服务类型
|
||||||
* @returns {Promise} 返回一个Promise对象
|
* @returns {Promise} 返回一个Promise对象
|
||||||
*/
|
*/
|
||||||
export const getProviderPromise = (opt) => {
|
export const getProviderPromise = (opt) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
uni.getProvider({
|
uni.getProvider({
|
||||||
service: opt,
|
service: opt,
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
console.log('获取支付服务提供商成功:', res.provider);
|
console.log("获取支付服务提供商成功:", res.provider);
|
||||||
resolve(res.provider);
|
resolve(res.provider);
|
||||||
},
|
},
|
||||||
fail: (err) => {
|
fail: (err) => {
|
||||||
console.error('获取支付服务提供商失败:', err);
|
console.error("获取支付服务提供商失败:", err);
|
||||||
reject(err);
|
reject(err);
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
//数组去重
|
//数组去重
|
||||||
export const uniqueByField = (arr, field) => {
|
export const uniqueByField = (arr, field) => {
|
||||||
const seen = {};
|
const seen = {};
|
||||||
return arr.filter(item => {
|
return arr.filter((item) => {
|
||||||
const key = item[field];
|
const key = item[field];
|
||||||
return seen.hasOwnProperty(key) ? false : (seen[key] = true);
|
return seen.hasOwnProperty(key) ? false : (seen[key] = true);
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
// 校验邮箱地址是否合法
|
// 校验邮箱地址是否合法
|
||||||
export const validateEmail = (email) => {
|
export const validateEmail = (email) => {
|
||||||
@ -470,8 +476,8 @@ export const validateEmail = (email) => {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true;
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 防抖函数
|
* 防抖函数
|
||||||
@ -499,8 +505,7 @@ export const debounce = (fn, delay = 300, immediate = false) => {
|
|||||||
timer = null;
|
timer = null;
|
||||||
}, delay);
|
}, delay);
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 格式化日期
|
* 格式化日期
|
||||||
@ -508,11 +513,10 @@ export const debounce = (fn, delay = 300, immediate = false) => {
|
|||||||
*/
|
*/
|
||||||
export const formatDate = (date) => {
|
export const formatDate = (date) => {
|
||||||
const year = date.getFullYear();
|
const year = date.getFullYear();
|
||||||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
const month = String(date.getMonth() + 1).padStart(2, "0");
|
||||||
const day = String(date.getDate()).padStart(2, '0');
|
const day = String(date.getDate()).padStart(2, "0");
|
||||||
const hours = String(date.getHours()).padStart(2, '0');
|
const hours = String(date.getHours()).padStart(2, "0");
|
||||||
const minutes = String(date.getMinutes()).padStart(2, '0');
|
const minutes = String(date.getMinutes()).padStart(2, "0");
|
||||||
const seconds = String(date.getSeconds()).padStart(2, '0');
|
const seconds = String(date.getSeconds()).padStart(2, "0");
|
||||||
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
|
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user