1
0

images.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <div>
  3. <el-image v-if="item.url" :fit="'contain'" class="imgv" :z-index="50000" :src="baseUrl + item.url" :preview-src-list="[baseUrl + item.url]"></el-image>
  4. <el-dialog title="图片剪裁" :visible.sync="open" :close-on-click-modal="false" @close="close" append-to-body>
  5. <div style="height: 400px">
  6. <vue-cropper ref="cropper" :img="options.img" :info="true" :autoCrop="options.autoCrop" :fixedBox="options.fixedBox" :fixed="options.fixed" :full="options.full" :fixedNumber="fixed_number" :centerBox="options.centerBox" :outputSize="options.outputSize" />
  7. </div>
  8. <div slot="footer" class="dialog-footer">
  9. <div class="lsw_flex">
  10. <div class="f">
  11. <div class="lsw_flex">
  12. <div class="f4">
  13. <el-upload action="#" :http-request="requestUpload" :show-file-list="false" :before-upload="beforeUpload">
  14. <el-button icon="el-icon-scissors">重新选取</el-button>
  15. </el-upload>
  16. </div>
  17. <div class="f left"><el-button type="primary" icon="el-icon-upload" @click="uploadImg()">确认上传</el-button></div>
  18. </div>
  19. </div>
  20. <div class="f">
  21. <el-button-group v-show="options.img">
  22. <el-button icon="el-icon-plus" @click="changeScale(1)"></el-button>
  23. <el-button icon="el-icon-minus" @click="changeScale(-1)"></el-button>
  24. <el-button icon="el-icon-refresh-left" @click="rotateLeft()"></el-button>
  25. <el-button icon="el-icon-refresh-right" @click="rotateRight()"></el-button>
  26. </el-button-group>
  27. </div>
  28. </div>
  29. </div>
  30. </el-dialog>
  31. <el-upload action="#" :show-file-list="false" accept=".jpg,.jpeg,.png,.gif" :http-request="requestUpload" :before-upload="beforeUpload" style="text-align: center;margin-top: 10px;">
  32. <el-button icon="el-icon-picture-outline" type="primary" style="border-radius: 30px;">选取图片</el-button>
  33. </el-upload>
  34. </div>
  35. </template>
  36. <script>
  37. import { VueCropper } from 'vue-cropper';
  38. import { uploadFile } from '@/api/common';
  39. export default {
  40. components: { VueCropper },
  41. props: {
  42. item: {
  43. type: Object,
  44. default: {}
  45. },
  46. fixed_number: {
  47. type: Array,
  48. default: () => [6, 4.5]
  49. }
  50. },
  51. data() {
  52. return {
  53. open: false,
  54. visible: false, //是否显示cropper
  55. options: {
  56. limit: 1,
  57. img: '', //裁剪图片的地址
  58. autoCrop: true, //是否默认生成截图框
  59. outputSize: 0.8, //裁剪生成图片的质量
  60. /* fixedNumber: [21, 9], //截图框的宽高比例 */
  61. fixed: true, // 是否开启截图框宽高固定比例
  62. full: true, //是否输出原图比例的截图
  63. fixedBox: false, //固定截图框大小 不允许改变
  64. centerBox: true //截图框是否被限制在图片里面
  65. }
  66. };
  67. },
  68. methods: {
  69. editCropper(data) {
  70. this.open = true;
  71. this.visible = true;
  72. this.options.img = data;
  73. },
  74. // 向左旋转
  75. rotateLeft() {
  76. this.$refs.cropper.rotateLeft();
  77. },
  78. // 向右旋转
  79. rotateRight() {
  80. this.$refs.cropper.rotateRight();
  81. },
  82. // 图片缩放
  83. changeScale(num) {
  84. num = num || 1;
  85. this.$refs.cropper.changeScale(num);
  86. },
  87. // 覆盖默认的上传行为
  88. requestUpload() {},
  89. // 上传预处理
  90. beforeUpload(file) {
  91. if (file.type.indexOf('image/') == -1) {
  92. this.$modal.msgError('文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。');
  93. return;
  94. } else {
  95. const reader = new FileReader();
  96. reader.readAsDataURL(file);
  97. reader.onload = () => {
  98. this.options.img = reader.result;
  99. this.open = true;
  100. };
  101. }
  102. },
  103. // 上传图片
  104. uploadImg() {
  105. if (this.options.img == '') {
  106. this.$modal.msgError('请选取图片');
  107. return;
  108. }
  109. this.$refs.cropper.getCropBlob(data => {
  110. let formData = new FormData();
  111. formData.append('file', data);
  112. formData.append('uncompressed', true); //是否压缩
  113. uploadFile(formData)
  114. .then(response => {
  115. if (response.code === 200) {
  116. //this.$emit('input', response.fileName);
  117. this.item.url = response.fileName;
  118. this.ajax({ method: 'post', url: '/work/banner/edit', data: { id: this.item.id, url: response.fileName } }).then(response => {
  119. this.$modal.msgSuccess('上传成功');
  120. this.open = false;
  121. });
  122. }
  123. })
  124. .catch(() => {
  125. this.$modal.msgError('上传失败');
  126. });
  127. });
  128. },
  129. // 关闭弹窗表单重置
  130. close() {
  131. this.open = false;
  132. }
  133. }
  134. };
  135. </script>
  136. <style lang="scss">
  137. .imgv {
  138. width: 100%;
  139. height: 150px;
  140. border-radius: 5px;
  141. }
  142. .lsw_flex {
  143. display: flex;
  144. .f {
  145. flex: 1;
  146. }
  147. .left {
  148. float: left;
  149. margin-left: 10px;
  150. }
  151. }
  152. </style>