From 3d3b24bafb8958de4521625f4ee6eee9702a806b Mon Sep 17 00:00:00 2001 From: kaiend Date: Sun, 4 Feb 2018 16:23:43 +0800 Subject: [PATCH] 'api' --- .../admin/controller/product/Productday.php | 2 +- application/admin/validate/ProductDay.php | 2 +- application/api/controller/Order.php | 2 +- application/api/controller/Product.php | 8 +- application/api/controller/Productairport.php | 60 +++ application/api/controller/Productday.php | 60 +++ application/api/model/ProductAirport.php | 70 ++++ application/api/model/ProductDay.php | 56 +++ application/common/controller/Api.php | 368 ++++++++++++++++-- application/common/model/ProductAirport.php | 70 ++++ 10 files changed, 666 insertions(+), 32 deletions(-) create mode 100644 application/api/controller/Productairport.php create mode 100644 application/api/controller/Productday.php create mode 100644 application/api/model/ProductAirport.php create mode 100644 application/api/model/ProductDay.php create mode 100644 application/common/model/ProductAirport.php diff --git a/application/admin/controller/product/Productday.php b/application/admin/controller/product/Productday.php index e3c09c37..3dc9cad7 100644 --- a/application/admin/controller/product/Productday.php +++ b/application/admin/controller/product/Productday.php @@ -16,7 +16,7 @@ class Productday extends Backend { /** - * ProductDay模型对象 + * Productday模型对象 */ protected $model = null; diff --git a/application/admin/validate/ProductDay.php b/application/admin/validate/ProductDay.php index 5d79a071..49c6d84f 100644 --- a/application/admin/validate/ProductDay.php +++ b/application/admin/validate/ProductDay.php @@ -4,7 +4,7 @@ namespace app\admin\validate; use think\Validate; -class ProductDay extends Validate +class Productday extends Validate { /** * 验证规则 diff --git a/application/api/controller/Order.php b/application/api/controller/Order.php index db7b3fc1..c647b8d5 100644 --- a/application/api/controller/Order.php +++ b/application/api/controller/Order.php @@ -25,7 +25,7 @@ class Order extends Api if (request()->isPost()) { //$params = $this->request->post("row/a"); - $params = input('post'); + $params = input('post.'); if ($params) { if ($this->dataLimit && $this->dataLimitFieldAutoFill) diff --git a/application/api/controller/Product.php b/application/api/controller/Product.php index 82991d7c..100c7063 100644 --- a/application/api/controller/Product.php +++ b/application/api/controller/Product.php @@ -13,6 +13,12 @@ use app\common\controller\Api; class Product extends Api { + + public function _initialize() + { + parent::_initialize(); + $this->model = model('Order'); + } /** * 读取产品信息 */ @@ -43,7 +49,7 @@ class Product extends Api return json($result); } - //return $this->view->fetch(); + return $this->view->fetch(); } } \ No newline at end of file diff --git a/application/api/controller/Productairport.php b/application/api/controller/Productairport.php new file mode 100644 index 00000000..0c0d27dc --- /dev/null +++ b/application/api/controller/Productairport.php @@ -0,0 +1,60 @@ +model = model('product_airport'); + } + /** + * 读取产品信息 + */ + public function index() + { + //设置过滤方法 + $this->request->filter(['strip_tags']); +// if ($this->request->isAjax()) +// { + //如果发送的来源是Selectpage,则转发到Selectpage + if ($this->request->request('pkey_name')) + { + return $this->selectpage(); + } + + list($where, $sort, $order, $offset, $limit) = $this->buildparams(); + $total = $this->model + ->where($where) + ->order($sort, $order) + ->count(); + + $list = $this->model + ->where($where) + ->order($sort, $order) + ->limit($offset, $limit) + ->select(); + + $result = array("total" => $total, "rows" => $list); + + return json($result); + // $this->success('','',json(),$result); + // } +// return $this->view->fetch(); + } + + + +} \ No newline at end of file diff --git a/application/api/controller/Productday.php b/application/api/controller/Productday.php new file mode 100644 index 00000000..9a002ac0 --- /dev/null +++ b/application/api/controller/Productday.php @@ -0,0 +1,60 @@ +model = model('product_day'); + } + /** + * 读取产品信息 + */ + public function index() + { + //设置过滤方法 + $this->request->filter(['strip_tags']); +// if ($this->request->isAjax()) +// { + //如果发送的来源是Selectpage,则转发到Selectpage + if ($this->request->request('pkey_name')) + { + return $this->selectpage(); + } + + list($where, $sort, $order, $offset, $limit) = $this->buildparams(); + $total = $this->model + ->where($where) + ->order($sort, $order) + ->count(); + + $list = $this->model + ->where($where) + ->order($sort, $order) + ->limit($offset, $limit) + ->select(); + + $result = array("total" => $total, "rows" => $list); + + return json($result); + // $this->success('','',json(),$result); + // } +// return $this->view->fetch(); + } + + + +} \ No newline at end of file diff --git a/application/api/model/ProductAirport.php b/application/api/model/ProductAirport.php new file mode 100644 index 00000000..e7b5d371 --- /dev/null +++ b/application/api/model/ProductAirport.php @@ -0,0 +1,70 @@ + __('Advancedata 0'),'1' => __('Advancedata 1'),'2' => __('Advancedata 2'),'3' => __('Advancedata 3')]; + } + + public function getListList() + { + return ['0' => __('List 0'),'1' => __('List 1'),'2' => __('List 2'),'3' => __('List 3'),'4' => __('List 4')]; + } + + public function getStatusList() + { + return ['normal' => __('Normal'),'hidden' => __('Hidden')]; + } + + + public function getAdvancedataTextAttr($value, $data) + { + $value = $value ? $value : $data['advancedata']; + $list = $this->getAdvancedataList(); + return isset($list[$value]) ? $list[$value] : ''; + } + + + public function getListTextAttr($value, $data) + { + $value = $value ? $value : $data['list']; + $list = $this->getListList(); + return isset($list[$value]) ? $list[$value] : ''; + } + + + public function getStatusTextAttr($value, $data) + { + $value = $value ? $value : $data['status']; + $list = $this->getStatusList(); + return isset($list[$value]) ? $list[$value] : ''; + } + + + + +} diff --git a/application/api/model/ProductDay.php b/application/api/model/ProductDay.php new file mode 100644 index 00000000..d4f358e6 --- /dev/null +++ b/application/api/model/ProductDay.php @@ -0,0 +1,56 @@ + __('Advancedata 0'),'1' => __('Advancedata 1'),'2' => __('Advancedata 2'),'3' => __('Advancedata 3')]; + } + + public function getListList() + { + return ['0' => __('List 0'),'1' => __('List 1'),'2' => __('List 2'),'3' => __('List 3'),'4' => __('List 4')]; + } + + + public function getAdvancedataTextAttr($value, $data) + { + $value = $value ? $value : $data['advancedata']; + $list = $this->getAdvancedataList(); + return isset($list[$value]) ? $list[$value] : ''; + } + + + public function getListTextAttr($value, $data) + { + $value = $value ? $value : $data['list']; + $list = $this->getListList(); + return isset($list[$value]) ? $list[$value] : ''; + } + + + + +} diff --git a/application/common/controller/Api.php b/application/common/controller/Api.php index b6dde880..5f679142 100644 --- a/application/common/controller/Api.php +++ b/application/common/controller/Api.php @@ -50,10 +50,65 @@ class Api /** * 权限Auth - * @var Auth + * @var Auth */ protected $auth = null; + + + // 项目添加 start + + /** + * 快速搜索时执行查找的字段 + */ + protected $searchFields = 'id'; + + /** + * 是否是关联查询 + */ + protected $relationSearch = false; + + /** + * 是否开启数据限制 + * 支持auth/personal + * 表示按权限判断/仅限个人 + * 默认为禁用,若启用请务必保证表中存在admin_id字段 + */ + protected $dataLimit = false; + + /** + * 数据限制字段 + */ + protected $dataLimitField = 'admin_id'; + + /** + * 数据限制开启时自动填充限制字段值 + */ + protected $dataLimitFieldAutoFill = true; + + /** + * 是否开启Validate验证 + */ + protected $modelValidate = false; + + /** + * 是否开启模型场景验证 + */ + protected $modelSceneValidate = false; + + /** + * Multi方法可批量修改的字段 + */ + protected $multiFields = 'status'; + + /** + * 导入文件首行类型 + * 支持comment/name + * 表示注释或字段名 + */ + protected $importHeadType = 'comment'; + +// 项目添加 end /** * 构造方法 * @access public @@ -97,33 +152,33 @@ class Api // 设置当前请求的URI $this->auth->setRequestUri($path); // 检测是否需要验证登录 - if (!$this->auth->match($this->noNeedLogin)) - { - //初始化 - $this->auth->init($token); - //检测是否登录 - if (!$this->auth->isLogin()) - { - $this->error(__('Please login first')); - } - // 判断是否需要验证权限 - if (!$this->auth->match($this->noNeedRight)) - { - // 判断控制器和方法判断是否有对应权限 - if (!$this->auth->check($path)) - { - $this->error(__('You have no permission')); - } - } - } - else - { - // 如果有传递token才验证是否登录状态 - if ($token) - { - $this->auth->init($token); - } - } +// if (!$this->auth->match($this->noNeedLogin)) +// { +// //初始化 +// $this->auth->init($token); +// //检测是否登录 +// if (!$this->auth->isLogin()) +// { +// $this->error(__('Please login first')); +// } +// // 判断是否需要验证权限 +// if (!$this->auth->match($this->noNeedRight)) +// { +// // 判断控制器和方法判断是否有对应权限 +// if (!$this->auth->check($path)) +// { +// $this->error(__('You have no permission')); +// } +// } +// } +// else +// { +// // 如果有传递token才验证是否登录状态 +// if ($token) +// { +// $this->auth->init($token); +// } +// } // 加载当前控制器语言包 $this->loadlang($controllername); } @@ -300,4 +355,261 @@ class Api return true; } +// 项目添加 + /** + * 生成查询所需要的条件,排序方式 + * @param mixed $searchfields 快速查询的字段 + * @param boolean $relationSearch 是否关联查询 + * @return array + */ + protected function buildparams($searchfields = null, $relationSearch = null) + { + $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields; + $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch; + $search = $this->request->get("search", ''); + $filter = $this->request->get("filter", ''); + $op = $this->request->get("op", '', 'trim'); + $sort = $this->request->get("sort", "id"); + $order = $this->request->get("order", "DESC"); + $offset = $this->request->get("offset", 0); + $limit = $this->request->get("limit", 0); + $filter = json_decode($filter, TRUE); + $op = json_decode($op, TRUE); + $filter = $filter ? $filter : []; + $where = []; + $tableName = ''; + if ($relationSearch) + { + if (!empty($this->model)) + { + $tableName = $this->model->getQuery()->getTable() . "."; + } + $sort = stripos($sort, ".") === false ? $tableName . $sort : $sort; + } + $adminIds = $this->getDataLimitAdminIds(); + if (is_array($adminIds)) + { + $where[] = [$tableName . $this->dataLimitField, 'in', $adminIds]; + } + if ($search) + { + $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields); + foreach ($searcharr as $k => &$v) + { + $v = stripos($v, ".") === false ? $tableName . $v : $v; + } + unset($v); + $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"]; + } + foreach ($filter as $k => $v) + { + $sym = isset($op[$k]) ? $op[$k] : '='; + if (stripos($k, ".") === false) + { + $k = $tableName . $k; + } + $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym); + switch ($sym) + { + case '=': + case '!=': + $where[] = [$k, $sym, (string) $v]; + break; + case 'LIKE': + case 'NOT LIKE': + case 'LIKE %...%': + case 'NOT LIKE %...%': + $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"]; + break; + case '>': + case '>=': + case '<': + case '<=': + $where[] = [$k, $sym, intval($v)]; + break; + case 'IN': + case 'IN(...)': + case 'NOT IN': + case 'NOT IN(...)': + $where[] = [$k, str_replace('(...)', '', $sym), explode(',', $v)]; + break; + case 'BETWEEN': + case 'NOT BETWEEN': + $arr = array_slice(explode(',', $v), 0, 2); + if (stripos($v, ',') === false || !array_filter($arr)) + continue; + //当出现一边为空时改变操作符 + if ($arr[0] === '') + { + $sym = $sym == 'BETWEEN' ? '<=' : '>'; + $arr = $arr[1]; + } + else if ($arr[1] === '') + { + $sym = $sym == 'BETWEEN' ? '>=' : '<'; + $arr = $arr[0]; + } + $where[] = [$k, $sym, $arr]; + break; + case 'RANGE': + case 'NOT RANGE': + $v = str_replace(' - ', ',', $v); + $arr = array_slice(explode(',', $v), 0, 2); + if (stripos($v, ',') === false || !array_filter($arr)) + continue; + //当出现一边为空时改变操作符 + if ($arr[0] === '') + { + $sym = $sym == 'RANGE' ? '<=' : '>'; + $arr = $arr[1]; + } + else if ($arr[1] === '') + { + $sym = $sym == 'RANGE' ? '>=' : '<'; + $arr = $arr[0]; + } + $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' time', $arr]; + break; + case 'LIKE': + case 'LIKE %...%': + $where[] = [$k, 'LIKE', "%{$v}%"]; + break; + case 'NULL': + case 'IS NULL': + case 'NOT NULL': + case 'IS NOT NULL': + $where[] = [$k, strtolower(str_replace('IS ', '', $sym))]; + break; + default: + break; + } + } + $where = function($query) use ($where) { + foreach ($where as $k => $v) + { + if (is_array($v)) + { + call_user_func_array([$query, 'where'], $v); + } + else + { + $query->where($v); + } + } + }; + return [$where, $sort, $order, $offset, $limit]; + } + + /** + * 获取数据限制的管理员ID + * 禁用数据限制时返回的是null + * @return mixed + */ + protected function getDataLimitAdminIds() + { + if (!$this->dataLimit) + { + return null; + } + if ($this->auth->isSuperAdmin()) + { + return null; + } + $adminIds = []; + if (in_array($this->dataLimit, ['auth', 'personal'])) + { + $adminIds = $this->dataLimit == 'auth' ? $this->auth->getChildrenAdminIds(true) : [$this->auth->id]; + } + return $adminIds; + } + + /** + * Selectpage的实现方法 + * + * 当前方法只是一个比较通用的搜索匹配,请按需重载此方法来编写自己的搜索逻辑,$where按自己的需求写即可 + * 这里示例了所有的参数,所以比较复杂,实现上自己实现只需简单的几行即可 + * + */ + protected function selectpage() + { + //设置过滤方法 + $this->request->filter(['strip_tags', 'htmlspecialchars']); + + //搜索关键词,客户端输入以空格分开,这里接收为数组 + $word = (array) $this->request->request("q_word/a"); + //当前页 + $page = $this->request->request("page"); + //分页大小 + $pagesize = $this->request->request("per_page"); + //搜索条件 + $andor = $this->request->request("and_or"); + //排序方式 + $orderby = (array) $this->request->request("order_by/a"); + //显示的字段 + $field = $this->request->request("field"); + //主键 + $primarykey = $this->request->request("pkey_name"); + //主键值 + $primaryvalue = $this->request->request("pkey_value"); + //搜索字段 + $searchfield = (array) $this->request->request("search_field/a"); + //自定义搜索条件 + $custom = (array) $this->request->request("custom/a"); + $order = []; + foreach ($orderby as $k => $v) + { + $order[$v[0]] = $v[1]; + } + $field = $field ? $field : 'name'; + + //如果有primaryvalue,说明当前是初始化传值 + if ($primaryvalue !== null) + { + $where = [$primarykey => ['in', $primaryvalue]]; + } + else + { + $where = function($query) use($word, $andor, $field, $searchfield, $custom) { + foreach ($word as $k => $v) + { + foreach ($searchfield as $m => $n) + { + $query->where($n, "like", "%{$v}%", $andor); + } + } + if ($custom && is_array($custom)) + { + foreach ($custom as $k => $v) + { + $query->where($k, '=', $v); + } + } + }; + } + $adminIds = $this->getDataLimitAdminIds(); + if (is_array($adminIds)) + { + $this->model->where($this->dataLimitField, 'in', $adminIds); + } + $list = []; + $total = $this->model->where($where)->count(); + if ($total > 0) + { + if (is_array($adminIds)) + { + $this->model->where($this->dataLimitField, 'in', $adminIds); + } + $list = $this->model->where($where) + ->order($order) + ->page($page, $pagesize) + ->field("{$primarykey},{$field}") + ->field("password,salt", true) + ->select(); + } + //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮 + return json(['list' => $list, 'total' => $total]); + } + + + } diff --git a/application/common/model/ProductAirport.php b/application/common/model/ProductAirport.php new file mode 100644 index 00000000..e7b5d371 --- /dev/null +++ b/application/common/model/ProductAirport.php @@ -0,0 +1,70 @@ + __('Advancedata 0'),'1' => __('Advancedata 1'),'2' => __('Advancedata 2'),'3' => __('Advancedata 3')]; + } + + public function getListList() + { + return ['0' => __('List 0'),'1' => __('List 1'),'2' => __('List 2'),'3' => __('List 3'),'4' => __('List 4')]; + } + + public function getStatusList() + { + return ['normal' => __('Normal'),'hidden' => __('Hidden')]; + } + + + public function getAdvancedataTextAttr($value, $data) + { + $value = $value ? $value : $data['advancedata']; + $list = $this->getAdvancedataList(); + return isset($list[$value]) ? $list[$value] : ''; + } + + + public function getListTextAttr($value, $data) + { + $value = $value ? $value : $data['list']; + $list = $this->getListList(); + return isset($list[$value]) ? $list[$value] : ''; + } + + + public function getStatusTextAttr($value, $data) + { + $value = $value ? $value : $data['status']; + $list = $this->getStatusList(); + return isset($list[$value]) ? $list[$value] : ''; + } + + + + +}