tab1Add.vue 11 KB

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