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.
159 lines
5.3 KiB
159 lines
5.3 KiB
const route = useRoute();
|
|
|
|
/**需要进行持久化的数据:把需要持久化的数据放在下面这个对象中,才会持久化,不需要持久化的数据就不用放到这里了。 */
|
|
const enduring: { [key: string]: () => Ref<any> } = {
|
|
useToken, useConfigInfo
|
|
}
|
|
|
|
//下面的俩函数在app.vue的onMounted中统一调用,或者在其它情况挂载后单独调用。
|
|
/**把所有指定数据保存到本地存储
|
|
* @param key 要保存的数据名。不填的话就是保存全部(一般不填,统一在页面关闭时保存。如果是特别重要的数据,就时不时单独保存一下即可。)
|
|
*/
|
|
export const setLocal = (key?: string) => {
|
|
if (key) {
|
|
console.log('只保存', key);
|
|
const useKey = 'use' + key.slice(0, 1).toUpperCase() + key.slice(1).toLowerCase(); //首字母大写,其它全部转小写
|
|
const func = enduring[useKey];
|
|
if (!func) {
|
|
console.log('没有找到', useKey, '对应的函数');
|
|
return;
|
|
}
|
|
localStorage.setItem(key, JSON.stringify(func().value));
|
|
} else {
|
|
console.log('正在保存全部数据');
|
|
for (const key in enduring) {
|
|
if (Object.prototype.hasOwnProperty.call(enduring, key)) {
|
|
const element = enduring[key];
|
|
const setKey = key.toLowerCase().substring(3); //去掉前面的use ,其它全部转小写
|
|
try {
|
|
localStorage.setItem(setKey, JSON.stringify(element().value));
|
|
} catch (error) {
|
|
console.log(`在设置${setKey}的数据时出现错误`, error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
/**从本地存储获取数据到state中 */
|
|
export const getLoacl = () => {
|
|
for (const key in enduring) {
|
|
if (Object.prototype.hasOwnProperty.call(enduring, key)) {
|
|
const element = enduring[key];
|
|
const setKey = key.toLowerCase().substring(3); //去掉前面的use ,其它全部转小写
|
|
try {
|
|
const localData = localStorage.getItem(setKey) || '';
|
|
if (localData) {
|
|
element().value = JSON.parse(localData);
|
|
console.log(setKey, '的本地存储数据获取成功', element().value);
|
|
}
|
|
} catch (error) {
|
|
console.log(`在获取${setKey}的数据时出现错误`, error);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 判断是否为整数
|
|
* @param num
|
|
*/
|
|
export const isInteger = (num: any) => {
|
|
return /^-?\d+$/.test(num);
|
|
}
|
|
|
|
/**
|
|
* 提取传参中的ID
|
|
* param 12334.html
|
|
* return 1234
|
|
* @param num
|
|
*/
|
|
export const getIdByParam = () => {
|
|
const split = String(route.params.id).split('.')
|
|
return split[0];
|
|
}
|
|
|
|
/**
|
|
* 提取传参中的ID
|
|
* param 12334.html
|
|
* return 1234
|
|
* @param num
|
|
*/
|
|
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;
|
|
}
|
|
|
|
|
|
/**
|
|
* 跳转页面函数
|
|
* 携带用于统计用户行为的参数
|
|
* @param path /product/detail.html
|
|
* @param id 128
|
|
* @param d 项目数据
|
|
* @param isOpen 是否新窗口打开
|
|
* 拼接规则: {域名}{path}?spm=c.{用户ID}.{租户ID}.{栏目ID}.{商品ID}.{timestamp}&token={token}
|
|
* @return https:///websoft.top/product/detail/128.html?spm=c.5.3057.10005.undefined&token=DDkr1PpE9DouIVMjLEMt9733QsgG7oNV
|
|
*/
|
|
export function openSpmUrl(path: string, d?: any, id = 0, isOpen?: boolean): void {
|
|
const domain = route.path;
|
|
const spm = ref<string>('');
|
|
const token = ref<string>();
|
|
const url = ref<string>();
|
|
const tid = d?.tenantId || 0;
|
|
const pid = d?.parentId || 0;
|
|
const cid = d?.categoryId || 0;
|
|
const uid = localStorage.getItem('UserId') || 0;
|
|
const timestamp = ref(Date.now() / 1000);
|
|
token.value = `&token=${localStorage.getItem('token')}`;
|
|
|
|
// TODO 封装租户ID和店铺ID
|
|
spm.value = `?spm=c.${uid}.${tid}.${pid}.${cid}.${id}.${timestamp.value}${token.value}`;
|
|
|
|
// TODO 含http直接跳转
|
|
if (path.slice(0, 4) == 'http') {
|
|
if(isOpen){
|
|
window.open(`${path}${spm.value}`);
|
|
return;
|
|
}
|
|
location.href = `${path}${spm.value}`;
|
|
return;
|
|
}
|
|
|
|
// 跳转页面
|
|
url.value = `${path}${spm.value}${token.value}`;
|
|
}
|
|
|
|
|
|
/**
|
|
* 生成spm路径
|
|
* 携带用于统计用户行为的参数
|
|
* @param path /product/detail.html
|
|
* @param id 128
|
|
* @param d 项目数据
|
|
* 拼接规则: {域名}{path}?spm=c.{用户ID}.{租户ID}.{栏目ID}.{商品ID}.{timestamp}&token={token}
|
|
* @return https:///websoft.top/product/detail/128.html?spm=c.5.3057.10005.undefined&token=DDkr1PpE9DouIVMjLEMt9733QsgG7oNV
|
|
*/
|
|
export function getSpmUrl(path: string, d?: any, id = 0): string {
|
|
const domain = route.path;
|
|
const spm = ref<string>('');
|
|
const token = ref<string>();
|
|
const url = ref<string>();
|
|
const tid = d?.tenantId || 0;
|
|
const pid = d?.parentId || 0;
|
|
const cid = d?.categoryId || 0;
|
|
const uid = localStorage.getItem('UserId') || 0;
|
|
const timestamp = ref(Date.now() / 1000);
|
|
token.value = `&token=${localStorage.getItem('token')}`;
|
|
|
|
// TODO 封装租户ID和店铺ID
|
|
spm.value = `?spm=c.${uid}.${tid}.${pid}.${cid}.${id}.${timestamp.value}${token.value}`;
|
|
return spm.value;
|
|
}
|