cqp 3 月之前
父节点
当前提交
e5a0c0c1f7
共有 2 个文件被更改,包括 52 次插入33 次删除
  1. 39 33
      app/Service/Weixin/WeixinService.php
  2. 13 0
      config/wechatOfficial.php

+ 39 - 33
app/Service/Weixin/WeixinService.php

@@ -5,20 +5,23 @@ namespace App\Service\Weixin;
 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 $config = [];
+
+    public function __construct()
+    {
+        $config = config('wechatOfficial');
+        $this->config = $config;
+    }
 
     public function getToken(){
-        $token_key = self::KEY.'_'.'token';
+        $config = $this->config;
+        $token_key = $config['tokenKeyName'] . '_' . 'token';
         $token = Redis::get($token_key);
         if(! $token){
-            $url = sprintf(self::ACCESS_URL,self::APPID,self::APPSECRET);
+            $url = sprintf($config['getTokenUrl'],$config['appId'],$config['appSecret']);
             $res = $this->curlOpen($url);
             $res = json_decode($res,true);
             if(isset($res['errmsg'])) return [false,$res['errmsg']];
@@ -32,45 +35,48 @@ class WeixinService extends Service
         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]];
-    }
-
     public function setWebHook($data){
-//        file_put_contents('22.txt',json_encode($data));
+        $config = $this->config;
+        //重定向地址参数
         $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);
+        //重定向地址 重定向到 getUnionid 去获取openid 然后定向到前端页面
+        $redirectUrl = sprintf($config['redirectUrl'],$uri,$param);
+        $redirectUrl = urlencode($redirectUrl);
+
+        $url = sprintf($config['setWebHookUrl'],$config['appId'],$redirectUrl);
         header("Location:".$url);exit;
         echo 'ok';die;
     }
 
     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'];
+            if(! $status) return [false, $openid];
+            $config = $this->config;
+            $uri = isset($data['uri']) ? $data['uri'] : '';
             $param = isset($data['param']) ? $data['param'] : '';
-            $url = 'https://t9.qingyaokeji.com/#/wxGet?uri='.$uri.'&openid='.$openid.'&param='.$param;
-            header('Location:'.$url);exit();
+            $openid = $openid['openid'];
+
+            //重定向前端地址
+            $redirectFrontEndUrl = sprintf($config['redirectFrontEndUrl'],$uri,$openid,$param);
+            header('Location:'.$redirectFrontEndUrl);
+            exit();
         }
     }
 
+    public function getOpenid($data){
+        $config = $this->config;
+        if(empty($data['code'])) return [false, 'code不能为空'];
+        $code = $data['code'];
+        $url = sprintf($config['getOpenIdUrl'],$config['appId'],$config['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]];
+    }
+
 //    public function sendTmpMsg($data){
 //        //        $openid = 'okXNa69ggEX61KvHUhCq9PcGrPKI';
 //        $data = [

+ 13 - 0
config/wechatOfficial.php

@@ -0,0 +1,13 @@
+<?php
+
+return [
+   "appId" => "",
+   "accessSecret" => "",
+   "getTokenUrl" => "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",
+   "tokenKeyName" => "tongyuekeji",
+   "getOpenIdUrl" => "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code",
+   "setWebHookUrl" => "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect",
+   "redirectUrl" => "https://api.qingyaokeji.com/wxapi/getUnionid?uri=%s&param=%s",//todo
+   "redirectFrontEndUrl" => "https://qingyaokeji.com/#/wxGet?uri=%s&openid=%s&param=%s",//todo
+];
+