tab4Add.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <template>
  2. <div class="tab4Add">
  3. <div class="insideTop">
  4. 学院宣传片管理 > {{ruleForm.id?'编辑':'新增'}}宣传片
  5. <div class="add">
  6. <el-button type="primary" @click="goBack">返 回</el-button>
  7. </div>
  8. </div>
  9. <div class="obstruct"></div>
  10. <!-- 主要内容 -->
  11. <div class="conten">
  12. <div class="con_top">基本信息</div>
  13. <!-- 表单 -->
  14. <el-form
  15. :model="ruleForm"
  16. :rules="rules"
  17. ref="ruleForm"
  18. label-width="140px"
  19. class="demo-ruleForm"
  20. >
  21. <el-form-item label="标题:" prop="name">
  22. <el-input
  23. v-model="ruleForm.name"
  24. maxlength="25"
  25. show-word-limit
  26. style="width: 500px"
  27. ></el-input>
  28. </el-form-item>
  29. <!-- 图片和附件 -->
  30. <el-form-item label="封面图片:">
  31. <i class="biaoshi"></i>
  32. <el-upload
  33. class="avatar-uploader"
  34. :action="baseURL + '/cms/video/upload'"
  35. :headers="{
  36. token,
  37. }"
  38. :show-file-list="true"
  39. :before-upload="beforethumbUpload"
  40. :on-success="upload_thumb_success"
  41. >
  42. <div v-if="ruleForm.thumb" class="imgdiv">
  43. <img
  44. style="width: 100%; height: 100%"
  45. :src="baseURL + ruleForm.thumb"
  46. />
  47. <i
  48. class="el-icon-circle-close"
  49. @click.stop="ruleForm.thumb = ''"
  50. ></i>
  51. </div>
  52. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  53. </el-upload>
  54. <p class="upHint">
  55. 格式要求:支持png、jpg、gif和jpeg的图片格式;最大支持20M。
  56. </p>
  57. </el-form-item>
  58. <!-- 上传附件 -->
  59. <el-form-item label="视频文件:">
  60. <i class="biaoshi"></i>
  61. <el-upload
  62. multiple
  63. class="upload-demo"
  64. :file-list="fileList"
  65. :action="baseURL + '/cms/video/video/upload'"
  66. :headers="{ token }"
  67. :before-upload="beforeFujian"
  68. :on-success="successFujian"
  69. :before-remove="beforeRemove"
  70. :on-remove="handleRemove"
  71. :limit="1"
  72. :on-exceed="handleExceed"
  73. :show-file-list="true"
  74. >
  75. <el-button size="small" type="primary">点击上传</el-button>
  76. <div class="el-upload__tip" slot="tip">
  77. <p class="upHint">格式要求:</p>
  78. <p class="upHint">
  79. 1、仅支持mp4视频格式文件。
  80. </p>
  81. <p class="upHint">2、最大支持2G的视频文件。</p>
  82. </div>
  83. </el-upload>
  84. </el-form-item>
  85. </el-form>
  86. <!-- 底部按钮 -->
  87. <div class="con_btn">
  88. <el-button type="primary" @click="saveGood">保 存</el-button>
  89. </div>
  90. </div>
  91. </div>
  92. </template>
  93. <script>
  94. import { videoSave, videoDetail } from '@/apis/tab4'
  95. import axios from '@/utils/request'
  96. export default {
  97. name: 'tab4Add',
  98. components: {},
  99. data () {
  100. return {
  101. fileList: [],
  102. // 服务器前缀地址
  103. baseURL: '',
  104. token: '',
  105. ruleForm: {
  106. name: '',
  107. thumb: '',
  108. filePath: '',
  109. fileName: ''
  110. },
  111. rules: {
  112. name: [{ required: true, message: '不能为空', trigger: 'blur' }]
  113. }
  114. }
  115. },
  116. computed: {},
  117. methods: {
  118. // 点击保存
  119. async saveGood () {
  120. if (this.ruleForm.name.trim() === '') { return this.$message.warning('标题不能为空') }
  121. if (this.ruleForm.thumb === '') { return this.$message.warning('图片不能为空') }
  122. if (this.ruleForm.filePath === '') { return this.$message.warning('文件不能为空') }
  123. const obj = { ...this.ruleForm }
  124. const res = await videoSave(obj)
  125. if (res.code === 0) {
  126. this.$message.success('操作成功')
  127. this.$router.push('/layout/tab4')
  128. } else this.$message.warning(res.msg)
  129. },
  130. // 点击返回
  131. goBack () {
  132. this.$confirm('点击返回后,编辑的信息将无法保存,是否继续?', '提示', {
  133. confirmButtonText: '确定',
  134. cancelButtonText: '取消',
  135. type: 'warning'
  136. })
  137. .then(async () => {
  138. this.$router.push('/layout/tab4')
  139. })
  140. .catch(() => {
  141. this.$message.info('已取消')
  142. })
  143. },
  144. // 上传图片
  145. beforethumbUpload (file) {
  146. // console.log(998, file)
  147. // 限制图片大小和格式
  148. const sizeOk = file.size / 1024 / 1024 < 20
  149. const typeOk =
  150. file.type === 'image/png' ||
  151. file.type === 'image/jpeg' ||
  152. file.type === 'image/gif'
  153. return new Promise((resolve, reject) => {
  154. if (!typeOk) {
  155. this.$message.error('照片格式有误!')
  156. reject(file)
  157. } else if (!sizeOk) {
  158. this.$message.error('照片大小超过20M!')
  159. reject(file)
  160. } else if (file.name.length > 32) {
  161. this.$message.error('照片名字不能超过32个字!')
  162. reject(file)
  163. } else {
  164. this.$message.success('上传成功')
  165. resolve(file)
  166. }
  167. })
  168. },
  169. upload_thumb_success (data) {
  170. // console.log('图片上传成功', data.data.urlPath)
  171. this.ruleForm.thumb = data.data.urlPath
  172. },
  173. // 上传附件
  174. beforeFujian (file) {
  175. console.log('附件上传前', file)
  176. const sizeOk = file.size / 1024 / 1024 < 2048
  177. const typeOk = file.type === 'video/mp4'
  178. return new Promise((resolve, reject) => {
  179. if (file.name.length > 32) {
  180. this.$message.error('视频名字不能超过32个字')
  181. reject(file)
  182. } else if (!sizeOk) {
  183. this.$message.error('视频大小超过2GB!')
  184. reject(file)
  185. } else if (!typeOk) {
  186. this.$message.error('视频格式有误!')
  187. reject(file)
  188. } else {
  189. resolve(file)
  190. }
  191. })
  192. },
  193. successFujian (file) {
  194. console.log('上传附件成功', file)
  195. if (file.code === 0) {
  196. this.ruleForm.filePath = file.data.urlPath
  197. this.ruleForm.fileName = file.data.fileName
  198. this.$message.success('上传成功')
  199. } else if (file.code === -1) {
  200. this.$message.warning('上传失败,不支持的文件格式')
  201. }
  202. },
  203. beforeRemove (file, fileList) {
  204. if (file && file.status === 'success') {
  205. return this.$confirm(`确定移除 ${file.name}?`)
  206. }
  207. },
  208. handleRemove (file, fileList) {
  209. this.ruleForm.filePath = ''
  210. this.ruleForm.fileName = ''
  211. },
  212. handleExceed (files, fileList) {
  213. this.$message.warning('只能上传一个视频,请删除原视频后操作')
  214. }
  215. },
  216. // 生命周期 - 创建完成(可以访问当前this实例)
  217. async created () {
  218. // 获取服务器前缀地址
  219. this.baseURL = axios.defaults.baseURL
  220. // 获取用户token
  221. this.token = localStorage.getItem('CQLJXU_token')
  222. // 通过父亲传过来的id获取详情
  223. if (this.$route.query.id) {
  224. const res = await videoDetail(Number(this.$route.query.id))
  225. this.ruleForm = res.data
  226. // 附件回显
  227. this.fileList = [{ name: res.data.fileName }]
  228. // console.log(998, res)
  229. }
  230. },
  231. // 生命周期 - 挂载完成(可以访问DOM元素)
  232. mounted () {},
  233. beforeCreate () {}, // 生命周期 - 创建之前
  234. beforeMount () {}, // 生命周期 - 挂载之前
  235. beforeUpdate () {}, // 生命周期 - 更新之前
  236. updated () {}, // 生命周期 - 更新之后
  237. beforeDestroy () {}, // 生命周期 - 销毁之前
  238. destroyed () {}, // 生命周期 - 销毁完成
  239. activated () {} // 如果页面有keep-alive缓存功能,这个函数会触发
  240. }
  241. </script>
  242. <style lang='less' scoped>
  243. .tab4Add {
  244. height: 100%;
  245. .conten {
  246. position: relative;
  247. /deep/.el-icon-plus {
  248. border: 1px dashed #ccc;
  249. }
  250. height: calc(100% - 32px);
  251. .con_top {
  252. margin-bottom: 12px;
  253. height: 40px;
  254. line-height: 40px;
  255. padding-left: 20px;
  256. background-color: #fbfbfb;
  257. }
  258. .biaoshi1::before {
  259. left: -120px;
  260. }
  261. .biaoshi2::before {
  262. top: -10px;
  263. }
  264. .avatar-uploader .el-upload {
  265. border-radius: 6px;
  266. cursor: pointer;
  267. position: relative;
  268. overflow: hidden;
  269. }
  270. .avatar-uploader .el-upload:hover {
  271. border-color: #3e5eb3;
  272. }
  273. .avatar-uploader-icon {
  274. font-size: 28px;
  275. color: #8c939d;
  276. width: 178px;
  277. height: 178px;
  278. line-height: 178px;
  279. text-align: center;
  280. }
  281. .con_btn {
  282. position: absolute;
  283. left: 50%;
  284. transform: translateX(-50%);
  285. bottom: 15px;
  286. }
  287. .imgdiv {
  288. max-width: 200px;
  289. max-height: 200px;
  290. & > img {
  291. border: 5px solid #ccc;
  292. }
  293. }
  294. .el-icon-circle-close {
  295. font-size: 24px;
  296. }
  297. }
  298. }
  299. </style>