PublishImage.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <template>
  2. <el-row class="movie-list">
  3. <el-col :md="16" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  4. <el-card class="box-card">
  5. <div slot="header" class="clearfix">
  6. <span>上传图片文件</span>
  7. </div>
  8. <div class="text item">
  9. <el-upload
  10. :action="imgOssUrl"
  11. :headers="imgHeaders"
  12. :data="imgData"
  13. :file-list="uploadImages"
  14. :multiple="true"
  15. :limit="40"
  16. :with-credentials="false"
  17. list-type="picture-card"
  18. :before-upload="handleBeforeUpload"
  19. :on-success="handleOnSuccess"
  20. :on-error="handleOnError"
  21. :on-remove="handleOnRemove"
  22. :on-preview="handleOnPreview"
  23. >
  24. <i class="el-icon-plus" />
  25. </el-upload>
  26. <el-dialog :visible.sync="dialogVisible">
  27. <img width="100%" :src="dialogImageUrl" alt="">
  28. </el-dialog>
  29. </div>
  30. </el-card>
  31. </el-col>
  32. <el-col :md="8" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  33. <el-card class="box-card">
  34. <div slot="header" class="clearfix">
  35. <span>稿件信息</span>
  36. <el-button style="float: right; padding: 3px 0" type="text" @click="onSubmit('submitForm')">发布</el-button>
  37. </div>
  38. <div class="text item">
  39. <el-form ref="submitForm" :model="submitForm" :rules="submitFormRules" label-width="80px">
  40. <el-form-item label="相册名">
  41. <el-input v-model="submitForm.albumName" style="width: 70%; padding-right: 2px" placeholder="相册名不能超过 50 个字符" />
  42. </el-form-item>
  43. <el-form-item label="可见范围">
  44. <el-select v-model="submitForm.scope" placeholder="选择稿件的可见范围">
  45. <el-option label="本人可见" value="1" />
  46. <el-option label="所有人可见" value="2" />
  47. <el-option label="VIP 可见" value="3" />
  48. <el-option label="验证码可见" value="4" />
  49. </el-select>
  50. </el-form-item>
  51. <el-form-item label="定时发布">
  52. <el-date-picker
  53. v-model="submitForm.scheduledPubDate"
  54. type="datetime"
  55. placeholder="选择定时发布的时间"
  56. />
  57. </el-form-item>
  58. </el-form>
  59. </div>
  60. </el-card>
  61. </el-col>
  62. </el-row>
  63. </template>
  64. <script>
  65. import { getServerInfo } from '@/api/content'
  66. import { submitAlbum } from '@/api/image'
  67. var imageFileMap = new Map()
  68. export default {
  69. name: 'PublishImage',
  70. components: {},
  71. data() {
  72. return {
  73. imgOssUrl: '',
  74. // ****************************************************************************************************************
  75. imgHeaders: {
  76. Authorization: ''
  77. },
  78. imgData: {
  79. channelId: process.env.VUE_APP_UPLOAD_PHOTO_CHANNEL
  80. },
  81. dialogImageUrl: '',
  82. dialogVisible: false,
  83. uploadImages: [],
  84. // ****************************************************************************************************************
  85. submitForm: {
  86. imageFileIds: [],
  87. albumName: null,
  88. channelId: process.env.VUE_APP_UPLOAD_PHOTO_CHANNEL,
  89. scope: '1',
  90. scheduledPubDate: null
  91. },
  92. submitFormRules: {
  93. imageFileIds: [
  94. { type: 'array', required: true, message: '至少上传一张图片', trigger: 'change' }
  95. ]
  96. }
  97. }
  98. },
  99. created() {
  100. getServerInfo(this.imgData.channelId).then(res => {
  101. if (res.code === 0) {
  102. const resData = res.data
  103. this.imgOssUrl = resData.ossUrl
  104. this.imgHeaders.Authorization = 'Bearer ' + resData.token
  105. } else {
  106. this.$notify({
  107. title: '提示',
  108. message: '获取 OSS 服务器地址失败, 暂时无法上传图片',
  109. type: 'error',
  110. duration: 3000
  111. })
  112. }
  113. }).catch(error => {
  114. this.$notify({
  115. title: '提示',
  116. message: '获取 OSS 服务器地址失败, 暂时无法上传图片',
  117. type: 'warning',
  118. duration: 3000
  119. })
  120. })
  121. },
  122. methods: {
  123. // ****************************************************************************************************************
  124. handleBeforeUpload(file) {
  125. // const fileType = file.type
  126. var isJPG = false
  127. if (file.type === 'image/jpeg' || file.type === 'image/webp' ||
  128. file.type === 'image/gif' || file.type === 'image/png') {
  129. isJPG = true
  130. }
  131. const isLt2M = file.size / 1024 / 1024 < 100
  132. if (!isJPG) {
  133. this.$message.error('图片只能是 jpeg/webp/gif/png 格式!')
  134. }
  135. if (!isLt2M) {
  136. this.$message.error('图片大小不能超过 100MB!')
  137. }
  138. return isJPG && isLt2M
  139. },
  140. handleOnSuccess(res, file) {
  141. if (res.code === 0) {
  142. const resData = res.data
  143. imageFileMap.set(file.name, resData.uploadId)
  144. } else {
  145. this.$notify({
  146. title: '提示',
  147. message: '图片上传失败,请重试!' + res.msg,
  148. type: 'warning',
  149. duration: 3000
  150. })
  151. }
  152. },
  153. handleOnError(err, file, fileList) {
  154. const errMsg = JSON.parse(err.message)
  155. this.$notify({
  156. title: '图片上传失败',
  157. message: errMsg.msg,
  158. type: 'error',
  159. duration: 3000
  160. })
  161. },
  162. handleOnRemove(file, fileList) {
  163. imageFileMap.delete(file.name)
  164. },
  165. handleOnPreview(file) {
  166. this.dialogImageUrl = file.url
  167. this.dialogVisible = true
  168. },
  169. // ****************************************************************************************************************
  170. onSubmit(formName) {
  171. this.$refs[formName].validate(valid => {
  172. if (!valid) return false
  173. this.submitForm.imageFileIds = Array.from(imageFileMap.values())
  174. submitAlbum(this.submitForm).then(res => {
  175. if (res.code === 0) {
  176. this.$router.push('/post/list/image')
  177. } else {
  178. this.$notify({
  179. title: '提示',
  180. message: res.msg,
  181. type: 'warning',
  182. duration: 3000
  183. })
  184. }
  185. }).catch(error => {
  186. this.$notify({
  187. title: '提示',
  188. message: error.message,
  189. type: 'warning',
  190. duration: 3000
  191. })
  192. })
  193. })
  194. }
  195. }
  196. }
  197. </script>
  198. <style>
  199. .avatar-uploader .el-upload {
  200. border: 1px dashed #d9d9d9;
  201. border-radius: 6px;
  202. cursor: pointer;
  203. position: relative;
  204. overflow: hidden;
  205. }
  206. .avatar-uploader .el-upload:hover {
  207. border-color: #409EFF;
  208. }
  209. .avatar-uploader-icon {
  210. font-size: 28px;
  211. color: #8c939d;
  212. width: 320px;
  213. height: 240px;
  214. line-height: 178px;
  215. text-align: center;
  216. }
  217. .uploader-example .uploader-btn {
  218. margin-right: 4px;
  219. }
  220. .uploader-example .uploader-list {
  221. max-height: 440px;
  222. overflow: auto;
  223. overflow-x: hidden;
  224. overflow-y: auto;
  225. }
  226. .avatar-uploader .el-upload {
  227. border: 1px dashed #d9d9d9;
  228. border-radius: 6px;
  229. cursor: pointer;
  230. position: relative;
  231. overflow: hidden;
  232. }
  233. .avatar-uploader .el-upload:hover {
  234. border-color: #409EFF;
  235. }
  236. .uploader-example .uploader-btn {
  237. margin-right: 4px;
  238. }
  239. .uploader-example .uploader-list {
  240. max-height: 440px;
  241. overflow: auto;
  242. overflow-x: hidden;
  243. overflow-y: auto;
  244. }
  245. .avatar-uploader .el-upload {
  246. border: 1px dashed #d9d9d9;
  247. border-radius: 6px;
  248. cursor: pointer;
  249. position: relative;
  250. overflow: hidden;
  251. }
  252. .avatar-uploader .el-upload:hover {
  253. border-color: #409EFF;
  254. }
  255. </style>