Advertisement
Devastano

cropper

Apr 13th, 2020
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // utils.js
  2.  
  3.     b64toBlob(base64) {
  4.       return new Promise((resolve, reject) => {
  5.         fetch(base64)
  6.           .then(res => res.blob())
  7.           .then(res => resolve(res))
  8.           .catch(reject)
  9.       })
  10.     },
  11.     async asyncForEach(array, callback) {
  12.       for (let index = 0; index < array.length; index++) {
  13.         await callback(array[index], index, array);
  14.       }
  15.     },
  16.     async UrlToBase64(url) {
  17.       return new Promise((resolve, reject) => {
  18.         const xhr = new XMLHttpRequest();
  19.         xhr.onload = () => {
  20.             var reader = new FileReader();
  21.             reader.onloadend = () => resolve(reader.result)
  22.             reader.readAsDataURL(xhr.response);
  23.         };
  24.         xhr.open('GET', url);
  25.         xhr.responseType = 'blob';
  26.         xhr.send();
  27.       })
  28.     },
  29.     async UrlsToBase64(urls) {
  30.       let vm = this
  31.  
  32.       return Promise.all(urls.map(async item => await vm.UrlToBase64(item.url)))
  33.     },
  34.     getFileExtension(fileUpload) {
  35.       let split = fileUpload.name.split('.')
  36.       if (split.length >= 2) {
  37.         return split.pop();
  38.       } else {
  39.         return 'txt'
  40.       }
  41.     },
  42.  
  43. // outros métodos
  44.  
  45.     setMoneyValue(ref, value) {
  46.       this.$nextTick(function (){
  47.         value = this.formatMoney(value)
  48.         this.$refs[ref].$el.getElementsByTagName('input')[0].value = value
  49.         this.property[ref] = value
  50.       })
  51.     },
  52.     async setImage(e) {
  53.       const file = e.target.files[0];
  54.  
  55.       if(!file) return;
  56.  
  57.       const ext = this.getFileExtension(file)
  58.  
  59.       // Caso a imagem não esteja em formato válido
  60.       if(!ext.match(/(jpg|jpeg|png)$/i)) {
  61.         this.profile_picture = null
  62.         return this.$store.commit('setDialog', {
  63.                   message: 'Formato de imagem inválido. Formatos aceitos: jpg, jpeg, png',
  64.                   status: -1
  65.                 })
  66.       }
  67.  
  68.       const reader = new FileReader();
  69.       reader.onload = (event) => {
  70.         this.imgSrc = event.target.result;
  71.         this.$refs.cropper.replace(event.target.result);
  72.       }
  73.  
  74.       reader.readAsDataURL(file);
  75.  
  76.       this.add_photo_dialog = true;
  77.     },
  78.     saveCrop() {
  79.       this.property.files.push(this.$refs.cropper.getCroppedCanvas().toDataURL())
  80.  
  81.       this.add_photo_dialog = false
  82.  
  83.       this.carousel_model = this.property.files.length - 1
  84.     },
  85.  
  86. // created()
  87.  
  88.     const { data } = await this.axios.get('condominium-places')
  89.  
  90.     this.condominium_places = data
  91.  
  92.     if(!this.isNew) {
  93.       const { data } = await this.axios.get(`property/${this.id}`)
  94.  
  95.       this.property = {
  96.         ...data,
  97.         files: await this.UrlsToBase64(data.files),
  98.         places: data.places.map(item => item.condominium_places_id)
  99.       }
  100.  
  101.       this.setMoneyValue('rent_price', this.property.rent_price)
  102.       this.setMoneyValue('condominium_price', this.property.condominium_price)
  103.       this.setMoneyValue('iptu', this.property.iptu)
  104.       this.setMoneyValue('fire_insurance', this.property.fire_insurance)
  105.     }
  106.  
  107. // submit
  108.  
  109.       let formData = new FormData()
  110.  
  111.       const upload = async () => {
  112.         await this.asyncForEach(this.property.files, async(file, i) => {
  113.           let blob = await this.b64toBlob(file.base64)
  114.  
  115.           formData.append(`photo[${i}]`, blob)
  116.         })
  117.       }
  118.  
  119.       await upload()
  120.  
  121.       let property_id
  122.  
  123.       if(this.isNew) {
  124.         const { data } = await this.axios.post('property', req)
  125.  
  126.         property_id = data
  127.       }
  128.       else {
  129.         await this.axios.put(`property/${this.id}`, req)
  130.  
  131.         property_id = this.id
  132.       }
  133.  
  134.       await this.axios.post(`property/${property_id}/upload`,
  135.         formData,
  136.         {
  137.           headers: {
  138.             'Content-Type': 'multipart/form-data'
  139.           },
  140.         }
  141.       )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement