2024-08-09 15:15:12 +08:00
|
|
|
import Vue from "vue"
|
|
|
|
import Vuex from "vuex"
|
2023-03-28 18:04:49 +08:00
|
|
|
|
|
|
|
Vue.use(Vuex)
|
|
|
|
|
|
|
|
export default new Vuex.Store({
|
2024-08-09 15:15:12 +08:00
|
|
|
state: {
|
|
|
|
historicalSearch: [], // 历史数据
|
|
|
|
allForumList: [], // 全部板块数据
|
|
|
|
homeRequestState: false, // 首页推荐和收藏接口的数据请求状态 这个是是否需要发送请求,因为用户点击收藏后需要重新获取
|
|
|
|
getUserInfoState: false, // 这个是是否在请求状态
|
|
|
|
favoriteList: [], // 收藏板块 数据
|
|
|
|
recommendList: [], // 推荐板块数据
|
|
|
|
menu: [],
|
|
|
|
user: {}, // 用户信息
|
|
|
|
hotSearchkeywords: [], // 热门搜索
|
|
|
|
loading: null,
|
2023-04-07 16:49:43 +08:00
|
|
|
},
|
2024-08-09 15:15:12 +08:00
|
|
|
getters: {},
|
2023-04-07 16:49:43 +08:00
|
|
|
|
2024-08-09 15:15:12 +08:00
|
|
|
mutations: {
|
|
|
|
setHistoricalSearch(state, payload) {
|
|
|
|
if (!Array.isArray(payload)) payload = [payload]
|
|
|
|
let targetArr = [...new Set([...payload, ...state.historicalSearch])]
|
|
|
|
if (targetArr.length > 10) targetArr = targetArr.slice(0, 10)
|
2023-04-10 15:37:48 +08:00
|
|
|
|
2024-08-09 15:15:12 +08:00
|
|
|
state.historicalSearch = targetArr
|
|
|
|
localStorage.setItem("historicalSearch", JSON.stringify(targetArr))
|
|
|
|
},
|
2023-04-10 15:37:48 +08:00
|
|
|
|
2024-08-09 15:15:12 +08:00
|
|
|
setAllForumList(state, payload) {
|
|
|
|
state.allForumList = payload
|
|
|
|
},
|
2023-04-11 17:56:16 +08:00
|
|
|
|
2024-08-09 15:15:12 +08:00
|
|
|
setHomeRequestState(state, payload) {
|
|
|
|
state.homeRequestState = payload
|
|
|
|
},
|
2023-04-11 17:56:16 +08:00
|
|
|
|
2024-08-09 15:15:12 +08:00
|
|
|
setFavoriteList(state, payload) {
|
|
|
|
state.favoriteList = payload
|
|
|
|
},
|
2023-04-11 17:56:16 +08:00
|
|
|
|
2024-08-09 15:15:12 +08:00
|
|
|
setRecommendList(state, payload) {
|
|
|
|
state.recommendList = payload
|
|
|
|
},
|
2023-04-12 19:29:41 +08:00
|
|
|
|
2024-08-09 15:15:12 +08:00
|
|
|
setUser(state, payload) {
|
|
|
|
state.user = payload
|
|
|
|
},
|
2023-04-07 16:49:43 +08:00
|
|
|
|
2024-08-09 15:15:12 +08:00
|
|
|
setHotSearchkeywords(state, payload) {
|
|
|
|
state.hotSearchkeywords = payload
|
|
|
|
},
|
2023-04-06 15:00:13 +08:00
|
|
|
|
2024-08-09 15:15:12 +08:00
|
|
|
setgetUserInfoState(state, payload) {
|
|
|
|
state.getUserInfoState = payload
|
|
|
|
},
|
2023-04-07 16:49:43 +08:00
|
|
|
|
2024-08-09 15:15:12 +08:00
|
|
|
setMenu(state, payload) {
|
|
|
|
state.menu = payload
|
|
|
|
},
|
2023-04-11 17:56:16 +08:00
|
|
|
},
|
|
|
|
|
2024-08-09 15:15:12 +08:00
|
|
|
actions: {
|
|
|
|
// 获取历史搜索的数据
|
|
|
|
fetchHistoricalSearch({ commit }) {
|
|
|
|
let historicalSearch = JSON.parse(localStorage.getItem("historicalSearch")) || []
|
|
|
|
commit("setHistoricalSearch", historicalSearch)
|
|
|
|
},
|
|
|
|
|
|
|
|
// 获取当前 HOST
|
|
|
|
// const host = window.location.hostname;
|
|
|
|
|
|
|
|
// 简单的哈希函数,返回十六进制字符串
|
|
|
|
simpleHash() {
|
|
|
|
const str = window.location.hostname
|
|
|
|
let hash = 0
|
|
|
|
for (let i = 0; i < str.length; i++) {
|
|
|
|
const char = str.charCodeAt(i)
|
|
|
|
hash = (hash << 5) - hash + char
|
|
|
|
hash |= 0 // Convert to 32bit integer
|
|
|
|
}
|
|
|
|
return (hash >>> 0).toString(16) // 将哈希值转换为无符号整数,然后转换为十六进制字符串
|
|
|
|
},
|
|
|
|
|
|
|
|
// // 生成唯一值
|
|
|
|
// const uniqueValue = simpleHash(host);
|
|
|
|
// console.log(uniqueValue); // 输出基于 HOST 生成的唯一值
|
|
|
|
|
|
|
|
// 获取全部板块的数据
|
|
|
|
getAllForum({ commit }, that) {
|
|
|
|
const str = window.location.hostname
|
|
|
|
let hash = 0
|
|
|
|
for (let i = 0; i < str.length; i++) {
|
|
|
|
const char = str.charCodeAt(i)
|
|
|
|
hash = (hash << 5) - hash + char
|
|
|
|
hash |= 0 // Convert to 32bit integer
|
|
|
|
}
|
|
|
|
const strr = (hash >>> 0).toString(16) // 将哈希值转换为无符号整数,然后转换为十六进制字符串
|
|
|
|
|
|
|
|
that.$http
|
|
|
|
.get("/api/home/allForum?" + strr)
|
|
|
|
.then(res => {
|
|
|
|
if (res.code != 200) return
|
|
|
|
let allForumList = res.data
|
|
|
|
commit("setAllForumList", allForumList)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
that.$message.error(err.message)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
// 获取用户信息 获取收藏信息那些数据
|
|
|
|
getUserInfo({ state, commit }, that) {
|
|
|
|
if (state.getUserInfoState) return
|
|
|
|
commit("setgetUserInfoState", true)
|
|
|
|
that.$http
|
|
|
|
.post("/api/home")
|
|
|
|
.then(res => {
|
|
|
|
if (res.code != 200) return
|
|
|
|
let data = res.data
|
|
|
|
let { config, favorite, hotSearchkeywords, recommend, user, menu } = data
|
|
|
|
|
|
|
|
commit("setHomeRequestState", true)
|
|
|
|
commit("setUser", user)
|
|
|
|
commit("setFavoriteList", favorite)
|
|
|
|
commit("setRecommendList", recommend)
|
|
|
|
commit("setHotSearchkeywords", hotSearchkeywords)
|
|
|
|
commit("setMenu", menu)
|
|
|
|
|
|
|
|
if (that.userInfo) {
|
|
|
|
// 这个是顶部用户数据的 这样不用监听是否请求成功
|
|
|
|
that.userInfo = user
|
|
|
|
that.islogin = user.uid > 0 ? true : false
|
|
|
|
that.hotSearchkeywords = hotSearchkeywords
|
|
|
|
that.menu = menu
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
that.$message.error(err.message)
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
// that.$closeUnderLoading(that)
|
|
|
|
commit("setgetUserInfoState", false)
|
|
|
|
})
|
|
|
|
},
|
2023-04-11 17:56:16 +08:00
|
|
|
},
|
2024-08-09 15:15:12 +08:00
|
|
|
modules: {},
|
2023-03-28 18:04:49 +08:00
|
|
|
})
|