21 Commit-ok b3ca2415ea ... 1d5ef8fe7f

Szerző SHA1 Üzenet Dátum
  songdb 1d5ef8fe7f 调整ios菜单 只有存在pay链接才设置 4 éve
  root 79eef1b14b Merge branch 'master' of wd:wangdu/wangdu_wechat into stabble 4 éve
  root 5943332bd3 Merge branch 'master' of wd:wangdu/wangdu_wechat into stabble 4 éve
  root ed285d699b Merge branch 'master' of wd:wangdu/wangdu_wechat into stabble 4 éve
  lh b9f4c18fec custom_send_msgs model新增custom_type字段 4 éve
  lh 5a46e6086c 更改关注回复第二条延迟5s发送 4 éve
  lh 22b9f1774a log 4 éve
  lh ca8bc4f79d 增加公共方法加解密emoji 4 éve
  lh 9e90a7254a test 4 éve
  lh a7791235db 1.关键字回复、关注回复、关注延时回复增加对文字类型和互动链的支持,增加对emoji表情的支持 4 éve
  lh 2329bad24d 修改互动链msgmenuid=hudonglian 4 éve
  lh 100357aaf9 '.' 4 éve
  lh cbb26e0b7d '修改公众号菜单点击事件逻辑,增加最近阅读关键字回复内容' 4 éve
  zhoulj 6d9891bb0b Merge branch 'stabble' of qk:wangdu/wangdu_wechat into stabble 4 éve
  zhoulj bf6ed07ec4 Merge branch 'master' of qk:wangdu/wangdu_wechat 4 éve
  zhoulj d8fea96217 关注不回复开关 4 éve
  zhoulj fa9c0b55a3 1 4 éve
  zz 3664ccca00 encodeDistributionChannelId 4 éve
  zhoulj 715d88c96f 1 4 éve
  root edcb89e9dc init 4 éve
  zhoulj f07a9c1e4f Initial commit 4 éve

+ 1 - 1
app/Console/Commands/SmartPush/remindSign.php

@@ -119,7 +119,7 @@ class remindSign extends Command
                 $openid = $item->openid;
                 $appid = $item->appid;
                 //$url = sprintf($continueReadUrlFormat,encodeDistributionChannelId($item->distribution_channel_id),env('CUSTOM_HOST'));
-                $url = 'weixin://bizmsgmenu?msgmenucontent=签到&msgmenuid=1';
+                $url = 'weixin://bizmsgmenu?msgmenucontent=签到&msgmenuid=hudonglian';
                 $guide_status = false;
                 if($user_info){
                     $guide_status = UserSignService::guideFans($item->uid,$item->distribution_channel_id,$user_info->openid,$user_info->created_at);

+ 10 - 1
app/Console/Commands/Test.php

@@ -4,6 +4,9 @@
 namespace App\Console\Commands;
 
 use Illuminate\Console\Command;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Log;
+use Redis;
 
 class Test extends Command
 {
@@ -28,7 +31,13 @@ class Test extends Command
      */
     public function handle()
     {
-       
+//        $data = DB::connection('api_mysql')->table('official_accounts')->select('distribution_channel_id', 'nickname', 'name', 'head_img', 'appid', 'appsecret', 'verify_txt', 'alias', 'qrcode_url', 'principal_name', 'service_type_info', 'func_info', 'authorizer_refresh_token', 'is_auth', 'cancel_auth_time', 'official_account_type', 'verify_type_info', 'subscribe_top_num', 'subscribe_day_maximum', 'sort_no', 'is_enabled')->where('appid', 'wxebcb86ec4b80eaca')->first();
+//        $data = (array)$data;
+//        Log::info('official_accounts_info: '.json_encode($data));
+//        Redis::hset('official_account_info:app_id:wxebcb86ec4b80eaca', 'official_account_info', json_encode($data));
+
+//        $books = \App\Modules\Book\Services\BookConfigService::getBookByRole('叶安');
+//        dd($books);
 
     }
 

A különbségek nem kerülnek megjelenítésre, a fájl túl nagy
+ 1 - 1
app/Http/Controllers/Wechat/GzhMsg/GzhMsgsController.php


+ 15 - 4
app/Http/Controllers/Wechat/Menu/MenusController.php

@@ -48,8 +48,9 @@ class MenusController
             $add_menu = $menu->add($add_menus);
             v('add_menu');v($add_menu);
 
-            //过滤iOS 充值入口
+            //存在pay链接 才过滤iOS 充值入口
             $ios_menus = [];
+            $is_exist_pay = false;
             foreach ($add_menus as $k=>$_menu)
             {
                 if(isset($_menu['sub_button']))
@@ -58,14 +59,24 @@ class MenusController
                     $sub_button = [];
                     foreach($_menu['sub_button'] as $j=>$_sub_menu)
                     {
-                        if(!(isset($_sub_menu['url']) && strstr($_sub_menu['url'],'/pay'))) $sub_button[] = $_sub_menu;
+                        if(isset($_sub_menu['url']) && strstr($_sub_menu['url'],'/pay'))
+                        {
+                            $is_exist_pay = true;
+                        }else{
+                            $sub_button[] = $_sub_menu;
+                        }
                     }
                     if($sub_button)  $ios_menus[] = compact('name','sub_button');
                 }else{
-                    if(!(isset($_menu['url']) && strstr($_menu['url'],'/pay'))) $ios_menus[] = $_menu;
+                    if(isset($_sub_menu['url']) && strstr($_menu['url'],'/pay'))
+                    {
+                        $is_exist_pay = true;
+                    }else{
+                        $ios_menus[] = $_menu;
+                    }
                 }
             }
-            $res = $menu->add($ios_menus,['client_platform_type'=>"1"]);
+            $res = $is_exist_pay ? $menu->add($ios_menus,['client_platform_type'=>"1"]) : '';
             v('add_menu');v($res);
         }catch(\Exception $e){
             v('set_menu_ept:'.$e->getMessage());

A különbségek nem kerülnek megjelenítésre, a fájl túl nagy
+ 143 - 4
app/Http/Controllers/Wechat/OfficialAccount/OfficialInteractiveEventController.php


+ 1 - 0
app/Http/Controllers/Wechat/Staff/StaffsController.php

@@ -37,6 +37,7 @@ class StaffsController
     	}
     	
     	$result = '';
