将 ClassTable:class 注册到服务容器内方便全局规范调用

This commit is contained in:
柳清爽
2025-05-15 10:20:20 +08:00
parent 764309d330
commit 916c39d737
5 changed files with 130 additions and 23 deletions

View File

@@ -0,0 +1,82 @@
<?php
namespace app\common\service;
use library\ClassTable;
use think\Container;
class ClassTableService
{
protected $app;
protected $classTable;
public function __construct(Container $app)
{
$this->app = $app;
$this->classTable = ClassTable::getSelfInstance();
}
/**
* 绑定实例或类到容器
* @param string|array $alias
* @param mixed $instance
* @param string|null $tag
*/
public function bind($alias, $instance = null, string $tag = null)
{
$this->classTable->bind($alias, $instance, $tag);
return $this;
}
/**
* 获取实例
* @param string|object $class
* @param array $parameters
* @return object
*/
public function getInstance($class, ...$parameters)
{
return $this->classTable->getInstance($class, ...$parameters);
}
/**
* 获取共享实例
* @param string $alias
* @param array $parameters
* @return object|null
*/
public function getShared($alias, array $parameters = [])
{
return $this->classTable->getShared($alias, $parameters);
}
/**
* 根据标签获取类
* @param string $tag
* @return array|null
*/
public function getClassByTag(string $tag)
{
return $this->classTable->getClassByTag($tag);
}
/**
* 检查别名是否存在
* @param string $alias
* @return bool
*/
public function has(string $alias)
{
return $this->classTable->has($alias);
}
/**
* 复制实例
* @param mixed $class
* @param string|null $name
* @return object
*/
public function copy($class, string $name = null)
{
return $this->classTable->copy($class, $name);
}
}

View File

@@ -2,6 +2,7 @@
namespace app\cunkebao\controller;
use app\common\service\ClassTableService;
use think\Controller;
/**
@@ -15,6 +16,21 @@ class BaseController extends Controller
*/
protected $user;
/**
* @var ClassTableService
*/
protected $classTable;
/**
* @inheritDoc
*/
public function __construct(ClassTableService $classTable)
{
$this->classTable = $classTable;
parent::__construct();
}
/**
* 初始化
*/

View File

