Last active
December 1, 2023 10:03
-
-
Save korrio/b743b3c4e7144ac5002e1ff7cb90b59b to your computer and use it in GitHub Desktop.
upload_slip.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function(){ | |
function decodeImageFromBase64(data, callback) { | |
// set callback | |
qrcode.callback = callback; | |
// Start decoding | |
qrcode.decode(data) | |
} | |
function readFile(event) { | |
console.log("readFile",readFile) | |
if (!this.files || !this.files[0]) return; | |
const FR = new FileReader(); | |
files_upload = event.target.files; | |
FR.addEventListener("load", function(evt) { | |
console.log("evt",evt); | |
document.querySelector("#img_preview").src = evt.target.result; | |
console.log('evt.target.result', evt.target.result); | |
imageURI = evt.target.result; | |
decodeImageFromBase64(imageURI, function(decodedInformation) { | |
raw_qr = (decodedInformation); | |
console.log('raw qr is ', raw_qr); | |
$('#qr_raw').val(raw_qr); | |
console.log('qr_raw', raw_qr); | |
}); | |
// document.querySelector("#b64").textContent = evt.target.result; | |
}); | |
// console.log('this.files[0]', this.files[0]); | |
FR.readAsDataURL(this.files[0]); | |
} | |
$("#qr_image").on('change',function(e){ | |
readFile(e); | |
}) | |
$(':file').on('change', function (e) { | |
var file = this.files[0]; | |
// if (file.size > 1024) { | |
// alert('max upload size is 1k'); | |
// } | |
readFile(e); | |
// Also see .name, .type | |
}); | |
document.querySelector("#qr_image").addEventListener("change", readFile); | |
$('#slip_upload').on('submit', (function(e) { | |
e.preventDefault(); | |
var formData = new FormData(this); | |
if (typeof files_upload !== 'undefined') { | |
console.log('files_upload', files_upload); | |
// alert('found image!'); | |
$.each(files_upload, function(key, value) { | |
formData.delete('qr_image'); | |
formData.append('qr_image', value); | |
}); | |
} else { | |
Swal.fire({ | |
position: 'center', | |
icon: 'error', | |
title: 'กรุณาเลือกไฟล์สลิปการโอนก่อน', | |
showConfirmButton: false, | |
timer: 10000 | |
}); | |
} | |
var xhr = new XMLHttpRequest(); | |
$.ajax({ | |
type: 'POST', | |
url: `${url}/deposit/verifyDepositSlip`, | |
data: formData, | |
cache: false, | |
contentType: false, | |
processData: false, | |
headers: { | |
"cache-control": "no-cache" | |
}, | |
success: function(data) { | |
console.log('data', data); | |
if (data.success == 0) { | |
Swal.fire({ | |
position: 'center', | |
icon: 'error', | |
title: 'เติมเงินไม่สำเร็จ กรุณาตรวจสอบสลิป หรือติดต่อแอดมินค่ะ', | |
text: data.error.message, | |
showConfirmButton: false, | |
timer: 10000 | |
}); | |
} else if (data && !data.success && data.message) { | |
Swal.fire({ | |
position: 'center', | |
icon: 'error', | |
title: data.message, | |
showConfirmButton: false, | |
timer: 10000 | |
}); | |
} else if (data && data.success) { | |
Swal.fire({ | |
position: 'center', | |
icon: 'success', | |
title: data.message, | |
showConfirmButton: false, | |
timer: 10000 | |
}); | |
location.reload(); | |
} | |
}, | |
error: function(data) { | |
if (data?.responseJSON?.message) { | |
Swal.fire({ | |
position: 'center', | |
icon: 'error', | |
title: data.responseJSON.message, | |
showConfirmButton: false, | |
timer: 10000 | |
}) | |
} | |
console.log(data); | |
} | |
}); | |
return false; | |
})); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment