p_ysk/app/queue/redis/SendMarkSms.php

128 lines
6.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\queue\redis;
use app\model\AlibabaSms;
use app\model\ShopInfo;
use ba\Exception;
use support\Log;
use support\Redis;
use support\think\Db;
use Webman\RedisQueue\Consumer;
// 发送营销短信
class SendMarkSms implements Consumer
{
public $queue = 'send.mark.sms';
public function consume($data)
{
try {
Db::startTrans();
Log::info('消息队列【发送营销短信】接收到消息' . $data);
// 区分数据
$arr = explode(',', $data);
if(is_array($arr)) {
$shop_id = $arr[0];
$sms_push_event_id = $arr[1];
}else {
Log::info('send.mark.sms数据格式错误');
return false;
}
if($data) {
Log::info('$data');
// 发送任务
// $record = Db::table('sms_push_event')->where(['id' => $sms_push_event_id, 'push_type' => 2])->find();
$record = Db::table('sms_push_event')->where(['id' => $sms_push_event_id])->find();
if($record) {
// 查询是否是定时发送
if($record['send_type'] == 2 && $record['send_time'] > date('Y-m-d H:i:s')) {
// 推给redis
$stortime = strtotime($record['send_time']);
Redis::setEx('expired:sms:'.$record['id'], $stortime - time(), 1);
Log::info('定时发送已存入redis');
}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']])->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('全部用户');
// 全部用户
$user_list = Db::table('tb_shop_user')->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();
Log::info('用户列表' . json_encode($user_list));
if($user_list && $y_temp) {
foreach ($user_list as $k => $v) {
// 检测余额
$shop_money = Db::table('sms_shop_money')->where(['shop_id' => $record['shop_id']])->find();
if($shop_money) {
// 余额计算
if($shop_money['money'] > config('cons.sms_price')) {
$templateParam = replace_json_keys($record['json'], $y_temp['content_json'], $v);
$data = [
'templateCode' => $y_temp['template_code'],
'templateParam' => $templateParam,
'phoneNumbers' => $v['phone'],
'signName' => config('cons.sms_sign'),
];
$res = AlibabaSms::main($data);
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'),
]);
if($res['Code'] == 'OK') {
Db::table('sms_push_event_record')->insert([
'shop_id' => $record['shop_id'],
'event_id' => $record['id'],
'user_id' => $v['user_id'],
'user_phone' => $v['phone'],
'biz_id' => $res['BizId'],
'send_time' => date('Y-m-d H:i:s'),
'create_time' => date('Y-m-d H:i:s'),
'status' => 1,
]);
Db::commit();
}
}else {
Log::info('商户【' . $record['shop_id'] . '】余额不足');
return false;
}
}else {
Log::info('商户【' . $record['shop_id'] . '】余额为空');
return false;
}
sleep(1);
}
}else {
Log::info('【发送营销短信】无目标用户---》' . $record['user_id']);
}
}
}
}else {
Log::info('发送营销短信未查询到');
}
}
}catch (Exception $e) {
Log::error($e->getMessage());
}
}
}