first commit
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service\qywx;
|
||||
|
||||
use EasyWeChat\Kernel\Exceptions\RuntimeException;
|
||||
use EasyWeChat\OpenWork\Contracts\SuiteTicket;
|
||||
use think\facade\Db;
|
||||
|
||||
/** 将企业微信每十分钟推送的 suite_ticket 加密持久化,避免进程/缓存重启后丢失。 */
|
||||
class QywxPromotionSuiteTicket implements SuiteTicket
|
||||
{
|
||||
public function __construct(private readonly string $suiteId)
|
||||
{
|
||||
}
|
||||
|
||||
public function getTicket(): string
|
||||
{
|
||||
$cipher = (string) (Db::name('qywx_promotion_provider_state')
|
||||
->where('suite_id', $this->suiteId)
|
||||
->value('suite_ticket_cipher') ?? '');
|
||||
if ($cipher === '') {
|
||||
throw new RuntimeException('No suite_ticket found. 请先在企业微信服务商后台配置并验证应用指令回调。');
|
||||
}
|
||||
|
||||
return QywxPromotionCredentialCipher::decrypt($cipher);
|
||||
}
|
||||
|
||||
public function setTicket(string $ticket): static
|
||||
{
|
||||
$now = time();
|
||||
$cipher = QywxPromotionCredentialCipher::encrypt($ticket);
|
||||
$exists = Db::name('qywx_promotion_provider_state')->where('suite_id', $this->suiteId)->find();
|
||||
if ($exists) {
|
||||
Db::name('qywx_promotion_provider_state')->where('suite_id', $this->suiteId)->update([
|
||||
'suite_ticket_cipher' => $cipher,
|
||||
'ticket_received_at' => $now,
|
||||
'update_time' => $now,
|
||||
]);
|
||||
} else {
|
||||
Db::name('qywx_promotion_provider_state')->insert([
|
||||
'suite_id' => $this->suiteId,
|
||||
'suite_ticket_cipher' => $cipher,
|
||||
'ticket_received_at' => $now,
|
||||
'create_time' => $now,
|
||||
'update_time' => $now,
|
||||
]);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user