Merge branch 'feature/task1.0.0_xg' into 'feature/task1.0.0'

feat: 修改热门分类请求时机

See merge request yanxuan-frontend/shop-pc!58
merge-requests/59/head
肖广 3 years ago
commit 4f90c93ebd

@ -39,7 +39,7 @@
class="tab-category-menu__right flex-1" class="tab-category-menu__right flex-1"
> >
<div <div
v-for="item in categroyData" v-for="item in list"
:key="item.id" :key="item.id"
@mouseenter="handleCategoryHover(item.id)" @mouseenter="handleCategoryHover(item.id)"
class="category-menu-right__wrap" class="category-menu-right__wrap"
@ -61,10 +61,8 @@
</div> </div>
</template> </template>
<script> <script>
import { import { mapState } from "vuex";
ApiGetCategoryOneList, import { ApiGetCategoryTwoAndGoods } from "@/plugins/api/goods";
ApiGetCategoryTwoAndGoods,
} from "@/plugins/api/goods";
import { CATEGROY_LEVEL } from "@/constants"; import { CATEGROY_LEVEL } from "@/constants";
const CATEGROY_HIDE_PAGES = [/\/account/]; // tab const CATEGROY_HIDE_PAGES = [/\/account/]; // tab
@ -76,10 +74,11 @@ export default {
categroyTwoVisible: false, // categroyTwoVisible: false, //
categroyVisible: false, // categroyVisible: false, //
currentCategroyId: 0, // id currentCategroyId: 0, // id
categroyData: [], list: [],
}; };
}, },
computed: { computed: {
...mapState(["categroyData"]),
showCategroyTab() { showCategroyTab() {
return !CATEGROY_HIDE_PAGES.some((reg) => { return !CATEGROY_HIDE_PAGES.some((reg) => {
return reg.test(this.$route.path); return reg.test(this.$route.path);
@ -91,28 +90,32 @@ export default {
return this.$route.path === "/"; return this.$route.path === "/";
}, },
}, },
created() { watch: {
this.getCategroyData(); categroyData: {
immediate: true,
handler(val) {
if (val.length > 0) {
this.getCategroyData();
}
},
},
}, },
methods: { methods: {
// //
async getCategroyData() { async getCategroyData() {
const { result } = await ApiGetCategoryOneList(); this.list = await Promise.all(
if (result && result.length > 0) { this.categroyData.map(async (item) => {
this.categroyData = await Promise.all( const { result: resultGoods } = await ApiGetCategoryTwoAndGoods({
result.map(async (item) => { categoryId: item.id,
const { result: resultGoods } = await ApiGetCategoryTwoAndGoods({ });
categoryId: item.id, if (resultGoods && resultGoods.length > 0) {
}); return {
if (resultGoods && resultGoods.length > 0) { ...item,
return { list: resultGoods,
...item, };
list: resultGoods, }
}; })
} );
})
);
}
}, },
// //
handleCategoryHover(id) { handleCategoryHover(id) {

@ -16,14 +16,14 @@ export default function ({$axios, store, route}, inject) {
config.headers.Authorization = store.state.token; config.headers.Authorization = store.state.token;
return config; return config;
}); });
$axiosTk.onResponse(async response => { $axiosTk.onResponse(response => {
const result = response.data; const result = response.data;
if(response.status === 200){ if(response.status === 200){
if(result.code === 'SUCCESS'){ if(result.code === 'SUCCESS'){
return result.data; return result.data;
} }
if(result.code === 'TOKEN_FAIL'){ if(result.code === 'TOKEN_FAIL'){
await store.commit('setLoginOut'); store.commit('setLoginOut');
store.commit('setLoginVisible'); store.commit('setLoginVisible');
return result; return result;
} }

@ -8,6 +8,7 @@
import { TOKEN_KEY } from "@/constants"; import { TOKEN_KEY } from "@/constants";
import { ApiGetCurrentUser, ApiPostLogout } from "@/plugins/api/account"; import { ApiGetCurrentUser, ApiPostLogout } from "@/plugins/api/account";
import { ApiGetCartList } from "@/plugins/api/cart"; import { ApiGetCartList } from "@/plugins/api/cart";
import { ApiGetCategoryOneList } from "@/plugins/api/goods";
const ONE_DAY = 86400000; // 一天的毫秒数 24 * 60 * 60 * 1000; const ONE_DAY = 86400000; // 一天的毫秒数 24 * 60 * 60 * 1000;
const state = () => ({ const state = () => ({
@ -16,6 +17,7 @@ const state = () => ({
loginVisible: false, // 是否展示登录弹窗 loginVisible: false, // 是否展示登录弹窗
seckillTabVisible: false, // 公共头是否展示秒杀tab seckillTabVisible: false, // 公共头是否展示秒杀tab
cartProducts: [], // 购物车列表 cartProducts: [], // 购物车列表
categroyData: [], // 公共页头分类数据
}); });
const mutations = { const mutations = {
setUserInfo(state, info) { setUserInfo(state, info) {
@ -42,14 +44,23 @@ const mutations = {
setCartProducts(state, val) { setCartProducts(state, val) {
state.cartProducts = val; state.cartProducts = val;
}, },
setCategroyData(state, val) {
state.categroyData = val;
},
}; };
const actions = { const actions = {
nuxtServerInit({ state, commit, dispatch }) { async nuxtServerInit({ state, commit, dispatch }) {
// 设置token数据
const token = this.$cookies.get(TOKEN_KEY); const token = this.$cookies.get(TOKEN_KEY);
if (!state.token && token) { if (!state.token && token) {
commit("setToken", token); commit("setToken", token);
dispatch("getUserInfo"); dispatch("getUserInfo");
} }
// 获取分类数据
const { result } = await ApiGetCategoryOneList();
if (result) {
commit("setCategroyData", result);
}
}, },
async getUserInfo({ commit }) { async getUserInfo({ commit }) {
const { result } = await ApiGetCurrentUser(); const { result } = await ApiGetCurrentUser();

Loading…
Cancel
Save