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. ref="upload"
  61. class="avatar-uploader"
  62. :action="baseURL + '/cms/goods/upload'"
  63. :headers="{ token }"
  64. :show-file-list="true"
  65. :before-upload="beforethumbUpload"
  66. :on-success="upload_thumb_success"
  67. >
  68. <div class="upImg">
  69. <i slot="default" class="el-icon-plus"></i>
  70. </div>
  71. </el-upload>
  72. </div>
  73. <p class="upHint upHint1">格式要求:</p>
  74. <p class="upHint">
  75. 1、支持png、jpg、gif和jpeg的图片格式;最大支持20M。
  76. </p>
  77. <p class="upHint">2、最多可上传18张图片。</p>
  78. </el-form-item>
  79. </el-form>
  80. <!-- 底部按钮 -->
  81. <div class="con_btn">
  82. <el-button type="primary" @click="goodsSave">保 存</el-button>
  83. </div>
  84. </div>
  85. </div>
  86. </template>
  87. <script>
  88. import { goodsSave, goodsDetail, goodsimgRemove } from '@/apis/tab1'
  89. import axios from '@/utils/request'
  90. export default {
  91. name: 'tab1Add2',
  92. components: {},
  93. data () {
  94. return {
  95. // 重新上传的数据
  96. imgActive: 0,
  97. // 上传成功后的图片数组
  98. imgList: [],
  99. imgListLook: [],
  100. conLeftId: 'img',
  101. fileList: [],
  102. // 服务器前缀地址
  103. baseURL: '',
  104. token: '',
  105. ruleForm: {
  106. code: new Date().valueOf(),
  107. name: '',
  108. type: 'img',
  109. thumb: ''
  110. },
  111. rules: {
  112. name: [{ required: true, message: '不能为空', trigger: 'blur' }]
  113. }
  114. }
  115. },
  116. computed: {},
  117. methods: {
  118. // 点击保存
  119. async goodsSave () {
  120. if (this.ruleForm.name.trim() === '') { return this.$message.warning('标题不能为空') }
  121. if (this.imgList.length === 0) { return this.$message.warning('图片不能为空') }
  122. if (this.imgList.length > 18) { return this.$message.warning('图片不能超过18张') }
  123. const temp = []
  124. this.imgList.forEach(v => {
  125. temp.push(v.id)
  126. })
  127. const obj = { ...this.ruleForm, fileIds: temp.join(','), thumb: this.imgList[this.imgActive].filePath, indexId: this.imgList[this.imgActive].id }
  128. const res = await goodsSave(obj)
  129. if (res.code === 0) {
  130. this.$message.success('操作成功')
  131. this.$router.push({
  132. path: '/layout/tab1',
  133. query: { conLeftId: 'img' }
  134. })
  135. } else this.$message.warning(res.msg)
  136. // console.log(998, res)
  137. },
  138. // 点击删除
  139. delGoodsImg (index, id) {
  140. this.$confirm('确定删除吗?', '提示', {
  141. confirmButtonText: '确定',
  142. cancelButtonText: '取消',
  143. type: 'warning'
  144. })
  145. .then(async () => {
  146. await goodsimgRemove(id)
  147. this.imgList.splice(index, 1)
  148. this.imgListLook.splice(index, 1)
  149. if (index < this.imgActive) this.imgActive--
  150. this.$message.success('删除成功')
  151. })
  152. .catch(() => {
  153. this.$message.info('已取消')
  154. })
  155. },
  156. // 点击设为封面
  157. async goodsImgIndex (index) {
  158. this.imgActive = index
  159. // this.ruleForm.thumb = this.imgList[index].filePath
  160. this.$message.success('操作成功')
  161. },
  162. // 点击返回
  163. goBack () {
  164. this.$confirm('点击返回后,编辑的信息将无法保存,是否继续?', '提示', {
  165. confirmButtonText: '确定',
  166. cancelButtonText: '取消',
  167. type: 'warning'
  168. })
  169. .then(async () => {
  170. this.$router.push({
  171. path: '/layout/tab1',
  172. query: { conLeftId: 'img' }
  173. })
  174. })
  175. .catch(() => {
  176. this.$message.info('已取消')
  177. })
  178. },
  179. // 上传图片
  180. beforethumbUpload (file) {
  181. // console.log(998, file)
  182. // 限制图片大小和格式
  183. const sizeOk = file.size / 1024 / 1024 < 20
  184. const typeOk =
  185. file.type === 'image/png' ||
  186. file.type === 'image/jpeg' ||
  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>