Parcourir la source

1.关键字回复、关注回复、关注延时回复增加对文字类型和互动链的支持,增加对emoji表情的支持
2.增加队列来执行小于1分钟的关注延时回复

lh il y a 3 ans
Parent
commit
a7791235db

+ 31 - 3
app/Http/Controllers/Wechat/OfficialAccount/OfficialInteractiveEventController.php

@@ -287,12 +287,40 @@ class OfficialInteractiveEventController extends Controller
             if (!empty($special_keyword)) {
                 \Log::info('officialInteractiveEventFeedback_has_special_keyword:' . $distribution_channel_id . ' content:' . $content);
                 \Log::info($special_keyword);
+
+                // 图文模式(页面类型)
+                if ($special_keyword['mode'] == 1 && $special_keyword['link_type'] == 'page') {
+                    if (isset($special_keyword['send_order_id']) && $special_keyword['send_order_id'] > 0) {
+                        $special_keyword['link'] .= '/yun/' . $special_keyword['send_order_id'];
+                    } else {
+                        $special_keyword['link'] .= '?fromtype=keyword_' . $special_keyword['id'];
+                    }
+
+                    $datatext['text'] = [
+                        [
+                            'url'         => $special_keyword['link'],
+                            'title'       => textDecode($special_keyword['send_title']),
+                            'description' => textDecode($special_keyword['desc']),
+                            'image'       => $special_keyword['send_cover']
+                        ]
+                    ];
+                    $datatext['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $datatext['text'], 'text_search');
+                    return response()->success($datatext);
+                }
+
+                if ($special_keyword['mode'] == 2) {
+                    $nickname = DB::connection('api_mysql')->table('users')->where(['openid'=>$openid, 'distribution_channel_id'=>$distribution_channel_id])->value('nickname');
+                    $datatext['text'] = textDecode(str_replace('{user}', $nickname, $special_keyword['content']));
+                    $datatext['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $datatext['text'], 'text_search', 'keyword_' . $special_keyword['id']);
+                    return response()->success($datatext);
+                }
+
                 $bid = Hashids::decode($special_keyword['bid'])[0];
                 \Log::info('bid:' . $bid);
                 $book_conf = BookConfig::getBookById($bid);
                 if (!empty($book_conf)) {
                     $special_keyword_new              = array();
-                    $special_keyword_new['book_name'] = isset($special_keyword['send_title']) && !empty($special_keyword['send_title']) ? $special_keyword['send_title'] : $book_conf['book_name'];
+                    $special_keyword_new['book_name'] = isset($special_keyword['send_title']) && !empty($special_keyword['send_title']) ? textDecode($special_keyword['send_title']) : $book_conf['book_name'];
                     $special_keyword_new['cover']     = isset($special_keyword['send_cover']) && !empty($special_keyword['send_cover']) ? $special_keyword['send_cover'] : $book_conf['cover'];
                     \Log::info('keyword_book_name:' . $special_keyword_new['book_name'] . ' cover:' . $special_keyword_new['cover']);
                     $special_keyword_new['intro'] = $book_conf['intro'];
@@ -388,8 +416,8 @@ class OfficialInteractiveEventController extends Controller
                         \Log::info('search_instro_too_long:' . $booksInfo[$i]['intro']);
                     }
                     $data[$i] = ['url'         => $base_url,
-                                 'title'       => $booksInfo[$i]['book_name'],
-                                 'description' => $booksInfo[$i]['intro'],
+                                 'title'       => textDecode($booksInfo[$i]['book_name']),
+                                 'description' => textDecode($booksInfo[$i]['intro']),
                                  'image'       => $booksInfo[$i]['cover']];
 
                     // 返回一条

+ 201 - 0
app/Jobs/ForceSubscribeDelayMsgJob.php

@@ -0,0 +1,201 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Jobs\Job;
+use App\Modules\OfficialAccount\Models\ForceSubscribeDelayMsg;
+use GuzzleHttp\Client;
+use GuzzleHttp\Psr7\Request as GuzzleRequest;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Log;
+use App\Http\Controllers\WechatController;
+
+class ForceSubscribeDelayMsgJob extends Job implements ShouldQueue
+{
+    use InteractsWithQueue, SerializesModels;
+
+    private $data;
+    /**
+     * Create a new job instance.
+     *
+     * @return void
+     */
+    public function __construct($data)
+    {
+        $this->data = $data;
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @return void
+     */
+    public function handle()
+    {
+        Log::info('~~~~~~~~~~~~~~~~~~开始执行关注后延时回复消息队列~~~~~~~~~~~~~~~~~~');
+        Log::info('参数: '.json_encode($this->data, 256));
+
+        $client = new Client();
+        $access_token_list = [];
+        $this->data = (object)$this->data;
+
+        //用户类型是否满足条件
+        $is_access = self::msgUserTypeIsAccess($this->data->distribution_channel_id,$this->data->time_delay,$this->data->openid,$msg);
+        if(!$is_access) {
+            DB::connection('api_mysql')->table('force_subscribe_delay_msg_send_record')->where('id',$this->data->id)
+                ->update([
+                    'send_status'=>$msg,
+                    'send_time'=>date('Y-m-d H:i:s'),
+                    'updated_at'=>date('Y-m-d H:i:s'),
+                    'send_result'=>''
+                ]);
+        }
+        if(isset($access_token_list[$this->data->appid])){
+            $access_token = $access_token_list[$this->data->appid];
+        }else{
+            $access_token = self::getAccessToken($this->data->appid);
+            if($access_token){
+                $access_token_list[$this->data->appid] = $access_token;
+            }
+        }
+        if ($this->data->mode == 1 && $this->data->link) {
+            if(stripos($this->data->link,'?') !== false){
+                $link = $this->data->link.'&fromtype=subscribe_delay_'.$this->data->id;
+            }else{
+                $link = $this->data->link.'?fromtype=subscribe_delay_'.$this->data->id;
+            }
+        }
+
+        if($access_token){
+            if ($this->data->mode == 1) {
+                $result = self::send($client,$access_token,$this->data->openid,textDecode($this->data->title),textDecode($this->data->desc),$link,$this->data->icon);
+            }elseif ($this->data->mode == 2) {
+                $nickname = DB::connection('api_mysql')->table('users')->where(['openid'=>$this->data->openid, 'distribution_channel_id'=>$this->data->distribution_channel_id])->value('nickname');
+                $content = textDecode(str_replace('{user}', $nickname, $this->data->content));
+                $result = self::sendMsg($client,$access_token,$this->data->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',$this->data->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
+                ]);
+        }
+        Log::info('~~~~~~~~~~~~~~~~~~结束执行关注后延时回复消息队列~~~~~~~~~~~~~~~~~~');
+    }
+
+    public static function msgUserTypeIsAccess($distribution_channel_id,$time_delay,$openid,&$msg){
+        $forceSubscribeDelayMsg = ForceSubscribeDelayMsg::where('distribution_channel_id',$distribution_channel_id)
+            ->where('time_delay',$time_delay)
+            ->where('is_show',1)
+            ->where('is_enable',1)
+            ->select('user_type')
+            ->first();
+        $msg = '';
+        if(!$forceSubscribeDelayMsg) {
+            $msg = 'sys-error';
+            return false;
+        }
+        if(strtoupper($forceSubscribeDelayMsg->user_type) == 'ALL') return true;
+        //已付费用户
+        $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'){
+            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'){
+            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;
+            }
+        }
+        $msg = 'unknown type';
+        return false;
+    }
+
+    private static function getAccessToken($appid){
+        try{
+            $WechatController = new WechatController($appid);
+            $accessToken = $WechatController->app->access_token;
+            $token = $accessToken->getToken();
+            return $token;
+        }catch(\Exception $e){
+            \Log::error($e);
+        }
+        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([
+            'touser'=>$openid,
+            'msgtype'=>'news',
+            'news'=>[
+                'articles'=>[compact('title','description','url','picurl')]
+            ]
+        ],JSON_UNESCAPED_UNICODE));
+        try{
+            return $client->send($request)->getBody()->getContents();
+        }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 '';
+    }
+
+    public function failed(\Exception $exception)
+    {
+        Log::error('关注后延迟消息发送异常,异常原因: '.$exception->getMessage());
+    }
+}

+ 4 - 3
app/Modules/Channel/Services/ChannelService.php

@@ -377,11 +377,12 @@ class ChannelService
     /**
      * 根据站点属性,转换对应的回复链接
      */
-    static function convertChannelReplyUrl($is_outer_site,$appid,$openid,$content,$fromwhere='')
+    static function convertChannelReplyUrl($is_outer_site,$appid,$openid,$content,$fromwhere='',$keyword='')
     {
     	if(!$is_outer_site) return $content;
     	$matchs = [];
     	$from_where_str = '&fromwhere='.$fromwhere;
+        if ($keyword) $keyword = '&fromtype='.$keyword;
     	\Log::Info('convertChannelReplyUrl_before:appid'.$appid.' openid:'.$openid.' is_outer_site:'.$is_outer_site.' fromwhere:'.$fromwhere);
     	$new_content = $content;
 //     	\Log::Info('$content');\Log::Info($content);
@@ -417,9 +418,9 @@ class ChannelService
     				$new_url = $url  = $match;
     				 
     				if(strpos($url,'?') > -1){
-    					$new_url = $url.'&appid='.$appid.'&openid='.$openid.$from_where_str;
+    					$new_url = $url.'&appid='.$appid.'&openid='.$openid.$from_where_str.$keyword;
     				}else{
-    					$new_url = $url.'?appid='.$appid.'&openid='.$openid.$from_where_str;
+    					$new_url = $url.'?appid='.$appid.'&openid='.$openid.$from_where_str.$keyword;
     				}
     				// 小链接可能多次被替换,所以一定要引号结尾
     				$new_content = str_replace($url.'\n"',$new_url.'"',$new_content);

+ 3 - 1
app/Modules/OfficialAccount/Models/ForceSubscribeDelayMsg.php

@@ -25,6 +25,8 @@ class ForceSubscribeDelayMsg extends Model
         'desc',
         'time_delay',
         'is_enable',
-        'is_show'
+        'is_show',
+        'mode',
+        'content'
     ];
 }

