From 916c39d737c04f053a396306b8007d102af3481a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E6=B8=85=E7=88=BD?= Date: Thu, 15 May 2025 10:20:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=20ClassTable:class=20=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E5=88=B0=E6=9C=8D=E5=8A=A1=E5=AE=B9=E5=99=A8=E5=86=85?= =?UTF-8?q?=E6=96=B9=E4=BE=BF=E5=85=A8=E5=B1=80=E8=A7=84=E8=8C=83=E8=B0=83?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/service/ClassTableService.php | 82 +++++++++++++++++++ .../cunkebao/controller/BaseController.php | 16 ++++ ...GetWechatOnDeviceSummarizeV1Controller.php | 32 +++----- Server/application/provider.php | 2 + .../WechatAccountWeightAssessment.php | 21 ++++- 5 files changed, 130 insertions(+), 23 deletions(-) create mode 100644 Server/application/common/service/ClassTableService.php diff --git a/Server/application/common/service/ClassTableService.php b/Server/application/common/service/ClassTableService.php new file mode 100644 index 00000000..365e8827 --- /dev/null +++ b/Server/application/common/service/ClassTableService.php @@ -0,0 +1,82 @@ +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); + } +} \ No newline at end of file diff --git a/Server/application/cunkebao/controller/BaseController.php b/Server/application/cunkebao/controller/BaseController.php index 852858f9..961497ef 100644 --- a/Server/application/cunkebao/controller/BaseController.php +++ b/Server/application/cunkebao/controller/BaseController.php @@ -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(); + } + /** * 初始化 */ diff --git a/Server/application/cunkebao/controller/wechat/GetWechatOnDeviceSummarizeV1Controller.php b/Server/application/cunkebao/controller/wechat/GetWechatOnDeviceSummarizeV1Controller.php index 53656a5c..1a7559dd 100644 --- a/Server/application/cunkebao/controller/wechat/GetWechatOnDeviceSummarizeV1Controller.php +++ b/Server/application/cunkebao/controller/wechat/GetWechatOnDeviceSummarizeV1Controller.php @@ -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()); diff --git a/Server/application/provider.php b/Server/application/provider.php index d0fcd245..d453b844 100644 --- a/Server/application/provider.php +++ b/Server/application/provider.php @@ -11,4 +11,6 @@ // 应用容器绑定定义 return [ + // 类的映射表 + 'ClassTable' => app\common\service\ClassTableService::class, ]; diff --git a/Server/extend/AccountWeight/WechatAccountWeightAssessment.php b/Server/extend/AccountWeight/WechatAccountWeightAssessment.php index cd773586..b4528f2a 100644 --- a/Server/extend/AccountWeight/WechatAccountWeightAssessment.php +++ b/Server/extend/AccountWeight/WechatAccountWeightAssessment.php @@ -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(