【程序目录】添加代码注释

This commit is contained in:
sugar1569 2023-11-21 16:16:30 +08:00
parent c885ebfac3
commit 073e9c9c65
13 changed files with 133 additions and 102 deletions

View File

@ -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([
'' => '@'
])

View File

@ -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); // 定义目录分隔符常量
}
}

View File

@ -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,//验证器异常
];
/**

View File

@ -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 {

View File

@ -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);//返回错误信息
}
}

View File

@ -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;

View File

@ -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);

View File

@ -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);
}

View File

@ -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();//验证码
}
/**

View File

@ -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);
}

View File

@ -11,5 +11,5 @@
// 容器Provider定义文件
return [
'think\exception\Handle' => \app\adminapi\AdminApiExceptionHandle::class,
'think\exception\Handle' => \app\adminapi\AdminApiExceptionHandle::class,//异常处理类
];

View File

@ -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,//异常处理类
];

View File

@ -9,5 +9,5 @@
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
return [
\app\AppService::class
\app\AppService::class // 应用服务提供者
];