index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <div class="custom-upload">
  3. <el-dialog
  4. title="图片裁剪"
  5. :visible.sync="showCropper"
  6. top="6vh"
  7. width="50%"
  8. height="700"
  9. class="cropper-dialog"
  10. center
  11. append-to-body
  12. >
  13. <vue-cropper
  14. v-if="showCropper"
  15. id="corpper"
  16. ref="cropper"
  17. :class="{'corpper-warp':showCropper}"
  18. v-bind="cropper"
  19. :style="{height:`${height}px`}"
  20. />
  21. <div v-if="showCropper" class="cropper-button">
  22. <el-button class="cancel-btn" size="small" @click.native="showCropper=false">取消</el-button>
  23. <el-button size="small" type="primary" :loading="loading" @click="uploadCover">完成</el-button>
  24. </div>
  25. </el-dialog>
  26. <input
  27. :id="id"
  28. type="file"
  29. style="display: none"
  30. name="single"
  31. accept="image/*"
  32. @change="onChange($event)"
  33. />
  34. <div class="avatar-uploader" @click="handleOpenFile()">
  35. <div class="el-upload el-upload--text">
  36. <div v-if="img" class="imgdiv">
  37. <img
  38. style="width: 100%;height:100%;"
  39. :src="$serverName.replace('/zhoushan','') + img"
  40. >
  41. <i class="el-icon-circle-close" @click.stop="clearImg"></i>
  42. </div>
  43. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  44. </div>
  45. </div>
  46. <div v-if="tips" class="tips clear-margin-top">{{ tips }}</div>
  47. </div>
  48. </template>
  49. <script>
  50. // 上传文件组件
  51. import { VueCropper } from 'vue-cropper'
  52. // 定义的接口根据自己项目更换
  53. import { isImageFile, isMaxFileSize, readFile } from '@/utils/upload' // 见下文
  54. import { Message } from 'element-ui'
  55. export default {
  56. components: {
  57. VueCropper
  58. },
  59. props: {
  60. // 最大上传文件的大小
  61. maxFileSize: {
  62. type: Number,
  63. default: 1024 // (MB)
  64. },
  65. uploadUrl: {
  66. type: String
  67. },
  68. img: {
  69. type: String
  70. },
  71. // 提示内容
  72. tips: {
  73. type: String
  74. },
  75. // 图片裁剪比列
  76. fixedNumber: {
  77. type: Array,
  78. default: function () {
  79. return []
  80. }
  81. },
  82. // 图片文件分辨率的宽度
  83. width: {
  84. type: Number,
  85. default: 8640
  86. },
  87. // 图片文件分辨率的高度
  88. height: {
  89. type: Number,
  90. default: 1920
  91. }
  92. },
  93. data () {
  94. return {
  95. id: 'cropper-input-' + new Date(),
  96. loading: false,
  97. showCropper: false,
  98. imgName: '',
  99. cropper: {
  100. img: '',
  101. info: true,
  102. size: 1024 * 4,
  103. outputType: 'jpg',
  104. canScale: true,
  105. autoCrop: true,
  106. full: true,
  107. // 只有自动截图开启 宽度高度才生效
  108. autoCropWidth: this.width,
  109. autoCropHeight: this.height,
  110. fixedBox: false,
  111. // 开启宽度和高度比例
  112. fixed: true,
  113. fixedNumber: this.fixedNumber,
  114. original: false,
  115. canMoveBox: true,
  116. canMove: true
  117. }
  118. }
  119. },
  120. methods: {
  121. // 打开文件
  122. clearImg () {
  123. this.$emit('clearImg')
  124. },
  125. handleOpenFile () {
  126. const input = document.getElementById(this.id)
  127. // 解决同一个文件不能监听的问题
  128. input.addEventListener(
  129. 'click',
  130. function () {
  131. this.value = ''
  132. },
  133. false
  134. )
  135. // 点击input
  136. input.click()
  137. },
  138. // 裁剪input 监听
  139. async onChange (e) {
  140. const file = e.target.files[0]
  141. if (!file) {
  142. return Message.error('选择图片失败')
  143. }
  144. // 验证文件类型
  145. if (!isImageFile(file)) {
  146. return
  147. }
  148. try {
  149. // 读取文件
  150. const src = await readFile(file)
  151. this.imgName = file.name
  152. this.showCropper = true
  153. this.cropper.img = src
  154. } catch (error) {
  155. console.log(error)
  156. }
  157. },
  158. // 封面上传功能
  159. uploadCover () {
  160. this.$refs.cropper.getCropBlob(async imgRes => {
  161. try {
  162. // 文件大小限制
  163. if (!isMaxFileSize(imgRes, this.maxFileSize)) {
  164. return
  165. }
  166. this.loading = true
  167. let formData = new FormData()
  168. formData.append('file', imgRes)
  169. formData.append('filename', this.imgName)
  170. const res = await this.$http.post(
  171. this.uploadUrl,
  172. formData,
  173. {'Content-Type': 'multipart/form-data'})
  174. this.$emit('subUploadSucceed', res.data)
  175. Message.success('上传成功')
  176. this.loading = false
  177. this.showCropper = false
  178. } catch (error) {
  179. this.loading = false
  180. this.showCropper = false
  181. Message.error(error.data.message)
  182. }
  183. })
  184. }
  185. }
  186. }
  187. </script>
  188. <style lang="less" >
  189. #corpper {
  190. width: 90%;
  191. margin: 0 auto;
  192. background-image: none;
  193. background: #fff;
  194. z-index: 1002;
  195. height: 512px;
  196. }
  197. .cropper-dialog {
  198. height: 800px;
  199. text-align: center;
  200. .el-dialog__header {
  201. padding-top: 15px;
  202. }
  203. .el-dialog--center .el-dialog__body {
  204. padding-top: 0;
  205. padding-bottom: 15px;
  206. }
  207. .el-dialog {
  208. text-align: center;
  209. }
  210. }
  211. .cropper-button {
  212. z-index: 1003;
  213. text-align: center;
  214. margin-top: 20px;
  215. .el-button {
  216. font-size: 16px;
  217. cursor: pointer;
  218. text-align: center;
  219. }
  220. .cancel-btn {
  221. color: #373737;
  222. }
  223. .el-button:last-child {
  224. margin-left: 100px;
  225. }
  226. }
  227. .cropper-modal {
  228. background-color: rgba(0, 0, 0, 0.5) !important;
  229. }
  230. .custom-upload {
  231. .tips {
  232. margin-top: 10px;
  233. color: red;
  234. font-size: 12px;
  235. }
  236. .clear-margin-top {
  237. margin-top: 0;
  238. }
  239. }
  240. </style>
  241. <style>
  242. .avatar-uploader{
  243. width: 180px;
  244. max-height: 180px;
  245. }
  246. .avatar-uploader .el-upload {
  247. border-radius: 6px;
  248. cursor: pointer;
  249. position: relative;
  250. overflow: hidden;
  251. }
  252. .avatar-uploader .el-upload:hover {
  253. border-color: #409eff;
  254. }
  255. .avatar-uploader-icon {
  256. font-size: 28px;
  257. color: #8c939d;
  258. width: 178px;
  259. height: 178px;
  260. line-height: 178px;
  261. text-align: center;
  262. }
  263. .avatar {
  264. width: 178px;
  265. /* height: 178px; */
  266. display: block;
  267. }
  268. </style>