代码功能更新
This commit is contained in:
Executable
+138
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace laytp\traits;
|
||||
|
||||
use laytp\library\CommonFun;
|
||||
use think\facade\Config;
|
||||
use think\facade\Db;
|
||||
|
||||
trait Backend
|
||||
{
|
||||
/**
|
||||
* 列表
|
||||
* @return mixed
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$where = $this->buildSearchParams();
|
||||
$order = $this->buildOrder();
|
||||
$data = $this->model->where($where)->order($order);
|
||||
$paging = $this->request->param('paging', false);
|
||||
if ($paging) {
|
||||
$limit = $this->request->param('limit', Config::get('paginate.limit'));
|
||||
$data = $data->paginate($limit)->toArray();
|
||||
$data['data'] = $this->getSelectedData($data['data']);
|
||||
} else {
|
||||
$data = $data->select()->toArray();
|
||||
}
|
||||
return $this->success('数据获取成功', $data);
|
||||
}
|
||||
|
||||
//查看详情
|
||||
public function info()
|
||||
{
|
||||
$id = $this->request->param('id');
|
||||
$info = $this->model->find($id);
|
||||
return $this->success('获取成功', $info);
|
||||
}
|
||||
|
||||
//添加
|
||||
public function add()
|
||||
{
|
||||
$post = CommonFun::filterPostData($this->request->post());
|
||||
try {
|
||||
if ($this->model->create($post)) {
|
||||
return $this->success('添加成功', $post);
|
||||
} else {
|
||||
return $this->error('操作失败');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $this->exceptionError($e);
|
||||
}
|
||||
}
|
||||
|
||||
//编辑
|
||||
public function edit()
|
||||
{
|
||||
$id = $this->request->param('id');
|
||||
$info = $this->model->find($id);
|
||||
if (!$info) {
|
||||
return $this->error('ID参数错误');
|
||||
}
|
||||
$post = CommonFun::filterPostData($this->request->post());
|
||||
foreach ($post as $k => $v) {
|
||||
$info->$k = $v;
|
||||
}
|
||||
try {
|
||||
$updateRes = $info->save();
|
||||
if ($updateRes) {
|
||||
return $this->success('编辑成功');
|
||||
} else {
|
||||
return $this->error('编辑失败');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $this->exceptionError($e);
|
||||
}
|
||||
}
|
||||
|
||||
//删除
|
||||
public function del()
|
||||
{
|
||||
$ids = array_filter($this->request->post('ids'));
|
||||
if (!$ids) {
|
||||
return $this->error('参数ids不能为空');
|
||||
}
|
||||
try {
|
||||
if ($this->model->destroy($ids)) {
|
||||
return $this->success('数据删除成功');
|
||||
} else {
|
||||
return $this->error('数据删除失败');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $this->exceptionError($e);
|
||||
}
|
||||
}
|
||||
|
||||
//回收站
|
||||
public function recycle()
|
||||
{
|
||||
$where = $this->buildSearchParams();
|
||||
$order = $this->buildOrder();
|
||||
$data = $this->model->onlyTrashed()->where($where)->order($order);
|
||||
$limit = $this->request->param('limit', Config::get('paginate.limit'));
|
||||
$data = $data->paginate($limit)->toArray();
|
||||
$data['data'] = $this->getSelectedData($data['data']);
|
||||
return $this->success('回收站数据获取成功', $data);
|
||||
}
|
||||
|
||||
//还原
|
||||
public function restore()
|
||||
{
|
||||
$where[] = ['id', 'in', $this->request->post('ids')];
|
||||
if ($this->model->restore($where)) {
|
||||
return $this->success('数据成功还原');
|
||||
} else {
|
||||
return $this->error('操作失败');
|
||||
}
|
||||
}
|
||||
|
||||
//真实删除
|
||||
public function trueDel()
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
$ids = $this->request->post('ids');
|
||||
$res = $this->model->onlyTrashed()->where('id', 'in', $ids)->select();
|
||||
foreach ($res as $key => $item) {
|
||||
$delRes = $item->force()->delete();
|
||||
if (!$delRes) throw new \Exception('删除失败');
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
return $this->success('数据已经彻底删除');
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
return $this->error('数据库异常,操作失败');
|
||||
}
|
||||
}
|
||||
}
|
||||
Executable
+22
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
/**
|
||||
* 设置错误和获取错误公用方法定义,任何类都可以使用这两个方法
|
||||
*/
|
||||
|
||||
namespace laytp\traits;
|
||||
|
||||
trait Error
|
||||
{
|
||||
protected $_error = null;
|
||||
|
||||
public function getError()
|
||||
{
|
||||
return $this->_error;
|
||||
}
|
||||
|
||||
public function setError($error)
|
||||
{
|
||||
$this->_error = $error;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Executable
+111
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace laytp\traits;
|
||||
|
||||
use app\model\api\Log;
|
||||
|
||||
/**
|
||||
* 所有接口success和error返回数据格式定义
|
||||
*/
|
||||
trait JsonReturn
|
||||
{
|
||||
/**
|
||||
* 操作成功返回数据
|
||||
* @param string $msg
|
||||
* @param null $data
|
||||
* @return false|string|\think\response\Json
|
||||
*/
|
||||
public function success($msg = '', $data = null)
|
||||
{
|
||||
$result = [
|
||||
'code' => 0,
|
||||
'msg' => $msg,
|
||||
'time' => time(),
|
||||
'data' => $data ?? new \stdClass(),
|
||||
];
|
||||
//response返回的Content-Type:text/html; charset=utf-8,使用浏览器单独访问接口地址方便调试,
|
||||
//但是注意在javascript进行ajax请求时,要设置dataType: "json",这样,javascript得到字符串后,会自动使用JSON.parse对字符串进行解析
|
||||
// return json_encode($result,JSON_UNESCAPED_UNICODE);
|
||||
|
||||
//使用json方法进行返回的好处:
|
||||
// 1.客户端在进行ajax请求时,不需要设置dataType: "json",依旧会认为得到的数据是json
|
||||
// 2.APP_DEBUG = true,客户端得到的也仅仅是json部分数据,不会夹杂trace等debug代码
|
||||
// return json($result);
|
||||
|
||||
//记录Api请求日志
|
||||
if (app('http')->getName() === 'api') {
|
||||
Log::create([
|
||||
'rule' => request()->url(),
|
||||
'request_body' => json_encode(request()->post(), JSON_UNESCAPED_UNICODE),
|
||||
'request_header' => json_encode(request()->header(), JSON_UNESCAPED_UNICODE),
|
||||
'ip' => request()->ip(),
|
||||
'status_code' => 200,
|
||||
'response_body' => json_encode($result, JSON_UNESCAPED_UNICODE),
|
||||
'create_time' => date('Y-m-d H:i:s'),
|
||||
]);
|
||||
}
|
||||
|
||||
//最终做如下处理:在测试环境且传入的debug=1的参数,就返回html,否则返回json数据
|
||||
//在测试环境可以单独用浏览器打开接口地址,传入debug=1的参数对接口进行调试
|
||||
if (env('APP_DEBUG') && request()->param('debug')) {
|
||||
return json_encode($result, JSON_UNESCAPED_UNICODE);
|
||||
} else {
|
||||
return json($result);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 操作失败返回数据
|
||||
* @param string $msg
|
||||
* @param int $code 错误码,为0表示没有错误,为1表示常规错误,前端仅需提示msg,其他值有具体含义,比如10401为未登录,前端需要跳转至登录界面
|
||||
* @param array $data
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function error($msg = '', $code = 1, $data = null)
|
||||
{
|
||||
$result = [
|
||||
'code' => $code,
|
||||
'msg' => $msg,
|
||||
'time' => time(),
|
||||
'data' => $data ?? new \stdClass(),
|
||||
];
|
||||
|
||||
//记录Api请求日志
|
||||
if (app('http')->getName() === 'api') {
|
||||
Log::create([
|
||||
'rule' => request()->url(),
|
||||
'request_body' => json_encode(request()->post(), JSON_UNESCAPED_UNICODE),
|
||||
'request_header' => json_encode(request()->header(), JSON_UNESCAPED_UNICODE),
|
||||
'ip' => request()->ip(),
|
||||
'status_code' => 200,
|
||||
'response_body' => json_encode($result, JSON_UNESCAPED_UNICODE),
|
||||
'create_time' => date('Y-m-d H:i:s'),
|
||||
]);
|
||||
}
|
||||
|
||||
if (env('APP_DEBUG') && request()->param('debug')) {
|
||||
return json_encode($result, JSON_UNESCAPED_UNICODE);
|
||||
} else {
|
||||
return json($result);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 捕获异常后返回数据
|
||||
* @param $e \Exception
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function exceptionError($e){
|
||||
$data = [];
|
||||
if(env('APP_DEBUG')){
|
||||
$data = [
|
||||
'message' => $e->getMessage(),
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine(),
|
||||
'trace' => $e->getTrace()
|
||||
];
|
||||
}
|
||||
return $this->error($e->getMessage(), 1, $data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user