You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.8 KiB

<?php
/**
* Created by PhpStorm.
* User: weizongsong
* Date: 2019-04-12
* Time: 22:34
*/
namespace App\Http\Controllers\Admin;
use App\Exports\ParamedicExport;
use App\Forms\ParamedicForm;
use App\Http\Controllers\Customer\AuthController;
use App\Models\Paramedic;
use App\Models\ParamedicLevel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use Kris\LaravelFormBuilder\Field;
use Kris\LaravelFormBuilder\FormBuilder;
use Maatwebsite\Excel\Facades\Excel;
class ParamedicController extends CommonController
{
public $bladePath = "admin.paramedic";
public $urlPrefix = "admin/paramedic";
public $modelName = "护工";
public $modelClass = Paramedic::class;
public $formClass = ParamedicForm::class;
public function index(Request $request)
{
$data = $this->model->with("project");
if (request()->keyword) {
$data->where(function ($query) {
$query->where("name", "like", "%" . request()->keyword . "%")
->orWhere("id_card_number", "like", "%" . request()->keyword . "%");
});
}
$data = $data->paginate(10);
return view($this->bladePath . ".index", compact("data"));
}
public function getLevels(Request $request)
{
$levels = (new ParamedicLevel())->where("project_id", $request->project_id)->get();
return $this->ajaxResponse($levels);
}
public function edit($id = null, Request $request, FormBuilder $formBuilder)
{
$vo = $this->model->find($id ?: $request->id);
$form = $formBuilder->create($this->formClass, [
"method" => "POST",
"id" => "fm",
"url" => url($this->urlPrefix . "/update/" . $vo->id),
"class" => "form form-horizontal validate-form",
"model" => $vo
]);
$form->modify("paramedic_level_id", "select", ["label" => "护工等级", "value" => $vo->paramedic_level_id, "choices" => (new ParamedicLevel())->where("project_id", $vo->project_id)->get()->pluck("name", "id")->toArray()]);
$form->add("_previous", "hidden", ["value" => (url()->previous())]);
return view($this->bladePath . ".create", compact("form"));
}
public function getQrcode($id)
{
$paramedic = Paramedic::find($id);
$access_token = (new AuthController())->getAccessToken();
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token;
$scene = "paramedic_" . $id;
$res = curl($url, json_encode([
"scene" => $scene,
"page" => "packages/pagesProduct/pages/productSelect/productSelect"
]), false);
if (is_null(json_decode($res, true))) {
$file = storage_path() . "/app/public/" . "paramedic_" . $id . ".jpg";
$img = Image::canvas(320, 320);
$img->save($file);
$qrcode = fopen($file, "w");//创建件准备写入
fwrite($qrcode, $res);//写入
fclose($qrcode);//关闭
$img = Image::make($file);
if ($paramedic->avatar) {
$img = $img->resizeCanvas(null, $img->height() + 100, "top")->save();
$avatar = base_path() . str_replace("storage", "storage/app/public", $paramedic->avatar);
$avatar = Image::make($avatar)->resize(null, $img->height(), function ($constraint) {
$constraint->aspectRatio();
});
return $img->response();
} else {
return $img->response();
}
} else {
return $this->error(json_decode($res, true)["errmsg"]);
}
}
public function export()
{
return Excel::download(new ParamedicExport(), "paramedics.xlsx");
}
}