PublishImage.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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="//oss.reghao.cn/"
  11. :headers="imgHeaders"
  12. :data="imgData"
  13. :file-list="uploadImages"
  14. :multiple="true"
  15. :limit="40"
  16. :with-credentials="true"
  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. // ****************************************************************************************************************
  74. imgHeaders: {
  75. Authorization: ''
  76. },
  77. imgData: {
  78. channelId: 6
  79. },
  80. dialogImageUrl: '',
  81. dialogVisible: false,
  82. uploadImages: [],
  83. // ****************************************************************************************************************
  84. submitForm: {
  85. imageFileIds: [],
  86. albumName: null,
  87. scope: '1',
  88. scheduledPubDate: null
  89. },
  90. submitFormRules: {
  91. imageFileIds: [
  92. { type: 'array', required: true, message: '至少上传一张图片', trigger: 'change' }
  93. ]
  94. }
  95. }
  96. },
  97. created() {
  98. getServerInfo(this.imgData.channelId).then(res => {
  99. if (res.code === 0) {
  100. const resData = res.data
  101. this.imgHeaders.Authorization = 'Bearer ' + resData.token
  102. } else {
  103. this.$notify({
  104. title: '失败提示',
  105. message: res.msg,
  106. type: 'warning',
  107. duration: 3000
  108. })
  109. }
  110. }).catch(error => {
  111. this.$notify({
  112. title: '错误提示',
  113. message: error.message,
  114. type: 'warning',
  115. duration: 3000
  116. })
  117. })
  118. },
  119. methods: {
  120. // ****************************************************************************************************************
  121. handleBeforeUpload(file) {
  122. const fileType = file.type
  123. var isJPG = false
  124. if (file.type === 'image/jpeg' || file.type === 'image/webp' ||
  125. file.type === 'image/gif' || file.type === 'image/png') {
  126. isJPG = true
  127. }
  128. const isLt2M = file.size / 1024 / 1024 < 100
  129. if (!isJPG) {
  130. this.$message.error('图片只能是 jpeg/webp/gif/png 格式!')
  131. }
  132. if (!isLt2M) {
  133. this.$message.error('图片大小不能超过 100MB!')
  134. }
  135. return isJPG && isLt2M
  136. },
  137. handleOnSuccess(res, file) {
  138. if (res.code === 0) {
  139. const resData = res.data
  140. imageFileMap.set(file.name, resData.uploadId)
  141. } else {
  142. this.$notify({
  143. title: '提示',
  144. message: '图片上传失败,请重试!' + res.msg,
  145. type: 'warning',
  146. duration: 3000
  147. })
  148. }
  149. },
  150. handleOnError(err, file, fileList) {
  151. const errMsg = JSON.parse(err.message)
  152. this.$notify({
  153. title: '图片上传失败',
  154. message: errMsg.msg,
  155. type: 'error',
  156. duration: 3000
  157. })
  158. },
  159. handleOnRemove(file, fileList) {
  160. imageFileMap.delete(file.name)
  161. },
  162. handleOnPreview(file) {
  163. this.dialogImageUrl = file.url
  164. this.dialogVisible = true
  165. },
  166. // ****************************************************************************************************************
  167. onSubmit(formName) {
  168. this.$refs[formName].validate(valid => {
  169. if (!valid) return false
  170. this.submitForm.imageFileIds = Array.from(imageFileMap.values())
  171. submitAlbum(this.submitForm).then(res => {
  172. if (res.code === 0) {
  173. this.$router.push('/my/post/list/image')
  174. } else {
  175. this.$notify({
  176. title: '提示',
  177. message: res.msg,
  178. type: 'warning',
  179. duration: 3000
  180. })
  181. }
  182. }).catch(error => {
  183. this.$notify({
  184. title: '提示',
  185. message: error.message,
  186. type: 'warning',
  187. duration: 3000
  188. })
  189. })
  190. })
  191. }
  192. }
  193. }
  194. </script>
  195. <style>
  196. .avatar-uploader .el-upload {
  197. border: 1px dashed #d9d9d9;
  198. border-radius: 6px;
  199. cursor: pointer;
  200. position: relative;
  201. overflow: hidden;
  202. }
  203. .avatar-uploader .el-upload:hover {
  204. border-color: #409EFF;
  205. }
  206. .avatar-uploader-icon {
  207. font-size: 28px;
  208. color: #8c939d;
  209. width: 320px;
  210. height: 240px;
  211. line-height: 178px;
  212. text-align: center;
  213. }
  214. .uploader-example .uploader-btn {
  215. margin-right: 4px;
  216. }
  217. .uploader-example .uploader-list {
  218. max-height: 440px;
  219. overflow: auto;
  220. overflow-x: hidden;
  221. overflow-y: auto;
  222. }
  223. .avatar-uploader .el-upload {
  224. border: 1px dashed #d9d9d9;
  225. border-radius: 6px;
  226. cursor: pointer;
  227. position: relative;
  228. overflow: hidden;
  229. }
  230. .avatar-uploader .el-upload:hover {
  231. border-color: #409EFF;
  232. }
  233. .uploader-example .uploader-btn {
  234. margin-right: 4px;
  235. }
  236. .uploader-example .uploader-list {
  237. max-height: 440px;
  238. overflow: auto;
  239. overflow-x: hidden;
  240. overflow-y: auto;
  241. }
  242. .avatar-uploader .el-upload {
  243. border: 1px dashed #d9d9d9;
  244. border-radius: 6px;
  245. cursor: pointer;
  246. position: relative;
  247. overflow: hidden;
  248. }
  249. .avatar-uploader .el-upload:hover {
  250. border-color: #409EFF;
  251. }
  252. </style>