增加部分api接口功能

This commit is contained in:
2020-02-18 10:21:17 +08:00
parent d4325e3016
commit 40c5e75558
10 changed files with 812 additions and 8 deletions

39
app/controller/Api.php Normal file
View File

@@ -0,0 +1,39 @@
<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\controller;
class Api extends Base {
public $middleware = [
'\app\http\middleware\Validate',
// 'sent\jwt\middleware\JWTAuth' => ['except' => ['login']],
'\app\http\middleware\ApiAuth',
'\app\http\middleware\Api',
// '\app\http\middleware\AllowCrossDomain',
];
protected $data = ['data' => [], 'code' => 0, 'msg' => ''];
protected function initialize() {
}
protected function success($msg, $url = '') {
$this->data['code'] = 1;
$this->data['msg'] = $msg;
$this->data['url'] = $url ? $url->__toString() : '';
return $this->data;
}
protected function error($msg, $url = '') {
$this->data['code'] = 0;
$this->data['msg'] = $msg;
$this->data['url'] = $url ? $url->__toString() : '';
return $this->data;
}
}

View File

@@ -0,0 +1,80 @@
<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\controller\api;
use app\controller\Api as ApiBase;
/**
* @title 接口功能
*/
class Api extends ApiBase {
public $filter_method = ['__construct'];
/**
* @title 功能列表
* @return [type] [description]
*/
public function index() {
$list = [];
$path = app()->getAppPath() . 'controller/api';
$classname = $this->scanFile($path);
foreach ($classname as $value) {
$class = "app\\controller\\api\\" . $value;
if (class_exists($class)) {
$reflection = new \ReflectionClass($class);
$group_doc = $this->Parser($reflection->getDocComment());
$method = $reflection->getMethods(\ReflectionMethod::IS_FINAL | \ReflectionMethod::IS_PUBLIC);
$group_doc['name'] = $value;
$item = [];
foreach ($method as $key => $v) {
if (!in_array($v->name, $this->filter_method)) {
$title_doc = $this->Parser($v->getDocComment());
if (isset($title_doc['title']) && $title_doc['title']) {
$item[] = array(
'url' => 'api/' . strtolower($value) . '/' . strtolower($v->name),
'name' => 'api/' . strtolower($value) . '/' . strtolower($v->name),
'method' => isset($title_doc['method']) ? strtoupper($title_doc['method']) : 'GET',
'title' => trim($title_doc['title']),
'group' => strtolower($value),
'status' => 1,
);
}
}
}
$group_doc['children'] = $item;
$list[] = $group_doc;
}
}
$this->data['data'] = $list;
return $this->data;
}
protected function scanFile($path) {
$result = array();
$files = scandir($path);
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
if (is_dir($path . '/' . $file)) {
$this->scanFile($path . '/' . $file);
} else {
$result[] = substr(basename($file), 0, -4);
}
}
}
return $result;
}
protected function Parser($text) {
$doc = new \doc\Doc();
return $doc->parse($text);
}
}

View File

@@ -0,0 +1,65 @@
<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\controller\api;
use app\controller\Api;
use app\model\Config as ConfigM;
/**
* @title 基础功能
*/
class Config extends Api {
/**
* @title 配置数据
* @param ConfigM $config [description]
* @return [type] [description]
*/
public function index(ConfigM $config) {
$param = $this->request->param();
$parse = isset($param['parse']) ? $param['parse'] : 1;
if (isset($param['parse']) && $param['parse'] == 1) {
$list = $config->getConfigList($this->request);
$this->data['data'] = $list;
}else{
$list = $config->getConfig($this->request);
$this->data['data'] = $list;
}
return $this->data;
}
/**
* @title 配置数据(树)
* @param ConfigM $config [description]
* @return [type] [description]
*/
public function tree(ConfigM $config) {
$list = $config->getConfigTree($this->request);
$this->data['data'] = $list;
return $this->data;
}
/**
* @title 配置更新
* @param ConfigM $config [description]
* @return [type] [description]
*/
public function save(ConfigM $config) {
$data = $this->request->post();
foreach ($data as $key => $value) {
$config->update(['value' => $value], ['name' => $key]);
}
$this->data['code'] = 1;
$this->data['msg'] = "更新成功!";
return $this->data;
}
}

View File

