vr-uniapp/src/utils/index.ts

413 lines
17 KiB
TypeScript
Raw Normal View History

import { createApp } from 'vue';
import { createPinia } from 'pinia';
import { commonStore } from '@/store';
import App from '@/App.vue';
const app = createApp(App);
const pinia = createPinia();
app.use(pinia);
const { common, set_common } = commonStore();
// 定义一组预定义的颜色数组,用于在各种场景中轻松引用这些颜色
// 这些颜色包括从白色到黑色的不同灰度以及一些鲜艳的颜色格式有十六进制、RGB、RGBA、HSV、HSL等
2024-08-30 10:40:12 +00:00
export const predefine_colors = ['#fff', '#ddd', '#ccc', '#999', '#666', '#333', '#000', '#ff4500', '#ff8c00', '#ffd700', '#90ee90', '#00ced1', '#c71585', 'rgba(255, 69, 0, 0.68)', 'rgb(255, 120, 0)', 'hsv(51, 100, 98)', 'hsva(120, 40, 94, 0.5)', 'hsl(181, 100%, 37%)', '#1F93FF', '#c7158577'];
/**
*
*
* 0
* 使`Object.keys(obj).length === 0`
* 使
*
* @param obj
* @returns truefalse
*/
export function is_obj_empty(obj: object): boolean {
return Object.keys(obj).length === 0;
}
2024-09-27 02:51:22 +00:00
/**
*
*
*
*
* @param obj
* @param maxDepth 100
* @param currentDepth 0
* @returns
*/
export function convert_strings_to_numbers(obj: any, maxDepth: number = 100, currentDepth: number = 0): any {
// 递归深度限制
if (currentDepth >= maxDepth) {
return obj;
}
if (Array.isArray(obj)) {
// 处理数组
return obj.map((item) => convert_strings_to_numbers(item, maxDepth, currentDepth + 1));
} else if (is_obj(obj)) {
// 处理对象
return Object.keys(obj).reduce((acc: any, key: string) => {
const value = obj[key];
if (typeof value === 'string') {
// 处理字符串
const numValue = Number(value);
if (!isNaN(numValue) && value.trim() !== '') {
acc[key] = numValue;
} else {
acc[key] = value;
}
} else if (is_obj(value)) {
// 递归处理子对象
acc[key] = convert_strings_to_numbers(value, maxDepth, currentDepth + 1);
} else {
// 其他类型直接保留
acc[key] = value;
}
return acc;
}, {});
} else {
// 非对象类型直接返回
return obj;
}
}
2024-08-20 07:46:49 +00:00
/**
*
*
*
* 1. `null` `null` JavaScript
* 2. 使 `typeof`
* 3. 使 `Object.prototype.toString.call(obj)`
*
* @param obj
* @returns true false
*/
export function is_obj(obj: unknown): boolean {
// 特殊处理 null值因为 typeof null 返回 "object",但 null 并不是我们要检查的对象
if (obj === null) return false;
// 使用 typeof 排除非对象类型
2024-09-25 10:19:18 +00:00
if (typeof obj != 'object') return false;
2024-08-20 07:46:49 +00:00
// 确认是普通对象
return Object.prototype.toString.call(obj) === '[object Object]';
}
/**
*
* color_list: []
* direction
* @param {string[], string} path
* @returns {string}
*/
2024-08-30 10:40:12 +00:00
export function gradient_computer(new_style: gradientStyle, is_return_all: boolean = true) {
let color_list = new_style.color_list;
let direction = new_style.direction;
2024-08-30 10:40:12 +00:00
return gradient_handle(color_list, direction, is_return_all);
}
/**
* 线CSS
*
* @param color_list
* @param direction CSS
2024-08-30 10:40:12 +00:00
* @param is_return_all false
* @returns 线
*/
2024-08-30 10:40:12 +00:00
export function gradient_handle(color_list: color_list[], direction: string, is_return_all: boolean = true) {
let container_common_styles = ``;
if (color_list && color_list.length > 0) {
2024-08-30 10:40:12 +00:00
if (is_return_all) {
container_common_styles += `background:`;
}
container_common_styles += `linear-gradient(${direction || '180deg'},`;
2024-08-14 09:03:33 +00:00
const new_color_list = JSON.parse(JSON.stringify(color_list));
2024-08-14 09:03:33 +00:00
new_color_list.forEach((item: any, index: number) => {
container_common_styles += `${item.color ? item.color : 'rgb(255 255 255 / 0%)'}`;
if (color_list.length == 1) {
2024-08-16 09:16:25 +00:00
container_common_styles += ` ${item.color_percentage || 0}%, ${item.color} 100%`;
} else {
if (typeof item.color_percentage === 'number') {
2024-08-14 09:03:33 +00:00
if (index == color_list.length - 1) {
2024-08-16 09:16:25 +00:00
container_common_styles += ` ${item.color_percentage}%`;
2024-08-14 09:03:33 +00:00
} else {
2024-08-16 09:16:25 +00:00
container_common_styles += ` ${item.color_percentage}%,`;
2024-08-14 09:03:33 +00:00
}
} else {
2024-08-14 09:03:33 +00:00
if (index == color_list.length - 1) {
container_common_styles += ` 100%`;
} else if (index == 0) {
container_common_styles += ` 0%,`;
} else {
container_common_styles += ` ${(100 / color_list.length) * index}%,`;
}
}
}
});
2024-08-30 10:40:12 +00:00
container_common_styles += `)`;
if (is_return_all) {
container_common_styles += `;`;
}
}
return container_common_styles;
}
/**
*
* new_style:
* @param {string[], string} path
* @returns {string}
*/
2024-08-21 10:28:21 +00:00
export function padding_computer(new_style: paddingStyle) {
return `padding: ${new_style.padding_top || 0}px ${new_style.padding_right || 0}px ${new_style.padding_bottom || 0}px ${new_style.padding_left || 0}px;`;
}
/**
*
* new_style:
* @param {string[], string} path
* @returns {string}
*/
export function margin_computer(new_style: marginStyle) {
return `margin: ${new_style.margin_top || 0}px ${new_style.margin_right || 0}px ${new_style.margin_bottom || 0}px ${new_style.margin_left || 0}px;`;
}
/**
*
* new_style:
* @param {string[], string} path
* @returns {string}
*/
export function radius_computer(new_style: radiusStyle) {
return `border-radius: ${new_style.radius_top_left || 0}px ${new_style.radius_top_right || 0}px ${new_style.radius_bottom_right || 0}px ${new_style.radius_bottom_left || 0}px;`;
}
/**
*
* new_style:
* @param {string[], string} path
* @returns {string}
*/
export function box_shadow_computer(new_style: boxShadowStyle) {
return `box-shadow: ${new_style.box_shadow_x || 0}px ${new_style.box_shadow_y || 0}px ${new_style.box_shadow_blur || 0}px ${new_style.box_shadow_spread || 0}px ${new_style.box_shadow_color || 'rgba(0,0,0,0)'};`;
}
/**
*
* new_style:
* @param {string[], string} path
* @returns {string}
*/
export function background_computer(new_style: backgroundImgUrlStyle) {
if (new_style.background_img.length > 0) {
let url_styke = '';
2024-10-10 03:17:33 +00:00
if (new_style.background_img_style == '1') {
url_styke = 'background-repeat: repeat;';
2024-10-10 03:17:33 +00:00
} else if (new_style.background_img_style == '2') {
} else {
url_styke = `background-repeat: no-repeat;background-position: center;`;
}
2024-10-10 03:17:33 +00:00
switch (new_style.background_img_style) {
case '1':
url_styke = `background-repeat: no-repeat;background-position: bottom;background-size: 100% auto;`;
break;
case '2':
url_styke = `background-repeat: no-repeat;background-position: center;background-size: 100% auto;`;
break;
case '3':
url_styke = 'background-repeat: repeat;';
break;
case '4':
url_styke = 'background-size: cover;background-position: center;';
break;
default:
url_styke = `background-repeat: no-repeat;background-position: top;background-size: 100% auto;`;
break;
}
return `background-image:url(${new_style.background_img[0].url});${url_styke}`;
} else {
return '';
}
}
/**
*
*
*
*
*
* @param new_style
* @returns
*/
export function common_styles_computer(new_style: componentsCommonCommonStyle) {
2024-08-27 03:18:37 +00:00
return gradient_computer(new_style) + padding_computer(new_style) + margin_computer(new_style) + radius_computer(new_style) + box_shadow_computer(new_style) + background_computer(new_style) + `overflow:hidden;`;
}
/**
*
* @returns {string} 636
*/
export function get_math() {
// 通过Math.random()生成随机数并转换为36进制的字符串
let randomString = Math.random().toString(36);
// 确保随机字符串至少有6位因为substring(2)可能会使短于6位的字符串产生错误。
// 如果字符串长度不足6位通过padStart将其前面填充为0直到长度达到6位。
randomString = randomString.length >= 6 ? randomString : randomString.padStart(6, '0');
// 截取掉随机字符串开头的'0.'部分获得最终的6位随机字符串。
return randomString.substring(2);
}
/**
*
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2024-07-21
* @desc description
* @param {int} size []
*/
export function annex_size_to_unit(size: number = 0) {
var unit = 'KB';
var kb = size / 1024;
if (kb < 1024) {
unit = 'KB';
size = Math.round(kb * 100) / 100;
} else if (kb < 1024 * 1024) {
unit = 'MB';
size = Math.round((size / (1024 * 1024)) * 100) / 100;
} else if (kb < 1024 * 1024 * 1024) {
unit = 'GB';
size = Math.round((size / (1024 * 1024 * 1024)) * 100) / 100;
}
return size + unit;
}
/**
*
*
*
*
*
* @param name
* @returns
*/
export const ext_name = (name: string) => {
// 查找最后一个点字符的位置。
const i = name.lastIndexOf('.');
// 如果找到了点字符,并且点字符不是在字符串的开头(即确保它是扩展名的一部分)。
if (i >= 0) {
// 从点字符位置到字符串末尾提取扩展名,并转换为小写。
return name.substring(i).toLowerCase();
}
// 如果没有找到点字符,或者点字符在字符串的开头,返回空字符串。
return '';
};
/**
*
*
* @param num
* @param size
* @returns 4
*/
export const percentage_count = (num: number, container_size: number) => {
2024-08-16 09:16:25 +00:00
const marks = (num / container_size) * 100;
return marks.toFixed(4) + '%';
};
/**
*
*
* @param size
* @param location
* @param container_size
* @returns
*/
export const location_compute = (size: number, location: number, container_size: number) => {
if (size + location >= container_size) {
const deviation = container_size - size;
if (deviation >= 0) {
return deviation;
} else {
return 0;
}
} else {
return location;
}
2024-08-16 09:16:25 +00:00
};
/**
* cookie
* @param name cookie
* @returns cookie
*/
export const get_cookie = (name: string) => {
// 初始化cookie值为空字符串
var cookievalue = '';
// 定义要搜索的cookie名称字符串
var search = name + '=';
// 检查是否存在cookie
if (document.cookie.length > 0) {
// 尝试查找cookie名称的位置
let offset = document.cookie.indexOf(search);
// 如果找到了cookie名称
if (offset != -1) {
// 跳过cookie名称的长度
offset += search.length;
// 查找cookie值的结束位置可能是分号或者字符串末尾
let end = document.cookie.indexOf(';', offset);
if (end == -1) end = document.cookie.length;
// 提取并解码cookie值
cookievalue = decodeURIComponent(document.cookie.substring(offset, end));
}
}
// 返回获取到的cookie值
return cookievalue;
};
/**
* cookie
* cookiecookie
* @param name {string} - cookie
* @param value {string} - cookie
* @param expire_time {number} - cookie
*/
export const set_cookie = (name: string, value: string, expire_time?: number) => {
// 构造cookie字符串
var cookie_str = name + '=' + encodeURIComponent(value);
if (expire_time) {
// 获取当前时间
var now = new Date();
// 计算过期时间
var expire_date = new Date(now.getTime() + expire_time * 86400);
cookie_str += ';expires=' + expire_date.toUTCString();
// 将新增的cookie储存到cookie中可以存储多个而不是替换
document.cookie = cookie_str;
}
};
2024-08-26 10:52:35 +00:00
// style 风格
export const tabs_style = (color: string, style: string | number | boolean | undefined) => {
const color_list = ['rgba(51,51,51,1)', 'rgba(255, 34, 34, 1)', 'rgba(255, 255, 255, 1)'];
if (color_list.includes(color)) {
2024-08-27 06:47:00 +00:00
if (style == '2' || style == '4') {
2024-08-26 10:52:35 +00:00
return 'rgba(255, 255, 255, 1)';
} else if (style == '3') {
return 'rgba(255, 34, 34, 1)';
} else {
return 'rgba(51,51,51,1)';
}
} else {
return color;
}
2024-08-27 06:47:00 +00:00
};
2024-08-27 09:19:07 +00:00
/**
* 线URL
* URL
* VITE_APP_BASE_API'/dev-api'
* cookie
* URL
2024-09-03 03:00:15 +00:00
* @param directory {string} -
2024-08-27 09:19:07 +00:00
* @returns {Promise<string>} PromiseURL
*/
2024-09-06 09:05:00 +00:00
export const online_url = async (directory: string = '') => {
2024-08-27 06:47:00 +00:00
if (import.meta.env.VITE_APP_BASE_API == '/dev-api') {
let temp_data = await import(import.meta.env.VITE_APP_BASE_API == '/dev-api' ? '../../temp.d' : '../../temp_pro.d');
2024-09-03 03:00:15 +00:00
return temp_data.default.temp_attachment_host + directory;
2024-08-27 06:47:00 +00:00
} else {
let attachemnt_host = common.config.attachment_host;
return attachemnt_host + directory;
2024-08-27 06:47:00 +00:00
}
};