172 lines
3.8 KiB
TypeScript
172 lines
3.8 KiB
TypeScript
import type {CmsArticle} from "~/api/cms/cmsArticle/model";
|
|
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) {
|
|
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();
|
|
}
|