tab6Add.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <template>
  2. <div class="tab6Add">
  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="120px"
  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 biaoshi1"></i>
  32. <el-upload
  33. class="avatar-uploader"
  34. :action="baseURL + '/cms/music/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">格式要求:支持png、jpg、gif和jpeg的图片格式;最大支持20M。</p>
  55. </el-form-item> -->
  56. <!-- 上传附件 -->
  57. <el-form-item label="音频文件:">
  58. <i class="biaoshi"></i>
  59. <el-upload
  60. multiple
  61. class="upload-demo"
  62. :file-list="fileList"
  63. :action="baseURL + '/cms/music/video/upload'"
  64. :headers="{ token }"
  65. :before-upload="beforeFujian"
  66. :on-success="successFujian"
  67. :before-remove="beforeRemove"
  68. :on-remove="handleRemove"
  69. :limit="1"
  70. :on-exceed="handleExceed"
  71. :show-file-list="true"
  72. >
  73. <el-button size="small" type="primary">点击上传</el-button>
  74. <div class="el-upload__tip" slot="tip">
  75. <p class="upHint">格式要求:</p>
  76. <p class="upHint">1、支持音频格式文件(例如:mp3、wav)。</p>
  77. <p class="upHint">2、最大支持5M的音频文件。</p>
  78. </div>
  79. </el-upload>
  80. </el-form-item>
  81. <el-form-item label="歌词:">
  82. <i class="biaoshi biaoshi1"></i>
  83. <div id="div1" style="z-index: 1"></div>
  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 { musicSave, musicDetail } from '@/apis/tab6'
  95. import axios from '@/utils/request'
  96. import E from 'wangeditor'
  97. export default {
  98. name: 'tab6Add',
  99. components: {},
  100. data () {
  101. return {
  102. fileList: [],
  103. // 服务器前缀地址
  104. baseURL: '',
  105. token: '',
  106. ruleForm: {
  107. name: '',
  108. filePath: '',
  109. fileName: '',
  110. thumb: ''
  111. },
  112. rules: {
  113. name: [{ required: true, message: '不能为空', trigger: 'blur' }]
  114. }
  115. }
  116. },
  117. computed: {},
  118. methods: {
  119. // 点击保存
  120. async saveGood () {
  121. if (this.ruleForm.name.trim() === '') { return this.$message.warning('标题不能为空') }
  122. // if (this.ruleForm.thumb === '') { return this.$message.warning('图片不能为空') }
  123. if (this.ruleForm.filePath === '') { return this.$message.warning('音频不能为空') }
  124. if (this.editor.txt.html() === '') { return this.$message.warning('歌词不能为空') }
  125. const obj = { ...this.ruleForm, description: this.editor.txt.html() }
  126. const res = await musicSave(obj)
  127. if (res.code === 0) {
  128. this.$message.success('操作成功')
  129. this.$router.push('/layout/tab6')
  130. } else this.$message.warning(res.msg)
  131. },
  132. // 点击返回
  133. goBack () {
  134. this.$confirm('点击返回后,编辑的信息将无法保存,是否继续?', '提示', {
  135. confirmButtonText: '确定',
  136. cancelButtonText: '取消',
  137. type: 'warning'
  138. })
  139. .then(async () => {
  140. this.$router.push('/layout/tab6')
  141. })
  142. .catch(() => {
  143. this.$message.info('已取消')
  144. })
  145. },
  146. // 上传图片
  147. beforethumbUpload (file) {
  148. // console.log(998, file)
  149. // 限制图片大小和格式
  150. const sizeOk = file.size / 1024 / 1024 < 20
  151. const typeOk =
  152. file.type === 'image/png' ||
  153. file.type === 'image/jpeg' ||
  154. file.type === 'image/gif'
  155. return new Promise((resolve, reject) => {
  156. if (!typeOk) {
  157. this.$message.error('照片格式有误!')
  158. reject(file)
  159. } else if (!sizeOk) {
  160. this.$message.error('照片大小超过20M!')
  161. reject(file)
  162. } else if (file.name.length > 32) {
  163. this.$message.error('照片名字不能超过32个字!')
  164. reject(file)
  165. } else {
  166. this.$message.success('上传成功')
  167. resolve(file)
  168. }
  169. })
  170. },
  171. upload_thumb_success (data) {
  172. // console.log('图片上传成功', data.data.urlPath)
  173. this.ruleForm.thumb = data.data.urlPath
  174. },
  175. // 上传附件
  176. beforeFujian (file) {
  177. console.log('附件上传前', file)
  178. const sizeOk = file.size / 1024 / 1024 < 10
  179. const typeOk =
  180. file.type === 'audio/mpeg' ||
  181. file.type === 'audio/wav'
  182. return new Promise((resolve, reject) => {
  183. if (file.name.length > 32) {
  184. this.$message.error('音频名字不能超过32个字')
  185. reject(file)
  186. } else if (!sizeOk) {
  187. this.$message.error('音频大小超过10M!')
  188. reject(file)
  189. } else if (!typeOk) {
  190. this.$message.error('音频格式有误!')
  191. reject(file)
  192. } else {
  193. resolve(file)
  194. }
  195. })
  196. },
  197. successFujian (file) {
  198. console.log('上传附件成功', file)
  199. if (file.code === 0) {
  200. this.ruleForm.filePath = file.data.urlPath
  201. this.ruleForm.fileName = file.data.fileName
  202. this.$message.success('上传成功')
  203. } else if (file.code === -1) {
  204. this.$message.warning('上传失败,不支持的文件格式')
  205. }
  206. },
  207. beforeRemove (file, fileList) {
  208. if (file && file.status === 'success') {
  209. return this.$confirm(`确定移除 ${file.name}?`)
  210. }
  211. },
  212. handleRemove (file, fileList) {
  213. this.ruleForm.filePath = ''
  214. this.ruleForm.fileName = ''
  215. },
  216. handleExceed (files, fileList) {
  217. this.$message.warning('只能上传一个音频,请删除原音频后操作')
  218. }
  219. },
  220. // 生命周期 - 创建完成(可以访问当前this实例)
  221. created () {
  222. // 获取服务器前缀地址
  223. this.baseURL = axios.defaults.baseURL
  224. // 获取用户token
  225. this.token = localStorage.getItem('CQLJXU_token')
  226. },
  227. // 生命周期 - 挂载完成(可以访问DOM元素)
  228. async mounted () {
  229. // 初始化富文本
  230. // 富文本
  231. this.editor = new E('#div1')
  232. // 配置字体
  233. this.editor.config.fontNames = [
  234. '黑体',
  235. '仿宋',
  236. '楷体',
  237. '标楷体',
  238. '华文仿宋',
  239. '华文楷体',
  240. '宋体',
  241. '微软雅黑',
  242. 'Arial',
  243. 'Tahoma',
  244. 'Verdana',
  245. 'Times New Roman'
  246. ]
  247. this.editor.config.uploadImgShowBase64 = true // 图片地址
  248. this.editor.config.showLinkVideo = false
  249. this.editor.create()
  250. // 如果是编辑
  251. if (this.$route.query.id) {
  252. const res = await musicDetail(Number(this.$route.query.id))
  253. this.ruleForm = res.data
  254. console.log(998, res)
  255. // 富文本回显
  256. this.editor.txt.html(res.data.description)
  257. // 附件回显
  258. this.fileList = [{ name: res.data.fileName }]
  259. }
  260. },
  261. beforeCreate () {}, // 生命周期 - 创建之前
  262. beforeMount () {}, // 生命周期 - 挂载之前
  263. beforeUpdate () {}, // 生命周期 - 更新之前
  264. updated () {}, // 生命周期 - 更新之后
  265. beforeDestroy () {}, // 生命周期 - 销毁之前
  266. destroyed () {}, // 生命周期 - 销毁完成
  267. activated () {} // 如果页面有keep-alive缓存功能,这个函数会触发
  268. }
  269. </script>
  270. <style lang='less' scoped>
  271. .tab6Add {
  272. height: 100%;
  273. .conten {
  274. overflow-y: auto;
  275. position: relative;
  276. /deep/.el-icon-plus {
  277. border: 1px dashed #ccc;
  278. }
  279. height: calc(100% - 32px);
  280. .con_top {
  281. margin-bottom: 12px;
  282. height: 40px;
  283. line-height: 40px;
  284. padding-left: 20px;
  285. background-color: #fbfbfb;
  286. }
  287. .biaoshi1::before {
  288. left: -64px;
  289. }
  290. .avatar-uploader .el-upload {
  291. border-radius: 6px;
  292. cursor: pointer;
  293. position: relative;
  294. overflow: hidden;
  295. }
  296. .avatar-uploader .el-upload:hover {
  297. border-color: #3e5eb3;
  298. }
  299. .avatar-uploader-icon {
  300. font-size: 28px;
  301. color: #8c939d;
  302. width: 178px;
  303. height: 178px;
  304. line-height: 178px;
  305. text-align: center;
  306. }
  307. .con_btn {
  308. width: 80px;
  309. margin: 0 auto;
  310. margin-bottom: 20px;
  311. }
  312. #div1{
  313. width: 1200px;
  314. }
  315. .imgdiv {
  316. max-width: 200px;
  317. max-height: 200px;
  318. & > img {
  319. border: 5px solid #ccc;
  320. }
  321. }
  322. .el-icon-circle-close {
  323. font-size: 24px;
  324. }
  325. }
  326. }
  327. </style>