|
@@ -8,6 +8,7 @@
|
|
|
|
|
|
namespace App\Modules\OfficialAccount\Services;
|
|
|
|
|
|
+use App\Jobs\ForceSubscribeDelayMsgJob;
|
|
|
use App\Jobs\SendNews;
|
|
|
use App\Jobs\SendTexts;
|
|
|
use App\Modules\OfficialAccount\Models\ForceSubscribeDelayMsg;
|
|
@@ -30,6 +31,8 @@ class ForceSubscribeDelayMsgService
|
|
|
'icon',
|
|
|
'desc',
|
|
|
'time_delay',
|
|
|
+ 'mode',
|
|
|
+ 'content',
|
|
|
'is_enable')
|
|
|
->get();
|
|
|
}
|
|
@@ -39,31 +42,68 @@ class ForceSubscribeDelayMsgService
|
|
|
if($list->isEmpty())return ;
|
|
|
foreach ($list as $item){
|
|
|
try{
|
|
|
- $inset_data = [
|
|
|
- 'openid'=>$openid,
|
|
|
- 'title'=>$item->title,
|
|
|
- 'link'=>$item->link,
|
|
|
- 'icon'=>$item->icon,
|
|
|
- 'desc'=>$item->desc,
|
|
|
- 'appid'=>$appid,
|
|
|
- 'time_delay'=>$item->time_delay,
|
|
|
- 'distribution_channel_id'=>$distribution_channel_id
|
|
|
- ];
|
|
|
+ if ($item->mode == 1) {
|
|
|
+ $inset_data = [
|
|
|
+ 'openid'=>$openid,
|
|
|
+ 'title'=>$item->title,
|
|
|
+ 'link'=>$item->link,
|
|
|
+ 'icon'=>$item->icon,
|
|
|
+ 'desc'=>$item->desc,
|
|
|
+ 'mode'=>1,
|
|
|
+ 'appid'=>$appid,
|
|
|
+ 'time_delay'=>$item->time_delay,
|
|
|
+ 'distribution_channel_id'=>$distribution_channel_id
|
|
|
+ ];
|
|
|
+ }elseif ($item->mode == 2) {
|
|
|
+ $inset_data = [
|
|
|
+ 'openid'=>$openid,
|
|
|
+ 'mode'=>2,
|
|
|
+ 'content'=>$item->content,
|
|
|
+ 'appid'=>$appid,
|
|
|
+ 'time_delay'=>$item->time_delay,
|
|
|
+ 'distribution_channel_id'=>$distribution_channel_id
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
$inset_data['created_at'] = date('Y-m-d H:i:s');
|
|
|
$inset_data['updated_at'] = date('Y-m-d H:i:s');
|
|
|
$inset_data['predict_send_time'] = date('Y-m-d H:i:s',time()+$item->time_delay);
|
|
|
$inset_data['send_status'] = 'ready';
|
|
|
//$id = DB::table('force_subscribe_delay_msg_send_record')->insertGetId($inset_data);
|
|
|
- DB::connection('api_mysql')->table('force_subscribe_delay_msg_send_record')->insert($inset_data);
|
|
|
+ if ($item->time_delay > 60) {
|
|
|
+ DB::connection('api_mysql')->table('force_subscribe_delay_msg_send_record')->insertGetId($inset_data);
|
|
|
+ }else {
|
|
|
+ $inset_data['send_status'] = 'send_success';
|
|
|
+ $id = DB::connection('api_mysql')->table('force_subscribe_delay_msg_send_record')->insertGetId($inset_data);
|
|
|
+ // 加入队列
|
|
|
+ $send_data = [
|
|
|
+ 'id'=>$id,
|
|
|
+ 'openid'=>$openid,
|
|
|
+ 'title'=>$item->title,
|
|
|
+ 'link'=>$item->link,
|
|
|
+ 'icon'=>$item->icon,
|
|
|
+ 'desc'=>$item->desc,
|
|
|
+ 'mode'=>$item->mode,
|
|
|
+ 'appid'=>$appid,
|
|
|
+ 'time_delay'=>$item->time_delay,
|
|
|
+ 'distribution_channel_id'=>$distribution_channel_id,
|
|
|
+ 'content'=>$item->content,
|
|
|
+ ];
|
|
|
+ $job = (new ForceSubscribeDelayMsgJob($send_data))->onConnection('rabbitmq')->delay($item->time_delay)->onQueue('force_subscribe_delay_msg');
|
|
|
+ dispatch($job);
|
|
|
+ }
|
|
|
+
|
|
|
if(stripos($item->link,'?') !== false){
|
|
|
$link = $item->link.'&fromtype=subscribe_delay&send_time='.(time()+$item->time_delay);
|
|
|
}else{
|
|
|
$link = $item->link.'?fromtype=subscribe_delay&send_time='.(time()+$item->time_delay);
|
|
|
}
|
|
|
+
|
|
|
+ // 原代码
|
|
|
$send_content = [];
|
|
|
$send_content[] = [
|
|
|
- ['title'=>$item->title],
|
|
|
- ['description'=>$item->desc],
|
|
|
+ ['title'=>textDecode($item->title)],
|
|
|
+ ['description'=>textDecode($item->desc)],
|
|
|
['url'=> $link],
|
|
|
['image'=>$item->icon]
|
|
|
];
|
|
@@ -77,8 +117,8 @@ class ForceSubscribeDelayMsgService
|
|
|
'news_content'=>json_encode($send_content)
|
|
|
]
|
|
|
);
|
|
|
- //$job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($item->time_delay)->onQueue('send_news_list');
|
|
|
- //dispatch($job);
|
|
|
+// $job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($item->time_delay)->onQueue('send_news_list');
|
|
|
+// dispatch($job);
|
|
|
}catch (\Exception $e){
|
|
|
|
|
|
}
|
|
@@ -97,7 +137,7 @@ class ForceSubscribeDelayMsgService
|
|
|
$result = DB::connection('api_mysql')->table('force_subscribe_delay_msg_send_record')->where('send_status','ready')
|
|
|
->where('predict_send_time','>=',date('Y-m-d H:i:s',time()-$time_delay*2))
|
|
|
->where('predict_send_time','<=',date('Y-m-d H:i:s',time()+$time_delay))
|
|
|
- ->select('openid','id','desc','link','icon','appid','title','distribution_channel_id','time_delay')
|
|
|
+ ->select('openid','id','desc','link','icon','appid','title','distribution_channel_id','time_delay','mode','content')
|
|
|
->get();
|
|
|
if($result){
|
|
|
$client = new Client();
|
|
@@ -124,27 +164,38 @@ class ForceSubscribeDelayMsgService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if(stripos($item->link,'?') !== false){
|
|
|
- $link = $item->link.'&fromtype=subscribe_delay_'.$item->id;
|
|
|
- }else{
|
|
|
- $link = $item->link.'?fromtype=subscribe_delay_'.$item->id;
|
|
|
+ if ($item->mode == 1 && $item->link) {
|
|
|
+ if(stripos($item->link,'?') !== false){
|
|
|
+ $link = $item->link.'&fromtype=subscribe_delay_'.$item->id;
|
|
|
+ }else{
|
|
|
+ $link = $item->link.'?fromtype=subscribe_delay_'.$item->id;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
if($access_token){
|
|
|
- $result = self::send($client,$access_token,$item->openid,$item->title,$item->desc,$link,$item->icon);
|
|
|
+ if ($item->mode == 1) {
|
|
|
+ $result = self::send($client,$access_token,$item->openid,textDecode($item->title),textDecode($item->desc),$link,$item->icon);
|
|
|
+ }elseif ($item->mode == 2) {
|
|
|
+ $nickname = DB::connection('api_mysql')->table('users')->where(['openid'=>$item->openid, 'distribution_channel_id'=>$item->distribution_channel_id])->value('nickname');
|
|
|
+ $content = textDecode(str_replace('{user}', $nickname, $item->content));
|
|
|
+ $result = self::sendMsg($client,$access_token,$item->openid,$content);
|
|
|
+ $content = $nickname = null;
|
|
|
+ }
|
|
|
+
|
|
|
$result_array = \GuzzleHttp\json_decode($result,1);
|
|
|
if(isset($result_array['errcode']) && $result_array['errcode'] == 0){
|
|
|
$send_status = 'send_success';
|
|
|
}else{
|
|
|
$send_status = 'send_fail';
|
|
|
}
|
|
|
+
|
|
|
DB::connection('api_mysql')->table('force_subscribe_delay_msg_send_record')->where('id',$item->id)
|
|
|
- ->update([
|
|
|
- 'send_status'=>$send_status,
|
|
|
- 'send_time'=>date('Y-m-d H:i:s'),
|
|
|
- 'updated_at'=>date('Y-m-d H:i:s'),
|
|
|
- 'send_result'=>$result
|
|
|
- ]);
|
|
|
+ ->update([
|
|
|
+ 'send_status'=>$send_status,
|
|
|
+ 'send_time'=>date('Y-m-d H:i:s'),
|
|
|
+ 'updated_at'=>date('Y-m-d H:i:s'),
|
|
|
+ 'send_result'=>$result
|
|
|
+ ]);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -164,30 +215,40 @@ class ForceSubscribeDelayMsgService
|
|
|
}
|
|
|
if(strtoupper($forceSubscribeDelayMsg->user_type) == 'ALL') return true;
|
|
|
//已付费用户
|
|
|
- $force_subscribe_user = DB::connection('api_mysql')->table('force_subscribe_users')->where('openid',$openid)->where('is_subscribed',1)->select('uid')->first();
|
|
|
- if(!$force_subscribe_user) {
|
|
|
- $msg = 'no subscribe';
|
|
|
- return false;
|
|
|
- }
|
|
|
- if(!$force_subscribe_user->uid) {
|
|
|
- $msg = 'no subscribe uid';
|
|
|
- return false;
|
|
|
- }
|
|
|
+ $force_subscribe_user = DB::connection('api_mysql')->table('temp_force_subscribe_users')->where('openid',$openid)->where('is_subscribed',1)->select('uid')->first();
|
|
|
+// if(!$force_subscribe_user) {
|
|
|
+// $msg = 'no subscribe';
|
|
|
+// return false;
|
|
|
+// }
|
|
|
+// if(!$force_subscribe_user->uid) {
|
|
|
+// $msg = 'no subscribe uid';
|
|
|
+// return false;
|
|
|
+// }
|
|
|
if(strtoupper($forceSubscribeDelayMsg->user_type) == 'PAID'){
|
|
|
- $paid_count = DB::connection('api_mysql')->table('orders')->where('uid',$force_subscribe_user->uid)->where('status','PAID')->count();
|
|
|
- if($paid_count && $paid_count >0)
|
|
|
- return true;
|
|
|
- $msg = 'need paid';
|
|
|
- return false;
|
|
|
+ if (isset($force_subscribe_user->uid) && !empty($force_subscribe_user->uid)) {
|
|
|
+ $paid_count = DB::connection('api_mysql')->table('orders')->where('uid',$force_subscribe_user->uid)->where('status','PAID')->count();
|
|
|
+ if($paid_count && $paid_count >0)
|
|
|
+ return true;
|
|
|
+ $msg = 'need paid';
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|
|
|
//未已付费用户
|
|
|
if(strtoupper($forceSubscribeDelayMsg->user_type) == 'UNPAID'){
|
|
|
- $paid_count = DB::connection('api_mysql')->table('orders')->where('uid',$force_subscribe_user->uid)->where('status','PAID')->count();
|
|
|
- if($paid_count && $paid_count >0) {
|
|
|
- $msg = 'need unpaid';
|
|
|
+ if (isset($force_subscribe_user->uid) && !empty($force_subscribe_user->uid)) {
|
|
|
+ $paid_count = DB::connection('api_mysql')->table('orders')->where('uid',$force_subscribe_user->uid)->where('status','PAID')->count();
|
|
|
+ if($paid_count && $paid_count >0) {
|
|
|
+ $msg = 'need unpaid';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }elseif ($force_subscribe_user) {
|
|
|
+ return true;
|
|
|
+ }else {
|
|
|
return false;
|
|
|
}
|
|
|
- return true;
|
|
|
}
|
|
|
$msg = 'unknown type';
|
|
|
return false;
|
|
@@ -204,6 +265,7 @@ class ForceSubscribeDelayMsgService
|
|
|
}
|
|
|
return '';
|
|
|
}
|
|
|
+
|
|
|
private static function send(Client $client,$access_token,$openid,$title,$description,$url,$picurl){
|
|
|
$push_url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$access_token;
|
|
|
$request = new GuzzleRequest('post',$push_url,[],\GuzzleHttp\json_encode([
|
|
@@ -218,4 +280,20 @@ class ForceSubscribeDelayMsgService
|
|
|
}catch (\Exception $e){}
|
|
|
return '';
|
|
|
}
|
|
|
+
|
|
|
+ // 发送文字消息
|
|
|
+ private static function sendMsg(Client $client,$access_token,$openid,$content){
|
|
|
+ $push_url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$access_token;
|
|
|
+ $request = new GuzzleRequest('post',$push_url,[],\GuzzleHttp\json_encode([
|
|
|
+ 'touser'=>$openid,
|
|
|
+ 'msgtype'=>'text',
|
|
|
+ 'text'=>[
|
|
|
+ 'content'=>$content
|
|
|
+ ]
|
|
|
+ ],JSON_UNESCAPED_UNICODE));
|
|
|
+ try{
|
|
|
+ return $client->send($request)->getBody()->getContents();
|
|
|
+ }catch (\Exception $e){}
|
|
|
+ return '';
|
|
|
+ }
|
|
|
}
|