Răsfoiți Sursa

公众号优化

shish 5 ani în urmă
părinte
comite
6470d69c25
2 a modificat fișierele cu 19 adăugiri și 102 ștergeri
  1. 1 1
      app-pt/controllers/WxController.php
  2. 18 101
      common/components/wxUtil.php

+ 1 - 1
app-pt/controllers/WxController.php

@@ -156,7 +156,7 @@ class WxController extends PublicController
                     //关注
                     case 'subscribe':
                         $merchant = WxOpenClass::getGhsWxInfo();
-                        $getUserInfo = wxUtil::userInfo($this->fromUsername, $merchant, 1);
+                        $getUserInfo = wxUtil::userInfo($this->fromUsername, $merchant, 2);
                         $source = UserClass::$userSourceId['official']['name'];
                         $getUserInfo['subscribe'] = 1;
                         \bizGhs\admin\services\AdminService::replaceAdmin($getUserInfo, $source);

+ 18 - 101
common/components/wxUtil.php

@@ -3,10 +3,8 @@
 namespace common\components;
 
 use bizHd\wx\classes\WxBaseClass;
-use bizHd\wx\classes\WxOpenClass;
 use bizHd\wx\services\WxBaseService;
 use bizHd\wx\services\WxMiniBaseService;
-use bizHd\wx\services\WxOpenService;
 use common\services\xhMerchantExtendService;
 use Yii;
 use yii\helpers\Json;
