You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
237 lines
6.1 KiB
237 lines
6.1 KiB
import { v4 as uuidv4 } from 'uuid';
|
|
import type {Company} from "~/api/system/company/model";
|
|
import {useWebsite} from "~/composables/configState";
|
|
const route = useRoute();
|
|
/**
|
|
* 判断是否为整数
|
|
* @param num
|
|
*/
|
|
export const isInteger = (num: any) => {
|
|
return /^-?\d+$/.test(num);
|
|
}
|
|
|
|
/**
|
|
* 提取传参中的ID
|
|
* param 12334.html
|
|
* return 1234
|
|
*/
|
|
export const getIdByParam = () => {
|
|
const split = String(route.params.id).split('.')
|
|
return split[0];
|
|
}
|
|
|
|
export const getLang = () => {
|
|
const i18n = useI18n();
|
|
if(i18n.locale.value == 'zh'){
|
|
return 'zh_CN';
|
|
}
|
|
return i18n.locale.value;
|
|
}
|
|
|
|
/**
|
|
* 提取传参中的ID
|
|
* param 12334.html
|
|
* return 1234
|
|
* @param index
|
|
*/
|
|
export const getIdBySpm = (index: number) => {
|
|
console.log('split',route.query)
|
|
const split = String(route.query.spm).split('.')
|
|
return split[index];
|
|
}
|
|
|
|
/**
|
|
* 获取当前网址的Path部分
|
|
*/
|
|
export const getPath = () => {
|
|
return route.path;
|
|
}
|
|
|
|
export const getKeywords = () => {
|
|
let keywords = ''
|
|
if(route.query?.keywords){
|
|
keywords = `${route.query?.keywords}`;
|
|
}
|
|
return keywords;
|
|
}
|
|
|
|
/**
|
|
* 跳转页面函数
|
|
* 携带用于统计用户行为的参数
|
|
* @param pathInfo /product/detail.html
|
|
* @param id 128
|
|
* @param d 项目数据
|
|
* @param isOpen 是否新窗口打开
|
|
* @param isToken 是否登录控制台
|
|
* 拼接规则: {域名}{path}?spm={模型}.{租户ID}.{商户ID}.{父栏目ID}.{栏目ID}.{详情页ID}.{用户ID}.{timestamp}&token={token}
|
|
* @return https:///websoft.top/item?spm=c.10123.3057.10005.789.18809.5678.1732684776.759&token=DDkr1PpE9DouIVMjLEMt9733QsgG7oNV
|
|
*/
|
|
export function openSpmUrl(pathInfo: string, d?: any, id = 0, isOpen?: boolean, isToken?: boolean): void {
|
|
const config = useWebsite();
|
|
const itemId = ref<number>(0);
|
|
const path = ref<string>('');
|
|
const spm = ref<string>('');
|
|
const tid = d?.tenantId || localStorage.getItem('TenantId');
|
|
const mid = config.value.loginUser?.merchantId || 0;
|
|
const pid = d?.parentId || 0;
|
|
const cid = d?.categoryId || 0;
|
|
const uid = localStorage.getItem('UserId') || 0;
|
|
const timestamp = ref(Date.now() / 1000);
|
|
let m = 'c';
|
|
let token = '';
|
|
|
|
// TODO 登录控制台
|
|
if(isToken){
|
|
token = `&token=${localStorage.getItem('token')}`;
|
|
}
|
|
// TODO 判断模型
|
|
if(d?.articleId){
|
|
m = 'a';
|
|
}
|
|
if(d?.goodsId){
|
|
m = 'g';
|
|
}
|
|
if(d?.productId){
|
|
m = 'p';
|
|
}
|
|
if(d?.appId){
|
|
m = 'app';
|
|
}
|
|
if(d?.plugId){
|
|
m = 'plug';
|
|
}
|
|
if(d?.itemId > 0){
|
|
path.value = d.path;
|
|
itemId.value = d.itemId;
|
|
}else {
|
|
path.value = pathInfo;
|
|
itemId.value = id;
|
|
}
|
|
if(d?.model === 'links'){
|
|
window.open(d.path,'_blank');
|
|
return;
|
|
}
|
|
// TODO 封装spm
|
|
spm.value = `?spm=${m}.${tid}.${mid}.${pid}.${cid}.${itemId.value}.${uid}.${timestamp.value}${token}`;
|
|
|
|
// TODO 账号密码
|
|
if(d?.account || d?.password){
|
|
spm.value += `&account=${d.account}&password=${d.password}`;
|
|
}
|
|
|
|
// TODO 新窗口打开
|
|
if(isOpen){
|
|
window.open(`${path.value}${spm.value}`,'_blank');
|
|
return;
|
|
}else {
|
|
window.open(`${path.value}${spm.value}`, '_top');
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 单点登录控制台
|
|
export function loginAdminByToken(): void {
|
|
const user = useUser();
|
|
const uid = user.value?.userId;
|
|
const tid = user.value?.tenantId;
|
|
openSpmUrl(`https://${tid}.websoft.top/token-login`,undefined, uid, true,true)
|
|
}
|
|
|
|
|
|
// 单点登录开发者中心
|
|
export function loginDeveloperCenterByToken(item: Company): void {
|
|
const user = useUser();
|
|
const tenantId = Number(user.value?.tenantId);
|
|
const token = localStorage.getItem('token');
|
|
if(!token){
|
|
window.open(`https://${item.domain}`, '_blank')
|
|
return;
|
|
}
|
|
openSpmUrl(`https://${item.domain}/token-login`,undefined, tenantId, true,true)
|
|
}
|
|
|
|
// 单点登录应用控制台
|
|
export function loginByToken(item: Company): void {
|
|
const user = useUser();
|
|
const tenantId = Number(user.value?.tenantId);
|
|
const token = localStorage.getItem('token');
|
|
window.open(`https://${item.domain}`, '_blank')
|
|
}
|
|
|
|
/**
|
|
* 获取SpmUrl
|
|
* 拼接规则: {域名}{path}?spm={模型}.{租户ID}.{商户ID}.{父栏目ID}.{栏目ID}.{详情页ID}.{用户ID}.{timestamp}&token={token}
|
|
* @param d
|
|
* @param model
|
|
*/
|
|
export function getSpmUrl(d?: any, model?: string): string {
|
|
const i18n = useI18n();
|
|
let origin = '';
|
|
let locale = '';
|
|
let path = d?.model;
|
|
let mid = d?.merchantId || 0;
|
|
let cid = d?.navigationId;
|
|
let id = d?.itemId;
|
|
let uid = localStorage.getItem('UserId') || 0;
|
|
let timestamp = ref(Date.now() / 1000);
|
|
|
|
// TODO 配置cid
|
|
if(!cid){
|
|
cid = d?.categoryId;
|
|
}
|
|
if(!id){
|
|
id = d?.articleId || 0;
|
|
}
|
|
// TODO 首页
|
|
if(d?.model == 'index'){
|
|
path = '';
|
|
}
|
|
if(model){
|
|
path = model;
|
|
}
|
|
// TODO 手机版
|
|
if(isMobileDevice()){
|
|
// path = 'm/' + path;
|
|
}
|
|
|
|
// TODO 顶级栏目则默认跳转到第一个子栏目
|
|
if(d?.parentId == 0 && d?.children && d?.children.length > 0){
|
|
cid = d?.children?.[0]?.navigationId;
|
|
}
|
|
|
|
// 生成环境
|
|
if(process.env.NODE_ENV === 'production'){
|
|
origin = 'https://' + localStorage.getItem('Domain');
|
|
}
|
|
// 开发环境
|
|
if(process.env.NODE_ENV === 'development'){
|
|
origin = 'http://localhost:10301';
|
|
}
|
|
|
|
// 国际化配置
|
|
if(i18n.locale.value){
|
|
if(i18n.locale.value == 'en'){
|
|
locale = '/en/';
|
|
}else {
|
|
locale = `/`;
|
|
}
|
|
}
|
|
|
|
// TODO 封装spm
|
|
return `${origin}${locale}${path}?spm=${d?.model}.${d?.tenantId}.${mid}.${d?.parentId}.${cid}.${id}.${uid}.${timestamp.value}`;
|
|
}
|
|
|
|
export function openUrl(url: string) {
|
|
window.open(url, '_blank')
|
|
}
|
|
|
|
export function navTo(url: string){
|
|
window.location.href = url;
|
|
}
|
|
|
|
/**
|
|
* 判断是否是移动设备
|
|
*/
|
|
export function isMobileDevice(): boolean {
|
|
return (typeof window.orientation !== "undefined") || /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
|
|
}
|