profileImg.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <div style="margin:10px;">
  3. <el-form>
  4. <el-col :offset="1">
  5. <el-form-item label-width="0px" prop="picture" style="width: 100%;">
  6. <el-upload ref="upload"
  7. action="#"
  8. list-type="picture-card"
  9. :on-preview="handlePictureCardPreview"
  10. :on-remove="handleRemove"
  11. :http-request="uploadAvatar"
  12. :before-upload="beforeAvatarUpload"
  13. :file-list="fileList"
  14. >
  15. <i class="el-icon-plus"></i>
  16. </el-upload>
  17. <el-dialog :visible.sync="dialogVisible">
  18. <img width="100%" :src="dialogImageUrl" alt="">
  19. </el-dialog>
  20. </el-form-item>
  21. </el-col>
  22. <el-col :span="6" v-for="(item,index) in picList" :offset="1" :style="{ padding: '0px 0px 10px' }" :key="item.key">
  23. <div @mouseover="enter(index)" @mouseleave="leave">
  24. <el-card :body-style="{ padding: '0px' }" shadow="always" class="father">
  25. <el-image :key="item.value" :src="item.value" :preview-src-list="[item.value]" class="image"></el-image>
  26. <div class="son" v-show="num==index" @click="delImg(item.key,index)">
  27. <i class="el-icon-delete" style="color:#fff"></i>
  28. </div>
  29. </el-card>
  30. </div>
  31. </el-col>
  32. </el-form>
  33. </div>
  34. </template>
  35. <script>
  36. import { uploadFile,allProfileImg,delProfileImg } from "@/api/system/profileImg";
  37. export default {
  38. name: "ProfileImg",
  39. data() {
  40. return {
  41. dialogImageUrl: '',
  42. dialogVisible: false,
  43. picList: [],
  44. fileList: [],
  45. profileImg: {
  46. profileId: null,
  47. url: null
  48. },
  49. num: -1,
  50. };
  51. },
  52. created() {
  53. const pid = this.$route.query.pid;
  54. this.profileImg.profileId = pid;
  55. this.getList(pid);
  56. },
  57. methods: {
  58. getList(pid){
  59. allProfileImg(this.profileImg).then(res => {
  60. if(res.code !== 200){
  61. this.$message.error('获取照片列表失败')
  62. return
  63. }
  64. this.picList = [];
  65. var apiUrl = process.env.VUE_APP_BASE_API;
  66. var items = res.data;
  67. for (const i in items) {
  68. var url = apiUrl + items[i].url;
  69. this.picList.push({ key: items[i].id, value: url})
  70. }
  71. console.log( this.picList)
  72. })
  73. },
  74. uploadAvatar(item) {
  75. const formData = new FormData()
  76. const pid = this.profileImg.profileId;
  77. formData.append('file', item.file)
  78. formData.append('profileId', pid)
  79. const uid = item.file.uid
  80. uploadFile(formData).then(res => {
  81. this.emptyUpload()
  82. this.getList(pid)
  83. // this.picList.push({ key: uid, value: res.url })
  84. }).catch(() => {
  85. this.$message.error('上传失败,请重新上传')
  86. this.emptyUpload()
  87. })
  88. },
  89. beforeAvatarUpload(file) {
  90. const isJPG = file.type === 'image/jpeg';
  91. const isPng = file.type === 'image/png';
  92. const isLt2M = file.size / 1024 / 1024 < 2;
  93. if (!isJPG && !isPng) {
  94. this.$message.error('上传图片只能是 JPG或png 格式!')
  95. }
  96. if (!isLt2M) {
  97. this.$message.error('上传图片大小不能超过 2MB!')
  98. }
  99. return (isJPG || isPng) && isLt2M
  100. },
  101. handleRemove(file, fileList) {
  102. for (const i in this.picList) {
  103. if (this.picList[i].key === file.uid) {
  104. this.picList.splice(i, 1)
  105. }
  106. }
  107. },
  108. handlePictureCardPreview(file) {
  109. this.dialogImageUrl = file.url
  110. this.dialogVisible = true
  111. },
  112. /**
  113. * 清空上传组件
  114. */
  115. emptyUpload() {
  116. const mainImg = this.$refs.upload
  117. if (mainImg) {
  118. if (mainImg.length) {
  119. mainImg.forEach(item => {
  120. item.clearFiles()
  121. })
  122. } else {
  123. this.$refs.upload.clearFiles()
  124. }
  125. }
  126. },
  127. enter(index){
  128. this.num = index;
  129. },
  130. leave(){
  131. this.num = -1;
  132. },
  133. delImg(_id,index){
  134. var listItem = this.picList;
  135. const imgId = _id;
  136. this.$confirm('确认删除这张照片?', "警告", {
  137. confirmButtonText: "确定",
  138. cancelButtonText: "取消",
  139. type: "warning"
  140. }).then(function() {
  141. delProfileImg(imgId).then(res => {
  142. if(res.code === 200) {
  143. listItem.splice(index,1);
  144. this.picList = listItem;
  145. }
  146. })
  147. }).catch(function() {});
  148. }
  149. }
  150. }
  151. </script>
  152. <style>
  153. .father{
  154. position: relative;
  155. }
  156. .son{
  157. position: absolute;
  158. z-index: 999;
  159. width: 100%;
  160. background: rgba(0,0,0,0.5);
  161. bottom: 0;
  162. left: 0;
  163. right: 0;
  164. text-align: center;
  165. padding: 5px 0;
  166. }
  167. .disn{
  168. visibility: visible;
  169. }
  170. .el-upload-list--picture-card .el-upload-list__item {
  171. height: auto;
  172. width: 20%;
  173. vertical-align: middle;
  174. }
  175. .image {
  176. width: 100%;
  177. display: block;
  178. }
  179. .button {
  180. padding: 5px 0;
  181. display: flex;
  182. align-content: center;
  183. justify-items: center;
  184. }
  185. .clearfix:before, .clearfix:after {
  186. display: table;
  187. content: "";
  188. }
  189. .clearfix:after {
  190. clear: both
  191. }
  192. </style>