+ 19 - 19
app/Modules/OfficialAccount/Models/WechatKeywordMsgs.php

@@ -7,26 +7,26 @@ use DB;
 
 class WechatKeywordMsgs extends Model
 {
-	protected $connection = 'api_mysql';
+	protected $connection = 'api_mysql';
 	
     protected $tables = 'wechat_keyword_msgs';
-    protected $fillable = ['appids', 'keyword', 'distribution_channel_id','bid','cid','book_name','chapter_name','appid','status','send_title','send_cover','send_order_id','created_at','updated_at'];
+    protected $fillable = ['appids', 'keyword', 'distribution_channel_id','bid','cid','book_name','chapter_name','appid','status','send_title','send_cover','send_order_id','created_at','updated_at','link_type','link','desc','mode','content'];
 
     
-    /**
-     * 根据渠道获取关键字列表
-     */
-    static function wechatKeywordMsgsBydistributionChannelId($distribtion_channel_id)
-    {
-    	return self::where('distribution_channel_id', $distribtion_channel_id)->where('status', 1)->paginate();
+    /**
+     * 根据渠道获取关键字列表
+     */
+    static function wechatKeywordMsgsBydistributionChannelId($distribtion_channel_id)
+    {
+    	return self::where('distribution_channel_id', $distribtion_channel_id)->where('status', 1)->paginate();
     }
     
-    /**
-     * 根据渠道获取关键字列表
-     */
-    static function wechatKeywordByDistributionChannelIdAndKeyword($distribtion_channel_id,$keyword,$status=1)
-    {
-    	return self::where(['distribution_channel_id'=>$distribtion_channel_id,'keyword'=>$keyword,'status'=>$status])->first();
+    /**
+     * 根据渠道获取关键字列表
+     */
+    static function wechatKeywordByDistributionChannelIdAndKeyword($distribtion_channel_id,$keyword,$status=1)
+    {
+    	return self::where(['distribution_channel_id'=>$distribtion_channel_id,'keyword'=>$keyword,'status'=>$status])->first();
     }
     
   
@@ -38,12 +38,12 @@ class WechatKeywordMsgs extends Model
         return self::where('id', $id)->first();
     }
     
-    /**
-     * 更新关键字状态
-     */
-    static function updateWechatKeywordMsgStatus($id,$distribution_channel_id,$status)
+    /**
+     * 更新关键字状态
+     */
+    static function updateWechatKeywordMsgStatus($id,$distribution_channel_id,$status)
     {
-    	return self::where('id', $id)->where('distribution_channel_id', $distribution_channel_id)->update(['status'=>$status,'updated_at'=>date('Y-m-d H:i:s')]);
+    	return self::where('id', $id)->where('distribution_channel_id', $distribution_channel_id)->update(['status'=>$status,'updated_at'=>date('Y-m-d H:i:s')]);
     }
 
 }

+ 123 - 45
app/Modules/OfficialAccount/Services/ForceSubscribeDelayMsgService.php

@@ -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 '';
+    }
 }

