拼团
This commit is contained in:
@@ -27,176 +27,231 @@ class SendWechatTemp implements Consumer
|
|||||||
if(is_array($arr)) {
|
if(is_array($arr)) {
|
||||||
$shop_id = $arr[0];
|
$shop_id = $arr[0];
|
||||||
$sms_push_event_id = $arr[1];
|
$sms_push_event_id = $arr[1];
|
||||||
|
$type = $arr[2];
|
||||||
}else {
|
}else {
|
||||||
Log::info('send.wechat.temp数据格式错误');
|
Log::info('send.wechat.temp数据格式错误');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if($data) {
|
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,ui.wechat_open_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',
|
|
||||||
];
|
|
||||||
// 公众号存放 AccessToken
|
|
||||||
$acc_token = Redis::get('wx:ac:AccessToken');
|
|
||||||
// 公众号发送模版消息通知
|
|
||||||
$app = Factory::officialAccount($config);
|
|
||||||
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'],
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
Log::info('公众号模版发送结果' . json_encode($wechat_res));
|
|
||||||
}else {
|
|
||||||
Log::info('用户' . $user['u_user_id'] . 'wechat_ac_open_id为空。不发送');
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!empty($user['wechat_open_id'])) {
|
if($type == 'sendWechatTemp') {
|
||||||
$mini_config = [
|
// 发送任务
|
||||||
'app_id' => config('cons.mini_app_id'),
|
$record = Db::table('ac_push_event')->where(['id' => $sms_push_event_id, 'status' => 0, 'is_del' => 0])->find();
|
||||||
'secret' => config('cons.mini_secret'),
|
if($record) {
|
||||||
'response_type' => 'array',
|
// 查询是否是定时发送
|
||||||
];
|
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')) {
|
||||||
$mini_app = Factory::miniProgram($mini_config);
|
// 推给redis
|
||||||
// 小程序存放 AccessToken
|
$stortime = strtotime($record['send_time']);
|
||||||
$mini_token = Redis::get('wx:mini:AccessToken');
|
$a = Redis::setEx('expired:wechat:'.$record['id'], $stortime - time(), 1);
|
||||||
if($mini_token) {
|
Log::info('发送微信模版消息定时发送,已存入redis, 过期时间->>>' .$record['send_time'] . '/' . $stortime - time() . '保存结果' . $a);
|
||||||
$mini_app['access_token']->setToken($acc_token);
|
}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'];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// 小程序发送模版消息通知
|
|
||||||
$mini_wechat_res = $mini_app->subscribe_message->send([
|
|
||||||
'template_id' => 'JGPAGmqcPEgWB6mvAl0SC5cMqr5H5Qjcim8JCpHAZd0',
|
|
||||||
'touser' => $user['wechat_open_id'],
|
|
||||||
'page' => 'pages/index/index',
|
|
||||||
'data' => [
|
|
||||||
'thing1' => [
|
|
||||||
'value' => $record['shop_name'],
|
|
||||||
],
|
|
||||||
'thing2' => [
|
|
||||||
'value' => $record['activity_detail'],
|
|
||||||
],
|
|
||||||
'time3' => [
|
|
||||||
'value' => $record['activity_time'],
|
|
||||||
],
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
Log::info('小程序订阅消息发送结果' . json_encode($mini_wechat_res));
|
|
||||||
}else {
|
}else {
|
||||||
Log::info('用户' . $user['u_user_id'] . 'wechat_open_id为空。不发送');
|
Log::info('范围用户查找出错');
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
if(!empty($user['wechat_open_id']) || !empty($user['wechat_ac_open_id'])) {
|
}elseif ($record['user_type'] == 1) {
|
||||||
Db::table('ac_push_event')->where(['id' => $record['id']])->update([
|
Log::info('全部用户');
|
||||||
'status' => 1,
|
// 全部用户
|
||||||
'update_time' => date('Y-m-d H:i:s'),
|
$shop = Db::name('tb_shop_info')->where(['id' => $record['shop_id']])->find();
|
||||||
'send_time' => date('Y-m-d H:i:s'),
|
if(!$shop) {
|
||||||
]);
|
Log::info('微信模版推送时商户未找到' . $record['id']);
|
||||||
}
|
}
|
||||||
if((isset($wechat_res) && $wechat_res['errcode'] == 0 && $wechat_res['errmsg'] == 'ok') || (isset($mini_wechat_res) && $mini_wechat_res['errcode'] == 0 && $mini_wechat_res['errmsg'] == 'ok')) {
|
if(!empty($shop['main_id'])) {
|
||||||
Db::startTrans();
|
$shop_id = $shop['main_id'];
|
||||||
Db::table('sms_push_event_record')->insert([
|
}else {
|
||||||
'type' => 'ac',
|
$shop_id = $shop['id'];
|
||||||
'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();
|
|
||||||
}
|
}
|
||||||
|
$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,ui.wechat_open_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',
|
||||||
|
];
|
||||||
|
// 公众号存放 AccessToken
|
||||||
|
$acc_token = Redis::get('wx:ac:AccessToken');
|
||||||
|
// 公众号发送模版消息通知
|
||||||
|
$app = Factory::officialAccount($config);
|
||||||
|
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'],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
Log::info('公众号模版发送结果' . json_encode($wechat_res));
|
||||||
|
}else {
|
||||||
|
Log::info('用户' . $user['u_user_id'] . 'wechat_ac_open_id为空。不发送');
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!empty($user['wechat_open_id'])) {
|
||||||
|
$mini_config = [
|
||||||
|
'app_id' => config('cons.mini_app_id'),
|
||||||
|
'secret' => config('cons.mini_secret'),
|
||||||
|
'response_type' => 'array',
|
||||||
|
];
|
||||||
|
$mini_app = Factory::miniProgram($mini_config);
|
||||||
|
// 小程序存放 AccessToken
|
||||||
|
$mini_token = Redis::get('wx:mini:AccessToken');
|
||||||
|
if($mini_token) {
|
||||||
|
$mini_app['access_token']->setToken($mini_token);
|
||||||
|
}
|
||||||
|
// 小程序发送模版消息通知
|
||||||
|
$mini_wechat_res = $mini_app->subscribe_message->send([
|
||||||
|
'template_id' => 'JGPAGmqcPEgWB6mvAl0SC5cMqr5H5Qjcim8JCpHAZd0',
|
||||||
|
'touser' => $user['wechat_open_id'],
|
||||||
|
'page' => 'pages/index/index',
|
||||||
|
'data' => [
|
||||||
|
'thing1' => [
|
||||||
|
'value' => $record['shop_name'],
|
||||||
|
],
|
||||||
|
'thing2' => [
|
||||||
|
'value' => $record['activity_detail'],
|
||||||
|
],
|
||||||
|
'time3' => [
|
||||||
|
'value' => $record['activity_time'],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
Log::info('小程序订阅消息发送结果' . json_encode($mini_wechat_res));
|
||||||
|
}else {
|
||||||
|
Log::info('用户' . $user['u_user_id'] . 'wechat_open_id为空。不发送');
|
||||||
|
}
|
||||||
|
if(!empty($user['wechat_open_id']) || !empty($user['wechat_ac_open_id'])) {
|
||||||
|
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'),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
if((isset($wechat_res) && $wechat_res['errcode'] == 0 && $wechat_res['errmsg'] == 'ok') || (isset($mini_wechat_res) && $mini_wechat_res['errcode'] == 0 && $mini_wechat_res['errmsg'] == 'ok')) {
|
||||||
|
Db::startTrans();
|
||||||
|
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('【发送营销短信】无目标用户---》' . $record['user_id']);
|
||||||
}
|
}
|
||||||
}else {
|
|
||||||
Log::info('【发送营销短信】无目标用户---》' . $record['user_id']);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}else {
|
||||||
|
Log::info('发送营销短信未查询到');
|
||||||
|
}
|
||||||
|
}elseif ($type == 'groupBuyYes') {
|
||||||
|
$mini_config = [
|
||||||
|
'app_id' => config('cons.mini_app_id'),
|
||||||
|
'secret' => config('cons.mini_secret'),
|
||||||
|
'response_type' => 'array',
|
||||||
|
];
|
||||||
|
$mini_app = Factory::miniProgram($mini_config);
|
||||||
|
// 小程序存放 AccessToken
|
||||||
|
$mini_token = Redis::get('wx:mini:AccessToken');
|
||||||
|
if($mini_token) {
|
||||||
|
$mini_app['access_token']->setToken($mini_token);
|
||||||
|
}
|
||||||
|
$user_list = Db::name('gb_order_detail')
|
||||||
|
->alias('order_d')
|
||||||
|
->join('tb_user_info ui', 'order_d.user_id = ui.id')
|
||||||
|
->where('order_d.is_del', 0)
|
||||||
|
->where('order_d.status', '<>', '已退款')
|
||||||
|
->field('ui.wechat_open_id,ui.id,ui.phone,ui.id as u_user_id, order_d.create_time')
|
||||||
|
->select();
|
||||||
|
Log::info('需要发送拼团成功订阅消息' . json_encode($user_list));
|
||||||
|
$shop = Db::name('tb_shop_info')->where(['id' => $shop_id])->find();
|
||||||
|
if(!$shop) {
|
||||||
|
Log::info($shop . '店铺消息未找到');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
foreach ($user_list as $k => $user) {
|
||||||
|
if(!empty($user['wechat_open_id'])) {
|
||||||
|
// 小程序发送模版消息通知
|
||||||
|
$mini_wechat_res = $mini_app->subscribe_message->send([
|
||||||
|
'template_id' => 'F4OyUhe_ZQ9BR731jlkaN2QXAUaA3HBZuUeVPfraSz0',
|
||||||
|
'touser' => $user['wechat_open_id'],
|
||||||
|
'page' => 'pages/index/index',
|
||||||
|
'data' => [
|
||||||
|
'thing1' => [
|
||||||
|
'value' => $shop['shop_name'],
|
||||||
|
],
|
||||||
|
'time3' => [
|
||||||
|
'value' => $user['create_time'],
|
||||||
|
],
|
||||||
|
'thing2' => [
|
||||||
|
'value' => $shop['address'],
|
||||||
|
],
|
||||||
|
'thing4' => [
|
||||||
|
'value' => '订单拼团成功,请至店内扫码核销',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
Log::info('小程序订阅消息发送结果' . $user['u_user_id'] . json_encode($mini_wechat_res));
|
||||||
|
}else {
|
||||||
|
Log::info('用户' . $user['u_user_id'] . 'wechat_open_id 为空不发送小程序订阅消息');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}else {
|
|
||||||
Log::info('发送营销短信未查询到');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}catch (\Throwable $e) {
|
}catch (\Throwable $e) {
|
||||||
Db::rollback();
|
Db::rollback();
|
||||||
Log::error($e->getMessage());
|
Log::error($e->getTraceAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -46,8 +46,8 @@ try {
|
|||||||
}elseif ($type == 'sendMarkSms') {
|
}elseif ($type == 'sendMarkSms') {
|
||||||
// 发给队列 营销短信发送
|
// 发给队列 营销短信发送
|
||||||
Redis::send('send.mark.sms', $data);
|
Redis::send('send.mark.sms', $data);
|
||||||
}elseif ($type == 'sendWechatTemp') {
|
}elseif ($type == 'sendWechatTemp' || $type == 'groupBuyYes') {
|
||||||
// 发给队列 发送微信模版消息
|
// 发给队列 发送微信模版消息 和小程序消息 groupBuyYes为拼团成功通知
|
||||||
Redis::send('send.wechat.temp', $data);
|
Redis::send('send.wechat.temp', $data);
|
||||||
}
|
}
|
||||||
}else {
|
}else {
|
||||||
|
|||||||
Reference in New Issue
Block a user