@@ -0,0 +1,167 @@
<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\controller\api;
use app\controller\Api;
use app\model\Department as DepartmentM;
use app\model\Role;
use sent\tree\Tree;
/**
* @title 部门管理
*/
class Department extends Api {
/**
* @title 部门列表
* @param DepartmentM $depart [description]
* @return [type] [description]
*/
public function index(DepartmentM $depart) {
$param = $this->request->param();
$tree = isset($param['tree']) ? $param['tree'] : 0;
$map = [];
if (isset($param['name']) && $param['name'] != '') {
$map[] = ['name', 'LIKE', '%' . $param['name'] . '%'];
}
if (isset($param['status']) && $param['status'] != '') {
$map[] = ['status', '=', $param['status']];
}
$list = $depart->where($map)->select()->toArray();
if($tree == 1){
$tree = (new Tree())->listToTree($list, 'id', 'pid', 'children');
}else{
$tree = (new Tree())->toFormatTree($list);
}
$this->data['data'] = $tree;
return $this->data;
}
/**
* @title 部门添加
* @method POST
* @param CustomerM $customer [description]
* @return [type] [description]
*/
public function add(DepartmentM $depart) {
$data = $this->request->post();
$result = $depart->save($data);
if (false !== $result) {
$this->data['code'] = 1;
$this->data['msg'] = '添加成功!';
}else{
$this->data['code'] = 0;
$this->data['msg'] = '添加失败!';
}
return $this->data;
}
/**
* @title 部门编辑
* @method POST
* @param CustomerM $customer [description]
* @return [type] [description]
*/
public function edit(DepartmentM $depart) {
$data = $this->request->post();
$result = $depart->update($data, ['id'=>$data['id']]);
if (false !== $result) {
$this->data['code'] = 1;
$this->data['msg'] = '修改成功!';
}else{
$this->data['code'] = 0;
$this->data['msg'] = '修改失败!';
}
return $this->data;
}
/**
* @title 角色列表
* @param Role $role [description]
* @return [type] [description]
*/
public function role(Role $role) {
$list = $role->getDataList($this->request)
->append(['status_text'])
->toArray();
$this->data['data'] = $list;
return $this->data;
}
/**
* @title 角色添加
* @method POST
* @param CustomerM $customer [description]
* @return [type] [description]
*/
public function addrole(Role $role) {
$data = $this->request->post();
$result = $role->save($data);
if (false !== $result) {
$this->data['code'] = 1;
$this->data['msg'] = '添加成功!';
}else{
$this->data['code'] = 0;
$this->data['msg'] = '添加失败!';
}
return $this->data;
}
/**
* @title 角色编辑
* @method POST
* @param CustomerM $customer [description]
* @return [type] [description]
*/
public function editrole(Role $role) {
$data = $this->request->post();
$result = $role->update($data, ['id'=>$data['id']]);
if (false !== $result) {
$this->data['code'] = 1;
$this->data['msg'] = '修改成功!';
}else{
$this->data['code'] = 0;
$this->data['msg'] = '修改失败!';
}
return $this->data;
}
/**
* @title 删除角色
* @method GET
* @param CustomerM $customer [description]
* @return [type] [description]
*/
public function delrole(Role $role) {
$param = $this->request->param();
if (!isset($param['id']) || !$param['id']) {
$this->data['code'] = 0;
$this->data['msg'] = '非法操作!';
}
$result = $role->where('id', $param['id'])->delete();
if (false !== $result) {
$this->data['code'] = 1;
$this->data['msg'] = '删除成功!';
}else{
$this->data['code'] = 0;
$this->data['msg'] = '删除失败!';
}
return $this->data;
}
}

View File

@@ -0,0 +1,70 @@
<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\controller\api;
use app\model\Member;
use think\facade\Event;
use think\Request;
/**
* @title 登录注册
*/
class Login {
protected $data = ['data' => [], 'code' => 0, 'msg' => ''];
protected $middleware = [
// \app\http\middleware\AllowCrossDomain::class,
'\app\http\middleware\Validate',
'\app\http\middleware\Api',
];
/**
* @title 登录
* @method POST
* @param Member $member [description]
* @param Request $request [description]
* @return [type] [description]
*/
public function index(Member $member, Request $request) {
$data = $member->login($request);
if (false !== $data) {
// 触发UserLogin事件 用于执行用户登录后的一系列操作
Event::trigger('UserLogin');
$this->data['code'] = 1;
$this->data['msg'] = '成功登录!';
$this->data['data'] = $data;
} else {
$this->data['code'] = 0;
$this->data['msg'] = $member->error;
}
return $this->data;
}
/**
* @title 注册
* @method POST
* @param Member $member [description]
* @param Request $request [description]
* @return [type] [description]
*/
public function register(Member $member, Request $request) {
$data = $member->register($request);
if (false !== $data) {
// 触发UserRegister事件 用于执行用户注册后的一系列操作
Event::trigger('UserRegister');
$this->data['data'] = $data;
} else {
$this->data['code'] = 0;
$this->data['msg'] = $member->error;
}
return $this->data;
}
}

277
app/controller/api/User.php Normal file
View File

