vr-uniapp/src/views/layout/index.vue

277 lines
8.9 KiB
Vue
Raw Normal View History

<template>
2024-09-02 10:16:16 +00:00
<div v-loading.fullscreen.lock="loading" class="app-wrapper no-copy" element-loading-background="rgba(255,255,255,1)" element-loading-custom-class="loading-custom">
2024-08-22 09:53:35 +00:00
<template v-if="!is_empty">
<navbar v-model="form.model" @preview="preview" @save="save" @save-close="save_close" />
<div class="app-wrapper-content flex-row">
2024-09-05 06:36:28 +00:00
<app-main :diy-data="form.diy_data" :header="form.header" :footer="form.footer" @right-update="right_update" @import="import_data_event" @export="export_data_event"></app-main>
2024-08-22 09:53:35 +00:00
<settings :key="key" :value="diy_data_item"></settings>
</div>
</template>
<template v-else>
2024-09-05 06:36:28 +00:00
<no-data height="100vh" img-width="260px" size="16px" text="编辑数据有误"></no-data>
2024-08-22 09:53:35 +00:00
</template>
</div>
</template>
<script setup lang="ts">
2024-09-05 06:36:28 +00:00
import type { UploadFile, UploadFiles } from 'element-plus';
import { Navbar, Settings, AppMain } from './components/index';
import defaultSettings from './components/main/index';
import { cloneDeep } from 'lodash';
2024-08-22 09:53:35 +00:00
import DiyAPI, { diyData } from '@/api/diy';
2024-09-02 03:39:47 +00:00
import CommonAPI from '@/api/common';
import { commonStore } from '@/store';
const common_store = commonStore();
interface headerAndFooter {
name: string;
2024-09-05 06:29:43 +00:00
show_tabs: string;
key: string;
com_data: any;
}
interface diy_data_item {
2024-08-22 09:53:35 +00:00
id: string;
model: {
2024-08-23 09:14:43 +00:00
logo: string;
name: string;
2024-08-23 09:14:43 +00:00
is_enable: string;
describe: string;
};
header: headerAndFooter;
footer: headerAndFooter;
diy_data: Array<any>;
}
const form = ref<diy_data_item>({
2024-08-22 09:53:35 +00:00
id: '',
model: {
2024-08-23 09:14:43 +00:00
logo: '',
2024-08-22 10:44:48 +00:00
name: '',
2024-08-23 09:14:43 +00:00
is_enable: '1',
describe: '',
},
header: {
name: '页面设置',
2024-09-05 06:29:43 +00:00
show_tabs: '1',
key: 'page-settings',
com_data: defaultSettings.header_nav,
},
footer: {
name: '底部导航',
2024-09-05 06:29:43 +00:00
show_tabs: '0',
key: 'footer-nav',
com_data: defaultSettings.footer_nav,
},
diy_data: [],
});
const diy_data_item = ref({});
const key = ref('');
2024-09-02 10:16:16 +00:00
const api_count = ref(0);
const right_update = (item: any, diy: [Array<any>], header: headerAndFooter, footer: headerAndFooter) => {
diy_data_item.value = item;
form.value.diy_data = diy;
form.value.header = header;
form.value.footer = footer;
// 生成随机id
key.value = Math.random().toString(36).substring(2);
};
2024-09-05 06:36:28 +00:00
const import_data_event = (uploadFile: UploadFile) => {
// 截取document.location.search字符串内id/后面的所有字段
const form_data = new FormData();
if (get_id()) {
form_data.append('id', get_id());
}
if (uploadFile && uploadFile.raw) {
form_data.append('file', uploadFile?.raw);
}
DiyAPI.import(form_data).then((res: any) => {
ElMessage.success(res.msg);
history.pushState({}, '', '?s=diy/saveinfo/id/' + res.data + '.html');
init();
});
};
const export_data_event = () => {
if (get_id()) {
ElMessageBox.confirm('导出前需先保存最新数据,是否继续?', '提示')
.then(() => {
const index = window.location.href.lastIndexOf('?s=');
const pro_url = window.location.href.substring(0, index);
const new_url = import.meta.env.VITE_APP_BASE_API == '/dev-api' ? import.meta.env.VITE_APP_BASE_API_URL : pro_url;
window.open(new_url + '?s=diyapi/diydownload/id/' + get_id() + '.html', '_blank');
})
.catch(() => {});
} else {
ElMessage.warning('请先保存,再导出');
}
};
//#region 页面初始化数据 ---------------------start
// 页面加载
onMounted(() => {
init();
2024-09-02 03:39:47 +00:00
common_init();
});
2024-08-22 09:53:35 +00:00
const is_empty = ref(false);
const init = () => {
2024-09-05 06:36:28 +00:00
if (get_id()) {
DiyAPI.getInit({ id: get_id() }).then((res: any) => {
if (res.data) {
form.value = form_data_transfor_diy_data(res.data);
} else {
is_empty.value = true;
}
api_count.value += 1;
loading_event(api_count.value);
});
2024-09-02 10:16:16 +00:00
} else {
api_count.value = 1;
loading_event(api_count.value);
}
};
2024-09-02 03:39:47 +00:00
// 初始化公共数据
const common_init = () => {
CommonAPI.getInit().then((res: any) => {
common_store.set_common(res.data);
2024-09-02 10:16:16 +00:00
api_count.value += 1;
loading_event(api_count.value);
2024-09-02 03:39:47 +00:00
});
};
2024-09-02 10:16:16 +00:00
// 加载动画
const loading = ref(true);
const loading_event = (count: number) => {
if (count == 2) {
setTimeout(() => {
loading.value = false;
}, 500);
}
};
//#endregion 页面初始化数据 ---------------------end
//#region 顶部导航回调方法 ---------------------start
const preview = () => {
console.log('预览');
};
const save = () => {
save_formmat_form_data(form.value);
};
const save_close = () => {
save_formmat_form_data(form.value, true);
};
const save_formmat_form_data = (data: diy_data_item, close: boolean = false) => {
const clone_form = cloneDeep(data);
2024-09-05 06:29:43 +00:00
clone_form.header.show_tabs = '1';
clone_form.footer.show_tabs = '0';
// const new_array = ['goods-list', 'goods-tabs', 'article-list', 'article-tabs', 'coupon'];
clone_form.diy_data = clone_form.diy_data.map((item: any) => {
// if (new_array.includes(item.key)) {
// item.com_data.content.data_ids = item.com_data.content.data_list.map((item: any) => item.data.id).join(',') || '';
// item.com_data.content.data_list = [];
// } else if (item.key == 'data-magic') {
// item.com_data.content.data_magic_list.map((item1: any) => {
// item1.goods_ids = item.goods_list.map((item2: any) => item2.data.id).join(',') || '';
// item1.goods_list = [];
// });
// }
return {
...item,
2024-09-05 06:29:43 +00:00
show_tabs: '0',
};
});
// 将数据暂存到localStorage
2024-08-22 09:53:35 +00:00
// localStorage.setItem('diy_data', JSON.stringify(clone_form));
// 数据改造
const new_data = diy_data_transfor_form_data(clone_form);
DiyAPI.save(new_data).then((res) => {
ElMessage.success('保存成功');
2024-09-05 06:36:28 +00:00
if (close) {
ElMessageBox.confirm('您确定要关闭本页吗?', '提示')
.then(() => {
// 关闭页面
window.close();
})
.catch(() => {});
} else {
history.pushState({}, '', '?s=diy/saveinfo/id/' + res.data + '.html');
}
2024-08-22 09:53:35 +00:00
});
};
//#endregion 顶部导航回调方法 ---------------------end
2024-08-22 09:53:35 +00:00
// 数据改造
const diy_data_transfor_form_data = (clone_form: diy_data_item) => {
return {
id: clone_form.id,
2024-08-23 09:14:43 +00:00
logo: clone_form.model.logo,
2024-08-22 09:53:35 +00:00
name: clone_form.model.name,
2024-08-23 09:14:43 +00:00
is_enable: clone_form.model.is_enable,
describe: clone_form.model.describe,
2024-08-22 09:53:35 +00:00
config: JSON.stringify({
2024-08-22 10:40:15 +00:00
header: clone_form.header,
footer: clone_form.footer,
2024-08-22 09:53:35 +00:00
diy_data: clone_form.diy_data,
}),
};
};
const form_data_transfor_diy_data = (clone_form: diyData) => {
try {
return {
id: clone_form.id,
model: {
2024-08-23 09:14:43 +00:00
logo: clone_form.logo,
2024-08-22 09:53:35 +00:00
name: clone_form.name,
2024-08-23 09:14:43 +00:00
is_enable: clone_form.is_enable,
describe: clone_form.describe,
2024-08-22 09:53:35 +00:00
},
header: JSON.parse(clone_form.config).header,
footer: JSON.parse(clone_form.config).footer,
diy_data: JSON.parse(clone_form.config).diy_data,
};
} catch (error) {
return {
id: clone_form.id,
model: {
2024-08-23 09:14:43 +00:00
logo: clone_form.logo,
2024-08-22 09:53:35 +00:00
name: clone_form.name,
2024-08-23 09:14:43 +00:00
is_enable: clone_form.is_enable,
describe: clone_form.describe,
2024-08-22 09:53:35 +00:00
},
header: form.value.header,
footer: form.value.footer,
diy_data: form.value.diy_data,
};
}
};
2024-09-05 06:36:28 +00:00
// 截取document.location.search字符串内id/后面的所有字段
const get_id = () => {
let new_id = '';
if (document.location.search.indexOf('id/') !== -1) {
new_id = document.location.search.substring(document.location.search.indexOf('id/') + 3);
// 去除字符串的.html
let html_index = new_id.indexOf('.html');
if (html_index !== -1) {
new_id = new_id.substring(0, html_index);
}
return new_id;
} else {
return new_id;
}
};
</script>
<style scoped lang="scss">
.app-wrapper {
background-color: #f0f2f5;
.app-wrapper-content {
2024-09-03 06:27:36 +00:00
height: calc(100vh - 6rem);
}
}
.no-copy {
-webkit-touch-callout: none; /* iOS Safari */
-webkit-user-select: none; /* Safari */
-khtml-user-select: none; /* Konqueror HTML */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* Internet Explorer/Edge */
user-select: none; /* Non-prefixed version, currently supported by Chrome and Opera */
}
</style>