@@ -196,10 +194,6 @@ class wxUtil
 
     /**
      * 上传视频文件
-     * @param unknown_type
-     * @param $merchant
-     * @param $fullFile
-     * @return mixed
      */
     public function uploadVideoMedia($merchant, $fullFile)
     {
@@ -229,8 +223,6 @@ class wxUtil
 
     /**
      * 下载多媒体文件
-     * @param unknown_type $access_token
-     * @param unknown_type $media_id
      */
     public static function uploadMedia($media_id, $access_token)
     {
@@ -272,7 +264,6 @@ class wxUtil
 
     /**
      * 解除绑定删除自定义菜单
-     * @param string $accessToken
      */
     public static function menuDelete($merchant)
     {
@@ -383,8 +374,8 @@ class wxUtil
             util::fail('没有找到平台');
         }
 
-        $component_appid = $open['appId'];
-        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $appId . "&redirect_uri=" . urlencode($REDIRECT_URI) . "&response_type=code&scope=" . $scope . "&state=1&component_appid={$component_appid}#wechat_redirect";
+        $component_app_id = $open['appId'];
+        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $appId . "&redirect_uri=" . urlencode($REDIRECT_URI) . "&response_type=code&scope=" . $scope . "&state=1&component_appid={$component_app_id}#wechat_redirect";
         header("Location:" . $url);
         //如果这里没有exit(),在apache服务器下无法实现跳转
         exit();
@@ -412,9 +403,9 @@ class wxUtil
             util::fail('没有找到平台');
         }
 
-        $component_appid = $open['appId'];
+        $component_app_id = $open['appId'];
         $componentAccessToken = wxUtil::getComponentAccessToken($open);
-        $url = "https://api.weixin.qq.com/sns/oauth2/component/access_token?appid={$appId}&code={$code}&grant_type=authorization_code&component_appid={$component_appid}&component_access_token={$componentAccessToken}";
+        $url = "https://api.weixin.qq.com/sns/oauth2/component/access_token?appid={$appId}&code={$code}&grant_type=authorization_code&component_appid={$component_app_id}&component_access_token={$componentAccessToken}";
         $curl = new curl\Curl();
         $result = $curl->get($url);
         $data = Json::decode($result);
@@ -524,8 +515,6 @@ class wxUtil
 
     /**
      * 消息模板,发送任务通知
-     * @param unknown $data
-     * @return mixed
      */
     public static function sendTaskInform($data, $merchant, $isOpen = 0)
     {
@@ -540,9 +529,6 @@ class wxUtil
 
     /**
      * 创建模板消息
-     * @param unknown $accessToken
-     * @param unknown $template_id_short
-     * @return unknown
      */
     public static function tMessageCreate($merchant, $shortTemplateId, $isOpen = 0)
     {
@@ -557,9 +543,6 @@ class wxUtil
 
     /**
      * 删除模板消息
-     * @param unknown $accessToken
-     * @param unknown $templateId
-     * @return unknown
      */
     public static function delTMessage($merchant, $templateId)
     {
@@ -574,8 +557,6 @@ class wxUtil
 
     /**
      * 取所有模板消息
-     * @param unknown_type $appId
-     * @return unknown
      */
     public function getAllTMessage($merchant)
     {
@@ -589,9 +570,6 @@ class wxUtil
 
     /**
      * 设置模板消息的行业
-     * @param unknown $accessToken
-     * @param unknown $templateId
-     * @return unknown
      */
     public function setTMIndustry($merchant, $industry_id1, $industry_id2)
     {
@@ -606,8 +584,6 @@ class wxUtil
 
     /**
      * 获取公众号的accessToken 公众号调用凭据
-     * @param unknown $appId
-     * @return unknown|string
      */
     public static function getAuthorizerAccessToken($merchant, $isOpen = 0)
     {
@@ -625,42 +601,32 @@ class wxUtil
         if (empty($open)) {
             util::fail('没有找到平台信息');
         }
-        $component_appid = $open['appId'];
+        $component_app_id = $open['appId'];
         $componentAccessToken = wxUtil::getComponentAccessToken($open);
         $appId = $merchant['wxAppId'];
         $wxAccessToken = $merchant['wxAccessToken'];
         $wxAccessTokenTime = $merchant['wxAccessTokenTime'];
         $authorize_refresh_token = $merchant['wxRefreshToken'];
-        Yii::info('获取componentAccessToken 返回:' . $componentAccessToken);
         $now = time();
         if ($now > strtotime($wxAccessTokenTime)) {
             $url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token={$componentAccessToken}";
-            $data = ['component_appid' => $component_appid, 'authorizer_appid' => $appId, 'authorizer_refresh_token' => $authorize_refresh_token];
+            $data = ['component_appid' => $component_app_id, 'authorizer_appid' => $appId, 'authorizer_refresh_token' => $authorize_refresh_token];
             $curl = new curl\Curl();
             $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
-            Yii::info('获取access token 返回:' . $result);
+
+            Yii::info('通过refresh_token获取access_token返回:' . $result);
+
             $result = Json::decode($result);
             if (isset($result['authorizer_access_token'])) {
-                $authorize_access_token = $result['authorizer_access_token'];
+                $wxAccessToken = $result['authorizer_access_token'];
                 $expires_in = $result['expires_in'];
                 $authorize_refresh_token = $result['authorizer_refresh_token'];
                 $uData['wxRefreshToken'] = $authorize_refresh_token;;
-                $uData['wxAccessToken'] = $authorize_access_token;
+                $uData['wxAccessToken'] = $wxAccessToken;
                 $uData['wxAccessTokenTime'] = date("Y-m-d H:i:s", (time() + $expires_in - 100));
-                if ($isOpen == 1) {
-                    //零售
-                    $wxBaseId = isset($open['wxBaseId']) ? $open['wxBaseId'] : 0;
-                    if (!empty($wxBaseId)) {
-                        WxBaseService::updateById($wxBaseId, $uData);
-                    }
-                } elseif ($isOpen == 2) {
-                    $wxBaseId = isset($open['wxBaseId']) ? $open['wxBaseId'] : 0;
-                    if (!empty($wxBaseId)) {
-                        WxBaseService::updateById($wxBaseId, $uData);
-                    }
-                    //供货商
-                } elseif ($isOpen == 3) {
-                    $wxBaseId = isset($open['wxBaseId']) ? $open['wxBaseId'] : 0;
+                if (in_array($isOpen, [1, 2, 3])) {
+                    //零售、供货商和商城
+                    $wxBaseId = $open['wxBaseId'] ?? 0;
                     if (!empty($wxBaseId)) {
                         WxBaseService::updateById($wxBaseId, $uData);
                     }
@@ -668,12 +634,10 @@ class wxUtil
                     //其它
                     xhMerchantService::updateById($sjId, $uData);
                 }
-                return $authorize_access_token;
             }
-            return;
-        } else {
-            return $wxAccessToken;
         }
+        Yii::info('获取componentAccessToken 成功:' . $componentAccessToken);
+        return $wxAccessToken;
     }
 
     /**
@@ -694,7 +658,7 @@ class wxUtil
             util::fail('没有找到平台');
         }
 
-        $component_appid = $open['appId'];
+        $component_app_id = $open['appId'];
         $componentAccessToken = wxUtil::getComponentAccessToken($open);
         $appId = $merchant['miniAppId'];
         if ($isOpen == 1) {
@@ -719,7 +683,7 @@ class wxUtil
         Yii::info("当前时间:{$now} 小程序accessToken过期时间:" . strtotime($miniAccessTokenTime) . " merchant:" . json_encode($merchant));
         if ($now > strtotime($miniAccessTokenTime)) {
             $url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token={$componentAccessToken}";
-            $data = ['component_appid' => $component_appid, 'authorizer_appid' => $appId, 'authorizer_refresh_token' => $miniRefreshToken];
+            $data = ['component_appid' => $component_app_id, 'authorizer_appid' => $appId, 'authorizer_refresh_token' => $miniRefreshToken];
             $curl = new curl\Curl();
             $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
             $result = Json::decode($result);
@@ -932,10 +896,6 @@ class wxUtil
 
     /**
      * 批量取消用户标签
-     * @param unknown_type $appId
-     * @param unknown_type $openIdList
-     * @param unknown_type $tagId
-     * @return unknown
      */
     public static function membersCancelTag($merchant, $openIdList, $tagId)
     {
@@ -950,8 +910,6 @@ class wxUtil
 
     /**
      * 新增临时素材
-     * @param unknown_type $appId
-     * @return unknown
      */
     public static function uploadTempMedia($merchant, $imgUrl, $type = 'image')
     {
@@ -971,8 +929,6 @@ class wxUtil
 
     /**
      * 获取临时素材
-     * @param unknown_type $appId
-     * @param unknown_type $mediaId
      */
     public static function getTempMaterial($merchant, $mediaId)
     {
@@ -997,8 +953,6 @@ class wxUtil
 
     /**
      * 获取临时素材的头信息等
-     * @param unknown_type $url
-     * @return unknown
      */
     public static function getTempMaterialInfo($url)
     {
@@ -1017,8 +971,6 @@ class wxUtil
 
     /**
      * 读取临时素材的资源
-     * @param unknown_type $filename
-     * @param unknown_type $fileContent
      */
     function readTempMaterial($filename, $fileContent)
     {
@@ -1032,9 +984,6 @@ class wxUtil
 
     /**
      * 创建门店
-     * @param $merchant
-     * @param $menu
-     * @return mixed
      */
     public function createStore($info)
     {
@@ -1046,7 +995,6 @@ class wxUtil
 
     /**
      * 查询门店信息
-     * @param $poi_id
      */
     public function getStoreInfo($poi_id)
     {
@@ -1059,8 +1007,6 @@ class wxUtil
 
     /**
      * 查询门店列表
-     * @param int $begin 开始数据
-     * @param int $count 返回数据条数,最大允许50,默认为20
      */
     public function getStoreList($begin = 0, $count = 20)
     {
@@ -1073,8 +1019,6 @@ class wxUtil
 
     /**
      * 查询门店列表 -- 重复 可删除
-     * @param $data
-     * @return mixed
      */
     function getShopList($data)
     {
@@ -1098,8 +1042,6 @@ class wxUtil
 
     /**
      * 删除门店
-     * @param $poi_id
-     * @return mixed
      */
     public function delStore($poi_id)
     {
@@ -1123,9 +1065,6 @@ class wxUtil
     /******************************* wifi *************************************/
     /**
      * 获取wifi门店列表
-     * @param $pageindex    开始页
-     * @param $pagesize     每页数据条数(最大20)
-     * @return mixed
      */
     function getShopWifiList($pageindex = 1, $pagesize = 10)
     {
@@ -1139,10 +1078,6 @@ class wxUtil
 
     /**
      * 添加密码型设备
-     * @param $shopId   门店ID
-     * @param $ssid     无线网络设备的ssid
-     * @param $pwd      无线网络设备的密码
-     * @return mixed
      */
     function addPasswordDevice($shopId, $ssid, $pwd)
     {
@@ -1156,11 +1091,6 @@ class wxUtil
 
     /**
      * 修改门店Wi-Fi(修改门店网络信息)
-     * @param $shopId
-     * @param $oldSsid
-     * @param $ssid
-     * @param string $pwd
-     * @return mixed
      */
     function updateDevice($shopId, $oldSsid, $ssid, $pwd = '')
     {
@@ -1177,8 +1107,6 @@ class wxUtil
 
     /**
      * 删除密码型设备
-     * @param $bssid   无线网络设备无线mac地址
-     * @return mixed
      */
     function delPasswordDevice($bssid)
     {
@@ -1192,8 +1120,6 @@ class wxUtil
 
     /**
      * 查询门店Wi-Fi信息
-     * @param $shopId   门店ID
-     * @return mixed
      */
     function getShopWifi($shopId)
     {
@@ -1207,10 +1133,6 @@ class wxUtil
 
     /**
      * 获取物料二维码
-     * @param $shopId   门店ID
-     * @param $ssid     无线网络设备的ssid
-     * @param $img_id   物料样式编号(0-纯二维码,可用于自由设计宣传材料;1-二维码物料,155mm×215mm(宽×高),可直接张贴)
-     * @return mixed
      */
     function getWifiQrCode($shopId, $ssid, $img_id)
     {
@@ -1263,7 +1185,6 @@ class wxUtil
 
     /**
      * 拉取门店所有的设备信息
-     * @return array
      */
     public function getShopWifiListAll()
     {
@@ -1284,8 +1205,6 @@ class wxUtil
 
     /**
      * 通过 poi_id 从Wi-Fi门店列表中 获取 shopId
-     * @param $poiId
-     * @return bool
      */
     public function getShopId($poiId, $list = [])
     {
@@ -1692,7 +1611,6 @@ class wxUtil
 
     /**
      * 微信开放平台使用授权码换取公众号的接口调用凭据和授权信息
-     * authorizer_access_token authorizer_refresh_token
      */
     public static function getAuthorizer($code, $open)
     {
@@ -1719,5 +1637,4 @@ class wxUtil
         return $result;
     }
 
-
 }