feat : auto dispense coupon

This commit is contained in:
2025-03-19 16:25:54 +08:00
parent f4d681a23a
commit 44170132e7
10 changed files with 331 additions and 59 deletions

View File

@@ -15,6 +15,7 @@ use App\Cache\Redis\Common\CommonRedisKey;
use App\Cache\Redis\RedisCache;
use App\Constants\RedisCode;
use App\Exception\ErrException;
use App\Extend\SystemUtil;
use App\Lib\Log;
use GuzzleHttp\Exception\GuzzleException;
use Hyperf\Di\Annotation\Inject;
@@ -194,4 +195,54 @@ trait WxMiniTrait
throw new ErrException($e->getMessage());
}
}
/**
* @param string $templateId
* @param string $toUserOpenId
* @param array $data
* @param string|null $page
* @return mixed
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function sendSubMessage(string $templateId, string $toUserOpenId, array $data ,string $page = null): mixed
{
$url = sprintf(
'/cgi-bin/message/subscribe/send?access_token=%s',
$this->getAccessTokenCache()
);
$params = [
'template_id' => $templateId,
'touser' => $toUserOpenId,
'data' => $data,
'miniprogram_state' => !SystemUtil::checkProEnv() ? 'developer' : 'formal', //不是正式环境 使用开发版
'lang' => 'zh_CN'
];
if (!empty($page)) $params['page'] = $page;
try {
$wxResponse = $this->clientFactory->create([
'base_uri' => $this->BaseUri,
'timeout' => 5
])->post($url,[
'headers' => [
'Content-Type' => 'application/json'
],
'body' => json_encode($params)
]);
$contents = $wxResponse->getBody()->getContents();
$this->log->callbackLog(__class__.':微信服务器返回send_sub_message信息:'.json_encode($contents));
$res = json_decode($contents,true);
if (!empty($res['errcode']) && $res['errcode'] != 0) throw new ErrException($res['errmsg'] ?? '系统繁忙');
return $res;
}catch (GuzzleException $e){
throw new ErrException($e->getMessage());
}
}
}