Image.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <template>
  2. <div>
  3. <!--电影列表,与推荐列表-->
  4. <el-row id="movie-list">
  5. <!--电影列表-->
  6. <el-col :md="24">
  7. <div>
  8. <el-col
  9. v-for="(image, index) in dataList"
  10. :key="image.thumbnailUrl"
  11. :md="6"
  12. :sm="12"
  13. :xs="12"
  14. style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px"
  15. >
  16. <el-card :body-style="{ padding: '0px' }" class="card">
  17. <div class="imgs">
  18. <el-image
  19. lazy
  20. fit="cover"
  21. class="coverImg"
  22. :src="image.thumbnailUrl"
  23. @click="showImages(index)"
  24. />
  25. </div>
  26. <div style="padding: 14px;">
  27. <span style="position: relative; bottom: 0; left: 0%; color:green">
  28. <i v-if="image.collected" class="el-icon-star-on" @click="collectItem(image)" />
  29. <i v-else class="el-icon-star-off" @click="collectItem(image)" />
  30. </span>
  31. <span style="position: relative; bottom: 0; left: 0; color:blue">
  32. 来自相册:
  33. </span>
  34. <router-link target="_blank" :to="`/image/album/${image.albumId}`">
  35. <span style="position: relative; bottom: 0; left: 0; color:blue">
  36. {{ image.albumName | ellipsis }}
  37. </span>
  38. </router-link>
  39. </div>
  40. </el-card>
  41. </el-col>
  42. </div>
  43. <!--
  44. 分页按钮:
  45. page-size:每页显示条数
  46. total:总条数
  47. hide-on-single-page: 页数为一时隐藏
  48. -->
  49. <el-col :span="24" class="pagination">
  50. <el-pagination
  51. background
  52. :small="screenWidth <= 768"
  53. layout="prev, pager, next"
  54. :page-size="pageSize"
  55. :current-page="currentPage"
  56. :total="totalSize"
  57. @current-change="handleCurrentChange"
  58. />
  59. </el-col>
  60. </el-col>
  61. </el-row>
  62. <el-row v-if="dataList.length === 0" class="not-result">
  63. <el-col :span="12" :offset="6">
  64. <img src="@/assets/img/icon/not-result.png">
  65. <div>没有图片数据</div>
  66. </el-col>
  67. </el-row>
  68. </div>
  69. </template>
  70. <script>
  71. import { getImages } from '@/api/image'
  72. import { collectItem } from '@/api/collect'
  73. export default {
  74. name: 'Image',
  75. components: {},
  76. filters: {
  77. ellipsis(value) {
  78. if (!value) return ''
  79. const max = 10
  80. if (value.length > max) {
  81. return value.slice(0, max) + '...'
  82. }
  83. return value
  84. }
  85. },
  86. data() {
  87. return {
  88. // 屏幕宽度, 为了控制分页条的大小
  89. screenWidth: document.body.clientWidth,
  90. currentPage: 1,
  91. pageSize: 24,
  92. totalSize: 0,
  93. dataList: []
  94. }
  95. },
  96. created() {
  97. document.title = '图片主页'
  98. this.getImagesWrapper(this.currentPage)
  99. },
  100. mounted() {
  101. // 当窗口宽度改变时获取屏幕宽度
  102. window.onresize = () => {
  103. return () => {
  104. window.screenWidth = document.body.clientWidth
  105. this.screenWidth = window.screenWidth
  106. }
  107. }
  108. },
  109. methods: {
  110. handleCurrentChange(currentPage) {
  111. this.currentPage = currentPage
  112. this.getImagesWrapper(this.currentPage)
  113. // 回到顶部
  114. scrollTo(0, 0)
  115. },
  116. getImagesWrapper(page) {
  117. getImages(page).then(resp => {
  118. if (resp.code === 0) {
  119. this.dataList = resp.data.list
  120. this.totalSize = resp.data.totalSize
  121. } else {
  122. this.$notify({
  123. title: '获取数据失败',
  124. message: resp.msg,
  125. type: 'warning',
  126. duration: 3000
  127. })
  128. }
  129. }).catch(error => {
  130. this.$notify({
  131. title: '获取数据错误',
  132. message: error.message,
  133. type: 'error',
  134. duration: 3000
  135. })
  136. })
  137. },
  138. showImages(index) {
  139. const imageUrls = []
  140. for (const i of this.dataList) {
  141. imageUrls.push(i.originalUrl)
  142. }
  143. this.$viewerApi({
  144. images: imageUrls,
  145. options: {
  146. initialViewIndex: index,
  147. movable: true,
  148. fullscreen: false,
  149. keyboard: true
  150. }
  151. })
  152. },
  153. collectItem(image) {
  154. console.log(image)
  155. const jsonData = {}
  156. jsonData.contentType = 1001
  157. jsonData.contentId = image.imageFileId
  158. if (image.collected) {
  159. jsonData.collected = false
  160. collectItem(jsonData).then(resp => {
  161. if (resp.code === 0) {
  162. this.$notify({
  163. title: '取消收藏图片',
  164. type: 'info',
  165. duration: 3000
  166. })
  167. image.collected = false
  168. }
  169. })
  170. } else {
  171. jsonData.collected = true
  172. collectItem(jsonData).then(resp => {
  173. if (resp.code === 0) {
  174. this.$notify({
  175. title: '图片已收藏',
  176. type: 'info',
  177. duration: 3000
  178. })
  179. image.collected = true
  180. }
  181. })
  182. }
  183. }
  184. }
  185. }
  186. </script>
  187. <style scoped>
  188. /*处于手机屏幕时*/
  189. @media screen and (max-width: 768px){
  190. #movie-list {
  191. padding-top: 8px;
  192. padding-left: 0.5%;
  193. padding-right: 0.5%;
  194. }
  195. .coverImg {
  196. height: 120px !important;
  197. }
  198. }
  199. #movie-list {
  200. padding-top: 15px;
  201. padding-left: 6%;
  202. padding-right: 6%;
  203. }
  204. .coverImg {
  205. width: 100%;
  206. height: 320px;
  207. display: block;
  208. }
  209. .not-result {
  210. padding-top: 100px;
  211. padding-bottom: 100px;
  212. text-align: center;
  213. }
  214. .pagination {
  215. text-align: center;
  216. padding: 10px;
  217. }
  218. </style>