Procházet zdrojové kódy

feat: 附件功能

ChenYangfan před 6 roky
rodič
revize
916946b854

+ 1 - 10
src/components/Basic/CButton.vue

@@ -183,16 +183,7 @@ button {
 @include setTheme()
 
 .c-button--disabled {
-  background: #daeafa;
-  color: white;
+  opacity: .3;
   cursor: not-allowed;
-
-  &:hover {
-    background: #daeafa;
-  }
-
-  &:active {
-    background: #daeafa;
-  }
 }
 </style>

+ 1 - 0
src/components/Basic/CTag.vue

@@ -24,6 +24,7 @@ export default {
 @import '~@/style/mixin/set-theme.scss';
 
 div {
+  transition: .2s;
   display: inline-block;
   padding: 4px;
   border-radius: 4px;

+ 73 - 0
src/components/Cards/CourseFileCard.vue

@@ -0,0 +1,73 @@
+<template>
+  <div class="course-file-card" @click="handleClick">
+    <div class="course-file-card-title">
+      <c-icon theme="green" :left="-2" :right="4" bold :size="16">attach_file</c-icon>
+      {{courseFile.fileName}}
+    </div>
+    <show-transition-small>
+      <div class="course-file-card-info" v-if="!hideTime">
+        {{momentFromNow(courseFile.createAt)}}
+      </div>
+    </show-transition-small>
+  </div>
+</template>
+
+<script>
+import CIcon from '@/components/Basic/CIcon'
+import momentFromNow from '@/utils/moment-from-now'
+import ShowTransitionSmall from '@/animations/ShowTransitionSmall'
+
+export default {
+  components: { CIcon, ShowTransitionSmall },
+  props: {
+    courseFile: Object,
+    hideTime: Boolean,
+    clickHandler: {
+      type: Function,
+      default: () => {}
+    }
+  },
+  methods: {
+    momentFromNow,
+    handleClick () {
+      this.clickHandler(this.courseFile)
+    }
+  }
+}
+</script>
+
+<style lang="scss" scoped>
+@import '~@/style/theme.scss';
+
+.course-file-card {
+  padding: 6px 8px 6px 16px;
+  margin: 12px 0;
+  border-radius: 12px;
+  // border-left: 4px solid $input-background-color;
+  background-color: $card-background;
+  transition: .2s;
+  cursor: pointer;
+
+  &:hover {
+    background-color: darken($card-background, 5);
+    // border-left: 4px solid $theme-blue;
+  }
+}
+
+.course-file-card-title {
+  display: flex;
+  align-items: center;
+  font-size: 16px;
+  line-height: 1.9;
+}
+
+.course-file-card-info {
+  font-size: 12px;
+  color: #6a7a8a;
+  line-height: 1.8;
+}
+
+.c-icon {
+  transform: scale(1.2);
+}
+</style>

+ 1 - 1
src/components/Cards/SlideCard.vue

@@ -1,5 +1,5 @@
 <template>
-  <div class="slide-card" title="点击查看课件" @click="handleClick(123)">
+  <div class="slide-card" title="点击查看课件" @click="handleClick">
     <div class="slide-card-title">
       <c-icon theme="blue" :right="4" bold :size="16">slideshow</c-icon>
       {{ slide.name }}

+ 5 - 0
src/config/server-info.js

@@ -7,6 +7,10 @@ const slideUrl = host + '/api/slide'
 
 const useSlideServer = postfix => postfix ? slideUrl + postfix : slideUrl
 
+const courseFileUrl = host + '/api/courseFile'
+
+const useCourseFileServer = postfix => postfix ? courseFileUrl + postfix : courseFileUrl
+
 const courseUrl = host + '/api/course'
 
 const useCourseServer = postfix => postfix ? courseUrl + postfix : courseUrl
@@ -47,6 +51,7 @@ const useQuestionServer = (postfix = '') => questionUrl + postfix
 
 export {
   useSlideServer,
+  useCourseFileServer,
   useCourseServer,
   useCodeServer,
   useUserServer,

+ 8 - 0
src/router/index.js

@@ -98,6 +98,10 @@ const routes = [{
       path: 'slide-detail/:slideId',
       name: 'teacher-explore-slide-detail',
       component: () => import(/* webpackChunkName: "teacher" */ '../views/teacher/components/TeacherSlideDetail.vue')
+    }, {
+      path: 'course-file-detail/:courseFileId',
+      name: 'teacher-explore-course-file-detail',
+      component: () => import(/* webpackChunkName: "teacher" */ '../views/teacher/components/TeacherCourseFileDetail.vue')
     }]
   }, {
     path: 'create-slide',
@@ -143,6 +147,10 @@ const routes = [{
       path: 'slide-detail/:slideId',
       name: 'student-explore-slide-detail',
       component: () => import(/* webpackChunkName: "student" */ '../views/student/components/StudentSlideDetail.vue')
+    }, {
+      path: 'course-file-detail/:courseFileId',
+      name: 'student-explore-course-file-detail',
+      component: () => import(/* webpackChunkName: "student" */ '../views/student/components/StudentCourseFileDetail.vue')
     }]
   }, {
     path: 'quiz',

+ 41 - 0
src/server/course-file.js

@@ -0,0 +1,41 @@
+import axios from '@/plugins/axios'
+import { useCourseFileServer } from '@/config/server-info'
+
+export function uploadCourseFile ({ courseId, file }) {
+  let data = new FormData()
+  data.append('courseId', courseId)
+  data.append('file', file)
+
+  return axios
+    .post(useCourseFileServer(), data, {
+      headers: {
+        'Content-Type': 'multipart/form-data'
+      }
+    })
+    .then(res => res.data)
+}
+
+export function deleteCourseFile ({ courseFileId }) {
+  return axios
+    .delete(useCourseFileServer('/' + courseFileId))
+    .then(res => {})
+}
+
+export function getOneCourseFile ({ courseId, key = '', page = 0, size = 10, sort = '' }) {
+  return axios
+    .get(useCourseFileServer('/course/' + courseId), {
+      params: {
+        key,
+        page,
+        size,
+        sort
+      }
+    })
+    .then(res => res.data)
+}
+
+export function getCourseFileUrl ({ courseFileId }) {
+  return axios
+    .get(useCourseFileServer('/' + courseFileId + '/url'))
+    .then(res => res.data)
+}

+ 15 - 2
src/store/modules/main.js

@@ -1,9 +1,16 @@
-import { RESET_COURSE, RESET_USER_INFO, SET_COURSE, SET_USER_INFO } from '@/store/types/main-types'
+import {
+  RESET_COURSE, RESET_COURSE_FILE_INFO,
+  RESET_USER_INFO,
+  SET_COURSE,
+  SET_COURSE_FILE_INFO,
+  SET_USER_INFO
+} from '@/store/types/main-types'
 
 export default {
   state: {
     courseInfo: {},
-    userInfo: {}
+    userInfo: {},
+    courseFileInfo: {}
   },
 
   mutations: {
@@ -18,6 +25,12 @@ export default {
     },
     [RESET_USER_INFO] (state) {
       state.userInfo = {}
+    },
+    [SET_COURSE_FILE_INFO](state, payload) {
+      state.courseFileInfo = payload.courseFileInfo
+    },
+    [RESET_COURSE_FILE_INFO](state) {
+      state.courseFileInfo = {}
     }
   }
 }

+ 3 - 0
src/store/types/main-types.js

@@ -3,3 +3,6 @@ export const RESET_COURSE = 'resetCourse'
 
 export const SET_USER_INFO = 'setUserInfo'
 export const RESET_USER_INFO = 'resetUserInfo'
+
+export const SET_COURSE_FILE_INFO = 'setCourseFileInfo'
+export const RESET_COURSE_FILE_INFO = 'resetCourseFileInfo'

+ 60 - 0
src/views/student/components/StudentCourseFileDetail.vue

@@ -0,0 +1,60 @@
+<template>
+  <tab-side-component>
+    <back-title>
+      附件详情
+    </back-title>
+    <div class="info">附件名称:{{courseFile.fileName}}</div>
+    <div class="info">创建时间:{{momentFromNow(courseFile.createAt)}}</div>
+    <a :href="courseFileUrl">
+      <c-button block theme="green" no-bold class="mt-32 mb-16" icon="cloud_download">
+        下载附件
+      </c-button>
+    </a>
+  </tab-side-component>
+</template>
+
+<script>
+import TabSideComponent from '@/components/Layouts/TabSideComponent'
+import CButton from '@/components/Basic/CButton'
+import BackTitle from '@/components/Basic/BackTitle'
+import { mapState } from 'vuex'
+import { getCourseFileUrl } from '@/server/course-file'
+import momentFromNow from '@/utils/moment-from-now'
+
+momentFromNow()
+
+export default {
+  components: { BackTitle, CButton, TabSideComponent },
+  data () {
+    return {
+      showMore: false,
+      courseFileUrl: ''
+    }
+  },
+  computed: {
+    ...mapState({
+      courseFile: state => state.main.courseFileInfo
+    })
+  },
+  mounted () {
+    getCourseFileUrl({ courseFileId: this.courseFile.id }).then(res => {
+      this.courseFileUrl = res
+    }).catch(err => {
+      this.$setErrorMessage(err.response.data.msg)
+    })
+  },
+  methods: {
+    momentFromNow
+  }
+}
+</script>
+
+<style lang="scss" scoped>
+@import '~@/style/theme.scss';
+
+.info {
+  font-size: 14px;
+  margin: 8px 0;
+  color: #6a7a8a;
+}
+</style>

+ 26 - 3
src/views/student/components/StudentCourseSlides.vue

@@ -20,7 +20,7 @@
     <div class="no-content-warning" v-if="slides === null">
       <div class="no-content-warning-tip">加载中...</div>
     </div>
-    <div class="no-content-warning" v-else-if="slides.length === 0">
+    <div class="no-content-warning" v-else-if="slides.length === 0 && courseFiles.length === 0">
       <div class="no-content-warning-tip">本课程尚无课件~</div>
     </div>
     <template v-for="slide of slides">
@@ -28,6 +28,11 @@
                   :slide="slide" :hide-update-time="!showMore"
                   :click-handler="handleSlideCardClick"></slide-card>
     </template>
+    <template v-for="courseFile of courseFiles">
+      <course-file-card :key="courseFile.id" :hide-time="!showMore" :click-handler="handleCourseFileCardClick"
+                        :courseFile="courseFile">
+      </course-file-card>
+    </template>
   </tab-side-component>
 </template>
 
@@ -38,17 +43,20 @@ import SlideCard from '@/components/Cards/SlideCard'
 import CButton from '@/components/Basic/CButton'
 import { studentGetSlidesByCourse } from '@/server/slide'
 import { SET_SLIDE_INFO } from '@/store/types/pdf-types'
-import { SET_COURSE } from '@/store/types/main-types'
+import { SET_COURSE, SET_COURSE_FILE_INFO } from '@/store/types/main-types'
 import { mapState } from 'vuex'
 import CourseDetail from '@/components/Details/CourseDetail'
 import ShowTransition from '@/animations/ShowTransition'
 import TabActions from '@/components/Layouts/TabActions'
+import { getOneCourseFile } from '@/server/course-file'
+import CourseFileCard from '@/components/Cards/CourseFileCard'
 
 export default {
-  components: { TabActions, CourseDetail, CButton, SlideCard, BackTitle, TabSideComponent, ShowTransition },
+  components: { CourseFileCard, TabActions, CourseDetail, CButton, SlideCard, BackTitle, TabSideComponent, ShowTransition },
   data () {
     return {
       slides: null,
+      courseFiles: null,
       showMore: false,
       showActions: false
     }
@@ -71,6 +79,9 @@ export default {
       this.getSlides(20).then(() => {
         this.getSlides(10000)
       })
+      this.getCourseFiles(20).then(() => {
+        this.getCourseFiles(10000)
+      })
     },
     getSlides (size) {
       return studentGetSlidesByCourse({ courseId: this.course.id, size, sort: 'name' }).then(res => {
@@ -79,10 +90,22 @@ export default {
         this.$setErrorMessage('获取课程课件列表过程中:' + err.response.data.msg)
       })
     },
+    getCourseFiles (size) {
+      return getOneCourseFile({ courseId: this.course.id, size }).then(res => {
+        this.courseFiles = res.data
+      }).catch(err => {
+        this.courseFiles = []
+        this.$setErrorMessage('获取附件列表过程中:' + err.response.data.msg)
+      })
+    },
     handleSlideCardClick (slide) {
       this.$store.commit(SET_SLIDE_INFO, { slideInfo: slide })
       this.$router.push({ name: 'student-explore-slide-detail', params: { slideId: slide.id } })
     },
+    handleCourseFileCardClick (courseFile) {
+      this.$store.commit(SET_COURSE_FILE_INFO, { courseFileInfo: courseFile })
+      this.$router.push({ name: 'student-explore-course-file-detail', params: { courseFileId: courseFile.id } })
+    },
     handleToManager () {
       this.$store.commit(SET_COURSE, { courseInfo: this.course })
       this.$router.push({ name: 'student-course-detail', params: { courseId: this.course.id } })

+ 72 - 0
src/views/teacher/components/TeacherCourseFileDetail.vue

@@ -0,0 +1,72 @@
+<template>
+  <tab-side-component>
+    <back-title>
+      附件详情
+    </back-title>
+    <div class="info">附件名称:{{courseFile.fileName}}</div>
+    <div class="info">创建时间:{{momentFromNow(courseFile.createAt)}}</div>
+    <a :href="courseFileUrl">
+      <c-button block theme="green" no-bold class="mt-32 mb-16" icon="cloud_download">
+        下载附件
+      </c-button>
+    </a>
+    <c-button block theme="red" class="mb-16" @click="handleDeleteCourseFile" icon="close">删除附件</c-button>
+  </tab-side-component>
+</template>
+
+<script>
+import TabSideComponent from '@/components/Layouts/TabSideComponent'
+import CButton from '@/components/Basic/CButton'
+import BackTitle from '@/components/Basic/BackTitle'
+import { mapState } from 'vuex'
+import { deleteCourseFile, getCourseFileUrl } from '@/server/course-file'
+import momentFromNow from '@/utils/moment-from-now'
+
+momentFromNow()
+
+export default {
+  components: { BackTitle, CButton, TabSideComponent },
+  data () {
+    return {
+      showMore: false,
+      courseFileUrl: ''
+    }
+  },
+  computed: {
+    ...mapState({
+      courseFile: state => state.main.courseFileInfo
+    })
+  },
+  mounted () {
+    getCourseFileUrl({ courseFileId: this.courseFile.id }).then(res => {
+      this.courseFileUrl = res
+    }).catch(err => {
+      this.$setErrorMessage(err.response.data.msg)
+    })
+  },
+  methods: {
+    momentFromNow,
+    handleDeleteCourseFile () {
+      const msg = '确定删除附件' + this.courseFile.fileName + '吗?'
+      if (confirm(msg)) {
+        deleteCourseFile({ courseFileId: this.courseFile.id }).then(res => {
+          this.$setSuccessMessage('删除附件成功')
+          this.$router.push({ name: 'teacher-explore' })
+        }).catch(err => {
+          this.$setErrorMessage(err.response.data.msg)
+        })
+      }
+    }
+  }
+}
+</script>
+
+<style lang="scss" scoped>
+@import '~@/style/theme.scss';
+
+.info {
+  font-size: 14px;
+  margin: 8px 0;
+  color: #6a7a8a;
+}
+</style>

+ 31 - 5
src/views/teacher/components/TeacherCourseSlides.vue

@@ -6,7 +6,10 @@
       </c-button>
     </back-title>
     <tab-actions :if-condition="showActions">
-      <c-button theme="green" icon="settings" class="mr-8" @click="handleToManager">管理本课程</c-button>
+      <router-link :to="{name: 'create-slide'}">
+        <c-button :right="8" theme="green" icon="add">新建</c-button>
+      </router-link>
+      <c-button theme="orange" icon="settings" class="mr-8" @click="handleToManager">管理课程</c-button>
       <c-button theme="blue" :icon="showMore ? 'unfold_less' : 'unfold_more'" @click="showMore = !showMore">
         {{showMore ? '收起' : '展开'}}详情
       </c-button>
@@ -20,7 +23,7 @@
     <div class="no-content-warning" v-if="slides === null">
       <div class="no-content-warning-tip">加载中...</div>
     </div>
-    <div class="no-content-warning" v-else-if="slides.length === 0">
+    <div class="no-content-warning" v-else-if="slides.length === 0 && courseFiles.length === 0">
       <div class="no-content-warning-tip">本课程尚无课件~</div>
       <c-button block theme="blue" @click="handleToCreateCourse">去创建课件</c-button>
     </div>
@@ -28,6 +31,11 @@
       <slide-card :key="slide.id" :hide-teacher="!showMore" :hide-course="!showMore" :hide-update-time="!showMore"
                   :slide="slide" :click-handler="handleSlideCardClick"></slide-card>
     </template>
+    <template v-for="courseFile of courseFiles">
+      <course-file-card :key="'file-' + courseFile.id" :hide-time="!showMore" :click-handler="handleCourseFileCardClick"
+                  :courseFile="courseFile">
+      </course-file-card>
+    </template>
   </tab-side-component>
 </template>
 
@@ -38,17 +46,20 @@ import SlideCard from '@/components/Cards/SlideCard'
 import CButton from '@/components/Basic/CButton'
 import { teacherGetSlidesByCourse } from '@/server/slide'
 import { SET_SLIDE_INFO } from '@/store/types/pdf-types'
-import { SET_COURSE } from '@/store/types/main-types'
+import { SET_COURSE, SET_COURSE_FILE_INFO } from '@/store/types/main-types'
 import { mapState } from 'vuex'
 import TabActions from '@/components/Layouts/TabActions'
 import CourseDetail from '@/components/Details/CourseDetail'
 import ShowTransition from '@/animations/ShowTransition'
+import { getOneCourseFile } from '@/server/course-file'
+import CourseFileCard from '@/components/Cards/CourseFileCard'
 
 export default {
-  components: { CourseDetail, ShowTransition, TabActions, CButton, SlideCard, BackTitle, TabSideComponent },
+  components: { CourseFileCard, CourseDetail, ShowTransition, TabActions, CButton, SlideCard, BackTitle, TabSideComponent },
   data () {
     return {
       slides: null,
+      courseFiles: null,
       showActions: false,
       showMore: false
     }
@@ -68,9 +79,12 @@ export default {
   },
   methods: {
     init () {
-      this.getSlides(10).then(() => {
+      this.getSlides(20).then(() => {
         this.getSlides(10000)
       })
+      this.getCourseFiles(20).then(() => {
+        this.getCourseFiles(10000)
+      })
     },
     getSlides (size) {
       return teacherGetSlidesByCourse({ courseId: this.course.id, sort: 'name', size }).then(res => {
@@ -80,10 +94,22 @@ export default {
         this.$setErrorMessage('获取课程课件列表过程中:' + err.response.data.msg)
       })
     },
+    getCourseFiles (size) {
+      return getOneCourseFile({ courseId: this.course.id, size }).then(res => {
+        this.courseFiles = res.data
+      }).catch(err => {
+        this.courseFiles = []
+        this.$setErrorMessage('获取附件列表过程中:' + err.response.data.msg)
+      })
+    },
     handleSlideCardClick (slide) {
       this.$store.commit(SET_SLIDE_INFO, { slideInfo: slide })
       this.$router.push({ name: 'teacher-explore-slide-detail', params: { slideId: slide.id } })
     },
+    handleCourseFileCardClick (courseFile) {
+      this.$store.commit(SET_COURSE_FILE_INFO, { courseFileInfo: courseFile })
+      this.$router.push({ name: 'teacher-explore-course-file-detail', params: { courseFileId: courseFile.id } })
+    },
     handleToManager () {
       this.$store.commit(SET_COURSE, { courseInfo: this.course })
       this.$router.push({ name: 'teacher-course-detail', params: { courseId: this.course.id } })

+ 46 - 23
src/views/teacher/tabs/CreateSlide.vue

@@ -1,20 +1,25 @@
 <template>
   <div class="tab">
-    <h1 class="title">新建幻灯片</h1>
+    <h1 class="title">上传文件</h1>
+    <div class="type-bar mb-24">
+      <c-button block :right="6" @click="isSlide = true" :theme="isSlide ? 'blue' : 'normal'">课件</c-button>
+      <c-button block :left="6" @click="isSlide = false" :theme="isSlide ? 'normal' : 'green'">附件</c-button>
+    </div>
     <div class="sub-title">所属课程:</div>
     <c-select v-model="selectedCourseId" class="mb-16"
               :list="this.courses.map(course => ({value: course.id, display: course.name}))"></c-select>
-    <div class="sub-title">课件名称:</div>
-    <c-input class="mb-16"
-      v-model="slideName"
-      placeholder="请勿与同课程课件重名"
+    <div class="sub-title" v-if="isSlide">{{isSlide ? '课件' : '附件'}}名称:</div>
+    <c-input class="mb-16" v-if="isSlide"
+             v-model="slideName"
+             placeholder="请勿与同课程下文件重名"
     />
-    <div class="sub-title">课件文件
-      <c-tag theme="green">目前支持 PDF 格式</c-tag>
+    <div class="sub-title">{{isSlide ? '课件' : '附件'}}文件
+      <c-tag :theme="isSlide ? 'blue' : 'green'">{{isSlide ? '课件文件目前支持 PDF 格式' : '附件将直接提供链接供下载'}}</c-tag>
     </div>
-    <file-uploader accept="application/pdf" class="mb-16" @change="handleFileChanged"/>
-    <c-button theme="blue" block :no-click-warn="!formChecked()" @click="handleSubmit">
-      添加
+    <file-uploader :accept="isSlide ? 'application/pdf' : null" class="mb-16" @change="handleFileChanged"/>
+    <c-button standout :theme="isSlide ? 'blue' : 'green'" block :no-click-warn="!formChecked()" @click="handleSubmit"
+              icon="add">
+      确认上传{{isSlide ? '课件' : '附件'}}
     </c-button>
   </div>
 </template>
@@ -27,6 +32,7 @@ import { teacherGetCourses } from '@/server/course'
 import { createSlide } from '@/server/slide'
 import CTag from '@/components/Basic/CTag'
 import CSelect from '@/components/Basic/CSelect'
+import { uploadCourseFile } from '@/server/course-file'
 
 export default {
   components: { CSelect, CTag, FileUploader, CButton, CInput },
@@ -36,7 +42,8 @@ export default {
       selectedCourseId: '',
       slideName: '',
       file: null,
-      message: ''
+      message: '',
+      isSlide: true
     }
   },
   mounted () {
@@ -52,12 +59,12 @@ export default {
         this.message = '请选择所属课程'
         return false
       }
-      if (!this.slideName) {
+      if (this.isSlide && !this.slideName) {
         this.message = '请输入课件的名称'
         return false
       }
       if (!this.file) {
-        this.message = '请上传课件文件'
+        this.message = '请选择文件上传'
         return false
       }
       return true
@@ -68,18 +75,30 @@ export default {
         return
       }
 
-      this.$setInfoMessage('上传课件可能比较耗时,请稍等片刻')
+      this.$setInfoMessage('上传文件可能比较耗时,请稍等片刻')
       const interval = setInterval(() => {
-        this.$setInfoMessage('上传课件可能比较耗时,请稍等片刻')
+        this.$setInfoMessage('上传文件可能比较耗时,请稍等片刻')
       }, 500)
-      createSlide({ courseId: this.selectedCourseId, name: this.slideName, file: this.file }).then(res => {
-        clearInterval(interval)
-        this.$setSuccessMessage('创建课件成功!')
-        this.$router.push({ name: 'teacher-explore' })
-      }).catch(err => {
-        clearInterval(interval)
-        this.$setErrorMessage(err.response.data.msg)
-      })
+
+      if (this.isSlide) {
+        createSlide({ courseId: this.selectedCourseId, name: this.slideName, file: this.file }).then(res => {
+          clearInterval(interval)
+          this.$setSuccessMessage('上传课件成功!')
+          this.$router.push({ name: 'teacher-explore' })
+        }).catch(err => {
+          clearInterval(interval)
+          this.$setErrorMessage(err.response.data.msg)
+        })
+      } else {
+        uploadCourseFile({ courseId: this.selectedCourseId, file: this.file }).then(res => {
+          clearInterval(interval)
+          this.$setSuccessMessage('上传附件成功!')
+          this.$router.push({ name: 'teacher-explore' })
+        }).catch(err => {
+          clearInterval(interval)
+          this.$setErrorMessage(err.response.data.msg)
+        })
+      }
     },
     handleFileChanged (file) {
       this.file = file || null
@@ -89,4 +108,8 @@ export default {
 </script>
 <style lang="scss" scoped>
 @import "~@/views/shared/tab-common";
+
+.type-bar {
+  display: flex;
+}
 </style>