Prechádzať zdrojové kódy

首页加载用户信息

littleblue55 6 dní pred
rodič
commit
f4281a5b3f
3 zmenil súbory, kde vykonal 41 pridanie a 3 odobranie
  1. 15 0
      api/home.js
  2. 23 3
      pages/index/index.vue
  3. 3 0
      store/authStore.js

+ 15 - 0
api/home.js

@@ -1,6 +1,21 @@
 // 首页
 import request from '@/utils/request'
 
+// 获取首页轮播图数据
+export function homeSwiperList(){
+	return request({
+		'url': '/home/banner',
+		headers: {
+			isToken: false
+		},
+		'method': 'get'
+	})
+}
+
+
+
+
+
 // 获取推荐课程列表
 export function homeCourseList(){
 	return request({

+ 23 - 3
pages/index/index.vue

@@ -91,16 +91,17 @@
 	import { 
 		homeCourseList,
 		homeReportList,
-		homeSetting
+		homeSetting,
+		homeSwiperList
 	} from '@/api/home.js'
+	import { me } from '@/api/user.js'
 	const authStore = useAuthStore()
 	// 屏幕状态栏高度
 	const statusBarHeight = ref(0)
 	// 顶部导航栏高度
 	const navBarHeight = ref(0)
 	// 是否登录
-	const isLogin = authStore.isAuthenticated;
-
+	
 	const loginModal = ref(false)
 	// 是否为会员,从缓存里拿取用户信息
 	// const userInfo = uni.getStorageSync("userinfo") || {}
@@ -267,7 +268,18 @@
 		console.log(data, "课程预告数据")
 	}
 	
+	function initUser(){
+		me().then(res=>{
+			if(res?.data){
+				authStore.setUserInfo(res.data);
+			}
+		})
+	}
+	
 	function init() {
+		homeSwiperList().then(res=>{
+			console.log("轮播图", res)
+		})
 		homeCourseList().then(res=>{
 			// console.log(1000, res.data)
 			if(res?.data){
@@ -305,10 +317,18 @@
 	})
 	onLoad(() => {
 		init()
+		// initUser()
 	})
 	onShow(()=>{
+		const isLogin = authStore.isAuthenticated;
+		const isUserInfo = authStore.isUserInfo;
 		if(isLogin){
+			// 登录后才请求消息数量
 			getCharCount()
+			// 没有用户信息就加载一边
+			if(!isUserInfo){
+				initUser();
+			}
 		}
 	})
 </script>

+ 3 - 0
store/authStore.js

@@ -34,6 +34,9 @@ export const useAuthStore = defineStore('auth', {
 	getters: {
 		isAuthenticated(state) {
 			return !!state.token; // 如果 token 存在,返回 true;否则返回 false
+		},
+		isUserInfo(state){
+			return !!state.userInfo;
 		}
 	}
 })