+    	v('to_send_datas:'.json_encode($to_send_datas));
     	foreach($to_send_datas as $send_type=>$to_send_data){
     		if($send_type == 'image'){
     			$this->send_wechat_content_image($openid, $to_send_data);

+ 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());
+    }
+}

+ 31 - 0
app/Libs/functions/Helpers.php

@@ -483,3 +483,34 @@ function getProp($param, $key, $default = '')
 
     return $result;
 }
+
+//把用户输入的文本转义(主要针对特殊符号和emoji表情)
+function textEncode($str)
+{
+    if (!is_string($str)) return $str;
+    if ($str === '0') return '0';
+    if (!$str || $str == 'undefined') return '';
+    $text = json_encode($str); //暴露出unicode
+    return $text;
+    $text = preg_replace_callback("/(\\\u[2edf][0-9a-f]{3})/i", function ($str) {
+        return addslashes($str[0]);
+    }, $text); //将emoji的unicode留下,其他不变
+    return json_decode($text);
+}
+
+//解码上面的转义
+function textDecode($str)
+{
+    if (is_null(json_decode($str))) return $str;
+    return json_decode($str);
+    $text = json_encode($str); //暴露出unicode
+    $text = preg_replace_callback('/\\\\\\\\/i', function ($str) {
+        return '\\';
+    }, $text); //将两条斜杠变成一条,其他不变
+//    return json_decode($text);
+    $text = json_decode($text);
+    if (strstr($text, '&#039;')) {
+        $text = str_replace('&#039;', '\'', $text);
+    }
+    return $text;
+}

+ 7 - 2
app/Modules/Book/Models/BookConfig.php

@@ -918,7 +918,12 @@ class BookConfig extends Model
             unset($reco_books[$_bid]);
         }
 
-        $return_bids  = array_rand($reco_books, $num);
+        if ($reco_books) {
+            $return_bids  = array_rand($reco_books, $num);
+        }else {
+            $return_bids = [];
+        }
+
         $return_books = [];
         foreach ($return_bids as $item) {
             $return_books[] = json_decode($reco_books[$item]);//object
@@ -1249,6 +1254,6 @@ class BookConfig extends Model
     public static function getSimpleBooksByIds($bids)
     {
         $fields = ['book_name','bid','cover'];
-        return self::select($fields)->whereIn('bid',$bids)->where('is_on_shelf',2)->order('id','desc')->get();
+        return self::select($fields)->whereIn('bid',$bids)->where('is_on_shelf',2)->orderBy('id','desc')->get();
     }
 }

+ 1 - 1
app/Modules/Book/Models/BookRole.php

@@ -13,6 +13,6 @@ class BookRole extends Model
 
     public static function getBidByRole($role)
     {
-        return self::where('role',$role)->get()->plcuk('bid');
+        return self::where('role',$role)->get()->pluck('bid');
     }
 }

+ 1 - 1
app/Modules/Book/Services/BookConfigService.php

@@ -43,7 +43,7 @@ class BookConfigService
             if($bids)
             {
                 $books = BookConfig::getSimpleBooksByIds($bids);
-                $books && $book = array_shift($books);
+                $books && $book = $books[0];
             }
         }
         return $book;

+ 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);

+ 2 - 2
app/Modules/OfficialAccount/Models/CustomSendMsgs.php

@@ -7,11 +7,11 @@ use DB;
 
 class CustomSendMsgs extends Model
 {
-	protected $connection = 'api_mysql';
+	protected $connection = 'api_mysql';
 	
     protected $tables = 'custom_send_msgs';
 
-    protected $fillable = ['task_id', 'user_num', 'appid', 'name', 'send_time', 'content', 'redirect_url', 'status', 'distribution_channel_id', 'subscribe_time', 'sex', 'balance', 'order_type', 'category_id', 'del_flag', 'is_full_send', 'trusteeship', 'batch_no', 'is_show_list', 'description', 'book_name', 'chapter_name', 'is_activity'];
+    protected $fillable = ['task_id', 'user_num', 'appid', 'name', 'send_time', 'content', 'redirect_url', 'status', 'distribution_channel_id', 'subscribe_time', 'sex', 'balance', 'order_type', 'category_id', 'del_flag', 'is_full_send', 'trusteeship', 'batch_no', 'is_show_list', 'description', 'book_name', 'chapter_name', 'is_activity', 'custom_type'];
 
 
     /**

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

+ 1 - 1
app/Modules/OfficialAccount/Services/MsgService.php

@@ -197,7 +197,7 @@ class MsgService
     					'data' => $data
     			);
     		
-    			$delay = 1;
+    			$delay = 5;
     			 
     			if ($is_news){
     				$job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_news_list');

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