私域操盘手 - 调整微信号的权重策略计算规则,重构为适配器兼容扩展规则
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace AccountWeight\Exceptions;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class WechatAccountWeightAssessmentException extends \Exception
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function __construct($message = '', $code = 22921, Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
42
Server/extend/AccountWeight/UnitWeight/ActivityWeigth.php
Normal file
42
Server/extend/AccountWeight/UnitWeight/ActivityWeigth.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace AccountWeight\UnitWeight;
|
||||
|
||||
use library\interfaces\WechatAccountWeightResultSet as WechatAccountWeightResultSetInterface;
|
||||
|
||||
class ActivityWeigth implements WechatAccountWeightResultSetInterface
|
||||
{
|
||||
private $weight;
|
||||
|
||||
/**
|
||||
* 获取每天聊天次数。
|
||||
*
|
||||
* @param string $wechatId
|
||||
* @return int
|
||||
*/
|
||||
private function getChatTimesPerDay(string $wechatId): int
|
||||
{
|
||||
return mt_rand(0, 100);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function settingFactor($wechatId): WechatAccountWeightResultSetInterface
|
||||
{
|
||||
$times = intval($this->getChatTimesPerDay($wechatId) / 50 * 100);
|
||||
|
||||
// 规定每天发送50条消息起拥有最高权重
|
||||
$this->weight = $times > 100 ? 100 : $times;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getResult(): int
|
||||
{
|
||||
return $this->weight ?: 0;
|
||||
}
|
||||
}
|
||||
59
Server/extend/AccountWeight/UnitWeight/AgeWeight.php
Normal file
59
Server/extend/AccountWeight/UnitWeight/AgeWeight.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace AccountWeight\UnitWeight;
|
||||
|
||||
use library\interfaces\WechatAccountWeightResultSet as WechatAccountWeightResultSetInterface;
|
||||
|
||||
class AgeWeight implements WechatAccountWeightResultSetInterface
|
||||
{
|
||||
private $weight;
|
||||
|
||||
/**
|
||||
* 计算账号年龄(从创建时间到现在)
|
||||
*
|
||||
* @param string $wechatId
|
||||
* @return string
|
||||
*/
|
||||
private function getRegisterDate(string $wechatId): string
|
||||
{
|
||||
return date('Y-m-d H:i:s', strtotime('-15 months'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算两个时间相差几个月
|
||||
*
|
||||
* @param string $wechatId
|
||||
* @return int
|
||||
* @throws \DateMalformedStringException
|
||||
*/
|
||||
private function getDateTimeDiff(string $wechatId): int
|
||||
{
|
||||
$currentData = new \DateTime(date('Y-m-d H:i:s', time()));
|
||||
$registerDate = new \DateTime($this->getRegisterDate($wechatId));
|
||||
|
||||
$interval = date_diff($currentData, $registerDate);
|
||||
|
||||
return $interval->y * 12 + $interval->m;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function settingFactor($wechatId): WechatAccountWeightResultSetInterface
|
||||
{
|
||||
$cha = ceil($this->getDateTimeDiff($wechatId) / 60) * 100;
|
||||
|
||||
// 规定账号年龄五年起拥有最高权重
|
||||
$this->weight = $cha > 100 ? 100 : $cha;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getResult(): int
|
||||
{
|
||||
return $this->weight ?: 0;
|
||||
}
|
||||
}
|
||||
39
Server/extend/AccountWeight/UnitWeight/RealNameWeight.php
Normal file
39
Server/extend/AccountWeight/UnitWeight/RealNameWeight.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace AccountWeight\UnitWeight;
|
||||
|
||||
use library\interfaces\WechatAccountWeightResultSet as WechatAccountWeightResultSetInterface;
|
||||
|
||||
class RealNameWeight implements WechatAccountWeightResultSetInterface
|
||||
{
|
||||
private $weight;
|
||||
|
||||
/**
|
||||
* 使用微信要求必须得实名,所以统一返回满分
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
private function hereWeGo(): int
|
||||
{
|
||||
return 100;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function settingFactor($wechatId): WechatAccountWeightResultSetInterface
|
||||
{
|
||||
$this->weight = $this->hereWeGo();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getResult(): int
|
||||
{
|
||||
return $this->weight ?: 0;
|
||||
}
|
||||
}
|
||||
50
Server/extend/AccountWeight/UnitWeight/RestrictWeight.php
Normal file
50
Server/extend/AccountWeight/UnitWeight/RestrictWeight.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace AccountWeight\UnitWeight;
|
||||
|
||||
use app\common\model\WechatRestricts as WechatRestrictsModel;
|
||||
use library\interfaces\WechatAccountWeightResultSet as WechatAccountWeightResultSetInterface;
|
||||
|
||||
class RestrictWeight implements WechatAccountWeightResultSetInterface
|
||||
{
|
||||
private $weight;
|
||||
|
||||
/**
|
||||
* 获取限制记录
|
||||
*
|
||||
* @param string $wechatId
|
||||
* @return int
|
||||
*/
|
||||
private function getRestrictCount(string $wechatId): int
|
||||
{
|
||||
return WechatRestrictsModel::alias('r')
|
||||
->field(
|
||||
[
|
||||
'r.id', 'r.restrictTime date', 'r.level', 'r.reason'
|
||||
]
|
||||
)
|
||||
->where('r.wechatId', $wechatId)->select()
|
||||
->count('*');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function settingFactor($wechatId): WechatAccountWeightResultSetInterface
|
||||
{
|
||||
$restrict = 10 - $this->getRestrictCount($wechatId);
|
||||
|
||||
// 规定没有限制记录拥有最高权重,10条以上权重为0
|
||||
$this->weight = ($restrict < 0 ? 0 : $restrict) * 10;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getResult(): int
|
||||
{
|
||||
return $this->weight ?: 0;
|
||||
}
|
||||
}
|
||||
127
Server/extend/AccountWeight/WechatAccountWeightAssessment.php
Normal file
127
Server/extend/AccountWeight/WechatAccountWeightAssessment.php
Normal file
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
namespace AccountWeight;
|
||||
|
||||
use AccountWeight\Exceptions\WechatAccountWeightAssessmentException as WeightAssessmentException;
|
||||
use library\ClassTable;
|
||||
use library\interfaces\WechatAccountWeightResultSet as WechatAccountWeightResultSetInterface;
|
||||
use library\interfaces\WechatAccountWeightAssessment as WechatAccountWeightAssessmentInterface;
|
||||
|
||||
class WechatAccountWeightAssessment implements WechatAccountWeightAssessmentInterface
|
||||
{
|
||||
private $wechatId;
|
||||
private $ageWeight;
|
||||
private $activityWeigth;
|
||||
private $restrictWeight;
|
||||
private $realNameWeight;
|
||||
|
||||
/**
|
||||
* 获取言
|
||||
* @return string
|
||||
* @throws WechatAccountWeightAssessmentException
|
||||
*/
|
||||
private function getWechatId(): string
|
||||
{
|
||||
if (empty($this->wechatId)) {
|
||||
throw new WeightAssessmentException('缺少验证参数');
|
||||
}
|
||||
|
||||
return $this->wechatId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function calculAgeWeight(): WechatAccountWeightResultSetInterface
|
||||
{
|
||||
$AgeWeight = ClassTable::getSelfInstance()->getInstance(UnitWeight\AgeWeight::class);
|
||||
|
||||
if (!$this->ageWeight) {
|
||||
$this->ageWeight = $AgeWeight->settingFactor(
|
||||
$this->getWechatId()
|
||||
)
|
||||
->getResult();
|
||||
}
|
||||
|
||||
return $AgeWeight;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function calculActivityWeigth(): WechatAccountWeightResultSetInterface
|
||||
{
|
||||
$ActivityWeigth = ClassTable::getSelfInstance()->getInstance(UnitWeight\ActivityWeigth::class);
|
||||
|
||||
if (!$this->activityWeigth) {
|
||||
$this->activityWeigth = $ActivityWeigth->settingFactor(
|
||||
$this->getWechatId()
|
||||
)
|
||||
->getResult();
|
||||
}
|
||||
|
||||
return $ActivityWeigth;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function calculRestrictWeigth(): WechatAccountWeightResultSetInterface
|
||||
{
|
||||
$RestrictWeight = ClassTable::getSelfInstance()->getInstance(UnitWeight\RestrictWeight::class);
|
||||
|
||||
if (!$this->restrictWeight) {
|
||||
$this->restrictWeight = $RestrictWeight->settingFactor(
|
||||
$this->getWechatId()
|
||||
)
|
||||
->getResult();
|
||||
}
|
||||
|
||||
return $RestrictWeight;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function calculRealNameWeigth(): WechatAccountWeightResultSetInterface
|
||||
{
|
||||
$AccountWeight = ClassTable::getSelfInstance()->getInstance(UnitWeight\RealNameWeight::class);
|
||||
|
||||
if (!$this->realNameWeight) {
|
||||
$this->realNameWeight = $AccountWeight->settingFactor(
|
||||
$this->getWechatId()
|
||||
)
|
||||
->getResult();
|
||||
}
|
||||
|
||||
return $AccountWeight;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getWeightScope(): int
|
||||
{
|
||||
return ceil(
|
||||
(
|
||||
$this->calculAgeWeight()->getResult() +
|
||||
$this->calculActivityWeigth()->getResult() +
|
||||
$this->calculRestrictWeigth()->getResult() +
|
||||
$this->calculRealNameWeigth()->getResult()
|
||||
) / 4);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function settingFactor($params): WechatAccountWeightAssessmentInterface
|
||||
{
|
||||
if (!is_string($params)) {
|
||||
throw new WeightAssessmentException('参数错误,只能传微信ID');
|
||||
}
|
||||
|
||||
$this->wechatId = $params;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user