From 569e6ef693f183bf8ca4c0b2c370f299da935225 Mon Sep 17 00:00:00 2001 From: sunfree <17315650350@163.com> Date: Thu, 27 Jun 2024 22:14:23 +0800 Subject: [PATCH] add news --- src/services/admin/classtic.ts | 46 ++++---- src/stores/index.ts | 110 ++++-------------- .../admin/classticmanage/ClassticFormView.vue | 25 ++-- .../classticmanage/ClassticManageView.vue | 39 ++++--- 4 files changed, 80 insertions(+), 140 deletions(-) diff --git a/src/services/admin/classtic.ts b/src/services/admin/classtic.ts index 79f0cdd..b74ca9c 100644 --- a/src/services/admin/classtic.ts +++ b/src/services/admin/classtic.ts @@ -2,14 +2,9 @@ import { get, remove, put, post } from "@/tools/request" import { reactive, ref } from "vue"; import type { classticInterface, classticFormInterface } from "@/api/admin/classtic" import { classticStore } from "@/stores"; -const { searchValue } = classticStore() +const { searchValue, classticForm } = classticStore() const classticlist = ref([]) -const classticform = reactive({ - header: "", - text: "", - descr: "" -}) const classticList = async () => { try { const response = await get("/classtics/list"); @@ -28,7 +23,25 @@ const classticList = async () => { console.error("Failed to fetch data", error); } } - +const onSubmit = async (id?: any) => { + if (id) { + await put(`/classtics/update/${id}`, classticForm) + // 更新 classticlist + const updatedIndex = classticlist.value.findIndex(item => item.id === id); + console.log(classticlist.value[0]) + if (updatedIndex !== -1) { + classticlist.value[updatedIndex] = { + ...classticlist.value[updatedIndex], // 保留原始项的其他属性 + header: classticForm.header, + text: classticForm.text, + descr: classticForm.descr, + }; + } + } else { + await post("/classtics/add", classticForm) + await classticList() + } +} const classticSearch = async () => { try { const response = await get( @@ -54,29 +67,16 @@ const classticSearch = async () => { const classticDel = async (id: any) => { try { if (id) { - remove(`/classtics/delete/${id}`) + await remove(`/classtics/delete/${id}`) + await classticList() } else { console.log("id is null") } - await classticList() } catch (error) { console.log("request is error") } } -const onSubmit = async (id?: any) => { - if (id) { - await put(`/classtics/update/${id}`, classticform) - } else { - const response = post("/classtics/add", classticform) - const newClasstic = { - ...(await response).data.data, - key: (classticlist.value.length + 1).toString() - } - classticlist.value.push(newClasstic) - - } -} -export { classticlist, classticform, classticList, classticSearch, classticDel, onSubmit } \ No newline at end of file +export { classticlist, classticForm, classticList, classticSearch, classticDel, onSubmit } \ No newline at end of file diff --git a/src/stores/index.ts b/src/stores/index.ts index 2be6ae7..66a32f8 100644 --- a/src/stores/index.ts +++ b/src/stores/index.ts @@ -1,21 +1,13 @@ import { reactive, ref } from 'vue' import { defineStore } from 'pinia' -import { get, post, put, remove } from "@/tools/request" +import { get } from "@/tools/request" import dayjs from 'dayjs'; -import type { blogInterface, classticInterface, classticFormInterface } from '@/api/admin'; +import type { blogInterface, classticFormInterface } from '@/api/admin'; import type { classticSearchInterface } from '@/api/admin/classtic'; -export const indexStore=defineStore("index",()=>{ - const modal=reactive({ - // 控制对话框开关 - open:false, - // 控制对话框title - title:"", - // 控制对话框点击事件标识 - mode:"", - // 通过ids控制哪个功能事件 - ids:"" - }) - return {modal} + +export const indexStore = defineStore("index", () => { + + return {} }) export const classticStore = defineStore("classtic", () => { @@ -27,7 +19,21 @@ export const classticStore = defineStore("classtic", () => { text: "", descr: "" }) - return { searchValue,classticForm } + const modalForm = reactive({ + // 控制对话框开关 + open: false, + // 控制对话框title + title: "", + // 控制对话框点击事件标识 + mode: "", + // 通过ids控制哪个功能事件 + ids: "" + }) + + const modalDel=reactive({ + open:false + }) + return { searchValue, classticForm,modalForm,modalDel } }) @@ -44,8 +50,7 @@ export const classticStore = defineStore("classtic", () => { // 博客列表 const bloglist = ref([]) // 语录列表 -const classticlist = ref([]) -// 语录新增 + export const useAuthStore = defineStore("auth", () => { const tokenValue = ref("") @@ -58,77 +63,6 @@ export const useAuthStore = defineStore("auth", () => { return { setToken, removeToken } }) -// 语录列表接口 -export const classticContentStore = defineStore("classtic", () => { - - - const classticList = async () => { - try { - const response = await get("/classtics/list"); - if (response) { - classticlist.value = response.data.data.map((item: any, index: any) => ({ - key: (index + 1).toString(), - id: item.id, - header: item.header, - text: item.text, - descr: item.descr - })); - } else { - console.error("Response data structure is not as expected:"); - } - } catch (error) { - console.error("Failed to fetch data", error); - } - } - return { classticlist, classticList } -}) -// 语录查询接口 -export const classticSearchStore = defineStore("chassticsearch", () => { - const searchValue = reactive({ - title: "" - }) - const classticSearch = async () => { - try { - const response = await get( - "/classtics/list/search", - { header: searchValue.title } - ) - if (response) { - classticlist.value = response.data.data.map((items: any, index: any) => ({ - key: (index + 1).toString(), - id: items.id, - header: items.header, - text: items.text, - descr: items.descr - })) - } else { - console.log("classtic request is nulll") - } - } catch (error) { - console.log("classtic request is error") - } - } - return { classticSearch, searchValue } -}) - - -// 语录删除 -export const classticDelStore = defineStore("classticdel", () => { - const classticDel = (id: any) => { - try { - if (id) { - remove(`/classtics/delete/${id}`) - classticlist.value = classticlist.value.filter(item => item.id !== id); - } else { - console.log("id is null") - } - } catch (error) { - console.log("request is error") - } - - } - return { classticDel } -}) // 链接接口 export const comLinkContentStore = defineStore("comlink", () => { diff --git a/src/views/admin/classticmanage/ClassticFormView.vue b/src/views/admin/classticmanage/ClassticFormView.vue index 4b30f99..83bd628 100644 --- a/src/views/admin/classticmanage/ClassticFormView.vue +++ b/src/views/admin/classticmanage/ClassticFormView.vue @@ -1,30 +1,35 @@ diff --git a/src/views/admin/classticmanage/ClassticManageView.vue b/src/views/admin/classticmanage/ClassticManageView.vue index bfc773e..c31755a 100644 --- a/src/views/admin/classticmanage/ClassticManageView.vue +++ b/src/views/admin/classticmanage/ClassticManageView.vue @@ -6,8 +6,8 @@ 查询 - 新增 - + 新增 +
@@ -17,12 +17,13 @@
删除 -

确认删除吗?

- 编辑 + 编辑
@@ -32,30 +33,30 @@