<?php
namespace app\controller;
use app\BaseController;
use think\helper\Arr;
use think\helper\Str;
use think\Request;
class Automatic
{
protected $model;
public function __construct(request $request)
{
$name = $request->param("model");
$name = ucwords($name);
$model_name = "\\app\\model\\" . $name;
$this->model = new $model_name();
}
public function index(Request $request)
{
$data = $request->param();
$model = $this->model;
if (isset($data["choice"])) {
$model = $this->choice($data);
}
if (isset($data["with"])) {
$model = $model->with(explode(",", $data["with"]));
}
if (isset($data["size"])) {
$model = $model->paginate($data["size"]);
} else {
$model = $model->select();
}
if (!$model->isEmpty()) {
return $this->success($model, "獲取成功");
} else {
return $this->error("沒有資料");
}
}
public function choice($data)
{
$search = json_decode($data['choice'], true);
$query = $this->model->when($search, function ($q) use ($search) {
foreach ($search as $key => $value) {
if (is_bool($value) || is_numeric($value) || is_string($value)) {
$q->where($key, $value);
} else if (is_array($value)) {
if (Str::startsWith(key($value), '$')) {
$op = key($value);
$val = $value[$op];
switch ($op) {
case '$eq':
$q->where($key, $val);
break;
case '$ne':
$q->where($key, '<>', $val);
break;
case '$gt':
$q->where($key, '>', $val);
break;
case '$lt':
$q->where($key, '<', $val);
break;
case '$gte':
$q->where($key, '>=', $val);
break;
case '$lte':
$q->where($key, '<=', $val);
break;
case '$starts':
$q->where($key, 'like', $val . '%');
break;
case '$ends':
$q->where($key, 'like', '%' . $val);
break;
case '$cont':
$q->where($key, 'like', '%' . $val . '%');
break;
case '$excl':
$q->where($key, 'not like', '%' . $val . '%');
break;
case '$in':
$q->whereIn($key, $val);
break;
case '$notin':
$q->whereNotIn($key, $val);
break;
case '$isnull':
$q->whereNull($key);
break;
case '$notnull':
$q->whereNotNull($key);
break;
case '$between':
$q->whereBetween($key, $val);
break;
case '$order':
$q->order($key, $val);
break;
}
}
}
}
});
return $query;
}
public function save(Request $request)
{
$data = $request->post();
try {
if (empty($data)) {
return $this->error("數不能為空");
}
$data["create_time"] = time();
$data["ip"] = ip2long(\request()->ip());
$save = $this->model->strict(false)->insertGetId($data);
if ($save) {
return $this->success($save, "新增成功");
} else {
return $this->error("新增失敗");
}
} catch (\Exception $exception) {
return $this->error($exception->getMessage());
}
}
public function multistage(Request $request)
{
$sava_Data = $this->save($request);
$get_data = $sava_Data->getData();
if ($get_data["code"] == 0) {
$array = $request->param("array");
foreach ($array as $item) {
$model_name = "\\app\\model\\" . $item["model_name"];
$model = new $model_name();
$item["object_id"] = $get_data["data"];
$item["create_time"] = time();
$item["ip"] = isset($item["ip"]) ? ip2long($item["ip"]) : ip2long(\request()->ip());
$save = $model->strict(false)->insertGetId($item);
if (!$save) {
return $this->error("新增失敗");
}
}
return $this->success("", "新增成功");
} else {
return $this->error($get_data["msg"]);
}
}
public function update($id, Request $request)
{
$model = $this->model->find($id);
if (!$model) {
return $this->error("資料錯誤");
}
$data = $request->param();
if (empty($data)) {
return $this->error("請選擇要修改的資料");
}
$update = $this->model->where("id", $id)->strict(false)->update($data);
if ($update) {
return $this->success("", "修改成功");
} else {
return $this->error("修改失敗");
}
}
public function edit($id, Request $request)
{
$data = $request->param();
$model = $this->model;
if (isset($data["with"])) {
$model = $this->model->with(explode(",", $data["with"]));
}
$model = $model->find($id);
if (!$model) {
return $this->error("沒有資料");
}
return $this->success($model, "獲取成功");
}
public function delete($id)
{
$del = $this->model->destroy($id);
if ($del) {
return $this->success("", "刪除成功");
} else {
return $this->error("刪除失敗");
}
}
protected function success($data, $msg, $code = 0)
{
return json(['code' => $code, 'msg' => $msg, 'data' => $data]);
}
protected function error($msg, $code = 1)
{
return json(['code' => $code, 'msg' => $msg]);
}
}
laravel和tp6的語法幾乎一樣,開發完這套自動化介面後,我已經閒一個星期了,並且bug直線下降到沒有,我是不是沒有用武之地了
本作品採用《CC 協議》,轉載必須註明作者和本文連結