396 lines
17 KiB
PHP
396 lines
17 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\EventImprovedHealthCheckRequest;
|
|
use App\Http\Requests\EventImprovedHealthRequest;
|
|
use App\Models\EventImprovedHealth;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
use Carbon\Carbon;
|
|
use App\Mail\EventNotifyMail;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use App\Models\EventMetabolism;
|
|
use App\Models\EventRegistrationInfo;
|
|
|
|
use Exception;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Intervention\Image\ImageManager;
|
|
use Intervention\Image\Drivers\Gd\Driver;
|
|
|
|
class ImprovedHealthFillFormController extends Controller
|
|
{
|
|
/**
|
|
* Handle the incoming request.
|
|
*/
|
|
public function __invoke(Request $request)
|
|
{
|
|
//
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$enable = true;
|
|
$eventInfo = Cache::get('event-info-cache-2', function () {
|
|
$entry = EventRegistrationInfo::where('id', 2)->first();
|
|
if ($entry) {
|
|
return ['enable' => $entry->enable,'limit' => $entry->limit, 'start_at' => $entry->event_start_date, 'end_at' => $entry->event_end_date ];
|
|
}
|
|
return null;
|
|
});
|
|
|
|
if ($eventInfo) {
|
|
if ($eventInfo['enable'] != "1") {
|
|
$enable = false;
|
|
}
|
|
$date_now = new \DateTime("now");
|
|
$date_start = new \DateTime($eventInfo['start_at']);
|
|
$date_end = new \DateTime($eventInfo['end_at']);
|
|
|
|
if ($date_now > $date_start && $date_end > $date_now ) {
|
|
//
|
|
} else {
|
|
$enable = false;
|
|
}
|
|
}
|
|
|
|
return view('improved_health', [
|
|
'enable' => $enable
|
|
]);
|
|
}
|
|
|
|
public function doGet(Request $request)
|
|
{
|
|
return view('improved_health_fill_form');
|
|
}
|
|
|
|
public function doPostOk(Request $request)
|
|
{
|
|
return view('improved_health_fill_form_ok');
|
|
}
|
|
|
|
public function doPost(EventImprovedHealthRequest $request)
|
|
{
|
|
try
|
|
{
|
|
$eventInfo = Cache::get('event-info-cache-2', function () {
|
|
$entry = EventRegistrationInfo::where('id', 2)->first();
|
|
if ($entry) {
|
|
return ['enable' => $entry->enable,'limit' => $entry->limit, 'start_at' => $entry->event_start_date, 'end_at' => $entry->event_end_date ];
|
|
}
|
|
return null;
|
|
});
|
|
|
|
if ($eventInfo) {
|
|
if ($eventInfo['enable'] != "1") {
|
|
throw new \Exception('活動已截止。');
|
|
}
|
|
$date_now = new \DateTime("now");
|
|
$date_start = new \DateTime($eventInfo['start_at']);
|
|
$date_end = new \DateTime($eventInfo['end_at']);
|
|
|
|
if ($date_now > $date_start && $date_end > $date_now ) {
|
|
//
|
|
} else {
|
|
throw new \Exception('活動已截止。');
|
|
}
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
|
|
//$checkPhone = EventMetabolism::where('phone', '=', $request->input('phone'))->first();
|
|
//if ($checkPhone) {
|
|
// throw new \Exception('本電話號碼已經登記。');
|
|
//}
|
|
$twid = Str::upper($request->input('twid'));
|
|
$eventItem = EventImprovedHealth::where('twid', '=', $twid)->first();
|
|
if (!$eventItem) {
|
|
$eventItem = new EventImprovedHealth();
|
|
$eventItem->twid = $twid;
|
|
}
|
|
if ($eventItem->check_state == 1) {
|
|
throw new \Exception('您已經登記審查,請等待審查完成。');
|
|
}
|
|
if ($eventItem->check_state == 2) {
|
|
throw new \Exception('您已經登記審查通過。');
|
|
}
|
|
$eventItem->hospital_name = $request->input('hospital_name');
|
|
$eventItem->name = $request->input('name');
|
|
$eventItem->phone = $request->input('phone');
|
|
$eventItem->email = $request->input('email');
|
|
$eventItem->address = $request->input('address');
|
|
|
|
$uploadPath = 'uploads/improved_health';
|
|
$acceptExt = ['jpg', 'jpeg', 'png', 'pdf'];
|
|
$finfo = new \finfo(FILEINFO_MIME_TYPE);
|
|
$allowedMimeTypes = [
|
|
'image/png', // PNG
|
|
'image/jpeg', // JPG/JPEG
|
|
'application/pdf' // PDF
|
|
];
|
|
$manager = new ImageManager(Driver::class);
|
|
$uploadTime = time();
|
|
|
|
$fileName = $request->input('fileRecordCardFileName');
|
|
if ($fileName && $request->filled('fileRecordCardBase64')) {
|
|
$fileBase64 = $request->input('fileRecordCardBase64');
|
|
$fileData = base64_decode($fileBase64);
|
|
$fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
|
|
if (!in_array($fileExt, $acceptExt)) {
|
|
throw new \Exception('記錄小卡檔案 副檔名錯誤,只接受.jpg/.jpeg/.png/.pdf');
|
|
}
|
|
$mimeType = $finfo->buffer($fileData);
|
|
if (!in_array($mimeType, $allowedMimeTypes)) {
|
|
throw new \Exception('記錄小卡檔案 檔案類型錯誤,只接受.jpg/.jpeg/.png/.pdf');
|
|
}
|
|
if ($mimeType != 'application/pdf') {
|
|
$image = $manager->read($fileData);
|
|
$image->scaleDown(2048, 2048);
|
|
$fileData = $image->toJpeg(50);
|
|
$fileExt = 'jpg';
|
|
}
|
|
if ($eventItem->record_card_img_src) {
|
|
try {
|
|
Storage::disk('local')->delete($eventItem->record_card_img_src);
|
|
} catch (\Exception $ex2) {
|
|
|
|
}
|
|
}
|
|
$fileName = $eventItem->twid . "_RecordCard_" . $uploadTime . "." . $fileExt;
|
|
Storage::disk('local')->put($uploadPath. '/' . $fileName, $fileData);
|
|
$eventItem->record_card_img_src = $uploadPath.'/'.$fileName;
|
|
} else {
|
|
throw new \Exception('請上傳記錄小卡。');
|
|
}
|
|
|
|
$fileName = $request->input('fileTwidFrontFileName');
|
|
if ($fileName && $request->filled('fileTwidFrontBase64')) {
|
|
$fileBase64 = $request->input('fileTwidFrontBase64');
|
|
$fileData = base64_decode($fileBase64);
|
|
$fileExt = pathinfo($fileName, PATHINFO_EXTENSION);
|
|
if (!in_array($fileExt, $acceptExt)) {
|
|
throw new \Exception('身份證正面檔案 副檔名錯誤,只接受.jpg/.jpeg/.png/.pdf');
|
|
}
|
|
$mimeType = $finfo->buffer($fileData);
|
|
if (!in_array($mimeType, $allowedMimeTypes)) {
|
|
throw new \Exception('身份證正面檔案 檔案類型錯誤,只接受.jpg/.jpeg/.png/.pdf');
|
|
}
|
|
if ($mimeType != 'application/pdf') {
|
|
$image = $manager->read($fileData);
|
|
$image->scaleDown(2048, 2048);
|
|
$fileData = $image->toJpeg(50);
|
|
$fileExt = 'jpg';
|
|
}
|
|
if ($eventItem->twid_front_img_src) {
|
|
try {
|
|
Storage::disk('local')->delete($eventItem->twid_front_img_src);
|
|
} catch (\Exception $ex2) {
|
|
|
|
}
|
|
}
|
|
$fileName = $eventItem->twid . "_TwidFront_" . $uploadTime . "." . $fileExt;
|
|
Storage::disk('local')->put($uploadPath. '/' . $fileName, $fileData);
|
|
$eventItem->twid_front_img_src = $uploadPath.'/'.$fileName;
|
|
} else {
|
|
throw new \Exception('請上傳身份證正面。');
|
|
}
|
|
|
|
$fileName = $request->input('fileTwidBackFileName');
|
|
if ($fileName && $request->filled('fileTwidBackBase64')) {
|
|
$fileBase64 = $request->input('fileTwidBackBase64');
|
|
$fileData = base64_decode($fileBase64);
|
|
$fileExt = pathinfo($fileName, PATHINFO_EXTENSION);
|
|
if (!in_array($fileExt, $acceptExt)) {
|
|
throw new \Exception('身份證背面檔案 副檔名錯誤,只接受.jpg/.jpeg/.png/.pdf');
|
|
}
|
|
$mimeType = $finfo->buffer($fileData);
|
|
if (!in_array($mimeType, $allowedMimeTypes)) {
|
|
throw new \Exception('身份證背面檔案 檔案類型錯誤,只接受.jpg/.jpeg/.png/.pdf');
|
|
}
|
|
if ($mimeType != 'application/pdf') {
|
|
$image = $manager->read($fileData);
|
|
$image->scaleDown(2048, 2048);
|
|
$fileData = $image->toJpeg(50);
|
|
$fileExt = 'jpg';
|
|
}
|
|
if ($eventItem->twid_back_img_src) {
|
|
try {
|
|
Storage::disk('local')->delete($eventItem->twid_back_img_src);
|
|
} catch (\Exception $ex2) {
|
|
|
|
}
|
|
}
|
|
$fileName = $eventItem->twid . "_TwidBack_" . $uploadTime . "." . $fileExt;
|
|
Storage::disk('local')->put($uploadPath. '/' . $fileName, $fileData);
|
|
$eventItem->twid_back_img_src = $uploadPath.'/'.$fileName;
|
|
} else {
|
|
throw new \Exception('請上傳身份證背面。');
|
|
}
|
|
|
|
$fileName = $request->input('fileExamFileName');
|
|
if ($fileName && $request->filled('fileExamBase64')) {
|
|
$fileBase64 = $request->input('fileExamBase64');
|
|
$fileData = base64_decode($fileBase64);
|
|
$fileExt = pathinfo($fileName, PATHINFO_EXTENSION);
|
|
if (!in_array($fileExt, $acceptExt)) {
|
|
throw new \Exception('檢查報告檔案 副檔名錯誤,只接受.jpg/.jpeg/.png/.pdf');
|
|
}
|
|
$mimeType = $finfo->buffer($fileData);
|
|
if (!in_array($mimeType, $allowedMimeTypes)) {
|
|
throw new \Exception('檢查報告檔案 檔案類型錯誤,只接受.jpg/.jpeg/.png/.pdf');
|
|
}
|
|
if ($mimeType != 'application/pdf') {
|
|
$image = $manager->read($fileData);
|
|
$image->scaleDown(2048, 2048);
|
|
$fileData = $image->toJpeg(50);
|
|
$fileExt = 'jpg';
|
|
}
|
|
if ($eventItem->exam_img_src) {
|
|
try {
|
|
Storage::disk('local')->delete($eventItem->exam_img_src);
|
|
} catch (\Exception $ex2) {
|
|
|
|
}
|
|
}
|
|
$fileName = $eventItem->twid . "_Exam_" . $uploadTime . "." . $fileExt;
|
|
Storage::disk('local')->put($uploadPath. '/' . $fileName, $fileData);
|
|
$eventItem->exam_img_src = $uploadPath.'/'.$fileName;
|
|
} else {
|
|
throw new \Exception('請上傳檢查報告。');
|
|
}
|
|
|
|
$fileName = $request->input('fileExam2FileName');
|
|
if ($fileName && $request->filled('fileExam2Base64')) {
|
|
$fileBase64 = $request->input('fileExam2Base64');
|
|
$fileData = base64_decode($fileBase64);
|
|
$fileExt = pathinfo($fileName, PATHINFO_EXTENSION);
|
|
if (!in_array($fileExt, $acceptExt)) {
|
|
throw new \Exception('檢查報告檔案 副檔名錯誤,只接受.jpg/.jpeg/.png/.pdf');
|
|
}
|
|
$mimeType = $finfo->buffer($fileData);
|
|
if (!in_array($mimeType, $allowedMimeTypes)) {
|
|
throw new \Exception('檢查報告檔案 檔案類型錯誤,只接受.jpg/.jpeg/.png/.pdf');
|
|
}
|
|
if ($mimeType != 'application/pdf') {
|
|
$image = $manager->read($fileData);
|
|
$image->scaleDown(2048, 2048);
|
|
$fileData = $image->toJpeg(50);
|
|
$fileExt = 'jpg';
|
|
}
|
|
if ($eventItem->exam2_img_src) {
|
|
try {
|
|
Storage::disk('local')->delete($eventItem->exam2_img_src);
|
|
} catch (\Exception $ex2) {
|
|
|
|
}
|
|
}
|
|
$fileName = $eventItem->twid . "_Exam2_" . $uploadTime . "." . $fileExt;
|
|
Storage::disk('local')->put($uploadPath. '/' . $fileName, $fileData);
|
|
$eventItem->exam2_img_src = $uploadPath.'/'.$fileName;
|
|
}
|
|
|
|
// $file = $request->file('fileRecordCard');
|
|
// if ($file) {
|
|
// $fileExt = $file->getClientOriginalExtension();
|
|
// $fileName = $eventItem->twid . "_RecordCard." . $fileExt;
|
|
// $path = $file->storeAs($uploadPath, $fileName);
|
|
// $eventItem->record_card_img_src = $uploadPath.'/'.$fileName;
|
|
// } else {
|
|
// throw new \Exception('請上傳記錄小卡。');
|
|
// }
|
|
|
|
// $file = $request->file('fileTwidFront');
|
|
// if ($file) {
|
|
// $fileExt = $file->getClientOriginalExtension();
|
|
// $fileName = $eventItem->twid . "_TwidFront." . $fileExt;
|
|
// $path = $file->storeAs($uploadPath, $fileName);
|
|
// $eventItem->twid_front_img_src = $uploadPath.'/'.$fileName;
|
|
// } else {
|
|
// throw new \Exception('請上傳身份證正面。');
|
|
// }
|
|
|
|
// $file = $request->file('fileTwidBack');
|
|
// if ($file) {
|
|
// $fileExt = $file->getClientOriginalExtension();
|
|
// $fileName = $eventItem->twid . "_TwidBack." . $fileExt;
|
|
// $path = $file->storeAs($uploadPath, $fileName);
|
|
// $eventItem->twid_back_img_src = $uploadPath.'/'.$fileName;
|
|
// } else {
|
|
// throw new \Exception('請上傳身份證背面。');
|
|
// }
|
|
|
|
// $file = $request->file('fileExam');
|
|
// if ($file) {
|
|
// $fileExt = $file->getClientOriginalExtension();
|
|
// $fileName = $eventItem->twid . "_Exam." . $fileExt;
|
|
// $path = $file->storeAs($uploadPath, $fileName);
|
|
// $eventItem->exam_img_src = $uploadPath.'/'.$fileName;
|
|
// } else {
|
|
// throw new \Exception('請上傳檢查報告。');
|
|
// }
|
|
|
|
$eventItem->check_state = 1;
|
|
$eventItem->save();
|
|
|
|
DB::commit();
|
|
|
|
try {
|
|
Mail::to($request->input('email'))->send(new EventNotifyMail([
|
|
"title" => "健康達標GO 大獎汽車不是夢 登記成功!",
|
|
"body" => "登記資料:"
|
|
."\n流水號: B-".str_pad($eventItem->id, 6, '0', STR_PAD_LEFT)
|
|
."\n就醫院所:".$request->input('hospital_name')
|
|
."\n姓名:".$request->input('name')
|
|
."\n身份證字號:".substr($request->input('twid'),0,2)."xxxx".substr($request->input('twid'),-4)
|
|
."\n行動電話:".$request->input('phone')
|
|
."\n電子信箱:".$request->input('email')
|
|
."\n收件地址:".$request->input('address')
|
|
,
|
|
]));
|
|
} catch (Exception $ex) {
|
|
|
|
}
|
|
}
|
|
catch (\Exception $ex) {
|
|
DB::rollBack();
|
|
return redirect()->back()->withErrors([$ex->getMessage()])->withInput(Request()->all());
|
|
}
|
|
//return redirect()->back()->with('success', '登記成功!');
|
|
return view('improved_health_fill_form_ok');
|
|
}
|
|
|
|
public function doCheckGet(Request $request)
|
|
{
|
|
return view('improved_health_check');
|
|
}
|
|
|
|
public function doCheckPost(EventImprovedHealthCheckRequest $request)
|
|
{
|
|
$rejectReason = '';
|
|
try
|
|
{
|
|
|
|
$twid = Str::upper($request->input('twid'));
|
|
$phone = $request->input('phone');
|
|
$eventItem = EventImprovedHealth::where('twid', 'like', '%'.$twid)->where('phone', 'like', '%'.$phone)->first();
|
|
if (!$eventItem) {
|
|
throw new \Exception('找不到您登記的資料,請確認是否資訊正確。');
|
|
}
|
|
if ($eventItem->check_state == 1) {
|
|
throw new \Exception('您已經登記審查,請等待審查完成。');
|
|
}
|
|
if ($eventItem->check_state == 2) {
|
|
$rejectReason = $eventItem->check_reson;
|
|
throw new \Exception('您的審查未通過,請依以下原因說明重新登記。');
|
|
}
|
|
|
|
}
|
|
catch (\Exception $ex) {
|
|
return redirect()->back()->withErrors([$ex->getMessage(), $rejectReason])->withInput(Request()->all());
|
|
}
|
|
return redirect()->back()->with('success', '您已審格通過!');
|
|
}
|
|
}
|