1
This commit is contained in:
200
utils/common.ts
Normal file
200
utils/common.ts
Normal file
@@ -0,0 +1,200 @@
|
||||
import type {CmsArticle} from "~/api/cms/cmsArticle/model";
|
||||
import {FILE_SERVER} from "~/config";
|
||||
const route = useRoute();
|
||||
/**
|
||||
* 判断是否为整数
|
||||
* @param num
|
||||
*/
|
||||
export const isInteger = (num: any) => {
|
||||
return /^-?\d+$/.test(num);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否是移动设备
|
||||
*/
|
||||
export function isMobileDevice(): boolean {
|
||||
return (typeof window.orientation !== "undefined") || /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前语言版本
|
||||
*/
|
||||
export const getLang = () => {
|
||||
const i18n = useI18n();
|
||||
if(i18n.locale.value == 'zh'){
|
||||
return 'zh_CN';
|
||||
}
|
||||
return i18n.locale.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从spm提取参数
|
||||
* param spm?=0.1.2.3.4.5.6.7
|
||||
* @param index
|
||||
*/
|
||||
export const getIdBySpm = (index: number) => {
|
||||
if(route.query.spm){
|
||||
const split = String(route.query.spm).split('.')
|
||||
return split[index];
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前网址的Path部分
|
||||
* 示例 http://websoft.top/article/123.html
|
||||
* getPath(1) 将返回 article
|
||||
* @param index
|
||||
*/
|
||||
export const getPath = (index?: number) => {
|
||||
const path = route.path;
|
||||
const i18n = useI18n();
|
||||
if(i18n.locale.value == 'en' && index){
|
||||
index = index + 1;
|
||||
}
|
||||
if(index){
|
||||
const split = path.split('/');
|
||||
return split[index];
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前网址的ID部分
|
||||
* 示例 /article/123.html
|
||||
* @return 123
|
||||
*/
|
||||
export const paramsId = () => {
|
||||
let index;
|
||||
index = route.params.id;
|
||||
if(index){
|
||||
try {
|
||||
index = index.toString().split('.')[0];
|
||||
return Number(index);
|
||||
} catch (e) {
|
||||
return Number(index);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
export const getNavIdByParamsId = (text: any) => {
|
||||
if(typeof text === 'number'){
|
||||
return Number(text);
|
||||
}
|
||||
const index = text.toString().split('.')[0];
|
||||
if(typeof index){
|
||||
return Number(index);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 打开新窗口
|
||||
export function openUrl(url: string) {
|
||||
window.open(url, '_blank')
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前网址
|
||||
*/
|
||||
export function locationUrl ():string {
|
||||
return `${window.location}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 导航跳转(推荐使用)
|
||||
* @param d 栏目数据
|
||||
* @param path 手动指定path
|
||||
* @param spm 是否附加spm参数
|
||||
* @return https://websoft.top/article/123.html?spm=0.1.2.3.4.5.6.7
|
||||
*/
|
||||
export function navTo(d?: any, path?: string,spm?: boolean){
|
||||
if(!path){
|
||||
path = d?.path;
|
||||
}
|
||||
// 是否移动设备
|
||||
// if(isMobileDevice()){
|
||||
// path = '/m' + path;
|
||||
// }
|
||||
// 国际化配置
|
||||
const i18n = useI18n();
|
||||
if(i18n.locale.value){
|
||||
if(i18n.locale.value == 'en'){
|
||||
path = '/en' + path;
|
||||
}
|
||||
}
|
||||
// 首页
|
||||
if(d?.model == 'index'){
|
||||
return path;
|
||||
}
|
||||
// if(d?.model == 'links'){
|
||||
// return openUrl(d?.path)
|
||||
// }
|
||||
// 是否附加spm参数
|
||||
if(spm){
|
||||
let uid = localStorage.getItem('UserId') || 0;
|
||||
let timestamp = ref(Date.now() / 1000);
|
||||
return `${path}?spm=${d?.tenantId||0}.0.${d?.parentId}.${d?.navigationId||0}.${uid}.${timestamp.value}`
|
||||
}
|
||||
return `${path}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 详情页跳转
|
||||
* @param d
|
||||
*/
|
||||
export function detail(d?: any) {
|
||||
// console.log(d.detail,'9999999')
|
||||
return navTo(d,`/${d?.detail}/${d?.articleId}.html`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 详情页跳转(手机版专用)
|
||||
* @param d
|
||||
*/
|
||||
export function mDetail(d?: any) {
|
||||
return navTo(d,`/${d?.detail}/${d?.articleId}.html`);
|
||||
}
|
||||
|
||||
export function getViews(form: CmsArticle): number{
|
||||
return Number(form?.actualViews) + Number(form?.virtualViews)
|
||||
}
|
||||
|
||||
|
||||
// 单点登录控制台
|
||||
export function loginAdminByToken(): void {
|
||||
const user = useUser();
|
||||
const token = localStorage.getItem('token');
|
||||
const tid = user.value?.tenantId;
|
||||
openUrl(`https://${tid}.websoft.top/token-login?token=${token}`)
|
||||
}
|
||||
|
||||
export function getTimeStamp(): number {
|
||||
return new Date().getTime();
|
||||
}
|
||||
|
||||
// 复制文本
|
||||
export const copyText = (text: string) => {
|
||||
// 模拟 输入框
|
||||
const cInput = document.createElement('input');
|
||||
cInput.value = text;
|
||||
document.body.appendChild(cInput);
|
||||
cInput.select(); // 选取文本框内容
|
||||
|
||||
// 执行浏览器复制命令
|
||||
// 复制命令会将当前选中的内容复制到剪切板中(这里就是创建的input标签)
|
||||
// Input要在正常的编辑状态下原生复制方法才会生效
|
||||
ElMessage.success(`复制成功`);
|
||||
document.execCommand('copy');
|
||||
|
||||
// 复制成功后再将构造的标签 移除
|
||||
document.body.removeChild(cInput);
|
||||
};
|
||||
|
||||
|
||||
export const getImage = (text: any) => {
|
||||
// 是否包含字符串
|
||||
if(text.indexOf('/upfile') > -1){
|
||||
return FILE_SERVER + text;
|
||||
}
|
||||
return `${text}`
|
||||
}
|
||||
Reference in New Issue
Block a user