170 lines
9.1 KiB
PHP
170 lines
9.1 KiB
PHP
<?php
|
||
|
||
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;
|
||
|
||
// 发送短信模版
|
||
class SendWechatTemp implements Consumer
|
||
{
|
||
|
||
public $queue = 'send.wechat.temp';
|
||
|
||
public function consume($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, 'is_del' => 0])->find();
|
||
if($record) {
|
||
// 查询是否是定时发送
|
||
if($record['send_type'] == 2 && $record['send_time'] > date('Y-m-d H:i:s') && $record['send_time'] <= date('Y-m-d 23:59:59')) {
|
||
// 推给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) {
|
||
Log::info('范围用户列表为空');
|
||
return false;
|
||
}
|
||
foreach ($user_list as $k => &$v) {
|
||
$u = Db::name('tb_user_info')->where(['id' => $v['u_user_id']])->find();
|
||
if($u) {
|
||
$v['wechat_ac_open_id'] = $u['wechat_ac_open_id'];
|
||
}
|
||
}
|
||
}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.is_ac', 1)
|
||
->where('ui.wechat_ac_open_id', '<>', '')
|
||
->whereNotNull('ui.wechat_ac_open_id')
|
||
->field('DISTINCT ui.wechat_ac_open_id,ui.id,ui.phone,ui.id as u_user_id')
|
||
->select();
|
||
}
|
||
Log::info('模版消息用户列表' . json_encode($user_list));
|
||
if($user_list) {
|
||
foreach ($user_list as $k => $user) {
|
||
if(!empty($user['wechat_ac_open_id'])) {
|
||
$config = [
|
||
'app_id' => config('cons.app_id'),
|
||
'secret' => config('cons.secret'),
|
||
'response_type' => 'array',
|
||
];
|
||
$app = Factory::officialAccount($config);
|
||
$acc_token = Redis::get('wx:ac:AccessToken');
|
||
if($acc_token) {
|
||
$app['access_token']->setToken($acc_token);
|
||
}
|
||
$wechat_res = $app->template_message->send([
|
||
'touser' => $user['wechat_ac_open_id'],
|
||
'template_id' => 'hGsUGZlWqWC9TMm4ZGZuz0OwE4gwSmvgbr5ecmTDvq4',
|
||
'miniprogram' => [
|
||
'appid' => 'wxd88fffa983758a30', // 零点八零
|
||
'pagepath' => 'pages/index/index', // 零点八零
|
||
],
|
||
'data' => [
|
||
'thing7' => $record['shop_name'],
|
||
'thing14' => $record['activity_detail'],
|
||
'time3' => $record['activity_time'],
|
||
'thing16' => $record['address'],
|
||
],
|
||
]);
|
||
|
||
Db::table('ac_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') {
|
||
Db::startTrans();
|
||
Log::info('OKKKKK了');
|
||
Db::table('sms_push_event_record')->insert([
|
||
'type' => 'ac',
|
||
'shop_id' => $record['shop_id'],
|
||
'event_id' => $record['id'],
|
||
'user_id' => $user['u_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::table('ac_push_event')->where(['id' => $record['id']])->update([
|
||
'status' => 2,
|
||
'update_time' => date('Y-m-d H:i:s'),
|
||
'send_time' => date('Y-m-d H:i:s'),
|
||
]);
|
||
Db::commit();
|
||
}
|
||
}else {
|
||
Log::info('用户' . $user['u_user_id'] . 'open_id 为空。不发送');
|
||
}
|
||
sleep(1);
|
||
}
|
||
}else {
|
||
Log::info('【发送营销短信】无目标用户---》' . $record['user_id']);
|
||
}
|
||
}
|
||
}
|
||
}else {
|
||
Log::info('发送营销短信未查询到');
|
||
}
|
||
}
|
||
}catch (\Throwable $e) {
|
||
Db::rollback();
|
||
Log::error($e->getMessage());
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|
||
} |