Files
fe.ems.vue3/src/views/system/setting/components/change-login-bg.vue
2025-02-26 11:12:06 +08:00

227 lines
6.3 KiB
Vue

<script lang="ts" setup>
import { Modal, message } from 'ant-design-vue/es';
import { onMounted, reactive } from 'vue';
import useAppStore from '@/store/modules/app';
import useI18n from '@/hooks/useI18n';
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
import { changeValue } from '@/api/system/config';
import { uploadFile } from '@/api/tool/file';
import { FileType } from 'ant-design-vue/es/upload/interface';
import { UploadRequestOption } from 'ant-design-vue/es/vc-upload/interface';
import { sessionGet } from '@/utils/cache-session-utils';
import { parseUrlPath } from '@/plugins/file-static-url';
const appStore = useAppStore();
const { t } = useI18n();
type StateType = {
edite: boolean;
loading: boolean;
filePath: string;
flag: string;
};
let state: StateType = reactive({
edite: false,
loading: false,
filePath: '',
flag: '',
});
/**上传前检查或转换压缩 */
function fnBeforeUpload(file: FileType) {
if (state.loading) return false;
const isJpgOrPng = [
'image/jpeg',
'image/png',
'image/svg+xml',
'image/webp',
].includes(file.type);
if (!isJpgOrPng) {
message.error(
t('views.system.setting.uploadFormat', { format: 'jpg、png、svg、webp' }),
3
);
}
const isLt10M = file.size / 1024 / 1024 < 10;
if (!isLt10M) {
message.error(t('views.system.setting.uploadSize', { size: 10 }), 3);
}
return isJpgOrPng && isLt10M;
}
/**上传变更 */
function fnUpload(up: UploadRequestOption) {
Modal.confirm({
title: t('common.tipTitle'),
content: t('views.system.setting.sysLoginBgTipContentUpload'),
onOk() {
// 发送请求
const hide = message.loading(t('common.loading'), 0);
state.loading = true;
let formData = new FormData();
formData.append('file', up.file);
formData.append('subPath', 'default');
uploadFile(formData).then(res => {
state.loading = false;
hide();
if (res.code === RESULT_CODE_SUCCESS) {
message.success(t('views.system.setting.uploadSuccess'), 3);
state.filePath = res.data.filePath;
// 兼容旧前端可改配置文件
const baseUrl = import.meta.env.PROD
? sessionGet('baseUrl') || import.meta.env.VITE_API_BASE_URL
: import.meta.env.VITE_API_BASE_URL;
state.flag = `${baseUrl}${res.data.filePath}`;
} else {
message.error(res.msg, 3);
}
});
},
});
}
/**进入可编辑 */
function fnEdit(v: boolean) {
state.edite = v;
if (!v) {
state.filePath = '#';
state.flag = parseUrlPath(appStore.loginBackground);
}
}
/**提交保存 */
function fnSave() {
Modal.confirm({
title: t('common.tipTitle'),
content: t('views.system.setting.sysLoginBgTipContent'),
onOk() {
// 发送请求
const hide = message.loading(t('common.loading'), 0);
state.loading = true;
changeValue({ key: 'sys.loginBackground', value: state.filePath }).then(
res => {
state.loading = false;
hide();
if (res.code === RESULT_CODE_SUCCESS) {
message.success(t('views.system.setting.saveSuccess'), 3);
appStore.loginBackground = state.filePath;
fnEdit(false);
} else {
message.error(res.msg, 3);
}
}
);
},
});
}
/**还原初始背景 */
function fnRevert() {
Modal.confirm({
title: t('common.tipTitle'),
content: t('views.system.setting.sysLoginBgTipContentRevert'),
onOk() {
// 发送请求
const hide = message.loading(t('common.loading'), 0);
state.loading = true;
state.filePath = '#';
changeValue({ key: 'sys.loginBackground', value: state.filePath }).then(
res => {
state.loading = false;
hide();
if (res.code === RESULT_CODE_SUCCESS) {
message.success(t('views.system.setting.revertSuccess'), 3);
appStore.loginBackground = state.filePath;
fnEdit(false);
} else {
message.error(res.msg, 3);
}
}
);
},
});
}
onMounted(() => {
state.filePath = parseUrlPath(appStore.loginBackground);
state.flag = parseUrlPath(appStore.loginBackground);
});
</script>
<template>
<a-row>
<a-col :lg="12" :md="12" :xs="24" style="margin-bottom: 30px">
<div class="sys-login-bg">
<span v-if="state.flag === '#'">
{{ t('views.system.setting.sysLoginBgNone') }}
</span>
<a-image :src="state.flag" v-else />
</div>
<a-form layout="vertical" v-if="state.edite">
<a-form-item>
<a-upload
name="file"
list-type="picture"
accept=".jpg,.png,.svg,.webp"
:max-count="1"
:show-upload-list="false"
:before-upload="fnBeforeUpload"
:custom-request="fnUpload"
>
<a-button type="link" :disabled="state.loading">
{{ t('views.system.setting.uploadFile') }}
</a-button>
</a-upload>
</a-form-item>
<a-form-item>
<a-button
type="primary"
:disabled="state.filePath === state.flag"
@click="fnSave"
>
{{ t('views.system.setting.saveSubmit') }}
</a-button>
<a-button style="margin-left: 10px" @click="fnEdit(false)">
{{ t('common.cancel') }}
</a-button>
</a-form-item>
</a-form>
<template v-else>
<a-button type="default" @click="fnEdit(true)">
{{ t('common.editText') }}
</a-button>
<a-button
type="text"
danger
style="margin-left: 10px"
@click="fnRevert"
>
{{ t('views.system.setting.revert') }}
</a-button>
</template>
</a-col>
<a-col :lg="12" :md="12" :xs="24">
<a-typography>
<a-typography-paragraph>
{{ t('views.system.setting.sysLoginBgInstruction') }}<br />
{{ t('views.system.setting.sysLoginBgInstruction1') }}<br />
{{ t('views.system.setting.sysLoginBgInstruction2') }}
</a-typography-paragraph>
</a-typography>
</a-col>
</a-row>
</template>
<style lang="less" scoped>
.sys-login-bg {
width: 300px;
min-height: 150px;
border: 1px var(--ant-primary-color) solid;
margin-bottom: 24px;
text-align: center;
}
</style>