@@ -0,0 +1,277 @@
<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\controller\api;
use app\controller\Api;
use app\model\Member;
use app\model\MemberLog;
use app\model\Role;
use app\model\RoleAccess;
use xin\helper\Str;
/**
* @title 用户管理
*/
class User extends Api {
/**
* @title 用户列表
* @param CustomerM $customer [description]
* @return [type] [description]
*/
public function index(Member $user) {
$list = $user->getUserList($this->request);
$this->data['data'] = $list;
return $this->data;
}
/**
* @title 用户详情
* @param CustomerM $customer [description]
* @return [type] [description]
*/
public function detail(Member $user) {
$info = $user->getUserDetail($this->request);
$this->data['data'] = $info;
return $this->data;
}
/**
* @title 用户添加
* @method POST
* @param CustomerM $customer [description]
* @return [type] [description]
*/
public function add(Member $user) {
$data = $this->request->post();
$data['salt'] = Str::random(6);
$result = $user->save($data);
if (false !== $result) {
$this->data['code'] = 1;
$this->data['msg'] = '添加成功!';
} else {
$this->data['code'] = 0;
$this->data['msg'] = '添加失败!';
}
return $this->data;
}
/**
* @title 用户编辑
* @method POST
* @param CustomerM $customer [description]
* @return [type] [description]
*/
public function edit(Member $user) {
$data = $this->request->post();
unset($data['password']);
if (isset($data['repassword']) && $data['repassword'] != '') {
$data['password'] = $data['repassword'];
$data['salt'] = Str::random(6);
}
$result = $user->update($data, ['uid' => $data['uid']]);
if (false !== $result) {
$this->data['code'] = 1;
$this->data['msg'] = '修改成功!';
} else {
$this->data['code'] = 0;
$this->data['msg'] = '修改失败!';
}
return $this->data;
}
/**
* @title 用户删除
* @method GET
* @param CustomerM $customer [description]
* @return [type] [description]
*/
public function delete(Member $user) {
$param = $this->request->param();
if (isset($param['id']) && $param['id'] != '') {
$result = $user->where('uid', $param['id'])->update(['status' => -1]);
if (false !== $result) {
$this->data['code'] = 1;
$this->data['msg'] = '成功删除!';
} else {
$this->data['code'] = 0;
$this->data['msg'] = '删除失败!';
}
} else {
$this->data['code'] = 0;
$this->data['msg'] = '非法操作!';
}
return $this->data;
}
/**
* @title 密码修改
* @method POST
* @param CustomerM $customer [description]
* @return [type] [description]
*/
public function editpasswd(Member $user) {
$data = $this->request->post();
$uid = (isset($data['uid']) && $data['uid']) ? $data['uid'] : $this->request->user['uid'];
$userInfo = $user->where('uid', $uid)->find();
if ($userInfo['password'] !== md5($data['oldpassword'] . $userInfo['salt'])) {
$this->data['code'] = 0;
$this->data['msg'] = "旧密码不正确!";
return $this->data;
}
$save = [
'salt' => Str::random(6),
'password' => $data['password']
];
$result = $user->update($save, ['uid' => $uid]);
if (false !== $result) {
$this->data['code'] = 1;
$this->data['msg'] = '修改成功!';
} else {
$this->data['code'] = 0;
$this->data['msg'] = '修改失败!';
}
return $this->data;
}
/**
* @title 权限信息
* @method GET
* @param CustomerM $customer [description]
* @return [type] [description]
*/
public function authinfo(Member $user, Role $role) {
$this->data['data'] = $role->getUserAuthInfo($this->request);
$this->data['data']['userInfo'] = $user->getUserDetail($this->request);
$this->data['data']['roles'] = $this->data['data']['module'];
$this->data['data']['permission'] = [];
$this->data['code'] = 1;
return $this->data;
}
/**
* @title 更新权限
* @method POST
* @param CustomerM $customer [description]
* @return [type] [description]
*/
public function auth(Member $user, RoleAccess $role) {
$data = $this->request->post();
//更新部门信息
$user->update(['department' => $data['department']], ['uid' => $data['uid']]);
//更新角色信息
$role->where('uid', $data['uid'])->delete();
$role->save(['uid' => $data['uid'], 'group_id' => $data['role']]);
$this->data['code'] = 1;
$this->data['msg'] = "更新成功!";
return $this->data;
}
/**
* 用户日志
* @param MemberLog $log [description]
* @return [type] [description]
*/
public function log(MemberLog $log) {
$list = $log->getMemberLogList($this->request)->each(function ($item, $key) {
$item['params'] = json_encode($item['param']);
return $item;
});
$this->data['data'] = $list;
return $this->data;
}
/**
* 用户日志
* @param MemberLog $log [description]
* @return [type] [description]
*/
public function clearlog(MemberLog $log) {
$result = $log->where('create_time', '<', time())->delete();
if (false !== $result) {
$this->data['msg'] = '已清空!';
$this->data['code'] = 1;
} else {
$this->data['msg'] = '未清空!';
$this->data['code'] = 0;
}
return $this->data;
}
/**
* 左侧菜单
* @param MemberLog $log [description]
* @return [type] [description]
*/
public function getMenu(MemberLog $log) {
$this->data['data'] = [
[
'label' => "客户管理",
'path' => "/customer",
'icon' => 'el-icon-document',
'meta' => [
'i18n' => 'customer',
],
'children' => [
[
'label' => "客户列表",
'path' => "/index",
'component' => 'views/customer/index',
'icon' => 'el-icon-document',
'meta' => [
'i18n' => 'customer',
],
],
[
'label' => "厂商列表",
'path' => "/firm",
'component' => 'views/customer/index',
'icon' => 'el-icon-document',
'meta' => [
'i18n' => 'customer',
],
],
[
'label' => "标注列表",
'path' => "/named",
'component' => 'views/customer/index',
'icon' => 'el-icon-document',
'meta' => [
'i18n' => 'customer',
],
],
]
]
];
return $this->data;
}
/**
* 顶部菜单
* @param MemberLog $log [description]
* @return [type] [description]
*/
public function getTopMenu() {
return $this->data;
}
}

