作者 赵彬吉

Merge branch 'dev' of http://47.244.231.31:8099/zhl/globalso-v6 into dev

... ... @@ -5,12 +5,10 @@ namespace App\Exceptions;
use App\Enums\Common\Code;
use App\Enums\Common\Common;
use App\Services\DingService;
use App\Traits\RedisTrait;
use App\Utils\EncryptUtils;
use App\Utils\LogUtils;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Route;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
... ... @@ -60,5 +58,86 @@ class Handler extends ExceptionHandler
*/
public function report(Throwable $exception)
{
//日志记录
$exceptionMessage = "错误CODE:" . $exception->getCode() .
"-----错误message:" . $exception->getMessage() .
'------错误文件:' . $exception->getFile() .
'-------错误行数:' . $exception->getLine();
//A端错误
if ($exception instanceof AsideGlobalException) {
LogUtils::error("AsideGlobalException", [], $exceptionMessage);
}
//B端错误
elseif($exception instanceof BsideGlobalException) {
LogUtils::error("BsideGlobalException", [], $exceptionMessage);
}
//验证错误(非手动抛出)
elseif ($exception instanceof ValidationException) {
LogUtils::error("参数验证失败", [], $exceptionMessage);
}
//Redis错误(非手动抛出)
elseif ($exception instanceof \RedisException) {
LogUtils::error("Redis服务异常", [], $exceptionMessage);
}
//Mysql错误(非手动抛出)
elseif ($exception instanceof \PDOException) {
LogUtils::error("数据库服务异常", [], $exceptionMessage);
}
//模型未找到错误(非手动抛出)
elseif ($exception instanceof ModelNotFoundException) {
LogUtils::error("模型资源未找到", [], $exceptionMessage);
}
//其他
else {
LogUtils::error("全局系统异常", [], $exceptionMessage);
}
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
*/
public function render($request, Throwable $exception)
{
$message = $exception->getMessage();
if ($exception instanceof AsideGlobalException) {
$code = $exception->getCode();
}elseif ($exception instanceof BsideGlobalException) {
$code = $exception->getCode();
} elseif ($exception instanceof NotFoundHttpException || $exception instanceof MethodNotAllowedHttpException) {
return response('404 Not Found', 404);
} else {
$code = Code::SYSTEM_ERROR();
}
//钉钉通知错误信息
if (in_array(config('app.env'), ['test', 'production']) && (in_array(substr($code, 0, 1), ['B', 'C']))) {
$exceptionMessage = "路由:" . Route::current()->uri .
"-----错误CODE:" . $exception->getCode() .
"-----错误message:" . $exception->getMessage() .
'------错误文件:' . $exception->getFile() . '-------错误行数:' .
$exception->getLine();
(new DingService())->handle(['keyword' => "ERROR", 'msg' => config('app.env') . '环境报错:' . $exceptionMessage, 'isAtAll' => false]);
}
//开启debug 错误原样输出
$debub = config('app.debug');
$message = $debub ? $message : ($code->description ?? $message);
$response = [
'code' => $code,
'message' => $message
];
//加密返回
if (config('app.params_encrypt')) {
$k = config('app.params_encrypt_key');
$i = config('app.params_encrypt_iv');
$response = [
'p' => (new EncryptUtils())->openssl_en($response, $k, $i)];
}
return response($response);
}
}
... ...
... ... @@ -31,24 +31,12 @@ class BaseController extends Controller
$this->request = $request;
$this->param = $this->request->all();
$this->token = $this->request->header('token');
$this->get_param();
$this->auth_token();
}
/**
* @name
* @return void
* @author :liyuhang
* @method
*/
public function auth_token(){
$this->token = '810f28c1d80d0ccf27509816bf44b329';
if(!empty($this->token) && !empty(Cache::get($this->token))){
$info = Cache::get($this->token);
if(!empty($info)){
$this->user = $info;
$this->uid = $info['id'];
}
$this->get_param();
}
/**
* 成功返回
... ... @@ -133,7 +121,23 @@ class BaseController extends Controller
}
/**
* 菜单权限->得到子级数组
* @param int
* @return array
*/
public function _get_child($my_id, $arr)
{
$new_arr = array();
foreach ($arr as $k => $v) {
$v = (array)$v;
if ($v['pid'] == $my_id) {
$v['sub'] = $this->_get_child($v['id'],$arr);
$new_arr[] = $v;
}
}
return $new_arr ? $new_arr : false;
}
/**
* @name :上传图片
... ...
... ... @@ -60,7 +60,7 @@ class ComController extends BaseController
$info = $projectRoleModel->read(['id'=>$this->user['role_id']]);
$projectMenuModel = new ProjectMenuModel();
$info['role_menu'] = trim($info['role_menu'],',');
$lists = $this->where(['status'=>0,'is_role'=>0])->whereIn('id',explode(',',$info['role_menu']))->get();
$lists = $projectMenuModel->where(['status'=>0,'is_role'=>0])->whereIn('id',explode(',',$info['role_menu']))->get();
$lists = $lists->toArray();
$menu = array();
foreach ($lists as $k => $v){
... ... @@ -74,24 +74,6 @@ class ComController extends BaseController
}
/**
* 菜单权限->得到子级数组
* @param int
* @return array
*/
public function _get_child($my_id, $arr)
{
$new_arr = array();
foreach ($arr as $k => $v) {
$v = (array)$v;
if ($v['pid'] == $my_id) {
$v['sub'] = $this->_get_child($v['id'],$arr);
$new_arr[] = $v;
}
}
return $new_arr ? $new_arr : false;
}
/**
* @name :获取当前项目详情
* @return void
* @author :liyuhang
... ... @@ -114,17 +96,11 @@ class ComController extends BaseController
*/
public function edit_info(){
$rules = [
'id'=>'required',
'mobile'=>'required|string|max:11',
'password'=>'required|string|min:5',
'name'=>'required|max:20',
];
//验证的提示信息
$message = [
'id.required'=>'主键不能为空',
'mobile.required'=>'号码必须填写',
'mobile.string'=>'号码中含有非法文字',
'mobile.max' => '号码不大于11字符.',
'password.required'=>'密码必须填写',
'password.string'=>'密码中含有非法文字',
'password.min' => '密码不小于5字符.',
... ... @@ -135,8 +111,13 @@ class ComController extends BaseController
if($validate->fails()){
return $this->response($validate->errors()->first(),Code::USER_PARAMS_ERROE,$this->param);
}
$user = new UserModel();
$userModel = new UserModel();
$this->param['id'] = $this->uid;
$rs = $userModel->edits($this->param);
if($rs === false){
$this->response('参数错误或其他服务器原因,编辑失败',Code::USER_ERROR);
}
$this->response('编辑成功');
}
/**
... ... @@ -146,6 +127,10 @@ class ComController extends BaseController
* @method :post
*/
public function logout(){
$rs = Cache::pull($this->token);
if($rs === false){
$this->response('error',Code::USER_ERROR);
}
$this->response('success');
}
}
... ...
... ... @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Bside;
use App\Enums\Common\Code;
use App\Models\ProjectMenu as ProjectMenuModel;
use App\Models\ProjectRole as ProjectRoleModel;
use App\Models\User as UserModel;
use Illuminate\Support\Facades\Validator;
... ... @@ -25,6 +26,31 @@ class ProjectRoleController extends BaseController
$this->allCount = $projectRoleModel->allCount;
$this->result($lists);
}
/**
* @name :添加/编辑角色时获取菜单列表
* @return void
* @author :liyuhang
* @method
*/
public function get_role_menu(){
//根据当前登录用户角色返回用户菜单列表
$projectRoleModel = new ProjectRoleModel();
$info = $projectRoleModel->read(['id'=>$this->user['role_id']]);
$projectMenuModel = new ProjectMenuModel();
$info['role_menu'] = trim($info['role_menu'],',');
$lists = $projectMenuModel->where(['status'=>0])->whereIn('id',explode(',',$info['role_menu']))->get();
$lists = $lists->toArray();
$menu = array();
foreach ($lists as $k => $v){
$v = (array)$v;
if ($v['pid'] == 0) {
$v['sub'] = $this->_get_child($v['id'], $lists);
$menu[] = $v;
}
}
$this->response('当前用户菜单列表',Code::SUCCESS,$menu);
}
/**
* @name :添加角色
* @return void
... ... @@ -60,7 +86,7 @@ class ProjectRoleController extends BaseController
if($rs === false){
$this->response('添加失败',Code::USER_PARAMS_ERROE);
}
$this->response('添加成功',Code::SUCCESS);
$this->response('添加成功');
}
/**
... ...
... ... @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Bside;
use App\Enums\Common\Code;
use App\Http\Logic\Bside\UserLogic;
use App\Models\User as UserModel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Validator;
use Symfony\Component\HttpFoundation;
... ... @@ -25,8 +26,7 @@ class UserController extends BaseController
if(empty($lists)){
$this->response('请求失败',Code::USER_ERROR,[]);
}
$this->allCount = $userModel->allCount;
$this->result($lists);
$this->response('列表',Code::SUCCESS,$lists);
}
/**
... ...
<?php
namespace App\Http\Logic\Bside;
use App\Models\User;
class UserLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->model = new User();
}
}
... ...
... ... @@ -22,14 +22,16 @@ class LoginAuthMiddleware
public function handle(Request $request, Closure $next)
{
$token = $request->header('token');
var_dump(Cache::get($token));
die();
if(!isset($token) || empty($token)){
$this->response('当前用户未登录',Code::USER_ERROR);
$res = [
'code'=>'A00010',
'msg' =>'当前用户未登录'
];
return response(['code'=>Code::USER_ERROR,'msg'=>'当前用户未登录']);
}
$info = Cache::get($token);
if(empty($info)){
$this->response('当前用户未登录',Code::USER_ERROR);
return response(['code'=>Code::USER_ERROR,'msg'=>'当前用户未登录']);
}
//操作权限设置
$projectRoleModel = new ProjectRoleModel();
... ... @@ -39,27 +41,12 @@ class LoginAuthMiddleware
//查询当前用户是否拥有权限操作
$projectMenuModel = new ProjectMenu();
$menu_id = $projectMenuModel->read(['action'=>$action['as']],['id']);
if($menu_id !== false){
if(strpos($role_info['role_menu'], $menu_id['id']) < 0){
$this->response('当前用户没有权限',Code::USER_ERROR);
return response(['code'=>Code::USER_ERROR,'msg'=>'当前用户没有权限']);
}
return $next($request);
}
/**
* @name 统一返回参数
* @return void
* @author :liyuhang
* @method
*/
public function response($msg,$code,$data = [],$result_code = 200,$type = 'application/json'){
$code === null && $code = $result_code;
$result = [
'msg' =>$msg,
'code'=>$code,
'data'=>$data
];
$header['token'] = $type;
$response = response($result,$result_code,$header);
throw new HttpResponseException($response);
return $next($request);
}
}
... ...
... ... @@ -37,7 +37,7 @@ class Base extends Model
*/
public function lists($map, $p, $row, $order = 'id', $fields = ['*']){
//TODO::where(['id'=>'','name'=>''])
$lists = $this->select($fields)->where($map)->forPage($p,$row)->orderBy($order)->get();
$lists = DB::table($this->table)->select($fields)->where($map)->orderBy($order)->paginate($row, ['*'], 'page', $p);
if (empty($lists)) {
return false;
}
... ...
... ... @@ -83,6 +83,7 @@ class User extends Base
//生成新token
$token = md5(uniqid().$info['id']);
//存储缓存
$info['token'] = $token;
Cache::add($token,$info);
$rs = $this->edit(['token'=>$token],['id'=>$info['id']]);
if($rs === false){
... ...
... ... @@ -6,7 +6,12 @@ use \Illuminate\Support\Facades\Route;
//必须登录验证的路由组
Route::middleware(['bloginauth'])->group(function () {
//登录用户编辑个人资料
Route::any('/edit_info', [\App\Http\Controllers\Bside\ComController::class, 'edit_info'])->name('edit_info');
Route::any('/logout', [\App\Http\Controllers\Bside\ComController::class, 'logout'])->name('logout');
//获取当前登录用户菜单
Route::any('/get_menu', [\App\Http\Controllers\Bside\ComController::class, 'get_menu'])->name('get_menu');
//获取当前登录用户项目详情
Route::any('/get_project', [\App\Http\Controllers\Bside\ComController::class, 'get_project'])->name('get_project');
//用户相关路由
Route::any('/user/add', [\App\Http\Controllers\Bside\UserController::class, 'add'])->name('user_add');
... ... @@ -16,6 +21,7 @@ Route::middleware(['bloginauth'])->group(function () {
Route::any('/user/del', [\App\Http\Controllers\Bside\UserController::class, 'del'])->name('user_del');
//用户角色相关路由
Route::any('/project_role/lists', [\App\Http\Controllers\Bside\ProjectRoleController::class, 'lists'])->name('project_role_lists');
Route::any('/project_role/get_role_menu', [\App\Http\Controllers\Bside\ProjectRoleController::class, 'get_role_menu'])->name('project_get_role_add');
Route::any('/project_role/add', [\App\Http\Controllers\Bside\ProjectRoleController::class, 'add'])->name('project_role_add');
Route::any('/project_role/edit', [\App\Http\Controllers\Bside\ProjectRoleController::class, 'edit'])->name('project_role_edit');
Route::any('/project_role/status', [\App\Http\Controllers\Bside\ProjectRoleController::class, 'status'])->name('project_role_status');
... ...
... ... @@ -14,5 +14,5 @@ use Illuminate\Support\Facades\Route;
*/
Route::get('/', function () {
return view('welcome');
// return view('welcome');
});
... ...