tab3Dialog.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <template>
  2. <div class="tab3Dialog">
  3. <el-dialog title="新增" :visible="isShow" @close="btnX()">
  4. <el-form
  5. :model="ruleForm"
  6. :rules="rules"
  7. ref="ruleForm"
  8. label-width="100px"
  9. class="demo-ruleForm"
  10. >
  11. <el-form-item label="标题:" prop="name">
  12. <el-input
  13. v-model="ruleForm.name"
  14. maxlength="25"
  15. show-word-limit
  16. style="width: 500px"
  17. ></el-input>
  18. </el-form-item>
  19. <!-- 图片和附件 -->
  20. <el-form-item label="封面图片:">
  21. <i class="biaoshi"></i>
  22. <el-upload
  23. class="avatar-uploader"
  24. :action="baseURL + '/cms/security/video/upload/img'"
  25. :headers="{
  26. token,
  27. }"
  28. :show-file-list="false"
  29. :before-upload="beforethumbUpload"
  30. :on-success="upload_thumb_success"
  31. >
  32. <div v-if="ruleForm.thumb" class="imgdiv">
  33. <img
  34. style="width: 100%; height: 100%"
  35. :src="baseURL + ruleForm.thumb"
  36. />
  37. <i
  38. class="el-icon-circle-close"
  39. @click.stop="ruleForm.thumb = ''"
  40. ></i>
  41. </div>
  42. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  43. </el-upload>
  44. <span>格式要求:支持png、jpg、gif和jpeg的图片格式;最大支持5M。</span>
  45. </el-form-item>
  46. <!-- 上传附件 -->
  47. <el-form-item label="视频文件:">
  48. <i class="biaoshi"></i>
  49. <el-upload
  50. multiple
  51. class="upload-demo"
  52. :file-list='fileList'
  53. :action="baseURL + '/cms/security/video/upload/video'"
  54. :headers="{ token }"
  55. :before-upload="beforeFujian"
  56. :on-success="successFujian"
  57. :before-remove="beforeRemove"
  58. :on-remove="handleRemove"
  59. :limit="1"
  60. :on-exceed="handleExceed"
  61. :show-file-list="true"
  62. >
  63. <el-button size="small" type="primary">点击上传</el-button>
  64. <div class="el-upload__tip" slot="tip">
  65. 支持AVI、mov、rmvb、rm、FLV、mp4、3GP等格式的视频文件,大小不超过1GB。
  66. </div>
  67. </el-upload>
  68. </el-form-item>
  69. </el-form>
  70. <div slot="footer" class="dialog-footer">
  71. <el-button @click="btnX">取 消</el-button>
  72. <el-button type="primary" @click="btnOk">确 定</el-button>
  73. </div>
  74. </el-dialog>
  75. </div>
  76. </template>
  77. <script>
  78. import { videoDetailById, videoSave } from '@/apis/tab3'
  79. import axios from '@/utils/request'
  80. export default {
  81. name: 'tab3Dialog',
  82. props: {
  83. isShow: {
  84. type: Boolean,
  85. default: false
  86. }
  87. },
  88. components: {},
  89. data () {
  90. return {
  91. fileList: [],
  92. // 服务器前缀地址
  93. baseURL: '',
  94. token: '',
  95. ruleForm: {
  96. id: '',
  97. name: '',
  98. thumb: '',
  99. video: '',
  100. fileName: ''
  101. },
  102. rules: {
  103. name: [{ required: true, message: '不能为空', trigger: 'blur' }]
  104. }
  105. }
  106. },
  107. computed: {},
  108. methods: {
  109. // 点击关闭
  110. btnX () {
  111. this.$emit('update:isShow', false)
  112. // 清空表单
  113. this.ruleForm = {
  114. id: '',
  115. name: '',
  116. thumb: '',
  117. video: '',
  118. fileName: ''
  119. }
  120. this.fileList = []
  121. },
  122. // 点击确定
  123. async btnOk () {
  124. if (this.ruleForm.name.trim() === '') return this.$message.warning('标题不能为空')
  125. if (this.ruleForm.thumb === '') return this.$message.warning('封面图片不能为空')
  126. if (this.ruleForm.video === '') return this.$message.warning('视频文件不能为空')
  127. const obj = { ...this.ruleForm }
  128. const res = await videoSave(obj)
  129. if (res.code === 0) {
  130. this.$message.success('操作成功')
  131. // 通知父组件刷新页面
  132. this.$emit('refresh')
  133. this.btnX()
  134. }
  135. // console.log(998, res)
  136. },
  137. // 上传图片
  138. beforethumbUpload (file) {
  139. // console.log(998, file)
  140. // 限制图片大小和格式
  141. const sizeOk = file.size / 1024 / 1024 < 5
  142. const typeOk =
  143. file.type === 'image/png' ||
  144. file.type === 'image/jpeg' ||
  145. file.type === 'image/gif'
  146. return new Promise((resolve, reject) => {
  147. if (!typeOk) {
  148. this.$message.error('照片格式有误!')
  149. reject(file)
  150. } else if (!sizeOk) {
  151. this.$message.error('照片大小超过5M!')
  152. reject(file)
  153. } else if (file.name.length > 32) {
  154. this.$message.error('照片名字不能超过32个字!')
  155. reject(file)
  156. } else {
  157. this.$message.success('上传成功')
  158. resolve(file)
  159. }
  160. })
  161. },
  162. upload_thumb_success (data) {
  163. // console.log('图片上传成功', data.data.urlPath)
  164. this.ruleForm.thumb = data.data.urlPath
  165. },
  166. // 上传附件
  167. beforeFujian (file) {
  168. // console.log('附件上传前', file)
  169. const sizeOk = file.size / 1024 / 1024 < 1024
  170. const typeOk =
  171. file.type === 'video/mp4' ||
  172. file.type === 'video/avi' ||
  173. file.type === 'video/mov' || file.type === 'video/rmvb' || file.type === 'video/rm' || file.type === 'video/flv' || file.type === 'video/3gp'
  174. return new Promise((resolve, reject) => {
  175. if (file.name.length > 32) {
  176. this.$message.error('视频名字不能超过32个字')
  177. reject(file)
  178. } else if (!sizeOk) {
  179. this.$message.error('视频大小超过1BG!')
  180. reject(file)
  181. } else if (!typeOk) {
  182. this.$message.error('视频格式有误!')
  183. reject(file)
  184. } else {
  185. resolve(file)
  186. }
  187. })
  188. },
  189. successFujian (file) {
  190. console.log('上传附件成功', file)
  191. if (file.code === 0) {
  192. this.ruleForm.video = file.data.urlPath
  193. this.ruleForm.fileName = file.data.fileName
  194. this.$message.success('上传成功')
  195. } else if (file.code === -1) {
  196. this.$message.warning('上传失败,不支持的文件格式')
  197. }
  198. },
  199. beforeRemove (file, fileList) {
  200. if (file && file.status === 'success') {
  201. return this.$confirm(`确定移除 ${file.name}?`)
  202. }
  203. },
  204. handleRemove (file, fileList) {
  205. this.ruleForm.video = ''
  206. this.ruleForm.fileName = ''
  207. },
  208. handleExceed (files, fileList) {
  209. this.$message.warning('只能上传一个视频,请删除原视频后操作')
  210. },
  211. // 通过id获取详情---让父组件调用
  212. async videoDetailById (id) {
  213. const res = await videoDetailById(id)
  214. this.ruleForm = res.data
  215. this.fileList = [{ name: res.data.fileName }]
  216. // console.log(998, res)
  217. }
  218. },
  219. // 生命周期 - 创建完成(可以访问当前this实例)
  220. created () {
  221. // 获取服务器前缀地址
  222. this.baseURL = axios.defaults.baseURL
  223. // 获取用户token
  224. this.token = localStorage.getItem('SZSBL_token')
  225. },
  226. // 生命周期 - 挂载完成(可以访问DOM元素)
  227. mounted () {},
  228. beforeCreate () {}, // 生命周期 - 创建之前
  229. beforeMount () {}, // 生命周期 - 挂载之前
  230. beforeUpdate () {}, // 生命周期 - 更新之前
  231. updated () {}, // 生命周期 - 更新之后
  232. beforeDestroy () {}, // 生命周期 - 销毁之前
  233. destroyed () {}, // 生命周期 - 销毁完成
  234. activated () {} // 如果页面有keep-alive缓存功能,这个函数会触发
  235. }
  236. </script>
  237. <style lang='less' scoped>
  238. .biaoshi::before {
  239. top: 1px;
  240. }
  241. /deep/.el-upload-list{
  242. width: 500px;
  243. }
  244. /deep/.el-icon-plus {
  245. border: 1px dashed #ccc;
  246. }
  247. .avatar-uploader .el-upload {
  248. border-radius: 6px;
  249. cursor: pointer;
  250. position: relative;
  251. overflow: hidden;
  252. }
  253. .avatar-uploader .el-upload:hover {
  254. border-color: #3e5eb3;
  255. }
  256. .avatar-uploader-icon {
  257. font-size: 28px;
  258. color: #8c939d;
  259. width: 178px;
  260. height: 178px;
  261. line-height: 178px;
  262. text-align: center;
  263. }
  264. /deep/.el-icon-circle-close {
  265. font-size: 20px;
  266. }
  267. .imgdiv {
  268. max-width: 400px;
  269. }
  270. </style>