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.
 
 
 
 

191 lines
5.4 KiB

import { reactive, ref } from 'vue'
import { defineStore } from 'pinia'
import { get } from "@/tools/request"
import dayjs from 'dayjs';
import { useRouter } from "vue-router"
import type { blogInterface,classticInterface } from '@/api/blog';
const router = useRouter()
// 博客列表
const bloglist = ref<blogInterface[]>([])
// 语录列表
const classticlist = ref<classticInterface[]>([])
export const useAuthStore = defineStore("auth", () => {
const tokenValue = ref("")
function setToken(token: string) {
tokenValue.value = token
}
function removeToken() {
localStorage.removeItem('token');
}
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 comLinkContentStore = defineStore("comlink", () => {
interface comlinkInterface {
id: number,
linktext: string,
linkurl: string
}
const comlinklist = ref<comlinkInterface[]>([])
const comLinkList = async () => {
try {
const response = await get("/comlink/list");
if (response) {
comlinklist.value = response.data.data.map((items: any) => ({
id: items.id,
linktext: items.linktext,
linkurl: items.linkurl
}))
} else {
console.log("response data is not exist")
}
} catch (error) {
console.error("Failed to fetch data", error);
}
}
const comLinkClick = (url: string) => {
if (url.startsWith('http://') || url.startsWith('https://')) {
window.open(url, "_blank"); // 使用 window.location.href 打开外部链接
} else {
router.push(url); // 否则使用 Vue Router 的 push 方法导航
}
}
return { comlinklist, comLinkList, comLinkClick }
})
// 博客列表接口
export const blogContentStore = defineStore("blog", () => {
const blogList = async () => {
try {
const response = await get("/blogs/list");
if (response) {
bloglist.value = response.data.data.map((items: any, index: any) => ({
key: (index + 1).toString(),
blogtitle: items.blogtitle,
create_at: dayjs(items.create_at).format('YYYY-MM-DD HH:mm:ss'),
update_at: dayjs(items.update_at).format('YYYY-MM-DD HH:mm:ss'),
readnum: items.readnum,
readminite: items.readminite,
wordcount: items.wordcount,
img: items.img,
blogcontent: items.blogcontent,
typename: items.typename,
labelnames: items.labelnames
}))
} else {
console.log("bloglist is not exits")
}
} catch (error) {
console.log("bloglist is error")
}
}
return { blogList, bloglist }
})
// 博客查询接口
export const blogSearchStore = defineStore('blogsearch', () => {
const searchValue = reactive({
blogtitle: '',
typename: '',
start_date: '',
end_date: ''
});
const onChange = (date: string, dateString: string[]) => {
if (date && dateString.length === 2) {
searchValue.start_date = dateString[0];
searchValue.end_date = dateString[1];
} else {
searchValue.start_date = '';
searchValue.end_date = '';
}
console.log(dateString[0]);
};
const blogSearch = async () => {
try {
const response = await get("/blogs/list/search", {
blogtitle: searchValue.blogtitle,
typename: searchValue.typename,
start_date: searchValue.start_date,
end_date: searchValue.end_date
});
if (response && response.data) {
bloglist.value = response.data.data.map((items: any, index: any) => ({
key: (index + 1).toString(),
blogtitle: items.blogtitle,
typename: items.typename,
blogcontent: items.blogcontent,
create_at: dayjs(items.create_at).format('YYYY-MM-DD HH:mm:ss'),
update_at: dayjs(items.update_at).format('YYYY-MM-DD HH:mm:ss'),
descr: items.descr
}));
console.log(response)
} else {
console.log("request has no data");
}
} catch (error) {
console.error("request is exception", error);
}
};
return { searchValue, onChange, blogSearch };
});