From 62f76e851c5335b895e8104ddc8f86a8d572926a Mon Sep 17 00:00:00 2001 From: prr <3099672575@qq.com> Date: Tue, 26 Nov 2024 16:28:51 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9A=E5=AF=86=E7=A0=81=E7=AE=B1?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/setting/SetFilePwd.vue | 121 ++++++++++++------ frontend/src/stores/filePwd.ts | 46 ++++++- 2 files changed, 125 insertions(+), 42 deletions(-) diff --git a/frontend/src/components/setting/SetFilePwd.vue b/frontend/src/components/setting/SetFilePwd.vue index 4af9e9e..6411c6d 100644 --- a/frontend/src/components/setting/SetFilePwd.vue +++ b/frontend/src/components/setting/SetFilePwd.vue @@ -4,35 +4,37 @@

{{ item.pwdName }}

- + default
- - + + - + + :active-value="1" + :inactive-value="0" + /> - + 确认 @@ -45,50 +47,98 @@ import { Plus, Delete } from '@element-plus/icons-vue' import { reactive, ref, onMounted } from "vue"; import { useFilePwdStore } from '@/stores/filePwd'; +import { fetchGet, getApiUrl } from "@/system/config"; +import { md5 } from 'js-md5'; +import { ElMessageBox } from 'element-plus' const dialogShow = ref(false) const formData = reactive({ pwdName: '', pwd: '', - isDefault: '' + isDefault: 0 }) +const defaultChoose = ref(false) const filePwdStore: any = useFilePwdStore() + +const rule = { + pwdName: [ + { required: true, message: '密码提示不能为空', trigger: 'blur'}, + { min: 2, max: 10, message: '昵称长度应该在2到10位', trigger: 'blur'} + ], + pwd: [ + { required: true, message: '密码不能为空', trigger: 'blur'}, + { min: 6, max: 10, message: '密码长度应该在6到10位', trigger: 'blur'} + ] +} +const pwdRef:any = ref(null) function closeDialog (val: boolean) { dialogShow.value = val } +async function operationPwd(type: string, item?: any) { + if (type == 'add') { + formData.isDefault == 1 ? showPwdDialog(type) : addPwd() + } else { + item.isDefault == 1 ? showPwdDialog(type, item) : delPwd(item) + } +} +// 弹窗提示用户是否操作默认密码 +function showPwdDialog(type: string, item?: any) { + const content = type == 'add' ? '确定设置为默认密码吗?' : '确定删除默认密码吗?' + ElMessageBox.alert(content, '提示', { + confirmButtonText: '确定', + callback: (action: any) => { + if (type == 'add') { + action !== 'cancle' ? addPwd() : '' + }else { + action !== 'cancle' ? delPwd(item) : '' + } + } + }) +} async function addPwd() { + await pwdRef.value.validate() closeDialog(false) const temp = { ...formData } + if (formData.isDefault == 1) { + //保证默认密码唯一性 + filePwdStore.hasDefaultPwd ? await filePwdStore.setDefaultPwd() : '' + await fetchDefaultPwd(formData.pwd) + } await filePwdStore.addPwd(temp) await initData() } +// 将默认密码传递给后端 +async function fetchDefaultPwd(pwd: string) { + const head = { + pwd: pwd !== '' ? md5(pwd) : '' + } + await fetchGet(`${getApiUrl()}/file/setfilepwd`, head); +} async function initData () { await filePwdStore.getPage() + formData.pwdName = '' + formData.pwd = '' + if (!filePwdStore.hasDefaultPwd) { + formData.isDefault = 1 + defaultChoose.value = true + return + } + formData.isDefault = 0 + defaultChoose.value = false +} +async function handleCurrentChange(val: number) { + await filePwdStore.pageChange(val) +} +async function delPwd(item: any) { + if (item.isDefault == 1) { + await fetchDefaultPwd('') + } + await filePwdStore.delPwd(item.id) + await initData() } onMounted(async() => { await initData() - // console.log('数据:', filePwdStore.pwdList, filePwdStore.page.total); + console.log('数据:', filePwdStore.pwdList, filePwdStore.page); }) - // import { t } from "@/system"; - // import { - // fetchGet, - // getApiUrl, - // getSystemConfig, - // setSystemKey, - // } from "@/system/config"; - // const config = getSystemConfig(); - // const isSetPwd = ref(false); - // async function setFilePwd() { - // isSetPwd.value = !isSetPwd.value - // const params = { - // isPwd: isSetPwd.value ? 1 : 0 - // } - // await fetchGet(`${getApiUrl()}/file/changeispwd?ispwd=${params.isPwd}`); - // setSystemKey("file", params); - - // } - // onMounted(() => { - // isSetPwd.value = config.file.isPwd ? true : false; - // }); diff --git a/frontend/src/stores/filePwd.ts b/frontend/src/stores/filePwd.ts index 317b098..c87ffa9 100644 --- a/frontend/src/stores/filePwd.ts +++ b/frontend/src/stores/filePwd.ts @@ -11,7 +11,7 @@ export const useFilePwdStore = defineStore('filePwd', () => { visible: 5 }) const pwdList = ref([]) - + const hasDefaultPwd = ref(false) const addPwd = async (params: any) => { await db.addOne('filePwdBox', params) } @@ -22,6 +22,23 @@ export const useFilePwdStore = defineStore('filePwd', () => { pwdList.value = await db.getPage('filePwdBox', page.value.current, page.value.size) } await getPageCount() + await checkDefault() + } + const delPwd = async (id: number) => { + await db.delete('filePwdBox', id) + //如果之前存在默认密码,则清除 + // if (params.isDefault == 1) { + // const list = await db.getAll('filePwdBox') + // if (list && list?.length > 0) { + // const item = list[list.length - 1] + // item.isDefault = 1 + // await db.update('filePwdBox', item.id, item) + // } + // } + } + const pageChange = async (current: number) => { + page.value.current = current + getPage() } const getPageCount = async () => { page.value.total = await db.countSearch('filePwdBox') @@ -32,13 +49,36 @@ export const useFilePwdStore = defineStore('filePwd', () => { // 如果有余数,则加1 page.value.pages++; } - //console.log(pageCount.value) return page.value } + // 判断是否有默认密码 + const checkDefault = async () => { + const res = await db.getByField('filePwdBox', 'isDefault', 1) + if (res && res.length > 0) { + hasDefaultPwd.value = true + return + } + hasDefaultPwd.value = false + } + //将其余默认密码设置为空 + const setDefaultPwd = async () => { + const res = await db.getByField('filePwdBox', 'isDefault', 1) + if (!res && res.length == 0) { + return + }; + for (const item of res) { + item.isDefault = 0 + db.update('filePwdBox', item.id, item) + } + } return { page, pwdList, + hasDefaultPwd, addPwd, - getPage + getPage, + pageChange, + delPwd, + setDefaultPwd } }) \ No newline at end of file