tab1Add2.vue 10 KB

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