短信完结

This commit is contained in:
2025-09-19 13:37:56 +08:00
parent fd82eaabb8
commit e9ae2c62f6
5 changed files with 708 additions and 30 deletions

66
app/model/AlibabaSms.php Normal file
View File

@@ -0,0 +1,66 @@
<?php
namespace app\model;
use think\Exception;
use support\Log;
use think\Model;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
class AlibabaSms extends Model
{
/**
* 使用AK&SK初始化账号Client
* @param string $accessKeyId
* @param string $accessKeySecret
* @return Dysmsapi Client
*/
public static function createClient($accessKeyId, $accessKeySecret){
$config = new Config([
"accessKeyId" => $accessKeyId,
"accessKeySecret" => $accessKeySecret
]);
$config->endpoint = "dysmsapi.aliyuncs.com";
return new Dysmsapi($config);
}
/**
* @param array $args
* @return void
*/
public static function main($args){
$client = self::createClient(config('cons.alibaba_sms.access_key_id'), config('cons.alibaba_sms.access_key_secret'));
$sendSmsRequest = new SendSmsRequest($args);
$runtime = new RuntimeOptions([]);
try {
// 复制代码运行请自行打印 API 的返回值
$res = $client->sendSmsWithOptions($sendSmsRequest, $runtime);
if($res->body->code == 'OK') {
return true;
}else {
return false;
}
}
catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
// 如有需要,请打印 error
Utils::assertAsString($error->message);
Log::info('短信发送错误--' . $error->message);
return false;
}
}
}

View File

@@ -25,14 +25,6 @@ class MessagePushTask
foreach ($res as $rk => &$rv) {
if(empty($rv['open_id'])) {
unset($res[$rk]);
}else {
$rv['push_event_id'] = $event['id'];
$rv['template_id'] = $event['template_id'];
$rv['keyword1'] = $event['keyword1'];
$rv['keyword2'] = $event['keyword2'];
$rv['keyword3'] = $event['keyword3'];
$rv['keyword4'] = $event['keyword4'];
$rv['keyword5'] = $event['keyword5'];
}
}
@@ -40,12 +32,12 @@ class MessagePushTask
// 需要接收推送的用户 准备发送
foreach ($user_list as $k => $user) {
$data = [
'thing2' => $user['keyword1'],
'amount3' => $user['keyword2'],
'time6' => $user['keyword3'],
'thing2' => $event['keyword1'],
'amount3' => $event['keyword2'],
'time6' => $event['keyword3'],
];
$user_push_list_res_id = Db::name('tb_user_push_event')->insertGetId([
'push_event_id' => $user['push_event_id'],
'push_event_id' => $event['id'],
'user_id' => $user['user_id'],
'create_time' => date("Y-m-d H:i:s"),
'content' => json_encode($data),
@@ -60,10 +52,10 @@ class MessagePushTask
// 正式推送
$push = $app->template_message->send([
'touser' => $user['open_id'],
'template_id' => $user['template_id'],
'template_id' => $event['template_id'],
'data' => $data
]);
Log::info('用户-' . $user['user_id'] . ' 推送模版ID'. $user['template_id'] .' 内容:' . json_encode($data, 256) . ' 推送结果:' . json_encode($push));
Log::info('用户-' . $user['user_id'] . ' 推送模版ID'. $event['template_id'] .' 内容:' . json_encode($data, 256) . ' 推送结果:' . json_encode($push));
if($push['errcode'] == 0) {
Db::name('tb_user_push_event')->where(['id' => $user_push_list_res_id])->update(['update_time' => date("Y-m-d H:i:s"), 'status' => 1]);
// 送券
@@ -107,22 +99,16 @@ class MessagePushTask
foreach ($res as $rk => &$rv) {
if(empty($rv['phone'])) {
unset($res[$rk]);
}else {
$rv['push_event_id'] = $event['id'];
$rv['sms_sign'] = $event['sms_sign'];
$rv['sms_code'] = $event['sms_code'];
$rv['sms_content'] = $event['sms_content'];
}
}
$user_list = uniqueMultidimensionalArray($res);
// 需要接收推送的用户 准备发送
foreach ($user_list as $k => $user) {
// 查询商家短信余额
$sms_shop = Db::name('tb_shop_sms_money')->where(['shop_id' => $event['shop_id']])->find();
if (!$sms_shop) {
$sms_shop_res_id = Db::name('tb_shop_sms_money')->insertGetId([
'shop_id' => $user['push_event_id'],
Db::name('tb_shop_sms_money')->insertGetId([
'shop_id' => $event['shop_id'],
'create_time' => date("Y-m-d H:i:s"),
]);
log::info('商户' . $event['shop_id'] . '不存在或余额不足,停止发送');
@@ -135,18 +121,21 @@ class MessagePushTask
continue 2;
}else {
$user_push_list_res_id = Db::name('tb_user_push_event')->insertGetId([
'push_event_id' => $user['push_event_id'],
'push_event_id' => $event['id'],
'user_id' => $user['user_id'],
'create_time' => date("Y-m-d H:i:s"),
'content' => json_encode($user),
]);
if ($user_push_list_res_id) {
// 发送短信空位置
// 发送短信
$ret = AlibabaSms::main([
'templateCode' => $event['sms_code'],
'templateParam' => json_encode($event['sms_content']),
'phoneNumbers' => $user['phone'],
'signName' => $event['sms_sign'],
]);
Log::info('用户-' . $user['user_id'] . ' 短信发送ID' . $user['template_id'] . ' 内容:' . json_encode($data, 256) . ' 推送结果:' . json_encode($push));
if ($push['errcode'] == 0) {
if ($ret) {
Db::name('tb_user_push_event')->where(['id' => $user_push_list_res_id])->update(['update_time' => date("Y-m-d H:i:s"), 'status' => 1]);
// 加入明细
$tb_shop_sms_money = Db::name('tb_shop_sms_money_detail')->insertGetId([