ImagePostEdit.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <template>
  2. <el-row class="movie-list">
  3. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  4. <el-card class="box-card" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  5. <div slot="header" class="clearfix">
  6. <span>添加图片</span>
  7. <el-button style="float: right; padding: 10px" type="text" @click="onReturnAlbum">返回图片稿件列表</el-button>
  8. </div>
  9. <div class="text item">
  10. <el-upload
  11. v-if="actionUrl !== null"
  12. :action="actionUrl"
  13. :headers="imgHeaders"
  14. :data="imgData"
  15. :file-list="uploadImages"
  16. :multiple="true"
  17. :limit="limit"
  18. :with-credentials="true"
  19. list-type="picture-card"
  20. :before-upload="handleBeforeUpload"
  21. :on-success="handleOnSuccess"
  22. :on-error="handleOnError"
  23. :on-remove="handleOnRemove"
  24. :on-preview="handleOnPreview"
  25. >
  26. <i class="el-icon-plus" />
  27. </el-upload>
  28. </div>
  29. </el-card>
  30. </el-row>
  31. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  32. <el-col :md="24">
  33. <div v-if="dataList.length !== 0">
  34. <el-col
  35. v-for="(image, index) in dataList"
  36. :key="image.thumbnailUrl"
  37. :md="6"
  38. :sm="12"
  39. :xs="12"
  40. style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px"
  41. >
  42. <el-card :body-style="{ padding: '0px' }" class="card">
  43. <div class="imgs">
  44. <el-image
  45. lazy
  46. fit="cover"
  47. class="coverImg"
  48. :src="image.thumbnailUrl"
  49. @click="showImages(index)"
  50. />
  51. </div>
  52. </el-card>
  53. </el-col>
  54. </div>
  55. </el-col>
  56. </el-row>
  57. <el-pagination
  58. :small="screenWidth <= 768"
  59. hide-on-single-page
  60. layout="prev, pager, next"
  61. :page-size="pageSize"
  62. :current-page="currentPage"
  63. :total="totalSize"
  64. @current-change="handleCurrentChange"
  65. @prev-click="handleCurrentChange"
  66. @next-click="handleCurrentChange"
  67. />
  68. </el-row>
  69. </template>
  70. <script>
  71. import { addAlbumImage, getImagePostItems } from '@/api/image'
  72. import { getPhotoChannelInfo } from '@/api/file'
  73. var imageFileMap = new Map()
  74. export default {
  75. name: 'ImagePostEdit',
  76. data() {
  77. return {
  78. actionUrl: null,
  79. /** *********************************************************************/
  80. imgHeaders: {
  81. Authorization: ''
  82. },
  83. imgData: {
  84. channelCode: 105
  85. },
  86. dialogImageUrl: '',
  87. dialogVisible: false,
  88. uploadImages: [],
  89. /** *********************************************************************/
  90. // 屏幕宽度, 为了控制分页条的大小
  91. screenWidth: document.body.clientWidth,
  92. currentPage: 1,
  93. pageSize: 12,
  94. totalSize: 0,
  95. albumId: null,
  96. data: null,
  97. dataList: [],
  98. postType: 0,
  99. imageCount: 0,
  100. limit: 0
  101. }
  102. },
  103. created() {
  104. document.title = '编辑相册稿件'
  105. this.albumId = this.$route.params.albumId
  106. this.getAlbumItemsWrapper()
  107. getPhotoChannelInfo().then(res => {
  108. if (res.code === 0) {
  109. const resData = res.data
  110. this.actionUrl = resData.ossUrl
  111. this.imgHeaders.Authorization = 'Bearer ' + resData.token
  112. this.imgData.channelCode = resData.channelCode
  113. } else {
  114. this.$notify({
  115. title: '提示',
  116. message: '获取 OSS 服务器地址失败, 暂时无法上传图片',
  117. type: 'error',
  118. duration: 3000
  119. })
  120. }
  121. }).catch(error => {
  122. this.$notify({
  123. title: '获取 OSS 服务器地址失败, 暂时无法上传图片',
  124. message: error.message,
  125. type: 'warning',
  126. duration: 3000
  127. })
  128. })
  129. },
  130. mounted() {
  131. // 当窗口宽度改变时获取屏幕宽度
  132. window.onresize = () => {
  133. return () => {
  134. window.screenWidth = document.body.clientWidth
  135. this.screenWidth = window.screenWidth
  136. }
  137. }
  138. },
  139. methods: {
  140. handleCurrentChange(pageNumber) {
  141. this.currentPage = pageNumber
  142. this.dataList = []
  143. this.getAlbumItemsWrapper()
  144. // 回到顶部
  145. scrollTo(0, 0)
  146. },
  147. getAlbumItemsWrapper() {
  148. getImagePostItems(this.albumId, this.currentPage).then(resp => {
  149. if (resp.code === 0) {
  150. this.data = resp.data
  151. const images = resp.data.images
  152. this.dataList = images.list
  153. this.totalSize = images.totalSize
  154. this.imageCount = images.length
  155. this.limit = 40 - images.length
  156. }
  157. })
  158. },
  159. /** *********************************************************************/
  160. handleBeforeUpload(file) {
  161. var isJPG = false
  162. if (file.type === 'image/jpeg' || file.type === 'image/webp' ||
  163. file.type === 'image/gif' || file.type === 'image/png') {
  164. isJPG = true
  165. }
  166. const isLt2M = file.size / 1024 / 1024 < 100
  167. if (!isJPG) {
  168. this.$message.error('图片只能是 jpeg/webp/gif/png 格式!')
  169. }
  170. if (!isLt2M) {
  171. this.$message.error('图片大小不能超过 100MB!')
  172. }
  173. return isJPG && isLt2M
  174. },
  175. handleOnSuccess(res, file) {
  176. if (res.code === 0) {
  177. const resData = res.data
  178. imageFileMap.set(file.name, resData.uploadId)
  179. const jsonData = {}
  180. jsonData.albumId = this.data.albumId
  181. jsonData.imageFileId = resData.uploadId
  182. addAlbumImage(jsonData).then(resp => {
  183. if (resp.code === 0) {
  184. this.$notify({
  185. title: '图片已添加到相册',
  186. type: 'info',
  187. duration: 1000
  188. })
  189. } else {
  190. this.$notify({
  191. title: '图片添加失败',
  192. message: resp.msg,
  193. type: 'warning',
  194. duration: 3000
  195. })
  196. }
  197. }).catch(error => {
  198. this.$notify.error({
  199. title: '图片添加错误',
  200. message: error.message,
  201. type: 'error',
  202. duration: 3000
  203. })
  204. })
  205. } else {
  206. this.$notify({
  207. title: '提示',
  208. message: '图片上传失败,请重试!' + res.msg,
  209. type: 'warning',
  210. duration: 3000
  211. })
  212. }
  213. },
  214. handleOnError(err, file, fileList) {
  215. const errMsg = JSON.parse(err.message)
  216. this.$notify({
  217. title: '图片上传失败',
  218. message: errMsg.msg,
  219. type: 'error',
  220. duration: 3000
  221. })
  222. },
  223. handleOnRemove(file, fileList) {
  224. imageFileMap.delete(file.name)
  225. },
  226. handleOnPreview(file) {
  227. this.dialogImageUrl = file.url
  228. this.dialogVisible = true
  229. },
  230. /** *********************************************************************/
  231. showImages(index) {
  232. const imageUrls = []
  233. for (const i of this.dataList) {
  234. imageUrls.push(i.originalUrl)
  235. }
  236. this.$viewerApi({
  237. images: imageUrls,
  238. options: {
  239. initialViewIndex: index,
  240. movable: true,
  241. fullscreen: false,
  242. keyboard: true
  243. }
  244. })
  245. },
  246. onReturnAlbum() {
  247. this.$router.push('/bg/post/image')
  248. }
  249. }
  250. }
  251. </script>
  252. <style scoped>
  253. /*处于手机屏幕时*/
  254. @media screen and (max-width: 768px){
  255. .movie-list {
  256. padding-top: 8px;
  257. padding-left: 0.5%;
  258. padding-right: 0.5%;
  259. }
  260. .coverImg {
  261. height: 120px !important;
  262. }
  263. }
  264. .movie-list {
  265. padding-top: 15px;
  266. padding-left: 6%;
  267. padding-right: 6%;
  268. }
  269. .coverImg {
  270. width: 100%;
  271. height: 240px;
  272. display: block;
  273. }
  274. .card {
  275. margin-bottom: 20px;
  276. transition: all 0.6s; /*所有属性变化在0.6秒内执行动画*/
  277. }
  278. .imgs {
  279. position: relative;
  280. }
  281. </style>