From 073e9c9c65d6d7c5caae07b578ad235cd7f55f36 Mon Sep 17 00:00:00 2001 From: sugar1569 Date: Tue, 21 Nov 2023 16:16:30 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=A8=8B=E5=BA=8F=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E3=80=91=E6=B7=BB=E5=8A=A0=E4=BB=A3=E7=A0=81=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crmeb/.phpstorm.meta.php | 7 ++- crmeb/app/AppService.php | 26 +++++++++-- crmeb/app/ExceptionHandle.php | 10 ++-- crmeb/app/Request.php | 43 ++++++++--------- .../app/adminapi/AdminApiExceptionHandle.php | 37 ++++++++------- crmeb/app/adminapi/common.php | 22 +++++---- .../adminapi/controller/AuthController.php | 8 ++-- crmeb/app/adminapi/controller/Common.php | 46 +++++++++---------- crmeb/app/adminapi/controller/Login.php | 6 +-- .../adminapi/controller/PublicController.php | 22 ++++++--- crmeb/app/adminapi/provider.php | 2 +- crmeb/app/provider.php | 4 +- crmeb/app/service.php | 2 +- 13 files changed, 133 insertions(+), 102 deletions(-) diff --git a/crmeb/.phpstorm.meta.php b/crmeb/.phpstorm.meta.php index ad7d71586..e0c843020 100644 --- a/crmeb/.phpstorm.meta.php +++ b/crmeb/.phpstorm.meta.php @@ -4,16 +4,15 @@ namespace PHPSTORM_META { use think\Container; use function \app; - + // 容器注入 override( \app(), map([ - 'json' => \crmeb\utils\Json::class + 'json' => \crmeb\utils\Json::class // json类 ]) ); - override( - \think\Container::make(), + \think\Container::make(),// 容器实例化 map([ '' => '@' ]) diff --git a/crmeb/app/AppService.php b/crmeb/app/AppService.php index 21d6c6d01..abb6e9dcc 100644 --- a/crmeb/app/AppService.php +++ b/crmeb/app/AppService.php @@ -15,17 +15,33 @@ use crmeb\services\GroupDataService; use crmeb\utils\Json; use think\Service; + +/** + * AppService类 + * + * @package App\Services + */ class AppService extends Service { + /** + * 定义服务绑定 + * + * @var array + */ public $bind = [ - 'json' => Json::class, - 'sysConfig' => SystemConfigService::class, - 'sysGroupData' => GroupDataService::class + 'json' => Json::class, // JSON服务绑定 + 'sysConfig' => SystemConfigService::class, // 系统配置服务绑定 + 'sysGroupData' => GroupDataService::class // 分组数据服务绑定 ]; - + /** + * 服务启动时的操作 + * + * @return void + */ public function boot() { - defined('DS') || define('DS', DIRECTORY_SEPARATOR); + defined('DS') || define('DS', DIRECTORY_SEPARATOR); // 定义目录分隔符常量 } } + diff --git a/crmeb/app/ExceptionHandle.php b/crmeb/app/ExceptionHandle.php index 858ec2670..19e83da5a 100644 --- a/crmeb/app/ExceptionHandle.php +++ b/crmeb/app/ExceptionHandle.php @@ -30,11 +30,11 @@ class ExceptionHandle extends Handle * @var array */ protected $ignoreReport = [ - HttpException::class, - HttpResponseException::class, - ModelNotFoundException::class, - DataNotFoundException::class, - ValidateException::class, + HttpException::class,//HTTP异常 + HttpResponseException::class,//http响应异常 + ModelNotFoundException::class,//模型没找到异常 + DataNotFoundException::class,//数据没找到异常 + ValidateException::class,//验证器异常 ]; /** diff --git a/crmeb/app/Request.php b/crmeb/app/Request.php index 8259a7aa3..5b8715ec4 100644 --- a/crmeb/app/Request.php +++ b/crmeb/app/Request.php @@ -27,7 +27,7 @@ use Spatie\Macroable\Macroable; */ class Request extends \think\Request { - use Macroable; + use Macroable;// 允许注入公共方法 /** * 不过滤变量名 @@ -45,30 +45,31 @@ class Request extends \think\Request */ public function more(array $params, bool $suffix = false, bool $filter = true): array { - $p = []; - $i = 0; - foreach ($params as $param) { - if (!is_array($param)) { + $p = []; // 初始化一个空数组 + $i = 0; // 初始化计数器 + foreach ($params as $param) { // 遍历参数数组 + if (!is_array($param)) { // 如果当前元素不是数组 $p[$suffix == true ? $i++ : $param] = $this->filterWord(is_string($this->param($param)) ? trim($this->param($param)) : $this->param($param), $filter && !in_array($param, $this->except)); - } else { - if (!isset($param[1])) $param[1] = null; - if (!isset($param[2])) $param[2] = ''; - if (is_array($param[0])) { - $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1]; - $keyName = $param[0][0]; - } else { - $name = is_array($param[1]) ? $param[0] . '/a' : $param[0]; - $keyName = $param[0]; + } else { // 如果当前元素是数组 + if (!isset($param[1])) $param[1] = null; // 如果第二个元素不存在则设置为null + if (!isset($param[2])) $param[2] = ''; // 如果第三个元素不存在则设置为空字符串 + if (is_array($param[0])) { // 如果第一个元素也是数组 + $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1]; // 根据第二个元素是否为数组来构造参数名 + $keyName = $param[0][0]; // 参数名作为键名 + } else { // 如果当前元素是数组 + $name = is_array($param[1]) ? $param[0] . '/a' : $param[0]; // 根据第二个元素是否为数组来构造参数名 + $keyName = $param[0]; // 参数名作为键名 } - - $p[$suffix == true ? $i++ : ($param[3] ?? $keyName)] = $this->filterWord( - is_string($this->param($name, $param[1], $param[2])) ? - trim($this->param($name, $param[1], $param[2])) : - $this->param($name, $param[1], $param[2]), - $filter && !in_array($keyName, $this->except)); + // 获取当前元素的值并存入新数组 + $p[$suffix == true ? $i++ : ($param[3] ?? $keyName)] = $this->filterWord( // 对当前元素进行过滤并存入新数组 + is_string($this->param($name, $param[1], $param[2])) ? // 获取当前元素的值并去除首尾空格 + trim($this->param($name, $param[1], $param[2])) : + $this->param($name, $param[1], $param[2]), + $filter && !in_array($keyName, $this->except)); } } return $p; + } /** @@ -92,7 +93,7 @@ class Request extends \think\Request if (is_array($v)) { foreach ($v as &$vv) { if (!is_array($vv)) { - $vv = $this->replaceWord($farr, $vv); + $vv = $this->replaceWord($farr, $vv);// 替换特殊字符 } } } else { diff --git a/crmeb/app/adminapi/AdminApiExceptionHandle.php b/crmeb/app/adminapi/AdminApiExceptionHandle.php index eb8eeaa03..85eeefdbb 100644 --- a/crmeb/app/adminapi/AdminApiExceptionHandle.php +++ b/crmeb/app/adminapi/AdminApiExceptionHandle.php @@ -31,10 +31,11 @@ class AdminApiExceptionHandle extends Handle * @var array */ protected $ignoreReport = [ - ValidateException::class, - AuthException::class, - AdminException::class, - ApiException::class, + ValidateException::class,//验证错误 + DbException::class,//数据库错误 + AuthException::class,//权限错误 + AdminException::class,//后台错误 + ApiException::class,//接口错误 ]; /** @@ -48,10 +49,10 @@ class AdminApiExceptionHandle extends Handle if (!$this->isIgnoreReport($exception)) { try { $data = [ - 'file' => $exception->getFile(), - 'line' => $exception->getLine(), - 'message' => $this->getMessage($exception), - 'code' => $this->getCode($exception), + 'file' => $exception->getFile(),//文件 + 'line' => $exception->getLine(),//行数 + 'message' => $this->getMessage($exception),//错误信息 + 'code' => $this->getCode($exception),//错误码 ]; //日志内容 @@ -66,9 +67,9 @@ class AdminApiExceptionHandle extends Handle json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), //报错数据 ]; - Log::write(implode("|", $log), "error"); + Log::write(implode("|", $log), "error");//记录日志 } catch (\Throwable $e) { - Log::write($e->getMessage(), "error"); + Log::write($e->getMessage(), "error");//记录日志 } } } @@ -82,22 +83,24 @@ class AdminApiExceptionHandle extends Handle */ public function render($request, Throwable $e): Response { + // 如果是响应异常,直接返回 if ($e instanceof HttpResponseException) { - return parent::render($request, $e); + return parent::render($request, $e);//直接返回 } + // 调试模式下返回异常信息 $massageData = Env::get('app_debug', false) ? [ - 'message' => $e->getMessage(), - 'file' => $e->getFile(), - 'line' => $e->getLine(), - 'trace' => $e->getTrace(), - 'previous' => $e->getPrevious(), + 'message' => $e->getMessage(),//错误信息 + 'file' => $e->getFile(),//文件 + 'line' => $e->getLine(),//行数 + 'trace' => $e->getTrace(),// 异常的追踪 + 'previous' => $e->getPrevious(),// 异常的上一个异常 ] : []; $message = $e->getMessage(); // 添加自定义异常处理机制 if ($e instanceof AuthException || $e instanceof AdminException || $e instanceof ApiException || $e instanceof ValidateException) { return app('json')->make($e->getCode() ?: 400, $message, $massageData); } else { - return app('json')->fail($message, $massageData); + return app('json')->fail($message, $massageData);//返回错误信息 } } diff --git a/crmeb/app/adminapi/common.php b/crmeb/app/adminapi/common.php index e9a7a5db7..91f30f647 100644 --- a/crmeb/app/adminapi/common.php +++ b/crmeb/app/adminapi/common.php @@ -16,9 +16,11 @@ if (!function_exists('get_this_class_methods')) { */ function get_this_class_methods($class, $unarray = []) { - $arrayall = get_class_methods($class); - if ($parent_class = get_parent_class($class)) { - $arrayparent = get_class_methods($parent_class); + $arrayall = get_class_methods($class);//获取当前类的方法 + $parent_class = get_parent_class($class);//获取父类 + //如果有父类 + if ($parent_class) { + $arrayparent = get_class_methods($parent_class);//获取父类的方法 $arraynow = array_diff($arrayall, $arrayparent);//去除父级的 } else { $arraynow = $arrayall; @@ -49,7 +51,7 @@ if (!function_exists('setconfig')) { $pats[$i] = '/\'' . $pat[$i] . '\'(.*?),/'; $reps[$i] = "'" . $pat[$i] . "'" . "=>" . "'" . $rep[$i] . "',"; } - $fileurl = app()->getConfigPath() . $name . ".php"; + $fileurl = app()->getConfigPath() . $name . ".php";//配置文件路径 $string = file_get_contents($fileurl); //加载配置文件 $string = preg_replace($pats, $reps, $string); // 正则查找然后替换 @file_put_contents($fileurl, $string); // 写入配置文件 @@ -62,7 +64,7 @@ if (!function_exists('setconfig')) { $rep = str_replace('\'', "", $rep); $reps = "'" . $pat . "'" . "=>" . "'" . $rep . "',"; } - $fileurl = app()->getConfigPath() . $name . ".php"; + $fileurl = app()->getConfigPath() . $name . ".php";//配置文件路径 $string = file_get_contents($fileurl); //加载配置文件 $string = preg_replace($pats, $reps, $string); // 正则查找然后替换 @file_put_contents($fileurl, $string); // 写入配置文件 @@ -75,7 +77,7 @@ if (!function_exists('setconfig')) { } if (!function_exists('arrayToText')) { /** - * 修改config的函数 + * 将数组转成PHP文本 * @param $array * @return string */ @@ -114,8 +116,8 @@ if (!function_exists('attr_format')) { */ function attr_format($arr): array { - $len = count($arr); - $title = array_column($arr, 'value'); + $len = count($arr);//获取数组长度 + $title = array_column($arr, 'value');//获取数组中的value值 $result = []; if ($len > 0) { @@ -149,8 +151,8 @@ if (!function_exists('verify_domain')) { */ function verify_domain(string $domain): bool { - $res = "/^(?=^.{3,255}$)(http(s)?:\/\/)(www\.)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:\d+)*(\/\w+\.\w+)*$/"; - if (preg_match($res, $domain)) + $res = "/^(?=^.{3,255}$)(http(s)?:\/\/)(www\.)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:\d+)*(\/\w+\.\w+)*$/";//正则验证 + if (preg_match($res, $domain))//匹配正则 return true; else return false; diff --git a/crmeb/app/adminapi/controller/AuthController.php b/crmeb/app/adminapi/controller/AuthController.php index 943bcba06..61bd04199 100644 --- a/crmeb/app/adminapi/controller/AuthController.php +++ b/crmeb/app/adminapi/controller/AuthController.php @@ -45,9 +45,9 @@ class AuthController extends BaseController */ protected function initialize() { - $this->adminId = $this->request->adminId(); - $this->adminInfo = $this->request->adminInfo(); - $this->auth = $this->request->adminInfo['rule'] ?? []; + $this->adminId = $this->request->adminId();//获取当前登录管理员id + $this->adminInfo = $this->request->adminInfo();//获取当前登录管理员信息 + $this->auth = $this->request->adminInfo['rule'] ?? [];//获取当前管理员权限 } /** @@ -78,7 +78,7 @@ class AuthController extends BaseController $v->scene($message); } } - + if (is_array($message)) $v->message($message); diff --git a/crmeb/app/adminapi/controller/Common.php b/crmeb/app/adminapi/controller/Common.php index 0197c0259..88a1cbffe 100644 --- a/crmeb/app/adminapi/controller/Common.php +++ b/crmeb/app/adminapi/controller/Common.php @@ -37,9 +37,9 @@ class Common extends AuthController public function getLogo() { return app('json')->success([ - 'logo' => sys_config('site_logo'), - 'logo_square' => sys_config('site_logo_square'), - 'site_name' => sys_config('site_name') + 'logo' => sys_config('site_logo'),//站点LOGO + 'logo_square' => sys_config('site_logo_square'),//图标 + 'site_name' => sys_config('site_name')//站点名称 ]); } @@ -49,8 +49,8 @@ class Common extends AuthController */ public function auth() { - $version = get_crmeb_version(); - $host = $this->request->host(); + $version = get_crmeb_version();//当前版本号 + $host = $this->request->host();//当前域名 // 正常域名 $res = HttpService::request('http://authorize.crmeb.net/api/auth_cert_query', 'post', [ 'domain_name' => $host, @@ -159,8 +159,8 @@ class Common extends AuthController public function homeStatics() { /** @var StoreOrderServices $orderServices */ - $orderServices = app()->make(StoreOrderServices::class); - $info = $orderServices->homeStatics(); + $orderServices = app()->make(StoreOrderServices::class);//订单服务 + $info = $orderServices->homeStatics();//订单统计 return app('json')->success(compact('info')); } @@ -170,7 +170,7 @@ class Common extends AuthController if ($lastValue == 0 && $nowValue == 0) return 0; if ($lastValue == 0) return round($nowValue, 2); if ($nowValue == 0) return -round($lastValue, 2); - return bcmul(bcdiv((bcsub($nowValue, $lastValue, 2)), $lastValue, 2), 100, 2); + return bcmul(bcdiv((bcsub($nowValue, $lastValue, 2)), $lastValue, 2), 100, 2);//保留两位小数 } /** @@ -180,8 +180,8 @@ class Common extends AuthController { $cycle = $this->request->param('cycle') ?: 'thirtyday';//默认30天 /** @var StoreOrderServices $orderServices */ - $orderServices = app()->make(StoreOrderServices::class); - $chartdata = $orderServices->orderCharts($cycle); + $orderServices = app()->make(StoreOrderServices::class);//订单服务 + $chartdata = $orderServices->orderCharts($cycle);//订单统计 return app('json')->success($chartdata); } @@ -215,22 +215,22 @@ class Common extends AuthController public function jnotice() { /** @var StoreOrderServices $orderServices */ - $orderServices = app()->make(StoreOrderServices::class); - $data['ordernum'] = $orderServices->storeOrderCount(); + $orderServices = app()->make(StoreOrderServices::class);//订单服务 + $data['ordernum'] = $orderServices->storeOrderCount();//待发货 $store_stock = sys_config('store_stock'); if ($store_stock < 0) $store_stock = 2; /** @var StoreProductServices $storeServices */ - $storeServices = app()->make(StoreProductServices::class); + $storeServices = app()->make(StoreProductServices::class);//商品服务 $data['inventory'] = $storeServices->count(['type' => 5, 'store_stock' => $store_stock]);//警戒库存 /** @var StoreProductReplyServices $replyServices */ - $replyServices = app()->make(StoreProductReplyServices::class); - $data['commentnum'] = $replyServices->replyCount(); + $replyServices = app()->make(StoreProductReplyServices::class);//商品评价 + $data['commentnum'] = $replyServices->replyCount();//评价 /** @var UserExtractServices $extractServices */ - $extractServices = app()->make(UserExtractServices::class); + $extractServices = app()->make(UserExtractServices::class);//提现 $data['reflectnum'] = $extractServices->userExtractCount();//提现 $data['msgcount'] = intval($data['ordernum']) + intval($data['inventory']) + intval($data['commentnum']) + intval($data['reflectnum']); $data['newOrderId'] = $orderServices->newOrderId(1); - if (count($data['newOrderId'])) $orderServices->newOrderUpdate($data['newOrderId']); + if (count($data['newOrderId'])) $orderServices->newOrderUpdate($data['newOrderId']);//更新订单查看状态 $value = []; if ($data['ordernum'] != 0) { $value[] = [ @@ -333,8 +333,8 @@ class Common extends AuthController public function menusList() { /** @var SystemMenusServices $menusServices */ - $menusServices = app()->make(SystemMenusServices::class); - $list = $menusServices->getSearchList(); + $menusServices = app()->make(SystemMenusServices::class);//菜单服务 + $list = $menusServices->getSearchList();//获取菜单列表 $counts = $menusServices->getColumn([ ['is_show', '=', 1], ['auth_type', '=', 1], @@ -362,8 +362,8 @@ class Common extends AuthController */ public function copyright() { - $copyrightContext = sys_config('nncnL_crmeb_copyright', ''); - $copyrightImage = sys_config('nncnL_crmeb_copyright_image', ''); + $copyrightContext = sys_config('nncnL_crmeb_copyright', '');//版权内容 + $copyrightImage = sys_config('nncnL_crmeb_copyright_image', '');//版权图片 return app('json')->success(compact('copyrightContext', 'copyrightImage')); } @@ -375,7 +375,7 @@ class Common extends AuthController { [$copyright, $copyrightImg] = $this->request->postMore(['copyright', 'copyright_img',], true); /** @var SystemConfigServices $services */ - $services = app()->make(SystemConfigServices::class); + $services = app()->make(SystemConfigServices::class);//配置服务 if ($services->count(['menu_name' => 'nncnL_crmeb_copyright'])) { $services->update(['menu_name' => 'nncnL_crmeb_copyright'], ['value' => json_encode($copyright)]); } else { @@ -402,7 +402,7 @@ class Common extends AuthController 'info' => '' ]); } - CacheService::clear(); + CacheService::clear();//清除缓存 return app('json')->success(100000); } diff --git a/crmeb/app/adminapi/controller/Login.php b/crmeb/app/adminapi/controller/Login.php index 2155c1206..b57d59346 100644 --- a/crmeb/app/adminapi/controller/Login.php +++ b/crmeb/app/adminapi/controller/Login.php @@ -30,8 +30,8 @@ class Login extends AuthController */ public function __construct(App $app, SystemAdminServices $services) { - parent::__construct($app); - $this->services = $services; + parent::__construct($app);//调用父类构造函数 + $this->services = $services;//初始化services 注入services 系统管理员服务层 SystemAdminServices } protected function initialize() @@ -45,7 +45,7 @@ class Login extends AuthController */ public function captcha() { - return app()->make(Captcha::class)->create(); + return app()->make(Captcha::class)->create();//验证码 } /** diff --git a/crmeb/app/adminapi/controller/PublicController.php b/crmeb/app/adminapi/controller/PublicController.php index c934d4d6d..2ca313a8d 100644 --- a/crmeb/app/adminapi/controller/PublicController.php +++ b/crmeb/app/adminapi/controller/PublicController.php @@ -18,6 +18,9 @@ use app\services\system\SystemRouteServices; use crmeb\services\CacheService; use think\Response; +/** + * 公共控制器 + */ class PublicController { @@ -29,12 +32,13 @@ class PublicController public function download(string $key = '') { if (!$key) { - return Response::create()->code(500); + return Response::create()->code(500);//参数错误 } - $fileName = CacheService::get($key); + $fileName = CacheService::get($key);//缓存中的文件路径 + //判断文件是否存在 if (is_array($fileName) && isset($fileName['path']) && isset($fileName['fileName']) && $fileName['path'] && $fileName['fileName'] && file_exists($fileName['path'])) { - CacheService::delete($key); - return download($fileName['path'], $fileName['fileName']); + CacheService::delete($key);//删除缓存 + return download($fileName['path'], $fileName['fileName']);//返回文件 } return Response::create()->code(500); } @@ -45,6 +49,7 @@ class PublicController */ public function getWorkerManUrl() { + //获取当前长链接域名 return app('json')->success(getWorkerManUrl()); } @@ -65,20 +70,25 @@ class PublicController ['uploadToken', ''], ['pid', 0] ], true); + //附件服务 $service = app()->make(SystemAttachmentServices::class); - if (CacheService::get('scan_upload') != $uploadToken) { - return app('json')->fail(410086); + // 获取缓存中的上传令牌并与当前上传令牌比较 + if (CacheService::get('scan_upload') != $uploadToken) { + return app('json')->fail(410086); // 如果不一致则返回错误码410086 } + //上传文件 $service->upload((int)$pid, $file, $upload_type, $type, '', $uploadToken); return app('json')->success(100032); } public function import(Request $request) { + //获取文件路径 $filePath = $request->param('file_path', ''); if (empty($filePath)) { return app('json')->fail(12894); } + //导入数据 app()->make(SystemRouteServices::class)->import($filePath); return app('json')->success(100010); } diff --git a/crmeb/app/adminapi/provider.php b/crmeb/app/adminapi/provider.php index cd02380bf..3d6f2328d 100644 --- a/crmeb/app/adminapi/provider.php +++ b/crmeb/app/adminapi/provider.php @@ -11,5 +11,5 @@ // 容器Provider定义文件 return [ - 'think\exception\Handle' => \app\adminapi\AdminApiExceptionHandle::class, + 'think\exception\Handle' => \app\adminapi\AdminApiExceptionHandle::class,//异常处理类 ]; diff --git a/crmeb/app/provider.php b/crmeb/app/provider.php index 4b885d0ba..1b718074e 100644 --- a/crmeb/app/provider.php +++ b/crmeb/app/provider.php @@ -14,6 +14,6 @@ use app\Request; // 容器Provider定义文件 return [ - 'think\Request' => Request::class, - 'think\exception\Handle' => ExceptionHandle::class, + 'think\Request' => Request::class,//请求对象 + 'think\exception\Handle' => ExceptionHandle::class,//异常处理类 ]; diff --git a/crmeb/app/service.php b/crmeb/app/service.php index f53396a48..1036c47ab 100644 --- a/crmeb/app/service.php +++ b/crmeb/app/service.php @@ -9,5 +9,5 @@ // | Author: CRMEB Team // +---------------------------------------------------------------------- return [ - \app\AppService::class + \app\AppService::class // 应用服务提供者 ];