重构已有前端代码

newzwz
lee-zt 2 months ago
parent 9f79bd04e1
commit 1ae1607686

@ -7,17 +7,9 @@
</template> </template>
<!-- 控制模板数据与行为 --> <!-- 控制模板数据与行为 -->
<script> <script setup lang="js" name="App">
//import MainPage from './components/MainPage.vue';
import Header from './components/Header.vue'; import Header from './components/Header.vue';
import { RouterView } from 'vue-router'; import { RouterView } from 'vue-router';
export default {
name: 'App',
components: {
Header,
RouterView
}
}
</script> </script>
<!-- 样式部分,定义CSS样式 --> <!-- 样式部分,定义CSS样式 -->

@ -71,7 +71,7 @@
</div> </div>
</template> </template>
<script setup name="UserLogin"> <script lang="js" setup name="UserLogin">
import { ref, computed } from 'vue'; import { ref, computed } from 'vue';
import request from '@/utils/request'; import request from '@/utils/request';
import { ElMessage } from 'element-plus'; import { ElMessage } from 'element-plus';

@ -15,20 +15,14 @@
</div> </div>
</template> </template>
<script> <script setup lang="js" name="LoginRegisterModal">
import { ref } from 'vue'; import { ref } from 'vue';
// //
import UserLogin from './Login.vue'; import UserLogin from './Login.vue';
import UserRegister from './Register.vue'; import UserRegister from './Register.vue';
export default { const emit = defineEmits(['close']);
name: 'LoginRegisterModal',
components: {
UserLogin, //
UserRegister //
},
setup(props, { emit }) {
//
const isLogin = ref(true); const isLogin = ref(true);
// / // /
@ -36,13 +30,10 @@ export default {
isLogin.value = !isLogin.value; isLogin.value = !isLogin.value;
}; };
return { //
isLogin, const close = () => {
toggleForm, emit('close');
close: () => {emit('close')} //
}; };
}
}
</script> </script>
<style scoped> <style scoped>

