| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <?php
- class LewaimaiWeixinauth {
- public static function getComponentAccessToken()
- {
- $weixinauthcacheModel = WeixinauthCache::model()->findByPk(1);
-
- $component_access_token = false;
- if ($weixinauthcacheModel->component_access_token)
- {
- $exp_time = strtotime($weixinauthcacheModel->component_access_token_expires_time) - 100;
- $time = time();
- if($exp_time >= $time) {
- $component_access_token = $weixinauthcacheModel->component_access_token;
- }
- }
-
- if (!$component_access_token)
- {
- $component_verify_ticket = $weixinauthcacheModel->component_verify_ticket;
-
- $postArray = array();
- $postArray["component_appid"] = LEWAIMAI_PINGTAI_APPID;
- $postArray["component_appsecret"] = LEWAIMAI_PINGTAI_APPSECRET;
- $postArray["component_verify_ticket"] = $component_verify_ticket;
- $postJson = json_encode($postArray);
-
- $url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
- $post_res = LewaimaiHttp::POSTJSON($url, $postJson);
- $postresArray = json_decode($post_res, true);
-
- $component_access_token = $postresArray["component_access_token"];
- $expires_in = time() + $postresArray["expires_in"];
-
- $weixinauthcacheModel->component_access_token = $component_access_token;
- $weixinauthcacheModel->component_access_token_expires_time = date('Y-m-d H:i:s', $expires_in);
- if (!$weixinauthcacheModel->update())
- {
- LewaimaiDebug::LogModelError($weixinauthcacheModel);
- return false;
- }
- }
-
- return $component_access_token;
- }
-
- public static function getPreauthcode()
- {
- $pre_auth_code = false;
- $weixinauthcacheModel = WeixinauthCache::model()->findByPk(1);
- if ($weixinauthcacheModel->pre_auth_code)
- {
- $exp_time = strtotime($weixinauthcacheModel->pre_auth_code_expires_time) - 100;
- $time = time();
- if($exp_time >= $time) {
- $pre_auth_code = $weixinauthcacheModel->pre_auth_code;
- }
- }
-
- $pre_auth_code = false;
- if (!$pre_auth_code)
- {
- //如果缓存里面没有pre_auth_code,那么就重新获取
- $component_access_token = false;
- if ($weixinauthcacheModel->component_access_token)
- {
- $exp_time = strtotime($weixinauthcacheModel->component_access_token_expires_time) - 100;
- $time = time();
- if($exp_time >= $time) {
- $component_access_token = $weixinauthcacheModel->component_access_token;
- }
- }
-
- if (!$component_access_token)
- {
- $component_verify_ticket = $weixinauthcacheModel->component_verify_ticket;
-
- $postArray = array();
- $postArray["component_appid"] = LEWAIMAI_PINGTAI_APPID;
- $postArray["component_appsecret"] = LEWAIMAI_PINGTAI_APPSECRET;
- $postArray["component_verify_ticket"] = $component_verify_ticket;
- $postJson = json_encode($postArray);
-
- $url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
- $post_res = LewaimaiHttp::POSTJSON($url, $postJson);
- $postresArray = json_decode($post_res, true);
-
- $component_access_token = $postresArray["component_access_token"];
- $expires_in = time() + $postresArray["expires_in"];
-
- $weixinauthcacheModel->component_access_token = $component_access_token;
- $weixinauthcacheModel->component_access_token_expires_time = date('Y-m-d H:i:s', $expires_in);
- if (!$weixinauthcacheModel->update())
- {
- LewaimaiDebug::LogModelError($weixinauthcacheModel);
- return false;
- }
- }
-
- $postArray = array();
- $postArray["component_appid"] = LEWAIMAI_PINGTAI_APPID;
- $postJson = json_encode($postArray);
-
- $preauthcode_url = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=" . $component_access_token;
- $post_res = LewaimaiHttp::POSTJSON($preauthcode_url, $postJson);
- $postresArray = json_decode($post_res, true);
-
- $pre_auth_code = $postresArray["pre_auth_code"];
- $expires_in = time() + $postresArray["expires_in"];
-
- $weixinauthcacheModel->pre_auth_code = $pre_auth_code;
- $weixinauthcacheModel->pre_auth_code_expires_time = date('Y-m-d H:i:s', $expires_in);
- if (!$weixinauthcacheModel->update())
- {
- LewaimaiDebug::LogModelError($weixinauthcacheModel);
- return false;
- }
- }
-
- return $pre_auth_code;
- }
-
- //获取授权令牌
- public static function getAuthorizerAccessToken($adminId)
- {
- $adminweixinauthinfocacheModel = AdminWeixinauthInfoCache::model()->find("admin_id = " . $adminId);
- if (!$adminweixinauthinfocacheModel)
- {
- Yii::log("no this adminweixinauthinfocache!");
- return false;
- }
-
- $authorizer_access_token = false;
- if ($adminweixinauthinfocacheModel->authorizer_access_token)
- {
- $exp_time = strtotime($adminweixinauthinfocacheModel->authorizer_access_token_expires_time) - 100;
- $time = time();
- if($exp_time >= $time) {
- $authorizer_access_token = $adminweixinauthinfocacheModel->authorizer_access_token;
- }
- }
-
- if (!$authorizer_access_token)
- {
- //此时通过刷新令牌去刷新
- $component_access_token = LewaimaiWeixinauth::getComponentAccessToken();
- if (!$component_access_token) {
- return false;
- }
-
- $postArray = array();
- $postArray["component_appid"] = LEWAIMAI_PINGTAI_APPID;
- $postArray["authorizer_appid"] = $adminweixinauthinfocacheModel->authorizer_appid;
- $postArray["authorizer_refresh_token"] = $adminweixinauthinfocacheModel->authorizer_refresh_token;
- $postJson = json_encode($postArray);
-
- $url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=" . $component_access_token;
- $post_res = LewaimaiHttp::POSTJSON($url, $postJson);
- $postresArray = json_decode($post_res, true);
- if (!isset($postresArray["authorizer_access_token"]) || $postresArray["authorizer_access_token"] == "")
- {
- Yii::log("refresh authorizer_access_token failed! adminId:" . $adminId);
- return false;
- }
-
- $authorizer_access_token = $postresArray["authorizer_access_token"];
- $expires_in = time() + $postresArray["expires_in"];
- $authorizer_refresh_token = $postresArray["authorizer_refresh_token"];
-
- $adminweixinauthinfocacheModel->authorizer_access_token = $authorizer_access_token;
- $adminweixinauthinfocacheModel->authorizer_access_token_expires_time = date('Y-m-d H:i:s', $expires_in);
- $adminweixinauthinfocacheModel->authorizer_refresh_token = $authorizer_refresh_token;
-
- if (!$adminweixinauthinfocacheModel->update()) {
- LewaimaiDebug::LogModelError($adminweixinauthinfocacheModel);
- return false;
- }
- }
-
- return $authorizer_access_token;
- }
-
- //获取用户的信息(这个不用于第一次绑定时候的获取)
- public static function getAuthorizerInfo($adminId){
- $adminModel = Admin::model()->findByPk($adminId);
- if (!$adminModel)
- {
- Yii::log("adminModel is NULL!");
- return false;
- }
-
- $component_access_token = LewaimaiWeixinauth::getComponentAccessToken();
- if (!$component_access_token) {
- return false;
- }
-
- $postArray = array();
- $postArray["component_appid"] = LEWAIMAI_PINGTAI_APPID;
- $postArray["authorizer_appid"] = $adminModel->appid;
- $postJson = json_encode($postArray);
-
- $url = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=" . $component_access_token;
- $post_res = LewaimaiHttp::POSTJSON($url, $postJson);
- $postresArray = json_decode($post_res, true);
- if (isset($postresArray["errcode"]) && $postresArray["errcode"] != 0)
- {
- //获取信息失败,重新授权
- Yii::log("get authorizer_info failed! errmsg:" . $postresArray["errmsg"]);
- return false;
- }
-
- $adminModel->wx_name = $postresArray["authorizer_info"]["nick_name"];
- if (isset($postresArray["authorizer_info"]["head_img"]))
- {
- $adminModel->head_img = $postresArray["authorizer_info"]["head_img"];
- }
- $adminModel->wx_tousername = $postresArray["authorizer_info"]["user_name"];
- if (isset($postresArray["authorizer_info"]["alias"]))
- {
- $adminModel->wx_account = $postresArray["authorizer_info"]["alias"];
- }
- $adminModel->qrcode_url = $postresArray["authorizer_info"]["qrcode_url"];
- $service_type_info = $postresArray["authorizer_info"]["service_type_info"]["id"];
- $verify_type_info = $postresArray["authorizer_info"]["verify_type_info"]["id"];
-
- if (($service_type_info == 0 || $service_type_info == 1))
- {
- if (($verify_type_info == -1 || $verify_type_info == 1 || $verify_type_info == 2))
- {
- $adminModel->wx_type = 0;
- }
- else
- {
- $adminModel->wx_type = 1;
- }
- }
- else
- {
- if (($verify_type_info == -1 || $verify_type_info == 1 || $verify_type_info == 2))
- {
- $adminModel->wx_type = 2;
- }
- else
- {
- $adminModel->wx_type = 3;
- }
- }
-
- if (!$adminModel->update()) {
- LewaimaiDebug::LogModelError($adminModel);
- return false;
- }
-
- return true;
- }
- }
|