View File

@@ -0,0 +1,54 @@
<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\http\middleware;
use app\model\Config;
use app\model\Department;
use app\model\Dictionary;
use app\model\Firm;
use think\facade\Cache;
class Api {
protected $data = [];
public function handle($request, \Closure $next) {
$request->pageConfig = array(
'list_rows' => $request->param('limit', 30),
'page' => $request->param('page', 1),
);
$this->cacheData($request); //缓存基础数据
$response = $next($request);
if (is_array($response->getData())) {
$this->data = array_merge($this->data, $response->getData());
} else {
$this->data = $response->getData();
}
if ($request->isAjax()) {
return json($this->data);
} else {
if (\is_string($this->data) && $this->data != '') {
return $response;
} else {
return json($this->data);
}
}
}
public function cacheData($request) {
//缓存配置信息
$config = Cache::get('config');
if (!$config) {
Cache::set('config', Config::getConfigList($request));
}
}
}

View File

@@ -0,0 +1,46 @@
<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\http\middleware;
use app\model\MemberLog;
use app\model\RoleAccess;
use sent\jwt\exception\JWTException;
use sent\jwt\exception\TokenExpiredException;
use sent\jwt\JWTAuth as Auth;
class ApiAuth {
public $data = ['code' => 0];
public function __construct(Auth $auth) {
$this->auth = $auth;
}
public function handle($request, \Closure $next) {
try {
$auth = $this->auth->auth();
$user = (array) $auth['data']->getValue();
$user['role'] = RoleAccess::getRoleByUid($user['uid']);
$request->user = $user;
//记录用户操作记录
MemberLog::record($request);
} catch (TokenExpiredException $e) {
$this->data['msg'] = $e->getMessage();
$this->data['code'] = 2001;
return json($this->data)->code($this->data['code']);
} catch (JWTException $e) {
$this->data['code'] = 2000;
$this->data['msg'] = $e->getMessage();
return json($this->data)->code($this->data['code']);
}
return $next($request);
}
}

View File

@@ -21,9 +21,10 @@ class Validate {
//获取当前参数
$params = $request->param();
//获取访问控制器
$controller = strtr(strtolower($request->controller()), '.', '\\');
$controller = strtr($request->controller(), '.', '\\');
//获取操作名,用于验证场景scene
$scene = $request->action();
$scene = $request->action();
$validate = "app\\http\\validate\\" . $controller;
//仅当验证器存在时 进行校验
if (class_exists($validate) && $request->isPost()) {
@@ -35,8 +36,8 @@ class Validate {
if (!$v->check($params)) {
//校验不通过则直接返回错误信息
$data = array(
'msg' => $v->getError(),
'code' => 1,
'msg' => $v->getError(),
'code' => 0,
'data' => '',
'time' => time(),
);

View File

@@ -34,10 +34,15 @@ Route::group('user', function () {
});
Route::group('api', function () {
Route::rule('/', 'admin.Index/index');
Route::rule('login', 'api.Index/login');
Route::rule('register', 'api.Index/register');
Route::rule('/', 'api.Index/index');
Route::rule('login', 'api.Login/index');
Route::rule('register', 'api.Login/register');
Route::rule('logout', 'api.Login/logout');
Route::rule(':controller/:function', 'api.:controller/:function');
});
})->allowCrossDomain([
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Credentials' => 'true',
'Access-Control-Allow-Headers' => 'authorization, token, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With',
]);
Route::miss('Index/miss');