@ -27,12 +27,9 @@
</div> </div>
</template> </template>
<script> <script setup lang="js" name="NoticeBoard">
import { ref, onMounted, onUnmounted } from 'vue'; import { ref, onMounted, onUnmounted } from 'vue';
export default {
name: 'NoticeBoard',
setup() {
const images = ref([ const images = ref([
{ src: require('@/assets/whu1.jpg'), alt: '公告图片1' }, { src: require('@/assets/whu1.jpg'), alt: '公告图片1' },
{ src: require('@/assets/whu2.jpg'), alt: '公告图片2' }, { src: require('@/assets/whu2.jpg'), alt: '公告图片2' },
@ -63,24 +60,12 @@ export default {
let interval = null; let interval = null;
onMounted(() => { onMounted(() => {
//
interval = setInterval(nextImage, 5000); interval = setInterval(nextImage, 5000);
}); });
onUnmounted(() => { onUnmounted(() => {
//
clearInterval(interval); clearInterval(interval);
}); });
return {
images,
currentIndex,
nextImage,
prevImage,
goToImage,
};
},
};
</script> </script>
<style scoped> <style scoped>

@ -36,12 +36,10 @@
</div> </div>
</template> </template>
<script> <script setup lang="js" name="PostPage">
import { ref } from 'vue'; import { ref } from 'vue';
import { useRouter } from 'vue-router'; import { useRouter } from 'vue-router';
export default {
name: 'PostPage',
setup() {
const categories = ref(['全部', '学习', '娱乐', '二手交易']); const categories = ref(['全部', '学习', '娱乐', '二手交易']);
const selectedCategory = ref('全部'); const selectedCategory = ref('全部');
const posts = ref([ const posts = ref([
@ -88,7 +86,9 @@ export default {
]); ]);
const filteredPosts = ref(posts.value); const filteredPosts = ref(posts.value);
const router = useRouter(); const router = useRouter();
const selectCategory = (category) => { const selectCategory = (category) => {
selectedCategory.value = category; selectedCategory.value = category;
filteredPosts.value = filteredPosts.value =
@ -100,16 +100,6 @@ export default {
const goToPostDetail = (postId) => { const goToPostDetail = (postId) => {
router.push({ name: 'PostDetail', params: { id: postId } }); router.push({ name: 'PostDetail', params: { id: postId } });
}; };
return {
categories,
selectedCategory,
filteredPosts,
selectCategory,
goToPostDetail,
};
},
};
</script> </script>
<style scoped> <style scoped>

@ -90,14 +90,13 @@
</div> </div>
</template> </template>
<script> <script setup lang="js" name="UserRegister">
import { ref, computed, onMounted } from 'vue'; import { ref, computed, onMounted } from 'vue';
import request from '@/utils/request'; import request from '@/utils/request';
import {ElMessage} from 'element-plus' import { ElMessage } from 'element-plus';
const emit = defineEmits(['toggleForm']);
export default {
name: 'UserRegister',
setup(props, { emit }) {
const currentType = ref('username'); const currentType = ref('username');
const cooldown = ref(0); // const cooldown = ref(0); //
@ -121,18 +120,17 @@ export default {
// //
const isValidInput = computed(() => { const isValidInput = computed(() => {
const value = registerForm.value[currentType.value] const value = registerForm.value[currentType.value];
if (!value) return false if (!value) return false;
switch (currentType.value) { switch (currentType.value) {
case 'email': case 'email':
return /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(value) return /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(value);
case 'phone': case 'phone':
return /^1[3-9]\d{9}$/.test(value) return /^1[3-9]\d{9}$/.test(value);
default: default:
return false return false;
} }
}) });
// URL // URL
const captchaUrl = ref('/user/captcha'); const captchaUrl = ref('/user/captcha');
@ -163,7 +161,6 @@ export default {
// /使 // /使
async function sendCode() { async function sendCode() {
if (cooldown.value > 0 || !isValidInput.value) return; if (cooldown.value > 0 || !isValidInput.value) return;
try { try {
// TODO // TODO
const response = await request.post(`/user/send-code`, { const response = await request.post(`/user/send-code`, {
@ -198,7 +195,6 @@ export default {
captchaUrl.value = `/user/captcha?t=${new Date().getTime()}`; captchaUrl.value = `/user/captcha?t=${new Date().getTime()}`;
} }
//TODO:/
// //
async function register() { async function register() {
try { try {
@ -260,7 +256,6 @@ export default {
duration: 500 duration: 500
}); });
} }
} }
catch (error) { catch (error) {
console.error('注册失败:', error); console.error('注册失败:', error);
@ -273,24 +268,6 @@ export default {
// //
refreshCaptcha(); refreshCaptcha();
}); });
// 使
return {
currentType,
registerTypes,
registerForm,
captchaUrl,
inputType,
placeholder,
showVerifyCode,
cooldown,
sendCode,
refreshCaptcha,
register,
isValidInput
};
}
}
</script> </script>
<style scoped> <style scoped>

@ -31,18 +31,15 @@
</div> </div>
</template> </template>
<script> <script setup lang="js" name="WelcomeCalendar">
import { ref } from 'vue'; import { ref } from 'vue';
export default {
name: 'WelcomeCalendar',
setup() {
const today = new Date(); const today = new Date();
// //
const currentMonthText = ref(['一', '月']); // const currentMonthText = ref(['一', '月']); //
const currentDay = ref(today.getDate()); // const currentDay = ref(today.getDate()); //
const currentWeekday = ref('星','期','日'); // const currentWeekday = ref(['星', '期', '日']); //
// //
const isCheckedIn = ref(false); const isCheckedIn = ref(false);
@ -67,16 +64,6 @@ export default {
// //
initializeDate(); initializeDate();
return {
currentMonthText,
currentDay,
currentWeekday,
isCheckedIn,
checkIn,
};
},
};
</script> </script>
<style scoped> <style scoped>

@ -95,7 +95,7 @@
</div> </div>
</template> </template>
<script setup> <script setup lang ="js" name="FeedBack">
import { ref, reactive } from 'vue' import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus' import { ElMessage } from 'element-plus'

@ -15,21 +15,12 @@
</div> </div>
</template> </template>
<script> <script setup lang="js" name="MainPage">
import NoticeBoard from '@/components/NoticeBoard.vue'; import NoticeBoard from '@/components/NoticeBoard.vue';
import WelcomeCalendar from '@/components/WelcomeCalendar.vue'; import WelcomeCalendar from '@/components/WelcomeCalendar.vue';
import PostPage from '@/components/PostPage.vue'; import PostPage from '@/components/PostPage.vue';
// import UserPage from './UserPage.vue'; // import UserPage from './UserPage.vue';
// import PostDetail from './PostDetail.vue'; // import PostDetail from './PostDetail.vue';
export default {
components: {
NoticeBoard,
WelcomeCalendar,
PostPage,
//UserPage,
// PostDetail
}
};
</script> </script>
<style scoped> <style scoped>

@ -19,40 +19,39 @@
</div> </div>
</template> </template>
<script> <script setup lang="js" name="NotificationDetail">
import { ref, onMounted } from 'vue';
import { useRoute } from 'vue-router';
const route = useRoute();
const currentNotice = ref(null);
const errorMessage = ref('');
//
const mockFetchNotice = (id) => { const mockFetchNotice = (id) => {
return new Promise((resolve) => { return new Promise((resolve) => {
setTimeout(() => { setTimeout(() => {
const notices = [ const notices = [
{ id: 1, title: "系统维护通知", /* 其他字段 */ }, { id: 1, title: "系统维护通知", time: "2024-05-01 10:00", content: "系统将于今晚23:00-24:00进行维护请提前保存数据。" },
{ id: 2, title: "新功能上线", /* 其他字段 */ } { id: 2, title: "新功能上线", time: "2024-05-10 09:00", content: "我们上线了新功能,欢迎体验!" }
] ];
resolve(notices.find(n => n.id === Number(id))) resolve(notices.find(n => n.id === Number(id)));
}, 500) }, 500);
}) });
} };
export default { onMounted(async () => {
props: ['id'], // props
data() {
return {
currentNotice: null,
errorMessage: ''
}
},
async created() {
try { try {
const data = await mockFetchNotice(this.id) const data = await mockFetchNotice(route.params.id);
if (data) { if (data) {
this.currentNotice = data currentNotice.value = data;
} else { } else {
this.errorMessage = '通知不存在' errorMessage.value = '通知不存在';
} }
} catch (error) { } catch (error) {
this.errorMessage = '加载失败,请稍后重试' errorMessage.value = '加载失败,请稍后重试';
}
}
} }
});
</script> </script>
<style scoped> <style scoped>

@ -18,11 +18,13 @@
</div> </div>
</template> </template>
<script> <script setup lang="js" name="NotificationList">
export default { import { ref } from 'vue';
data() { import { useRouter } from 'vue-router';
return {
notifications: [ const router = useRouter();
const notifications = ref([
{ {
id: 1, id: 1,
title: "系统维护通知", title: "系统维护通知",
@ -35,17 +37,13 @@ export default {
time: "2023-08-19 09:30", time: "2023-08-19 09:30",
content: "消息中心新增批量处理功能,欢迎体验" content: "消息中心新增批量处理功能,欢迎体验"
} }
] ]);
}
}, function viewDetail(noticeId) {
methods: { router.push({
viewDetail(noticeId) {
this.$router.push({
name: 'NotificationDetail', name: 'NotificationDetail',
params: { id: noticeId } // params: { id: noticeId }
}) });
}
}
} }
</script> </script>

@ -57,13 +57,10 @@
</div> </div>
</template> </template>
<script> <script setup lang="js" name="PostDetail">
import { ref, onMounted } from 'vue'; import { ref, onMounted } from 'vue';
import { useRoute } from 'vue-router'; import { useRoute } from 'vue-router';
export default {
name: 'PostDetail',
setup() {
const route = useRoute(); const route = useRoute();
const postId = ref(route.params.id); const postId = ref(route.params.id);
const postTitle = ref(''); const postTitle = ref('');
@ -91,14 +88,14 @@ export default {
const comments = ref([ const comments = ref([
{ {
avatar: require('@/assets/default-avatar/girl_1.png'), avatar: require('@/assets/default-avatar/girl_1.png'),
name: '评论者1', // name: '评论者1',
text: '这是第一条评论。', text: '这是第一条评论。',
time: '2025年4月20日 15:00', time: '2025年4月20日 15:00',
likes: 10, likes: 10,
}, },
{ {
avatar: require('@/assets/default-avatar/boy_2.png'), avatar: require('@/assets/default-avatar/boy_2.png'),
name: '评论者2', // name: '评论者2',
text: '这是第二条评论。', text: '这是第二条评论。',
time: '2025年4月20日 15:30', time: '2025年4月20日 15:30',
likes: 5, likes: 5,
@ -115,7 +112,7 @@ export default {
if (newComment.value.trim()) { if (newComment.value.trim()) {
comments.value.push({ comments.value.push({
avatar: require('@/assets/default-avatar/boy_1.png'), avatar: require('@/assets/default-avatar/boy_1.png'),
name: '新评论者', // name: '新评论者',
text: newComment.value, text: newComment.value,
time: new Date().toLocaleString(), time: new Date().toLocaleString(),
likes: 0, likes: 0,
@ -128,21 +125,6 @@ export default {
postTitle.value = `帖子标题 ${postId.value}`; postTitle.value = `帖子标题 ${postId.value}`;
postContent.value = `这是帖子 ${postId.value} 的内容,展示帖子的详细信息。`; postContent.value = `这是帖子 ${postId.value} 的内容,展示帖子的详细信息。`;
}); });
return {
postTitle,
postContent,
comments,
newComment,
sendComment,
author,
postStats,
postTime,
isFollowing,
toggleFollow,
};
},
};
</script> </script>
<style scoped> <style scoped>

@ -57,12 +57,10 @@
</div> </div>
</template> </template>
<script> <script setup lang="js" name="UserPage">
import { ref } from 'vue'; import { ref } from 'vue';
import { useRouter } from 'vue-router'; import { useRouter } from 'vue-router';
export default {
name: 'UserPage',
setup() {
const posts = ref([ const posts = ref([
{ {
id: 1, id: 1,
@ -81,16 +79,11 @@ export default {
favorites: 200, favorites: 200,
}, },
]); ]);
const router = useRouter(); const router = useRouter();
const goToPostDetail = (postId) => { const goToPostDetail = (postId) => {
router.push({ name: 'PostDetail', params: { id: postId } }); router.push({ name: 'PostDetail', params: { id: postId } });
}; };
return {
posts,
goToPostDetail,
};
},
};
</script> </script>
<style scoped> <style scoped>

Loading…
Cancel
Save