list.vue 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. <template>
  2. <section>
  3. <!--工具条-->
  4. <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
  5. <el-form :inline="true" @submit.native.prevent>
  6. <el-form-item label="课程分类">
  7. <el-select v-model="categoryValue" placeholder="请选择" clearable @change="searchList" size="small">
  8. <el-option v-for="item in categoryOptions" :key="item.value" :label="item.label"
  9. :value="item.value">
  10. </el-option>
  11. </el-select>
  12. </el-form-item>
  13. <el-form-item label="课程名称">
  14. <el-input v-model="keyword" placeholder="请输入" clearable @change="searchList"
  15. size="small"></el-input>
  16. </el-form-item>
  17. <el-form-item label="讲师">
  18. <el-input v-model="instructor" placeholder="请输入" clearable @change="searchList"
  19. size="small"></el-input>
  20. </el-form-item>
  21. <el-form-item>
  22. <el-button type="primary" @click="addCourse()" size="small">添加课程</el-button>
  23. </el-form-item>
  24. <el-form-item>
  25. <el-button type="primary" @click="batchManage" size="small">分类管理</el-button>
  26. </el-form-item>
  27. </el-form>
  28. </el-col>
  29. <!--列表-->
  30. <el-table :data="list" highlight-current-row v-loading="listLoading" :height="tableHeight" style="width: 100%;">
  31. <el-table-column type="index" width="50" align="center" label="#"></el-table-column>
  32. <el-table-column label="封面" width="120" align="center">
  33. <template slot-scope="scope">
  34. <img v-if="scope.row.coverImage" :src="scope.row.coverImage" class="course-cover-image" />
  35. <span v-else>无封面</span>
  36. </template>
  37. </el-table-column>
  38. <el-table-column prop="courseTypeName" label="课程分类" min-width="120" align="center"></el-table-column>
  39. <el-table-column prop="courseName" label="课程名称" min-width="180" align="center"></el-table-column>
  40. <el-table-column prop="courseInstructor" label="讲师" min-width="120" align="center"></el-table-column>
  41. <el-table-column prop="coursePrice" label="价格" min-width="100" align="center">
  42. <template slot-scope="scope">
  43. <span>¥{{ scope.row.coursePrice }}</span>
  44. </template>
  45. </el-table-column>
  46. <el-table-column prop="courseDuration" label="课程时间" min-width="120" align="center">
  47. <template slot-scope="scope">
  48. <span>{{ scope.row.courseDuration ? scope.row.courseDuration + '分钟' : '' }}</span>
  49. </template>
  50. </el-table-column>
  51. <el-table-column label="操作" width="340" class-name="btns" header-align="center" fixed="right">
  52. <template slot-scope="scope">
  53. <el-button size="mini" type="primary" @click="addVideo(scope.row)">添加视频</el-button>
  54. <el-button size="mini" type="primary" @click="addCourse(scope.row)">编辑</el-button>
  55. <el-button size="mini" type="danger" @click="deleteItem(scope.row)">删除</el-button>
  56. <el-button size="mini" :type="scope.row.courseStatus === 1 ? 'warning' : 'success'"
  57. @click="uploadItem(scope.row)">{{ scope.row.courseStatus === 1 ? '下架' : '上架' }}</el-button>
  58. </template>
  59. </el-table-column>
  60. </el-table>
  61. <!--工具条-->
  62. <el-col :span="24" class="toolbar">
  63. <el-pagination
  64. @size-change="handleSizeChange"
  65. @current-change="handleCurrentChange"
  66. :page-sizes="[10, 20, 50, 100]"
  67. :page-size="size"
  68. layout="total, sizes, prev, pager, next, jumper"
  69. :total="total"
  70. style="float:right;"
  71. ></el-pagination>
  72. </el-col>
  73. <!-- 分类管理 -->
  74. <el-dialog :visible.sync="categoryManageVisible" title="分类管理" width="600px">
  75. <el-table :data="categoryList" style="width: 100%" max-height="400">
  76. <el-table-column prop="label" label="分类名称" width="180"></el-table-column>
  77. <el-table-column label="封面" width="180">
  78. <template slot-scope="scope">
  79. <img v-if="scope.row.coverImage" :src="scope.row.coverImage" class="category-cover-image" />
  80. <span v-else>无封面</span>
  81. </template>
  82. </el-table-column>
  83. <el-table-column label="操作" width="180">
  84. <template slot-scope="scope">
  85. <el-button size="mini" type="primary" @click="setCategoryCover(scope.$index, scope.row)">设置封面</el-button>
  86. <el-button size="mini" type="danger" @click="deleteCategory(scope.$index, scope.row)">删除</el-button>
  87. </template>
  88. </el-table-column>
  89. </el-table>
  90. <div style="margin-top: 20px;">
  91. <el-form :inline="true" :model="newCategory" class="demo-form-inline">
  92. <el-form-item label="分类名称">
  93. <el-input v-model="newCategory.label" placeholder="请输入分类名称"></el-input>
  94. </el-form-item>
  95. <el-form-item>
  96. <el-button type="primary" @click="addCategory">添加</el-button>
  97. </el-form-item>
  98. </el-form>
  99. </div>
  100. <span slot="footer" class="dialog-dialog">
  101. <el-button @click="categoryManageVisible = false">关 闭</el-button>
  102. </span>
  103. </el-dialog>
  104. <!-- 设置分类封面 -->
  105. <el-dialog :visible.sync="coverDialogVisible" title="设置分类封面" width="500px">
  106. <div class="cover-upload-container">
  107. <el-upload
  108. class="cover-uploader"
  109. action="#"
  110. :show-file-list="false"
  111. :on-change="handleCoverChange"
  112. :auto-upload="false"
  113. :before-upload="beforeCoverUpload">
  114. <img v-if="coverImageUrl" :src="coverImageUrl" class="cover-image" />
  115. <i v-else class="el-icon-plus cover-uploader-icon"></i>
  116. </el-upload>
  117. <div class="cover-tip">请上传分类封面图片,建议尺寸 16:9</div>
  118. </div>
  119. <span slot="footer" class="dialog-footer">
  120. <el-button @click="coverDialogVisible = false">取 消</el-button>
  121. <el-button type="primary" @click="saveCategoryCover">确 定</el-button>
  122. </span>
  123. </el-dialog>
  124. <!-- 添加课程 -->
  125. <el-dialog :visible.sync="addDialogVisible" title="添加课程" width="800px" top="6.5vh">
  126. <el-form :model="courseForm" :rules="courseRules" ref="courseForm" label-width="120px">
  127. <el-form-item label="课程分类" prop="courseTypeId">
  128. <el-select v-model="courseForm.courseTypeId" placeholder="请选择" style="width:100%">
  129. <el-option v-for="item in categoryOptions" :key="item.value"
  130. :label="item.label" :value="item.value">
  131. </el-option>
  132. </el-select>
  133. </el-form-item>
  134. <el-form-item label="课程名称" prop="courseName">
  135. <el-input v-model="courseForm.courseName" placeholder="请输入课程名称"></el-input>
  136. </el-form-item>
  137. <el-form-item label="课程介绍" style="height: 200px;">
  138. <quill-editor style="height: 150px" ref="text" v-model="courseForm.courseDesc" class="myQuillEditor" :options="editorOption"/>
  139. </el-form-item>
  140. <el-form-item label="课程价格">
  141. <el-input-number v-model="courseForm.coursePrice" :min="0" :precision="2"></el-input-number>
  142. </el-form-item>
  143. <el-form-item label="课程时间(分钟)">
  144. <el-input-number v-model="courseForm.courseDuration" :min="0"></el-input-number>
  145. </el-form-item>
  146. <el-form-item label="课程封面">
  147. <el-upload
  148. class="cover-uploader"
  149. action="#"
  150. :show-file-list="false"
  151. :on-change="handleCourseCoverChange"
  152. :auto-upload="false"
  153. :before-upload="beforeCoverUpload">
  154. <img v-if="courseForm.coverImageUrl" :src="courseForm.coverImageUrl" class="cover-image" />
  155. <i v-else class="el-icon-plus cover-uploader-icon"></i>
  156. </el-upload>
  157. <div class="cover-tip">建议尺寸 16:9,大小不超过2MB</div>
  158. </el-form-item>
  159. </el-form>
  160. <span slot="footer" class="dialog-footer">
  161. <el-button @click="addDialogVisible = false">取 消</el-button>
  162. <el-button type="primary" @click="submitCourseForm">确 定</el-button>
  163. </span>
  164. </el-dialog>
  165. <!-- 添加视频 -->
  166. <el-dialog :visible.sync="addVideoVisable" width="900" top="6.5vh">
  167. <div slot="title">
  168. <div class="title-dios">
  169. <div>添加视频</div>
  170. <div style="margin-right: 30px;">
  171. <el-upload
  172. ref="uploadVideoRef"
  173. action="#"
  174. :disabled="addVideoLoading"
  175. :show-file-list="false"
  176. :limit="5"
  177. multiple
  178. :http-request="uploadVideo"
  179. accept="video/mp4,video/avi,video/x-msvideo">
  180. <el-button size="small" type="primary" :loading="addVideoLoading">上传视频</el-button>
  181. </el-upload>
  182. </div>
  183. </div>
  184. </div>
  185. <div class="addVideoClass">
  186. <addVideoCom ref="addVideoComRef" v-model="displayVideoList"></addVideoCom>
  187. </div>
  188. <span slot="footer" class="dialog-footer">
  189. <el-button type="primary" @click="saveVideo()" :disabled="!displayVideoList.length || displayVideoList.some(video => video.videoError) || displayVideoList.some(video => video.videoLoading)" size="small" :loading="saveVideoLoading">保存视频</el-button>
  190. </span>
  191. </el-dialog>
  192. </section>
  193. </template>
  194. <script>
  195. // 富文本样式
  196. import 'quill/dist/quill.core.css'
  197. import 'quill/dist/quill.snow.css'
  198. import 'quill/dist/quill.bubble.css'
  199. // 导入富文本
  200. import { quillEditor } from 'vue-quill-editor'
  201. import addVideoCom from './addVideo.vue'
  202. export default {
  203. components: {
  204. quillEditor, // 富文本
  205. addVideoCom, // 添加视频
  206. },
  207. data() {
  208. return {
  209. // 课程表单
  210. courseForm: {
  211. courseTypeId: '',
  212. courseName: '',
  213. courseDesc: '',
  214. coursePrice: 0,
  215. courseDuration: 0,
  216. coverImageUrl: '',
  217. coverImageFile: null
  218. },
  219. courseRules: {
  220. courseTypeId: [
  221. { required: true, message: '请选择课程分类', trigger: 'change' }
  222. ],
  223. courseName: [
  224. { required: true, message: '请输入课程名称', trigger: 'blur' }
  225. ]
  226. },
  227. // 课程分类选择
  228. categoryValue: '',
  229. categoryOptions: [],
  230. // 搜索条件
  231. keyword: null, // 课程名称
  232. instructor: null, // 讲师
  233. // 表格相关
  234. tableHeight: 0,
  235. listLoading: false,
  236. total: 0,
  237. page: 1,
  238. size: 20,
  239. list: [],
  240. // 对话框控制
  241. categoryManageVisible: false,
  242. editDialogVisible: false,
  243. addDialogVisible: false,
  244. // 分类管理
  245. categoryList: [],
  246. newCategory: {
  247. label: '',
  248. value: '',
  249. coverImage: ''
  250. },
  251. // 分类封面设置
  252. coverDialogVisible: false,
  253. currentCategoryIndex: -1,
  254. coverImageUrl: '',
  255. coverImageFile: null,
  256. // 用户信息
  257. user: sessionStorage.user ? JSON.parse(sessionStorage.user) : {},
  258. editorOption: { // 富文本框里面的默认值
  259. placeholder: '请输入内容...',
  260. modules: {
  261. toolbar:[
  262. ['bold', 'italic', 'underline', 'strike'], //加粗,斜体,下划线,删除线
  263. // ['blockquote', 'code-block'], //引用,代码块
  264. [{ 'header': 1 }, { 'header': 2 }], // 标题,键值对的形式;1、2表示字体大小
  265. // [{ 'list': 'ordered'}, { 'list': 'bullet' }], //列表
  266. // [{ 'script': 'sub'}, { 'script': 'super' }], // 上下标
  267. // [{ 'indent': '-1'}, { 'indent': '+1' }], // 缩进
  268. // [{ 'direction': 'rtl' }], // 文本方向
  269. [{ 'size': ['small', false, 'large', 'huge'] }], // 字体大小
  270. [{ 'header': [1, 2, 3, 4, 5, 6, false] }], //几级标题
  271. [{ 'color': [] }, { 'background': [] }], // 字体颜色,字体背景颜色
  272. // [{ 'font': [] }], //字体
  273. [{ 'align': [] }], //对齐方式
  274. ['clean'], //清除字体样式
  275. // ['image','video'] //上传图片、上传视频
  276. [] //上传图片、上传视频
  277. ], //工具栏设置
  278. },
  279. theme: 'snow',
  280. },
  281. // 添加视频
  282. addVideoRow: {},
  283. addVideoVisable: false,
  284. addVideoLoading: false,
  285. videoList: [],
  286. displayVideoList: [],
  287. videiListTime: null,
  288. saveVideoLoading: false,
  289. }
  290. },
  291. methods: {
  292. saveVideo() {
  293. console.log(this.displayVideoList, '<=== 看看数据')
  294. return
  295. this.addVideoVisable = false
  296. },
  297. uploadVideo(file) {
  298. this.videoList.unshift(file.file)
  299. if(this.videiListTime) {
  300. clearTimeout(this.videiListTime)
  301. }
  302. this.videiListTime = setTimeout(() => {
  303. this.batchUploadVideo()
  304. }, 500)
  305. // this.$refs.addVideoComRef.addNewRow()
  306. },
  307. batchUploadVideo() {
  308. for(let i = 0; i < this.videoList.length; i++) {
  309. const formData = new FormData()
  310. formData.append('multipartFile', this.videoList[i])
  311. this.displayVideoList.unshift({
  312. videoName: this.removeVideoSuffix(this.videoList[i].name),
  313. videoLoading: true,
  314. videoUrl: '',
  315. videoLecturerId: '',
  316. videoPreviewTime: 3,
  317. videoError: ''
  318. })
  319. this.http.uploadFile(`/common/uploadFile`, formData, res => {
  320. this.displayVideoList[i].videoUrl = res.data
  321. this.displayVideoList[i].videoLoading = false
  322. }, err => {
  323. this.displayVideoList[i].videoLoading = false
  324. this.displayVideoList[i].videoError = '视频上传失败'
  325. })
  326. }
  327. setTimeout(() => {
  328. this.videoList = []
  329. this.$refs.uploadVideoRef.clearFiles()
  330. }, 1500)
  331. },
  332. // 去除视频名称后缀
  333. removeVideoSuffix(str) {
  334. if (str.indexOf('.') > -1) {
  335. return str.substring(0, str.lastIndexOf('.'))
  336. } else {
  337. return str
  338. }
  339. },
  340. // 添加视频
  341. addVideo(row) {
  342. console.log(row, '<===== 单机的对象')
  343. this.addVideoRow = row
  344. this.videoList = []
  345. this.displayVideoList = []
  346. this.addVideoVisable = true
  347. },
  348. // 分类管理
  349. batchManage() {
  350. this.categoryManageVisible = true;
  351. // 加载分类数据
  352. this.http.post('/course-type/list', {}, res => {
  353. if (res.code == "ok") {
  354. // 将后端返回的数据转换为前端需要的格式
  355. this.categoryList = res.data.map(item => ({
  356. label: item.typeName,
  357. value: item.id,
  358. coverImage: item.coverImage || ''
  359. }));
  360. // 同步更新下拉选项
  361. this.categoryOptions = [...this.categoryList];
  362. } else {
  363. this.$message({
  364. message: res.msg || '获取分类列表失败',
  365. type: 'error'
  366. });
  367. }
  368. }, error => {
  369. this.$message({
  370. message: error || '获取分类列表失败',
  371. type: 'error'
  372. });
  373. });
  374. },
  375. // 搜索课程列表
  376. searchList() {
  377. this.page = 1;
  378. this.getList();
  379. },
  380. // 获取课程列表
  381. getList() {
  382. this.listLoading = true;
  383. // 调用后端API获取课程列表
  384. this.http.post('/course-info/list', {
  385. page: this.page,
  386. size: this.size,
  387. courseType: this.categoryValue,
  388. courseName: this.keyword,
  389. courseInstructor: this.instructor
  390. }, res => {
  391. this.listLoading = false;
  392. if (res.code == "ok") {
  393. this.list = res.data.records;
  394. this.total = res.data.total;
  395. } else {
  396. this.$message({
  397. message: res.msg || '获取课程列表失败',
  398. type: 'error'
  399. });
  400. }
  401. }, error => {
  402. this.listLoading = false;
  403. this.$message({
  404. message: error || '获取课程列表失败',
  405. type: 'error'
  406. });
  407. });
  408. },
  409. // 分页相关
  410. handleCurrentChange(val) {
  411. this.page = val;
  412. this.getList();
  413. },
  414. handleSizeChange(val) {
  415. this.page = 1;
  416. this.size = val;
  417. this.getList();
  418. },
  419. // 删除课程
  420. deleteItem(row) {
  421. this.$confirm('确认删除该课程?', '提示', {
  422. confirmButtonText: '确定',
  423. cancelButtonText: '取消',
  424. type: 'warning'
  425. }).then(() => {
  426. // 调用后端API删除课程
  427. this.http.post('/course-info/deleteCourse', {
  428. id: row.id
  429. }, res => {
  430. if (res.code == "ok") {
  431. this.$message({
  432. type: 'success',
  433. message: '删除成功!'
  434. });
  435. this.getList();
  436. } else {
  437. this.$message({
  438. message: res.msg || '删除课程失败',
  439. type: 'error'
  440. });
  441. }
  442. }, error => {
  443. this.$message({
  444. message: error || '删除课程失败',
  445. type: 'error'
  446. });
  447. });
  448. })
  449. },
  450. // 上架/下架课程
  451. uploadItem(row) {
  452. const action = row.courseStatus === 1 ? '下架' : '上架';
  453. this.$confirm(`确认${action}该课程?`, '提示', {
  454. confirmButtonText: '确定',
  455. cancelButtonText: '取消',
  456. type: 'warning'
  457. }).then(() => {
  458. // 调用后端API上架/下架课程
  459. this.http.post('/course-info/saveOrUpdate', {
  460. id: row.id,
  461. courseStatus: row.courseStatus === 1 ? 0 : 1 // 0: 下架, 1: 上架
  462. }, res => {
  463. if (res.code == "ok") {
  464. row.courseStatus = row.courseStatus === 1 ? 0 : 1;
  465. this.$message({
  466. type: 'success',
  467. message: `${action}成功!`
  468. });
  469. } else {
  470. this.$message({
  471. message: res.msg || `${action}失败`,
  472. type: 'error'
  473. });
  474. }
  475. }, error => {
  476. this.$message({
  477. message: error || `${action}失败`,
  478. type: 'error'
  479. });
  480. });
  481. })
  482. },
  483. // 保存分类封面
  484. saveCategoryCover() {
  485. if (!this.coverImageUrl) {
  486. this.$message.warning('请先上传封面图片!');
  487. return;
  488. }
  489. // 关闭对话框
  490. this.coverDialogVisible = false;
  491. this.$message({
  492. type: 'success',
  493. message: '设置封面成功!'
  494. });
  495. },
  496. // 处理封面图片变更
  497. handleCoverChange(file) {
  498. this.coverImageFile = file.raw;
  499. if (this.coverImageFile) {
  500. this.coverImageUrl = URL.createObjectURL(this.coverImageFile);
  501. // 获取当前分类的ID
  502. if (this.currentCategoryIndex >= 0) {
  503. const categoryId = this.categoryList[this.currentCategoryIndex].value;
  504. // 立即上传封面图片
  505. this.listLoading = true;
  506. // 创建FormData对象用于上传文件
  507. const formData = new FormData();
  508. formData.append('id', categoryId);
  509. formData.append('coverImage', this.coverImageFile);
  510. // 调用上传图片的API
  511. this.http.uploadFile('/course-type/uploadCover', formData, res => {
  512. this.listLoading = false;
  513. if (res.code == "ok") {
  514. // 上传成功后,获取返回的图片URL
  515. const imageUrl = res.data && res.data.url ? res.data.url : this.coverImageUrl;
  516. // 更新本地数据
  517. this.categoryList[this.currentCategoryIndex].coverImage = imageUrl;
  518. // 同步更新到分类选项中
  519. const optionIndex = this.categoryOptions.findIndex(item => item.value === categoryId);
  520. if (optionIndex !== -1) {
  521. this.categoryOptions[optionIndex].coverImage = imageUrl;
  522. }
  523. this.$message({
  524. type: 'success',
  525. message: '封面图片上传成功!'
  526. });
  527. } else {
  528. this.$message({
  529. message: res.msg || '上传封面图片失败',
  530. type: 'error'
  531. });
  532. }
  533. }, error => {
  534. this.listLoading = false;
  535. this.$message({
  536. message: error || '上传封面图片失败',
  537. type: 'error'
  538. });
  539. });
  540. }
  541. }
  542. },
  543. // 设置分类封面
  544. setCategoryCover(index, row) {
  545. this.currentCategoryIndex = index;
  546. this.coverImageUrl = row.coverImage || '';
  547. this.coverDialogVisible = true;
  548. },
  549. // 处理封面图片上传前的验证
  550. beforeCoverUpload(file) {
  551. const isImage = file.type.indexOf('image/') === 0;
  552. const isLt2M = file.size / 1024 / 1024 < 2;
  553. if (!isImage) {
  554. this.$message.error('上传封面图片只能是图片格式!');
  555. }
  556. if (!isLt2M) {
  557. this.$message.error('上传封面图片大小不能超过 2MB!');
  558. }
  559. return isImage && isLt2M;
  560. },
  561. // 添加课程
  562. addCourse(row) {
  563. this.addDialogVisible = true;
  564. if(!row) {
  565. this.courseForm = {
  566. courseTypeId: '',
  567. courseName: '',
  568. courseDesc: '',
  569. coursePrice: 0,
  570. courseDuration: 0,
  571. coverImageUrl: '',
  572. };
  573. if (this.$refs.courseForm) {
  574. this.$refs.courseForm.resetFields();
  575. }
  576. } else {
  577. console.log(row, '<==== 看看数据')
  578. const { courseTypeId, courseName, courseDesc, coursePrice, courseDuration, coverImage, id } = row
  579. this.courseForm = {
  580. id,
  581. courseTypeId,
  582. courseName,
  583. courseDesc,
  584. coursePrice,
  585. courseDuration,
  586. coverImageUrl: this.checkAndAddUpload(coverImage)
  587. }
  588. }
  589. },
  590. // 提交课程表单
  591. submitCourseForm() {
  592. this.$refs.courseForm.validate(valid => {
  593. if (!valid) {
  594. return false;
  595. }
  596. const formData = new FormData();
  597. if(this.courseForm.id) {
  598. formData.append('id', this.courseForm.id);
  599. }
  600. formData.append('courseTypeId', this.courseForm.courseTypeId);
  601. formData.append('courseName', this.courseForm.courseName);
  602. formData.append('courseDesc', this.courseForm.courseDesc);
  603. formData.append('coursePrice', this.courseForm.coursePrice);
  604. formData.append('courseDuration', this.courseForm.courseDuration);
  605. formData.append('coverImage', this.courseForm.coverImageUrl);
  606. this.listLoading = true;
  607. this.http.uploadFile('/course-info/saveOrUpdate', formData, res => {
  608. this.listLoading = false;
  609. if (res.code === "ok") {
  610. this.$message.success('添加课程成功');
  611. this.addDialogVisible = false;
  612. this.getList();
  613. } else {
  614. this.$message.error(res.msg || '添加课程失败');
  615. }
  616. }, error => {
  617. this.listLoading = false;
  618. this.$message.error(error || '添加课程失败');
  619. });
  620. });
  621. },
  622. // 处理课程封面图片变更
  623. handleCourseCoverChange(file) {
  624. const row = file.raw
  625. const formData = new FormData();
  626. formData.append('multipartFile', row);
  627. this.http.uploadFile('/common/uploadFile', formData, res => {
  628. if (res.code == "ok") {
  629. this.courseForm.coverImageUrl = this.checkAndAddUpload(res.data)
  630. } else {
  631. this.$message({
  632. message: res.msg || '图片上传失败',
  633. type: 'error'
  634. });
  635. }
  636. })
  637. },
  638. checkAndAddUpload(str) {
  639. if(!str) {
  640. return '';
  641. }
  642. if (str.includes('/upload/')) {
  643. return str;
  644. } else {
  645. return '/upload/' + str;
  646. }
  647. },
  648. // 添加分类
  649. addCategory() {
  650. if (!this.newCategory.label || !this.newCategory.label.trim()) {
  651. this.$message({
  652. type: 'warning',
  653. message: '请输入分类名称!'
  654. });
  655. return;
  656. }
  657. // 调用后端API保存课程分类
  658. this.http.post('/course-type/saveOrUpdate', {
  659. typeName: this.newCategory.label
  660. }, res => {
  661. if (res.code == "ok") {
  662. // 生成唯一ID作为value,实际项目中应该使用后端返回的ID
  663. const categoryId = res.data && res.data.id ? res.data.id : 'category_' + Date.now();
  664. this.newCategory.value = categoryId;
  665. // 添加到分类列表
  666. this.categoryList.push({...this.newCategory});
  667. this.categoryOptions.push({...this.newCategory});
  668. // 清空输入
  669. this.newCategory.label = '';
  670. this.newCategory.value = '';
  671. this.$message({
  672. type: 'success',
  673. message: '添加分类成功!'
  674. });
  675. } else {
  676. this.$message({
  677. message: res.msg || '添加分类失败',
  678. type: 'error'
  679. });
  680. }
  681. }, error => {
  682. this.$message({
  683. message: error || '添加分类失败',
  684. type: 'error'
  685. });
  686. });
  687. },
  688. // 删除分类
  689. deleteCategory(index, row) {
  690. this.$confirm('确认删除该分类?', '提示', {
  691. confirmButtonText: '确定',
  692. cancelButtonText: '取消',
  693. type: 'warning'
  694. }).then(() => {
  695. // 调用后端API删除课程分类
  696. this.http.post('/course-type/delete', {
  697. id: row.value
  698. }, res => {
  699. if (res.code == "ok") {
  700. // 从分类列表中删除
  701. this.categoryList.splice(index, 1);
  702. // 从下拉选项中删除
  703. const optionIndex = this.categoryOptions.findIndex(item => item.value === row.value);
  704. if (optionIndex !== -1) {
  705. this.categoryOptions.splice(optionIndex, 1);
  706. }
  707. this.$message({
  708. type: 'success',
  709. message: '删除分类成功!'
  710. });
  711. } else {
  712. this.$message({
  713. message: res.msg || '删除分类失败',
  714. type: 'error'
  715. });
  716. }
  717. }, error => {
  718. this.$message({
  719. message: error || '删除分类失败',
  720. type: 'error'
  721. });
  722. });
  723. })
  724. },
  725. },
  726. created() {
  727. let height = window.innerHeight;
  728. this.tableHeight = height - 195;
  729. const that = this;
  730. window.onresize = function temp() {
  731. that.tableHeight = window.innerHeight - 195;
  732. };
  733. // 初始化分类列表
  734. this.categoryList = [...this.categoryOptions];
  735. },
  736. mounted() {
  737. // 获取课程分类数据
  738. this.http.post('/course-type/list', {}, res => {
  739. if (res.code == "ok") {
  740. // 将后端返回的数据转换为前端需要的格式
  741. this.categoryList = res.data.map(item => ({
  742. label: item.typeName,
  743. value: item.id,
  744. coverImage: item.coverImage || ''
  745. }));
  746. // 同步更新下拉选项
  747. this.categoryOptions = [...this.categoryList];
  748. } else {
  749. this.$message({
  750. message: res.msg || '获取分类列表失败',
  751. type: 'error'
  752. });
  753. }
  754. }, error => {
  755. this.$message({
  756. message: error || '获取分类列表失败',
  757. type: 'error'
  758. });
  759. });
  760. // 获取课程列表
  761. this.getList();
  762. }
  763. }
  764. </script>
  765. <style lang="scss" scoped>
  766. .title-dios {
  767. font-size: 20px;
  768. color: #666;
  769. display: flex;
  770. align-items: center;
  771. width: 100%;
  772. justify-content: space-between;
  773. }
  774. .addVideoClass {
  775. height: 65vh;
  776. overflow-y: auto;
  777. }
  778. .rg_span {
  779. display: inline-block;
  780. }
  781. .rg_span span {
  782. text-align: right;
  783. box-sizing: border-box;
  784. padding-right: 10px;
  785. }
  786. .el-dialog__title {
  787. display: inline-table;
  788. margin-top: 20px;
  789. }
  790. .btns .el-button {
  791. margin-left: 10px;
  792. margin-bottom: 5px;
  793. }
  794. </style>
  795. <style>
  796. .course-cover-image {
  797. width: 100px;
  798. height: 100px;
  799. object-fit: cover;
  800. border-radius: 4px;
  801. }
  802. .otherForm .el-form-item {
  803. float: left;
  804. width: 50%;
  805. margin: 0;
  806. }
  807. /* 视频播放器样式 */
  808. .video-player-container {
  809. display: flex;
  810. justify-content: center;
  811. align-items: center;
  812. width: 100%;
  813. }
  814. .video-player {
  815. max-width: 100%;
  816. max-height: 500px;
  817. }
  818. .video-preview {
  819. margin-top: 10px;
  820. border: 1px solid #ebeef5;
  821. border-radius: 4px;
  822. padding: 10px;
  823. background-color: #f9f9f9;
  824. }
  825. .video-header {
  826. display: flex;
  827. justify-content: space-between;
  828. align-items: center;
  829. margin-bottom: 10px;
  830. padding-bottom: 8px;
  831. border-bottom: 1px solid #ebeef5;
  832. }
  833. .video-title {
  834. font-weight: bold;
  835. color: #303133;
  836. }
  837. .delete-video-btn {
  838. color: #f56c6c;
  839. padding: 0;
  840. }
  841. .delete-video-btn:hover {
  842. color: #f78989;
  843. }
  844. .video-name {
  845. margin-top: 5px;
  846. color: #606266;
  847. font-size: 14px;
  848. }
  849. /* 编辑器样式 */
  850. .editor-container {
  851. border: 1px solid #dcdfe6;
  852. border-radius: 4px;
  853. overflow: hidden;
  854. }
  855. .editor-toolbar {
  856. background-color: #f5f7fa;
  857. padding: 5px;
  858. border-bottom: 1px solid #dcdfe6;
  859. display: flex;
  860. align-items: center;
  861. }
  862. .editor-btn {
  863. background: none;
  864. border: none;
  865. padding: 5px 8px;
  866. margin: 0 2px;
  867. cursor: pointer;
  868. border-radius: 3px;
  869. }
  870. .editor-btn:hover {
  871. background-color: #e4e7ed;
  872. }
  873. .editor-separator {
  874. width: 1px;
  875. height: 16px;
  876. background-color: #dcdfe6;
  877. margin: 0 5px;
  878. }
  879. .editor-select {
  880. height: 24px;
  881. border: 1px solid #dcdfe6;
  882. border-radius: 3px;
  883. margin: 0 5px;
  884. }
  885. .editor-content {
  886. width: 100%;
  887. min-height: 120px;
  888. padding: 10px;
  889. border: none;
  890. resize: vertical;
  891. outline: none;
  892. font-family: Arial, sans-serif;
  893. font-size: 14px;
  894. }
  895. /* 上传链接样式 */
  896. .upload-item {
  897. margin-bottom: 20px;
  898. }
  899. .upload-link {
  900. color: #409eff;
  901. cursor: pointer;
  902. font-size: 14px;
  903. }
  904. .upload-link:hover {
  905. text-decoration: underline;
  906. }
  907. /* 分类封面图片样式 */
  908. .category-cover-image {
  909. width: 100px;
  910. height: 56px;
  911. object-fit: cover;
  912. border-radius: 4px;
  913. }
  914. .cover-upload-container {
  915. display: flex;
  916. flex-direction: column;
  917. align-items: center;
  918. padding: 20px 0;
  919. }
  920. .cover-uploader {
  921. border: 1px dashed #d9d9d9;
  922. border-radius: 6px;
  923. cursor: pointer;
  924. position: relative;
  925. overflow: hidden;
  926. width: 178px;
  927. height: 100px;
  928. margin-bottom: 10px;
  929. }
  930. .cover-uploader:hover {
  931. border-color: #409EFF;
  932. }
  933. .cover-uploader-icon {
  934. font-size: 28px;
  935. color: #8c939d;
  936. width: 178px;
  937. height: 100px;
  938. line-height: 100px;
  939. text-align: center;
  940. }
  941. .cover-image {
  942. width: 178px;
  943. height: 100px;
  944. display: block;
  945. object-fit: cover;
  946. }
  947. .cover-tip {
  948. color: #909399;
  949. font-size: 12px;
  950. margin-top: 10px;
  951. }
  952. /* 讲师照片上传样式 */
  953. .instructor-uploader {
  954. border: 1px dashed #d9d9d9;
  955. border-radius: 6px;
  956. cursor: pointer;
  957. position: relative;
  958. overflow: hidden;
  959. width: 100px;
  960. height: 100px;
  961. margin-bottom: 10px;
  962. }
  963. .instructor-uploader:hover {
  964. border-color: #409EFF;
  965. }
  966. .instructor-uploader-icon {
  967. font-size: 28px;
  968. color: #8c939d;
  969. width: 100px;
  970. height: 100px;
  971. line-height: 100px;
  972. text-align: center;
  973. }
  974. .instructor-image {
  975. width: 100px;
  976. height: 100px;
  977. display: block;
  978. object-fit: cover;
  979. }
  980. .upload-tip {
  981. color: #909399;
  982. font-size: 12px;
  983. margin-top: 5px;
  984. }
  985. .ql-snow .ql-picker-label::before {
  986. position: relative;
  987. top: -8px;
  988. }
  989. </style>