345 lines
9.2 KiB
JavaScript
345 lines
9.2 KiB
JavaScript
import dayjs from 'dayjs';
|
||
import { PRIVACY_EN,PRIVACY_CN,USER_EN,USER_CN } from './config.js';
|
||
|
||
/**
|
||
* 创建随机字符串
|
||
* @param num
|
||
* @returns {string}
|
||
*/
|
||
export function randomString(num) {
|
||
const chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
|
||
let res = ''
|
||
for (let i = 0; i < num; i++) {
|
||
var id = Math.ceil(Math.random() * 35)
|
||
res += chars[id]
|
||
}
|
||
return res
|
||
}
|
||
|
||
export const tencentMapKey="S2DBZ-W6S6B-KRXUN-NHF2N-DE2U6-7UBBD";// 腾讯地图key
|
||
|
||
/**
|
||
* 转换时间格式
|
||
* @param date 时间格式
|
||
* @returns {string} HH:mm
|
||
*/
|
||
export function formatDate(date){
|
||
if(!date){
|
||
return "";
|
||
}
|
||
return dayjs(date).format("YYYY-MM-DD HH:mm:ss");
|
||
}
|
||
/**
|
||
* 转换时间格式
|
||
* @param date 时间格式
|
||
* @returns {string} HH:mm
|
||
*/
|
||
export function formatDate1(date){
|
||
if(!date){
|
||
return "";
|
||
}
|
||
const newDate = dayjs(date).format("HH:mm:ss");
|
||
return dayjs(date).format("HH:mm:ss");
|
||
}
|
||
|
||
/**
|
||
* 千米转换
|
||
* @param {Object} distanceInMeters
|
||
*/
|
||
export function formatDistance(distanceInMeters) {
|
||
if (distanceInMeters >= 1000) {
|
||
// 将距离从米转换为千米,并保留两位小数
|
||
const distanceInKilometers = (distanceInMeters / 1000).toFixed(2);
|
||
return `${distanceInKilometers}km`;
|
||
} else {
|
||
return `${distanceInMeters.toFixed(2)}m`;
|
||
}
|
||
}
|
||
|
||
export function findItemByCode(items, value) {
|
||
return items.find(item => item.value === value);
|
||
}
|
||
|
||
export function timestampToTime(timestamp) {
|
||
var date = new Date(timestamp); // 时间戳为毫秒级别
|
||
var year = date.getFullYear();
|
||
var month = date.getMonth() + 1;
|
||
var day = date.getDate();
|
||
var hours = date.getHours();
|
||
var minutes = date.getMinutes();
|
||
var seconds = date.getSeconds();
|
||
|
||
// 补零操作
|
||
month = month < 10 ? '0' + month : month;
|
||
day = day < 10 ? '0' + day : day;
|
||
hours = hours < 10 ? '0' + hours : hours;
|
||
minutes = minutes < 10 ? '0' + minutes : minutes;
|
||
seconds = seconds < 10 ? '0' + seconds : seconds;
|
||
|
||
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
|
||
}
|
||
|
||
|
||
export function getParameter(path,name) {
|
||
var url = path;
|
||
var params = new Object();
|
||
if (url.indexOf("?") != -1) {
|
||
const str = url.substr(1);
|
||
const pathUrl = str.split("?")[1];
|
||
const strs = pathUrl.split("&");
|
||
for (var i = 0; i < strs.length; i++) {
|
||
params[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
|
||
}
|
||
}
|
||
if(name){
|
||
return params[name];
|
||
}else{
|
||
return params;
|
||
}
|
||
|
||
}
|
||
|
||
export function addAmount(amount){
|
||
if(amount && amount > 0){
|
||
return "+"+amount;
|
||
}
|
||
return amount;
|
||
}
|
||
|
||
/**
|
||
* @param {Object} url
|
||
*/
|
||
export function GetWxMiniProgramUrlParam(url) {
|
||
let theRequest = {};
|
||
if (url.indexOf("#") != -1) {
|
||
const str = url.split("#")[1];
|
||
const strs = str.split("&");
|
||
for (let i = 0; i < strs.length; i++) {
|
||
theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
|
||
}
|
||
} else if (url.indexOf("?") != -1) {
|
||
const str = url.split("?")[1];
|
||
const strs = str.split("&");
|
||
for (let i = 0; i < strs.length; i++) {
|
||
theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
|
||
}
|
||
}
|
||
return theRequest;
|
||
};
|
||
|
||
export function zhScenePath(scene){
|
||
const options = [];
|
||
for (var i = 0; i < scene.split('&').length;i++){
|
||
var arr = scene.split('&')[i].split('=');
|
||
options[arr[0]] = arr[1];
|
||
}
|
||
return options;
|
||
}
|
||
|
||
export function isNull(val){
|
||
if(val){
|
||
return val;
|
||
}
|
||
return "--";
|
||
}
|
||
// 保留四位小数
|
||
export function reservedDecimal(val,digit=4){
|
||
let result = val;
|
||
if(val){
|
||
result = parseInt(val,10).toFixed(digit)
|
||
}
|
||
return result;
|
||
}
|
||
|
||
/**
|
||
* 验证正负数
|
||
* @param {Object} str
|
||
*/
|
||
export function isPositiveNegativeOrZero(str) {
|
||
if(str){
|
||
const currentStr = parseInt(str);
|
||
return currentStr>0;
|
||
}
|
||
return str;
|
||
}
|
||
/**
|
||
* 获取当前语言
|
||
*/
|
||
export function getCurrentLanguage() {
|
||
let sysLanguage = uni.getLocale();
|
||
if(sysLanguage === "zh-Hans" || sysLanguage === "zh-Hant"){
|
||
sysLanguage = "zh_HK";
|
||
}
|
||
return sysLanguage;
|
||
}
|
||
/**
|
||
* 获取当前系统时间
|
||
*/
|
||
export function getCurrentSysLanguage(){
|
||
const systemLanguage = uni.getSystemInfoSync().language;
|
||
// 设置默认语言
|
||
let currentLanguage = 'zh_HK'; // 默认繁体
|
||
if (systemLanguage === 'en_US' || systemLanguage === 'en' || systemLanguage === 'ja') {
|
||
currentLanguage = 'en'; // 如果是英文系统,则设置默认英文
|
||
}else if (systemLanguage === 'zh_TW' || systemLanguage === 'zh_HK' || systemLanguage === 'zh_CN') {
|
||
currentLanguage = 'zh_HK'; // 繁体
|
||
}
|
||
return currentLanguage;
|
||
}
|
||
|
||
export function getBrowserUrl(type){
|
||
const locale = uni.getLocale();
|
||
let url = '';
|
||
if(type === "privacy" && (locale==="zh-Hans" || locale==="zh-Hant" || locale==="zh_CN" || locale==="zh_TW" || locale==="zh_HK")){
|
||
url= PRIVACY_CN;
|
||
}else if(type === "privacy" && (locale==="en" || locale==="ja")){
|
||
url= PRIVACY_EN;
|
||
}else if(type === "user" && (locale==="zh-Hans" || locale==="zh-Hant" || locale==="zh_CN" || locale==="zh_TW" || locale==="zh_HK")){
|
||
url= USER_CN;
|
||
}else if(type === "user" && (locale==="en" || locale==="ja")){
|
||
url= USER_EN;
|
||
}
|
||
return url;
|
||
}
|
||
/**
|
||
* 获取当前系统
|
||
*/
|
||
export function getCurrentSysPlatform(){
|
||
return uni.getSystemInfoSync().platform;
|
||
// return "ios";
|
||
}
|
||
/**
|
||
* 拷贝
|
||
* @param {Object} val
|
||
*/
|
||
export function copyData(val){
|
||
const then = this;
|
||
uni.setClipboardData({
|
||
data: val,
|
||
});
|
||
};
|
||
|
||
|
||
export function addChildNode(parentId, subNodes, treeOption) {
|
||
for (let i = 0; i < treeOption.length; i++) {
|
||
if (treeOption[i].parentId === parentId) {
|
||
// 将子节点插入到父节点的children数组中
|
||
treeOption[i].children = subNodes;
|
||
return; // 找到后直接返回,避免重复插入(假设树中不会有重复的parentId和value)
|
||
}
|
||
// 如果当前节点有children,则递归查找(但在这个特定情况下,我们不需要递归,因为parentId是直接的)
|
||
// 不过,为了通用性,这里还是保留递归的逻辑(虽然在这个例子中不会用到)
|
||
if (treeOption[i].children) {
|
||
addChildNode(parentId, subNodes, treeOption[i].children);
|
||
}
|
||
}
|
||
// 注意:在这个特定例子中,我们不需要上面的递归调用,因为parentId直接指向了树的一层节点
|
||
// 但如果你有一个更复杂的树结构,并且子节点可能需要插入到更深层次的节点中,那么递归调用就是必要的
|
||
}
|
||
|
||
/**
|
||
* 从新组装地址
|
||
* @param addressObj 需要组装的地址
|
||
*/
|
||
export function assembleAddress(addressObj){
|
||
const resutlObj={};
|
||
if(!addressObj || Object.keys(addressObj).length === 0){
|
||
return resutlObj;
|
||
}
|
||
const ids=[];
|
||
if(addressObj.countryId){
|
||
ids.push(addressObj.countryId)
|
||
}
|
||
if(addressObj.provinceId){
|
||
ids.push(addressObj.provinceId)
|
||
}
|
||
if(addressObj.cityId){
|
||
ids.push(addressObj.cityId)
|
||
}
|
||
if(addressObj.areaId){
|
||
ids.push(addressObj.areaId)
|
||
}
|
||
const names=[];
|
||
if(addressObj.countryName){
|
||
names.push(addressObj.countryName);
|
||
}
|
||
if(addressObj.provinceName){
|
||
names.push(addressObj.provinceName);
|
||
}
|
||
if(addressObj.cityName){
|
||
names.push(addressObj.cityName);
|
||
}
|
||
if(addressObj.areaName){
|
||
names.push(addressObj.areaName);
|
||
}
|
||
resutlObj.ids=ids.join(",");
|
||
resutlObj.names=names.join(" ");
|
||
return resutlObj;
|
||
}
|
||
|
||
export function handleAmount(amount){
|
||
let currentAmount = parseFloat(amount) || 0;
|
||
if(amount && amount>0){
|
||
currentAmount = "+"+currentAmount;
|
||
}
|
||
|
||
return currentAmount;
|
||
}
|
||
|
||
|
||
/**
|
||
* 获取后四位银行卡号
|
||
* @param {Object} number
|
||
*/
|
||
export function getBankNumber(number){
|
||
let currentAmount = number || '';
|
||
if(number && number.length > 4){
|
||
currentAmount = currentAmount.slice(-4);
|
||
}
|
||
return currentAmount;
|
||
}
|
||
|
||
/**
|
||
* 加密银行卡号
|
||
* @param {Object} cardNumber
|
||
*/
|
||
export function encipherBankNumber(cardNumber){
|
||
if (cardNumber.length <= 8) {
|
||
return cardNumber;
|
||
}
|
||
const firstFour = cardNumber.slice(0, 4); // 获取前四位
|
||
const lastFour = cardNumber.slice(-4); // 获取后四位
|
||
const mask = '****'; // 替换中间的字符
|
||
const maskedCard = firstFour + ' '+mask + ' '+lastFour; // 拼接字符串
|
||
return maskedCard;
|
||
}
|
||
|
||
/**
|
||
* 版本号名比较是否需要升级
|
||
* 返回 true 表示需要升级
|
||
* @param {Object} reqV 之前
|
||
* @param {Object} curV 当前最新
|
||
*/
|
||
export function comparisonVersionHandler(reqV, curV){
|
||
|
||
if (curV && reqV) {
|
||
curV = String(curV);
|
||
reqV = String(reqV);
|
||
//将两个版本号拆成数字
|
||
let arr1 = curV.split('.'), //當前
|
||
arr2 = reqV.split('.');
|
||
let minLength = Math.min(arr1.length, arr2.length),
|
||
position = 0,
|
||
diff = 0;
|
||
//依次比较版本号每一位大小,当对比得出结果后跳出循环
|
||
while (position < minLength && ((diff = parseInt(arr1[position]) - parseInt(arr2[position])) == 0)) {
|
||
position++;
|
||
}
|
||
diff = (diff != 0) ? diff : (arr1.length - arr2.length);
|
||
//若curV大于reqV,则返回true
|
||
return diff > 0;
|
||
} else {
|
||
//输入为空
|
||
return false;
|
||
}
|
||
} |