offlineTraining.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <template>
  2. <div class="offline-training-container">
  3. <el-card class="box-card">
  4. <div slot="header" class="clearfix">
  5. <span>线下研修班介绍</span>
  6. </div>
  7. <viewer ref="imageWrapper" :images="previewImages" style="opacity: 1;position: absolute;top: -9999px;">
  8. <img v-for="(image, index) in previewImages" :src="image" :key="index" />
  9. </viewer>
  10. <el-form ref="form" :model="form" label-width="120px">
  11. <div class="form-section">
  12. <el-form-item label="分类名称" prop="categoryName">
  13. <el-input v-model="form.categoryName" placeholder="请输入分类名称"></el-input>
  14. </el-form-item>
  15. </div>
  16. <div class="form-section">
  17. <el-form-item label="分类封面" prop="coverImage">
  18. <div class="upload-area">
  19. <el-upload
  20. class="cover-uploader"
  21. action="#"
  22. :show-file-list="false"
  23. :on-change="handleCoverChange"
  24. :auto-upload="false"
  25. :before-upload="beforeCoverUpload">
  26. <img v-if="form.coverImage" :src="form.coverImage" class="cover-preview" @click="previewImage(form.coverImage)">
  27. <i v-else class="el-icon-plus cover-uploader-icon"></i>
  28. </el-upload>
  29. <div class="upload-tip">建议尺寸:800x600px,大小不超过5MB</div>
  30. </div>
  31. </el-form-item>
  32. </div>
  33. <div class="form-section">
  34. <el-form-item label="内容上传" prop="contentType">
  35. <el-radio-group v-model="form.contentType" @change="handleContentTypeChange">
  36. <el-radio label="image">图片</el-radio>
  37. <el-radio label="video">视频</el-radio>
  38. </el-radio-group>
  39. <div class="upload-area" v-if="form.contentType === 'image'">
  40. <el-upload
  41. action="#"
  42. :show-file-list="false"
  43. :on-change="handleImageChange"
  44. :auto-upload="false"
  45. :before-upload="beforeImageUpload">
  46. <el-button size="small" type="primary">点击上传图片</el-button>
  47. </el-upload>
  48. <div class="upload-preview" v-if="form.contentImage">
  49. <img :src="form.contentImage" class="content-preview" @click="previewImage(form.contentImage)">
  50. </div>
  51. </div>
  52. <div class="upload-area" v-else-if="form.contentType === 'video'">
  53. <el-upload
  54. action="#"
  55. :show-file-list="false"
  56. :on-change="handleVideoChange"
  57. :auto-upload="false"
  58. :before-upload="beforeVideoUpload">
  59. <el-button size="small" type="primary">点击上传视频</el-button>
  60. </el-upload>
  61. <div class="upload-preview" v-if="form.contentVideo">
  62. <video controls class="video-preview">
  63. <source :src="form.contentVideo" type="video/mp4">
  64. </video>
  65. </div>
  66. </div>
  67. </el-form-item>
  68. </div>
  69. <div class="action-buttons">
  70. <el-button type="primary" @click="submitForm">提交</el-button>
  71. <el-button @click="resetForm">重置</el-button>
  72. </div>
  73. </el-form>
  74. </el-card>
  75. </div>
  76. </template>
  77. <script>
  78. export default {
  79. data() {
  80. return {
  81. form: {
  82. categoryName: '',
  83. coverImage: '',
  84. contentType: 'image',
  85. contentImage: '',
  86. contentVideo: ''
  87. },
  88. previewImages: []
  89. }
  90. },
  91. methods: {
  92. previewImage(url) {
  93. this.previewImages = [url];
  94. this.$nextTick(() => {
  95. if (this.$refs.imageWrapper && this.$refs.imageWrapper.$viewer) {
  96. this.$refs.imageWrapper.$viewer.show();
  97. }
  98. });
  99. },
  100. checkAndAddUpload(str) {
  101. if(!str) return '';
  102. return str.includes('/upload/') ? str : '/upload/' + str;
  103. },
  104. handleCoverChange(file) {
  105. const isImage = file.raw.type.includes('image');
  106. const isLt5M = file.raw.size / 1024 / 1024 < 5;
  107. if (!isImage) {
  108. this.$message.error('只能上传图片文件!');
  109. return;
  110. }
  111. if (!isLt5M) {
  112. this.$message.error('图片大小不能超过5MB!');
  113. return;
  114. }
  115. const formData = new FormData();
  116. formData.append('multipartFile', file.raw);
  117. this.http.uploadFile('/common/uploadFile', formData, res => {
  118. if (res.code === "ok") {
  119. this.form.coverImage = this.checkAndAddUpload(res.data);
  120. this.$message.success('封面图片上传成功');
  121. } else {
  122. this.$message.error(res.msg || '上传失败');
  123. }
  124. });
  125. },
  126. beforeCoverUpload(file) {
  127. const isImage = file.type.includes('image');
  128. const isLt5M = file.size / 1024 / 1024 < 5;
  129. if (!isImage) {
  130. this.$message.error('只能上传图片文件!');
  131. return false;
  132. }
  133. if (!isLt5M) {
  134. this.$message.error('图片大小不能超过5MB!');
  135. return false;
  136. }
  137. return true;
  138. },
  139. handleImageChange(file) {
  140. const isImage = file.raw.type.includes('image');
  141. const isLt5M = file.raw.size / 1024 / 1024 < 5;
  142. if (!isImage) {
  143. this.$message.error('只能上传图片文件!');
  144. return;
  145. }
  146. if (!isLt5M) {
  147. this.$message.error('图片大小不能超过5MB!');
  148. return;
  149. }
  150. const formData = new FormData();
  151. formData.append('multipartFile', file.raw);
  152. this.http.uploadFile('/common/uploadFile', formData, res => {
  153. if (res.code === "ok") {
  154. this.form.contentImage = this.checkAndAddUpload(res.data);
  155. this.$message.success('内容图片上传成功');
  156. } else {
  157. this.$message.error(res.msg || '上传失败');
  158. }
  159. });
  160. },
  161. beforeImageUpload(file) {
  162. return this.beforeCoverUpload(file);
  163. },
  164. handleVideoChange(file) {
  165. const isVideo = file.raw.type.includes('video');
  166. const isLt50M = file.raw.size / 1024 / 1024 < 50;
  167. if (!isVideo) {
  168. this.$message.error('只能上传视频文件!');
  169. return;
  170. }
  171. if (!isLt50M) {
  172. this.$message.error('视频大小不能超过50MB!');
  173. return;
  174. }
  175. const formData = new FormData();
  176. formData.append('multipartFile', file.raw);
  177. this.http.uploadFile('/common/uploadFile', formData, res => {
  178. if (res.code === "ok") {
  179. this.form.contentVideo = this.checkAndAddUpload(res.data);
  180. this.$message.success('视频上传成功');
  181. } else {
  182. this.$message.error(res.msg || '上传失败');
  183. }
  184. });
  185. },
  186. beforeVideoUpload(file) {
  187. const isVideo = file.type.includes('video');
  188. const isLt50M = file.size / 1024 / 1024 < 50;
  189. if (!isVideo) {
  190. this.$message.error('只能上传视频文件!');
  191. return false;
  192. }
  193. if (!isLt50M) {
  194. this.$message.error('视频大小不能超过50MB!');
  195. return false;
  196. }
  197. return true;
  198. },
  199. handleContentTypeChange(val) {
  200. this.form.contentImage = '';
  201. this.form.contentVideo = '';
  202. },
  203. submitForm() {
  204. this.$refs.form.validate(valid => {
  205. if (valid) {
  206. // 提交表单逻辑
  207. this.$message.success('提交成功!');
  208. }
  209. });
  210. },
  211. resetForm() {
  212. this.$refs.form.resetFields();
  213. this.form.contentImage = '';
  214. this.form.contentVideo = '';
  215. }
  216. }
  217. }
  218. </script>
  219. <style scoped>
  220. .offline-training-container {
  221. padding: 20px;
  222. max-width: 1000px;
  223. margin: 0 auto;
  224. }
  225. .box-card {
  226. border-radius: 8px;
  227. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  228. }
  229. .form-section {
  230. margin-bottom: 24px;
  231. }
  232. .upload-area {
  233. border: 1px dashed #dcdfe6;
  234. border-radius: 6px;
  235. padding: 20px;
  236. margin-bottom: 20px;
  237. background-color: #f5f7fa;
  238. }
  239. .upload-preview {
  240. margin-top: 15px;
  241. text-align: center;
  242. }
  243. .cover-uploader {
  244. border: 1px dashed #d9d9d9;
  245. border-radius: 6px;
  246. cursor: pointer;
  247. position: relative;
  248. overflow: hidden;
  249. width: 200px;
  250. height: 150px;
  251. margin: 0 auto;
  252. }
  253. .cover-uploader:hover {
  254. border-color: #409EFF;
  255. }
  256. .cover-uploader-icon {
  257. font-size: 28px;
  258. color: #8c939d;
  259. width: 200px;
  260. height: 150px;
  261. line-height: 150px;
  262. text-align: center;
  263. }
  264. .cover-preview {
  265. width: 200px;
  266. height: 150px;
  267. object-fit: cover;
  268. border-radius: 4px;
  269. cursor: pointer;
  270. }
  271. .content-preview {
  272. max-width: 100%;
  273. max-height: 300px;
  274. border-radius: 4px;
  275. cursor: pointer;
  276. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  277. }
  278. .video-preview {
  279. max-width: 100%;
  280. max-height: 300px;
  281. border-radius: 4px;
  282. background-color: #000;
  283. }
  284. .upload-tip {
  285. color: #909399;
  286. font-size: 12px;
  287. margin-top: 8px;
  288. text-align: center;
  289. }
  290. .action-buttons {
  291. margin-top: 24px;
  292. text-align: center;
  293. }
  294. </style>