Browse Source

青爻公众号

cqp 10 months ago
parent
commit
32aad3316a

+ 11 - 0
app/Http/Controllers/Api/WeixinController.php

@@ -31,4 +31,15 @@ class WeixinController extends BaseController
             die('fail');
         }
     }
+
+    public function getPublicWxArticle(Request $request){
+        $service = new WeixinService();
+        list($status,$data) = $service->getPublicWxArticle($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
 }

+ 13 - 0
app/Model/Settings.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Settings extends Model
+{
+    protected $table = "settings"; //指定表
+    const CREATED_AT = null;
+    const UPDATED_AT = null;
+    protected $dateFormat = 'U';
+}

+ 64 - 107
app/Service/Weixin/WeixinService.php

@@ -2,134 +2,91 @@
 
 namespace App\Service\Weixin;
 
+use App\Model\Settings;
 use App\Service\Service;
 use Illuminate\Support\Facades\Redis;
 
 class WeixinService extends Service
 {
-    const APPID = 'wxe048bcdcc21aae6e';
-    const APPSECRET = '191789c5b4ef2b3d5b9e79bb62428092';
-    const ACCESS_URL = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s';
-    const OPENID = '';
-    const TOKEN = '';
-    const KEY = 'weixintnine';
-
     public function getToken(){
-        $token_key = self::KEY.'_'.'token';
+        $config = config('qingyaoWx');
+        $token_key = $config['redis_key'];
         $token = Redis::get($token_key);
-        if(! $token){
-            $url = sprintf(self::ACCESS_URL,self::APPID,self::APPSECRET);
+        if(empty($token)){
+            $url = sprintf($config['get_token'], $config['appid'], $config['appsecret']);
             $res = $this->curlOpen($url);
             $res = json_decode($res,true);
-            if(isset($res['errmsg'])) return [false,$res['errmsg']];
-            if(!isset($res['access_token'])) return [false,'request error'];
+            if(isset($res['errmsg'])) return [false, $res['errmsg']];
+            if(! isset($res['access_token'])) return [false, 'request error'];
             $token = $res['access_token'];
             $expire_time = $res['expires_in']-300;
             Redis::set($token_key,$token);
             Redis::expire($token_key, $expire_time);
+
             return [true,$token];
         }
-        return [true,$token];
-    }
 
-    public function getOpenid($data){
-        if(empty($data['code'])) return [false, 'code不能为空'];
-        $code = $data['code'];
-        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code';
-        $url = sprintf($url,self::APPID,self::APPSECRET,$code);
-        $res = $this->curlOpen($url);
-        $res = json_decode($res,true);
-        if(!isset($res['openid'])) return [false,$res['errmsg']??'request error'];
-        $openid = $res['openid'];
-        return [true,['openid' => $openid]];
+        return [true, $token];
     }
 
-    public function setWebHook($data){
-//        file_put_contents('22.txt',json_encode($data));
-        $uri = isset($data['uri']) ? $data['uri'] : '';
-        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
-        $param = isset($data['param']) ? $data['param'] : '';
-        $redirect_uri = urlencode('https://t9api.qingyaokeji.com/wxapi/getUnionid?uri='.$uri.'&param='.$param);
-        $url = sprintf($url,self::APPID,$redirect_uri);
-        header("Location:".$url);exit;
-        echo 'ok';die;
+    public function getPublicWxArticle($data){
+        list($status, $msg) = $this->rule($data);
+        if(! $status) return [false, 'IP未入白名单'];
+
+        list($status, $msg) = $this->getToken();
+        if(! $status) return [false, $msg];
+
+        $config = config('qingyaoWx');
+        $url = sprintf($config['get_article'], $msg);
+
+        $offset = empty($data['page_index']) ? 1 : $data['page_index'] - 1;
+        $count = empty($data['count']) || $data['count'] > 10 ? 10 : $data['count'];
+        $post = [
+            'offset' => $offset,
+            'count' => $count,
+            'no_content' => 0,
+        ];
+        $result = $this->curlOpen($url, ['post' => json_encode($post)]);
+        $result = json_decode($result,true);
+
+        if(isset($result['errmsg'])) return [false, $result['errmsg']];
+
+        return [true, ['data' => $result['item'] ?? [], 'total' => $result['total_count']]];
     }
 
-    public function getUnionid($data){
-        file_put_contents('22.txt',date('YmdHis').json_encode($data));
-//        echo $data['code'];
-
-        if(isset($data['code'])) {
-            list($status,$openid) = $this->getOpenid($data);
-            file_put_contents('222.txt',date('YmdHis').json_encode($openid));
-            if(!$status) return [false,$openid];
-            $uri = $data['uri'];
-            $openid = $openid['openid'];
-            $param = isset($data['param']) ? $data['param'] : '';
-            $url = 'https://t9.qingyaokeji.com/#/wxGet?uri='.$uri.'&openid='.$openid.'&param='.$param;
-            header('Location:'.$url);exit();
+    public function rule($data){
+        // 获取用户的IP地址
+        $userIP = $_SERVER['REMOTE_ADDR'];
+        // 获取设置的IP地址
+        $allowedIPs = $this->allowedIPs();
+
+        if(empty($allowedIPs)) return [false, $userIP];
+        // 校验用户IP是否在允许的范围内
+        $isValidIP = false;
+        foreach ($allowedIPs as $allowedIP) {
+            if (strpos($allowedIP, '/') !== false) {
+                // IP段表示法校验
+                list($subnet, $mask) = explode('/', $allowedIP);
+                if ((ip2long($userIP) & ~((1 << (32 - $mask)) - 1)) == ip2long($subnet)) {
+                    $isValidIP = true;
+                    break;
+                }
+            } else {
+                // 单个IP地址校验
+                if ($allowedIP === $userIP) {
+                    $isValidIP = true;
+                    break;
+                }
+            }
         }
+
+        return [$isValidIP, $userIP];
     }
 
-//    public function sendTmpMsg($data){
-//        //        $openid = 'okXNa69ggEX61KvHUhCq9PcGrPKI';
-//        $data = [
-//            'openid' => 'o7B4f68DuDlBSevGdctFyP8MD-nw',
-//            'tempid' => '5azHlaoAu6MgRzkxn_HL6ygFt_wIkXEz9CklPWEdP70',
-//            'reload_url' => '',
-//            'first' => '工资发放',
-//            'remark' => '请查收',
-//            'detail' => [
-//                'thing2' => '姓名',
-//                'thing6' => '10',
-//                'time4' => '2023-09-01',
-//                'character_string3' => 'st.1231',
-//                'thing1' => '类型',
-//            ]
-//        ];
-//        if(!isset($data['detail'])) return [false,'invalid detail'];
-//        if(!isset($data['openid'])) return [false,'invalid openid'];
-//        if(!isset($data['tempid'])) return [false,'invalid tempid'];
-//        if(!isset($data['reload_url'])) return [false,'invalid reload_url'];
-//        $templateID = $data['tempid'];
-//        $reload_url = $data['reload_url'];
-//        list($status,$token) = $this->getToken();
-//        if(!$status) return [false,$token];
-//        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token;
-//        $post = '{
-//           "touser":"'.$data['openid'].'",
-//           "template_id":"'.$templateID.'",
-//           "url":"'.$reload_url.'",
-//           "data":{
-//                   "first": {
-//                       "value":"'.$data['first'].'",
-//                       "color":"#173177"
-//                   },
-//                   %s
-//                   "remark":{
-//                       "value":"'.$data['remark'].'",
-//                       "color":"#173177"
-//                   }
-//           }
-//       }';
-//        $content = "";
-//        foreach ($data['detail'] as $k=>$v){
-//
-//            $content .= '"'.$k.'": {
-//                       "value":"'.$v.'",
-//                       "color":"#173177"
-//                   },';
-//        }
-//        $post = sprintf($post,$content);
-////        var_dump($post);
-////        var_dump(json_decode($post));die;
-////        var_dump($url);
-////        var_dump(json_encode(json_decode($post)));
-//        $res = $this->curlOpen($url,['post'=>$post]);
-//        $res = json_decode($res,true);
-//        if(isset($res['errcode'])&&$res['errcode'] != 0) return [false,$res['errmsg']];
-//        if(isset($res['errcode'])&&$res['errcode'] === 0) return [true,''];
-//        return [false,json_encode($res)];
-//
-//    }
+    public function allowedIPs(){
+        $allowedIPs = Settings::where('setting_name','allowedIPs')->first();
+        if(empty($allowedIPs) || empty($allowedIPs->setting_value)) return [];
+
+        return explode(',',$allowedIPs->setting_value);
+    }
 }

+ 2 - 1
config/qingyaoWx.php

@@ -1,10 +1,11 @@
 <?php
 
 return [
+    "redis_key" => "qingyao_weixin",
     "token" => "qingyao",
     "appid" => "wxb207e156f593f699",
     "appsecret" => "25b2455c0cd6e87c73c9912ba1523aee",
     "get_token" => "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",
-
+    "get_article" => "https://api.weixin.qq.com/cgi-bin/freepublish/batchget?access_token=%s",
 ];
 

+ 1 - 0
routes/weixin.php

@@ -12,6 +12,7 @@
 */
 
 Route::any('checkForWx', 'Api\WeixinController@checkForWx');
+Route::any('getPublicWxArticle', 'Api\WeixinController@getPublicWxArticle');
 
 Route::group(['middleware'=> ['checkWeixin']],function ($route){