存客宝应用接口初始化
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);
|
||||
}
|
||||
}
|
||||
49
extend/AccountWeight/UnitWeight/ActivityWeigth.php
Normal file
49
extend/AccountWeight/UnitWeight/ActivityWeigth.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace AccountWeight\UnitWeight;
|
||||
|
||||
use app\common\model\WechatCustomer as WechatCustomerModel;
|
||||
use library\Interfaces\WechatAccountWeightResultSet as WechatAccountWeightResultSetInterface;
|
||||
|
||||
class ActivityWeigth implements WechatAccountWeightResultSetInterface
|
||||
{
|
||||
private $weight;
|
||||
|
||||
/**
|
||||
* 获取每天聊天次数。
|
||||
*
|
||||
* @param string $wechatId
|
||||
* @return int
|
||||
*/
|
||||
private function getChatTimesPerDay(string $wechatId): int
|
||||
{
|
||||
$activity = (string)WechatCustomerModel::where([
|
||||
'wechatId' => $wechatId,
|
||||
]
|
||||
)
|
||||
->value('activity');
|
||||
|
||||
return json_decode($activity)->yesterdayMsgCount ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
69
extend/AccountWeight/UnitWeight/AgeWeight.php
Normal file
69
extend/AccountWeight/UnitWeight/AgeWeight.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace AccountWeight\UnitWeight;
|
||||
|
||||
use app\common\model\WechatCustomer as WechatCustomerModel;
|
||||
use library\Interfaces\WechatAccountWeightResultSet as WechatAccountWeightResultSetInterface;
|
||||
|
||||
class AgeWeight implements WechatAccountWeightResultSetInterface
|
||||
{
|
||||
private $weight;
|
||||
|
||||
/**
|
||||
* 计算账号年龄(从创建时间到现在)
|
||||
*
|
||||
* @param string $wechatId
|
||||
* @return string
|
||||
*/
|
||||
private function getRegisterDate(string $wechatId): string
|
||||
{
|
||||
$basic = (string)WechatCustomerModel::where([
|
||||
'wechatId' => $wechatId,
|
||||
]
|
||||
)
|
||||
->value('basic');
|
||||
|
||||
$basic = json_decode($basic);
|
||||
|
||||
// 如果没有设置账号注册时间,则默认今天,即账号年龄为0
|
||||
return $basic && isset($basic->registerDate) ? $basic->registerDate : date('Y-m-d', time());
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算两个时间相差几个月
|
||||
*
|
||||
* @param string $wechatId
|
||||
* @return int
|
||||
* @throws \DateMalformedStringException
|
||||
*/
|
||||
private function getDateTimeDiff(string $wechatId): int
|
||||
{
|
||||
$currentData = new \DateTime(date('Y-m-d', 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
extend/AccountWeight/UnitWeight/RealNameWeight.php
Normal file
39
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
extend/AccountWeight/UnitWeight/RestrictWeight.php
Normal file
50
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;
|
||||
}
|
||||
}
|
||||
140
extend/AccountWeight/WechatAccountWeightAssessment.php
Normal file
140
extend/AccountWeight/WechatAccountWeightAssessment.php
Normal file
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace AccountWeight;
|
||||
|
||||
use AccountWeight\Exceptions\WechatAccountWeightAssessmentException as WeightAssessmentException;
|
||||
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
|
||||
{
|
||||
private $wechatId;
|
||||
private $ageWeight;
|
||||
private $activityWeigth;
|
||||
private $restrictWeight;
|
||||
private $realNameWeight;
|
||||
protected $classTable;
|
||||
|
||||
/**
|
||||
* 依赖注入
|
||||
*
|
||||
* @param ClassTableService|null $classTable
|
||||
*/
|
||||
public function __construct(ClassTableService $classTable = null)
|
||||
{
|
||||
$this->classTable = $classTable ?? app('ClassTable');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取言
|
||||
* @return string
|
||||
* @throws WechatAccountWeightAssessmentException
|
||||
*/
|
||||
private function getWechatId(): string
|
||||
{
|
||||
if (empty($this->wechatId)) {
|
||||
throw new WeightAssessmentException('缺少验证参数');
|
||||
}
|
||||
|
||||
return $this->wechatId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function calculAgeWeight(): WechatAccountWeightResultSetInterface
|
||||
{
|
||||
$AgeWeight = $this->classTable->getInstance(UnitWeight\AgeWeight::class);
|
||||
|
||||
if (!$this->ageWeight) {
|
||||
$this->ageWeight = $AgeWeight->settingFactor(
|
||||
$this->getWechatId()
|
||||
)
|
||||
->getResult();
|
||||
}
|
||||
|
||||
return $AgeWeight;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function calculActivityWeigth(): WechatAccountWeightResultSetInterface
|
||||
{
|
||||
$ActivityWeigth = $this->classTable->getInstance(UnitWeight\ActivityWeigth::class);
|
||||
|
||||
if (!$this->activityWeigth) {
|
||||
$this->activityWeigth = $ActivityWeigth->settingFactor(
|
||||
$this->getWechatId()
|
||||
)
|
||||
->getResult();
|
||||
}
|
||||
|
||||
return $ActivityWeigth;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function calculRestrictWeigth(): WechatAccountWeightResultSetInterface
|
||||
{
|
||||
$RestrictWeight = $this->classTable->getInstance(UnitWeight\RestrictWeight::class);
|
||||
|
||||
if (!$this->restrictWeight) {
|
||||
$this->restrictWeight = $RestrictWeight->settingFactor(
|
||||
$this->getWechatId()
|
||||
)
|
||||
->getResult();
|
||||
}
|
||||
|
||||
return $RestrictWeight;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function calculRealNameWeigth(): WechatAccountWeightResultSetInterface
|
||||
{
|
||||
$AccountWeight = $this->classTable->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;
|
||||
}
|
||||
}
|
||||
25
extend/AccountWeight/WechatFriendAddLimitAssessment.php
Normal file
25
extend/AccountWeight/WechatFriendAddLimitAssessment.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace AccountWeight;
|
||||
|
||||
use library\Interfaces\WechatAccountWeightAssessment as WechatAccountWeightAssessmentInterface;
|
||||
use library\Interfaces\WechatFriendAddLimitAssessment as WechatFriendAddLimitAssessmentInterface;
|
||||
|
||||
class WechatFriendAddLimitAssessment implements WechatFriendAddLimitAssessmentInterface
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function maxLimit(WechatAccountWeightAssessmentInterface $weight): int
|
||||
{
|
||||
$adjusted = $scope = $weight->getWeightScope();
|
||||
$lastDigit = $scope % 10;
|
||||
|
||||
if ($scope < 10) {
|
||||
$adjusted = $lastDigit < 5 ? 5 : 10;
|
||||
}
|
||||
|
||||
// 每5权重=1好友,最多20个
|
||||
return min(20, floor($adjusted / 5));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user