+ 39 - 34
app/Modules/OfficialAccount/Services/WechatKeywordMsgService.php

@@ -28,14 +28,14 @@ class WechatKeywordMsgService
 
     }
     
-    /**
-     * 更新状态
-     */
-    static function updateWechatKeywordMsgStatus($wechatKeywordPrams)
-    {
-    
-    	return WechatKeywordMsgs::updateWechatKeywordMsgStatus($wechatKeywordPrams['id'],$wechatKeywordPrams['distribution_channel_id'],$wechatKeywordPrams['status']);
-    
+    /**
+     * 更新状态
+     */
+    static function updateWechatKeywordMsgStatus($wechatKeywordPrams)
+    {
+    
+    	return WechatKeywordMsgs::updateWechatKeywordMsgStatus($wechatKeywordPrams['id'],$wechatKeywordPrams['distribution_channel_id'],$wechatKeywordPrams['status']);
+    
     }
 
     /**
@@ -71,16 +71,16 @@ class WechatKeywordMsgService
 
     }
     
-    /**
-     * 修改关键字
-     */
-    static function updateWechatKeywordMsg($wechatKeywordMsgPrams)
-    {
-    
-    	try {
-    
-    		$wechatKeywordMsg = WechatKeywordMsgs::wechatKeywordMsgsById($wechatKeywordMsgPrams['id']);
-    		\Log::info('updateWechatKeywordMsg_origin');\Log::info($wechatKeywordMsg);
+    /**
+     * 修改关键字
+     */
+    static function updateWechatKeywordMsg($wechatKeywordMsgPrams)
+    {
+    
+    	try {
+    
+    		$wechatKeywordMsg = WechatKeywordMsgs::wechatKeywordMsgsById($wechatKeywordMsgPrams['id']);
+    		\Log::info('updateWechatKeywordMsg_origin');\Log::info($wechatKeywordMsg);
     		if(!empty($wechatKeywordMsg)) {
 
     			$wechatKeywordMsg->appids = $wechatKeywordMsgPrams['appids'];
@@ -91,25 +91,30 @@ class WechatKeywordMsgService
     			$wechatKeywordMsg->send_title = $wechatKeywordMsgPrams['send_title'];
     			$wechatKeywordMsg->send_cover = $wechatKeywordMsgPrams['send_cover'];
     			$wechatKeywordMsg->keyword = $wechatKeywordMsgPrams['keyword'];
+                $wechatKeywordMsg->link_type = $wechatKeywordMsgPrams['link_type'];
+                $wechatKeywordMsg->link = $wechatKeywordMsgPrams['link'];
+                $wechatKeywordMsg->desc = $wechatKeywordMsgPrams['desc'];
+                $wechatKeywordMsg->mode = $wechatKeywordMsgPrams['mode'];
+                $wechatKeywordMsg->content = $wechatKeywordMsgPrams['content'];
     			\Log::info('after_update:');
     			\Log::info($wechatKeywordMsg);
-    			$wechatKeywordMsg->save();
-    
-    			return 1;
-    
+    			$wechatKeywordMsg->save();
+    
+    			return 1;
+    
     		}else{
-    			\Log::info('updateWechatKeywordMsg_not_exist');
-    			return 2;
-    			 
-    		}
-    
-    	} catch (\Exception $e) {
-    		\Log::info("updateWechatKeywordMsg_ept:".$e);
-    
-    		return 0;
-    
-    	}
-    
+    			\Log::info('updateWechatKeywordMsg_not_exist');
+    			return 2;
+    			 
+    		}
+    
+    	} catch (\Exception $e) {
+    		\Log::info("updateWechatKeywordMsg_ept:".$e);
+    
+    		return 0;
+    
+    	}
+    
     }