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

@@ -4,6 +4,7 @@ namespace App\Service\ServiceTrait\Api;
use App\Constants\Common\CouponCode;
use App\Constants\Common\OrderCode;
use App\Extend\DateUtil;
use App\Model\UserCoupon;
use Exception;
use Hyperf\Di\Annotation\Inject;
@@ -38,4 +39,25 @@ trait CouponTrait
if (!$couponInfo->save()) throw new Exception('CancelOrderConsumer:error:couponStatusUpdateError:'.json_encode($orderInfo->toArray()));
}
/**
* @param int $type
* @param string $value
* @return array
*/
protected function getValidityTime(int $type,string $value): array
{
switch ($type) {
case CouponCode::VALIDITY_TIME_TYPE_CYCLE:
$validityValue = json_decode($value,true);
return [
'start_time' => date('Y-m-d H:i:s'),
'end_time' => date('Y-m-d H:i:s', time() + ($validityValue['day_num'] * DateUtil::DAY))
];
case CouponCode::VALIDITY_TIME_TYPE_FIX:
return json_decode($value,true);
default:
return [];
}
}
}

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());
}
}
}