微信模版消息

This commit is contained in:
2025-10-20 15:52:32 +08:00
parent 9958530d54
commit 013a4ce5b6
5 changed files with 145 additions and 6 deletions

View File

@@ -55,7 +55,7 @@ class SendMarkSms implements Consumer
// 范围用户
if($record['user_type'] == 2) {
Log::info('范围用户');
$k_user_params = Db::table('sms_push_event_user')->where(['event_id' => $record['id']])->find();
$k_user_params = Db::table('sms_push_event_user')->where(['event_id' => $record['id'], 'type' => 'sms'])->find();
if($k_user_params) {
$user_list = ShopInfo::getUserList($k_user_params);
if(!$user_list) {
@@ -68,7 +68,7 @@ class SendMarkSms implements Consumer
}elseif ($record['user_type'] == 1) {
Log::info('全部用户');
// 全部用户
$user_list = Db::table('tb_shop_user')->whereNotNull('phone')->field('id,phone,nick_name')->select();
$user_list = Db::table('tb_shop_user')->where(['main_shop_id' => $record['shop_id']])->whereNotNull('phone')->field('id,phone,nick_name')->select();
$user_list = ShopInfo::evnuserlist($user_list);
}
$y_temp = Db::table('sms_shop_template')->where(['id' => $record['push_event_id']])->find();
@@ -101,6 +101,7 @@ class SendMarkSms implements Consumer
'shop_id' => $record['shop_id'],
'event_id' => $record['id'],
'user_id' => $v['user_id'],
'type' => 'sms',
'user_phone' => $v['phone'],
'biz_id' => $res['BizId'],
'send_time' => date('Y-m-d H:i:s'),

View File

@@ -4,7 +4,11 @@ namespace app\queue\redis;
use app\model\AlibabaSms;
use app\model\ShopInfo;
use ba\Exception;
use EasyWeChat\Factory;
use support\Log;
use support\Redis;
use support\think\Db;
use Webman\RedisQueue\Consumer;
@@ -16,9 +20,125 @@ class SendWechatTemp implements Consumer
public function consume($data)
{
Log::info('消息队列【发送微信模版消息】接收到消息' . $data);
try {
Log::info('消息队列【发送微信模版消息】接收到消息' . $data);
// 区分数据
$arr = explode(',', $data);
if(is_array($arr)) {
$shop_id = $arr[0];
$sms_push_event_id = $arr[1];
}else {
Log::info('send.wechat.temp数据格式错误');
return false;
}
if($data) {
// 发送任务
// $record = Db::table('sms_push_event')->where(['id' => $sms_push_event_id, 'push_type' => 2])->find();
$record = Db::table('ac_push_event')->where(['id' => $sms_push_event_id, 'status' => 0])->find();
if($record) {
// 查询是否是定时发送
if($record['send_type'] == 2 && $record['send_time'] > date('Y-m-d H:i:s')) {
// 推给redis
$stortime = strtotime($record['send_time']);
$a = Redis::setEx('expired:wechat:'.$record['id'], $stortime - time(), 1);
Log::info('发送微信模版消息定时发送已存入redis, 过期时间->>>' .$record['send_time'] . '/' . $stortime - time() . '保存结果' . $a);
}else {
// 待发送
if($record['status'] == 0) {
Log::info('待发送');
// 范围用户
if($record['user_type'] == 2) {
Log::info('范围用户');
$k_user_params = Db::table('sms_push_event_user')->where(['event_id' => $record['id'], 'type' => 'ac'])->find();
if($k_user_params) {
$user_list = ShopInfo::getUserList($k_user_params);
if(!$user_list) {
return false;
}
}else {
Log::info('范围用户查找出错');
return false;
}
}elseif ($record['user_type'] == 1) {
Log::info('全部用户');
// 全部用户
$shop = Db::name('tb_shop_info')->where(['id' => $record['shop_id']])->find();
if(!$shop) {
Log::info('微信模版推送时商户未找到' . $record['id']);
}
if(!empty($shop['main_id'])) {
$shop_id = $shop['main_id'];
}else {
$shop_id = $shop['id'];
}
$user_list = Db::name('tb_shop_user')
->alias('su')
->join('tb_user_info ui', 'su.user_id = ui.id')
->where('su.main_shop_id', $shop_id)
->where('ui.wechat_ac_open_id', '<>', '')
->whereNotNull('ui.wechat_ac_open_id')
->find('DISTINCT ui.wechat_ac_open_id,ui.id,ui.phone')
->select();
}
Log::info('模版消息用户列表' . json_encode($user_list));
if($user_list) {
foreach ($user_list as $k => $user) {
$config = [
'app_id' => config('cons.app_id'),
'secret' => config('cons.secret'),
'response_type' => 'array',
];
$app = Factory::officialAccount($config);
$wechat_res = $app->template_message->send([
'touser' => $user['wechat_ac_open_id'],
'template_id' => 'hGsUGZlWqWC9TMm4ZGZuz0OwE4gwSmvgbr5ecmTDvq4',
'miniprogram' => [
'appid' => 'wxd88fffa983758a30', // 零点八零
],
'data' => [
'thing7' => $record['shop_name'],
'thing14' => $record['activity_detail'],
'time3' => $record['activity_time'],
'thing16' => $record['address'],
],
]);
Db::startTrans();
Db::table('sms_push_event')->where(['id' => $record['id']])->update([
'status' => '1',
'update_time' => date('Y-m-d H:i:s'),
'send_time' => date('Y-m-d H:i:s'),
]);
Log::info('模版发送结果' . json_encode($wechat_res));
if($wechat_res['errcode'] == 0 && $wechat_res['errmsg'] == 'ok') {
Log::info('OKKKKK了');
Db::table('sms_push_event_record')->insert([
'shop_id' => $record['shop_id'],
'event_id' => $record['id'],
'user_id' => $user['id'],
'user_phone' => $user['phone'],
'biz_id' => '',
'send_time' => date('Y-m-d H:i:s'),
'create_time' => date('Y-m-d H:i:s'),
'status' => 2,
]);
}
Db::commit();
sleep(1);
}
}else {
Log::info('【发送营销短信】无目标用户---》' . $record['user_id']);
}
}
}
}else {
Log::info('发送营销短信未查询到');
}
}
}catch (Exception $e) {
Db::rollback();
Log::error($e->getMessage());
}