tab5Add.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <template>
  2. <div class="tab5Add">
  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="100px"
  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/img/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. </el-form>
  57. <!-- 底部按钮 -->
  58. <div class="con_btn">
  59. <el-button type="primary" @click="saveGood">保 存</el-button>
  60. </div>
  61. </div>
  62. </div>
  63. </template>
  64. <script>
  65. import { imgSave, imgDetail } from '@/apis/tab5'
  66. import axios from '@/utils/request'
  67. export default {
  68. name: 'tab5Add',
  69. components: {},
  70. data () {
  71. return {
  72. // 服务器前缀地址
  73. baseURL: '',
  74. token: '',
  75. ruleForm: {
  76. name: '',
  77. thumb: ''
  78. },
  79. rules: {
  80. name: [{ required: true, message: '不能为空', trigger: 'blur' }]
  81. }
  82. }
  83. },
  84. computed: {},
  85. methods: {
  86. // 点击保存
  87. async saveGood () {
  88. if (this.ruleForm.name.trim() === '') { return this.$message.warning('标题不能为空') }
  89. if (this.ruleForm.thumb === '') { return this.$message.warning('图片不能为空') }
  90. const obj = { ...this.ruleForm }
  91. // console.log(998, obj)
  92. const res = await imgSave(obj)
  93. if (res.code === 0) {
  94. this.$message.success('操作成功')
  95. this.$router.push('/layout/tab5')
  96. } else this.$message.warning(res.msg)
  97. },
  98. // 点击返回
  99. goBack () {
  100. this.$confirm('点击返回后,编辑的信息将无法保存,是否继续?', '提示', {
  101. confirmButtonText: '确定',
  102. cancelButtonText: '取消',
  103. type: 'warning'
  104. })
  105. .then(async () => {
  106. this.$router.push('/layout/tab5')
  107. })
  108. .catch(() => {
  109. this.$message.info('已取消')
  110. })
  111. },
  112. // 上传图片
  113. beforethumbUpload (file) {
  114. // console.log(998, file)
  115. // 限制图片大小和格式
  116. const sizeOk = file.size / 1024 / 1024 < 20
  117. const typeOk =
  118. file.type === 'image/png' ||
  119. (file.type === 'image/jpeg' && !file.name.includes('.jfif')) ||
  120. file.type === 'image/gif'
  121. return new Promise((resolve, reject) => {
  122. if (!typeOk) {
  123. this.$message.error('照片格式有误!')
  124. reject(file)
  125. } else if (!sizeOk) {
  126. this.$message.error('照片大小超过20M!')
  127. reject(file)
  128. } else if (file.name.length > 32) {
  129. this.$message.error('照片名字不能超过32个字!')
  130. reject(file)
  131. } else {
  132. this.$message.success('上传成功')
  133. resolve(file)
  134. }
  135. })
  136. },
  137. upload_thumb_success (data) {
  138. // console.log('图片上传成功', data.data.urlPath)
  139. this.ruleForm.thumb = data.data.urlPath
  140. }
  141. },
  142. // 生命周期 - 创建完成(可以访问当前this实例)
  143. async created () {
  144. // 获取服务器前缀地址
  145. this.baseURL = axios.defaults.baseURL
  146. // 获取用户token
  147. this.token = localStorage.getItem('CQLJXU_token')
  148. // 通过父亲传过来的id获取详情
  149. if (this.$route.query.id) {
  150. const res = await imgDetail(Number(this.$route.query.id))
  151. this.ruleForm = res.data
  152. // console.log(998, res)
  153. }
  154. },
  155. // 生命周期 - 挂载完成(可以访问DOM元素)
  156. mounted () {},
  157. beforeCreate () {}, // 生命周期 - 创建之前
  158. beforeMount () {}, // 生命周期 - 挂载之前
  159. beforeUpdate () {}, // 生命周期 - 更新之前
  160. updated () {}, // 生命周期 - 更新之后
  161. beforeDestroy () {}, // 生命周期 - 销毁之前
  162. destroyed () {}, // 生命周期 - 销毁完成
  163. activated () {} // 如果页面有keep-alive缓存功能,这个函数会触发
  164. }
  165. </script>
  166. <style lang='less' scoped>
  167. .tab5Add {
  168. height: 100%;
  169. .conten {
  170. position: relative;
  171. /deep/.el-icon-plus {
  172. border: 1px dashed #ccc;
  173. }
  174. height: calc(100% - 32px);
  175. .con_top {
  176. margin-bottom: 12px;
  177. height: 40px;
  178. line-height: 40px;
  179. padding-left: 20px;
  180. background-color: #fbfbfb;
  181. }
  182. .biaoshi1::before {
  183. left: -64px;
  184. }
  185. .avatar-uploader .el-upload {
  186. border-radius: 6px;
  187. cursor: pointer;
  188. position: relative;
  189. overflow: hidden;
  190. }
  191. .avatar-uploader .el-upload:hover {
  192. border-color: #3e5eb3;
  193. }
  194. .avatar-uploader-icon {
  195. font-size: 28px;
  196. color: #8c939d;
  197. width: 178px;
  198. height: 178px;
  199. line-height: 178px;
  200. text-align: center;
  201. }
  202. .con_btn {
  203. position: absolute;
  204. left: 50%;
  205. transform: translateX(-50%);
  206. bottom: 15px;
  207. }
  208. .imgdiv {
  209. max-width: 200px;
  210. max-height: 200px;
  211. & > img {
  212. border: 5px solid #ccc;
  213. }
  214. }
  215. .el-icon-circle-close {
  216. font-size: 24px;
  217. }
  218. }
  219. }
  220. </style>