@@ -43,7 +43,7 @@ class GetWechatOnDeviceSummarizeV1Controller extends BaseController
*/
protected function getChatTimesTotal(string $wechatId): int
{
return mt_rand(2000, 1000000);
return mt_rand(100, 1000000);
}
/**
@@ -109,7 +109,7 @@ class GetWechatOnDeviceSummarizeV1Controller extends BaseController
protected function getAccountWeight(string $wechatId): array
{
// 微信账号加友权重评估
$assessment = new WechatAccountWeightAssessment();
$assessment = $this->classTable->getInstance(WechatAccountWeightAssessment::class);
$assessment->settingFactor($wechatId);
return [
@@ -143,14 +143,15 @@ class GetWechatOnDeviceSummarizeV1Controller extends BaseController
* 获取账号加友统计数据.
*
* @param string $wechatId
* @param array $accountWeight
* @return array
*/
protected function getStatistics(string $wechatId, array $accountWeight): array
protected function getStatistics(string $wechatId): array
{
$scope = $this->classTable->getInstance(WechatAccountWeightAssessment::class)->getWeightScope();
return [
'todayAdded' => $this->getTodayNewFriendCount($wechatId),
'addLimit' => $this->_calAllowedFriends($accountWeight['scope'])
'addLimit' => $this->_calAllowedFriends($scope)
];
}
@@ -164,21 +165,14 @@ class GetWechatOnDeviceSummarizeV1Controller extends BaseController
try {
$wechatId = $this->request->param('id/s');
// 以下内容依次加工数据
$accountAge = $this->getRegisterDate($wechatId);
$activityLevel = $this->getActivityLevel($wechatId);
$accountWeight = $this->getAccountWeight($wechatId);
$statistics = $this->getStatistics($wechatId, $accountWeight);
$restrictions = $this->getRestrict($wechatId);
return ResponseHelper::success(
compact(
'accountAge',
'activityLevel',
'accountWeight',
'statistics',
'restrictions'
)
[
'accountAge' => $this->getRegisterDate($wechatId),
'activityLevel' => $this->getActivityLevel($wechatId),
'accountWeight' => $this->getAccountWeight($wechatId),
'statistics' => $this->getStatistics($wechatId),
'restrictions' => $this->getRestrict($wechatId),
]
);
} catch (\Exception $e) {
return ResponseHelper::error($e->getMessage(), $e->getCode());

View File

@@ -11,4 +11,6 @@
// 应用容器绑定定义
return [
// 类的映射表
'ClassTable' => app\common\service\ClassTableService::class,
];

View File

@@ -6,6 +6,8 @@ use AccountWeight\Exceptions\WechatAccountWeightAssessmentException as WeightAss
use library\ClassTable;
use library\interfaces\WechatAccountWeightResultSet as WechatAccountWeightResultSetInterface;
use library\interfaces\WechatAccountWeightAssessment as WechatAccountWeightAssessmentInterface;
use AccountWeight\UnitWeight;
use app\common\service\ClassTableService;
class WechatAccountWeightAssessment implements WechatAccountWeightAssessmentInterface
{
@@ -14,6 +16,17 @@ class WechatAccountWeightAssessment implements WechatAccountWeightAssessmentInte
private $activityWeigth;
private $restrictWeight;
private $realNameWeight;
protected $classTable;
/**
* 依赖注入
*
* @param ClassTableService|null $classTable
*/
public function __construct(ClassTableService $classTable = null)
{
$this->classTable = $classTable ?? app('ClassTable');
}
/**
* 获取言
@@ -34,7 +47,7 @@ class WechatAccountWeightAssessment implements WechatAccountWeightAssessmentInte
*/
public function calculAgeWeight(): WechatAccountWeightResultSetInterface
{
$AgeWeight = ClassTable::getSelfInstance()->getInstance(UnitWeight\AgeWeight::class);
$AgeWeight = $this->classTable->getInstance(UnitWeight\AgeWeight::class);
if (!$this->ageWeight) {
$this->ageWeight = $AgeWeight->settingFactor(
@@ -51,7 +64,7 @@ class WechatAccountWeightAssessment implements WechatAccountWeightAssessmentInte
*/
public function calculActivityWeigth(): WechatAccountWeightResultSetInterface
{
$ActivityWeigth = ClassTable::getSelfInstance()->getInstance(UnitWeight\ActivityWeigth::class);
$ActivityWeigth = $this->classTable->getInstance(UnitWeight\ActivityWeigth::class);
if (!$this->activityWeigth) {
$this->activityWeigth = $ActivityWeigth->settingFactor(
@@ -68,7 +81,7 @@ class WechatAccountWeightAssessment implements WechatAccountWeightAssessmentInte
*/
public function calculRestrictWeigth(): WechatAccountWeightResultSetInterface
{
$RestrictWeight = ClassTable::getSelfInstance()->getInstance(UnitWeight\RestrictWeight::class);
$RestrictWeight = $this->classTable->getInstance(UnitWeight\RestrictWeight::class);
if (!$this->restrictWeight) {
$this->restrictWeight = $RestrictWeight->settingFactor(
@@ -85,7 +98,7 @@ class WechatAccountWeightAssessment implements WechatAccountWeightAssessmentInte
*/
public function calculRealNameWeigth(): WechatAccountWeightResultSetInterface
{
$AccountWeight = ClassTable::getSelfInstance()->getInstance(UnitWeight\RealNameWeight::class);
$AccountWeight = $this->classTable->getInstance(UnitWeight\RealNameWeight::class);
if (!$this->realNameWeight) {
$this->realNameWeight = $AccountWeight->settingFactor(