big-qianduan.new1/allHomework.vue

150 lines
3.2 KiB
Vue

<template>
<view class="container">
<!-- 页面标题 -->
<view class="header">
<text class="title">作业</text>
</view>
<!-- 作业列表表格 -->
<view class="table-container">
<view class="table">
<!-- 表头 -->
<view class="table-row table-header">
<text class="table-cell">作业标题</text>
<text class="table-cell">详细信息</text>
</view>
<!-- 表格内容 -->
<view v-for="(item, index) in homeworkList" :key="index" class="table-row">
<text class="table-cell">{{ item.title }}</text>
<text class="table-cell">
<button class="detail-button" @click="goToSubmitPage(item)">提交作业</button>
</text>
</view>
</view>
</view>
</view>
</template>
<script>
import {
h
} from "vue";
export default {
data() {
return {
homeworkList: [] // 存储作业列表
};
},
methods: {
// 获取作业列表
fetchHomeworkList() {
const token = uni.getStorageSync('token'); // 从缓存中读取 token
if (!token) {
uni.showToast({
title: '未登录,请重新登录',
icon: 'none'
});
return;
}
uni.request({
url: `${this.$baseUrl}/homework/homework/list`,
method: 'GET',
header: {
Authorization: `Bearer ${token}` // 将 token 作为 Authorization 头传递
},
success: (res) => {
if (res.data.code === 200) {
this.homeworkList = res.data.rows; // 假设后端返回数据在 res.data.rows
} else {
uni.showToast({
title: res.data.message || '获取作业失败',
icon: 'none'
});
}
},
fail: () => {
uni.showToast({
title: '网络错误,请稍后重试',
icon: 'none'
});
}
});
},
// 跳转到提交作业页面
goToSubmitPage(homework) {
uni.navigateTo({
url: `/pages/allHomework/submitHomework?id=${homework.id}&title=${homework.title}&content=${homework.content}` // 跳转到提交作业页面并传递作业 ID
});
}
},
onLoad() {
this.fetchHomeworkList(); // 页面加载时获取作业列表
}
};
</script>
<style>
.container {
display: flex;
flex-direction: column;
background-color: #f8f9fa;
height: 100vh;
}
.header {
background-color: #007bff;
padding: 15px;
text-align: center;
}
.title {
color: #fff;
font-size: 24px;
font-weight: bold;
}
.table-container {
margin: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
overflow: hidden;
}
.table {
width: 100%;
}
.table-row {
display: flex;
flex-direction: row;
align-items: center;
border-bottom: 1px solid #ddd;
}
.table-header {
background-color: #f1f1f1;
font-weight: bold;
}
.table-cell {
flex: 1;
padding: 10px;
text-align: center;
font-size: 14px;
}
.detail-button {
background-color: #007bff;
color: #fff;
padding: 8px 12px;
border: none;
border-radius: 5px;
font-size: 14px;
cursor: pointer;
}
.detail-button:hover {
background-color: #0056b3;
}
</style>