微信公共平臺工具類整合[原創]
專案中對於微信的接入是常見的,本次對微信進行了簡單整合。
1.程式碼如下
一:實體類如下 <?php class WeCharAuthorize { /** * @var string */ private $code; /** * @var string */ private $AccessToken; /** * @var string */ private $AuthorizationRoute; /** * @var string */ private $state; /** * @var string */ private $openId; /** * @return string */ public function getOpenId() { return $this->openId; } /** * @param string $openId */ public function setOpenId($openId) { $this->openId = $openId; } /** * @return string */ public function getCode() { return $this->code; } /** * @param string $code */ public function setCode($code) { $this->code = $code; } /** * @return string */ public function getAccessToken() { return $this->AccessToken; } /** * @param string $AccessToken */ public function setAccessToken($AccessToken) { $this->AccessToken = $AccessToken; } /** * @return string */ public function getAuthorizationRoute() { return $this->AuthorizationRoute; } /** * @param string $AuthorizationRoute */ public function setAuthorizationRoute($AuthorizationRoute) { $this->AuthorizationRoute = $AuthorizationRoute; } /** * @return string */ public function getState() { return $this->state; } /** * @param string $state */ public function setState($state) { $this->state = $state; } } <?php class WeCharUserInfo { /** * @var string */ private $openid; /** * @var string */ private $nickname; /** * @var string */ private $sex; /** * @var string */ private $language; /** * @var string */ private $city; /** * @var string */ private $province; /** * @var string */ private $country; /** * @var string */ private $headimgurl; /** * @var array */ private $privilege; /** * @var string */ private $unionid; /** * @return string */ public function getUnionid() { return $this->unionid; } /** * @param string $unionid */ public function setUnionid($unionid) { $this->unionid = $unionid; } /** * @return string */ public function getOpenid() { return $this->openid; } /** * @param string $openid */ public function setOpenid($openid) { $this->openid = $openid; } /** * @return string */ public function getNickname() { return $this->nickname; } /** * @param string $nickname */ public function setNickname($nickname) { $this->nickname = $nickname; } /** * @return string */ public function getSex() { return $this->sex; } /** * @param string $sex */ public function setSex($sex) { $this->sex = $sex; } /** * @return string */ public function getLanguage() { return $this->language; } /** * @param string $language */ public function setLanguage($language) { $this->language = $language; } /** * @return string */ public function getCity() { return $this->city; } /** * @param string $city */ public function setCity($city) { $this->city = $city; } /** * @return string */ public function getProvince() { return $this->province; } /** * @param string $province */ public function setProvince($province) { $this->province = $province; } /** * @return string */ public function getCountry() { return $this->country; } /** * @param string $country */ public function setCountry($country) { $this->country = $country; } /** * @return string */ public function getHeadimgurl() { return $this->headimgurl; } /** * @param string $headimgurl */ public function setHeadimgurl($headimgurl) { $this->headimgurl = $headimgurl; } /** * @return array */ public function getPrivilege() { return $this->privilege; } /** * @param array $privilege */ public function setPrivilege($privilege) { $this->privilege = $privilege; } } </pre> <pre>二:工具類 <?php class PublicPlatformAuthorize extends ThirdPartyAuthorizeControllerimplements PublicPlatformInterface { /** * @var string */ private $code_not_exit = "未獲取到code"; /** * @var string */ private $state_not_exit = "未獲取到state"; /** * @var string */ private $access_token_not_exit = "未獲取到access_token"; /** * @var string */ private $open_id_not_exit = "未獲取到open_id"; /** * @var string */ private $user_info_not_exit = "未獲取到使用者資訊"; /** * @var string */ private $success = "操作成功"; /** * @var string */ private $access_token_return_empty_error = "accessToken獲取時返回空"; /** * @var Config 配置 */ protected static $config = null; /** * @var WeCharAuthorize 微信授權物件 */ private $weCharAuthorize = null; /** * @var WeCharUserInfo 微信使用者資訊物件 */ private $weCharUserInfo = null; function __construct(ContainerInterface $container) { parent::__construct($container); if(self::$config == null){ self::$config = Config::getSDKConfig($container); } $this->weCharAuthorize = new WeCharAuthorize(); $this->weCharUserInfo = new WeCharUserInfo(); } /** * 設定授權物件 code與state * * @return array */ public function setWeCharAuthorizeCodeAndState() { if(!isset($_GET['state'])){ return ['message' => ['message'=>$this->state_not_exit], 'errorCode'=>1]; } if (!isset($_GET['code'])) { return ['message' => ['message'=>$this->state_not_exit], 'errorCode'=>1]; } //設定授權物件 $this->weCharAuthorize->setCode($_GET['code']); $this->weCharAuthorize->setState($_GET['state']); return ['message' => ['message'=>$this->success], 'errorCode' => 0]; } /** * 設定授權物件 access_token與open_id * * @param $code * @return array */ public function setWeCharAuthorizeAccessTokenAndOpenId($code) { //獲取配置引數 $app_id = self::$config->public_platform_app_id; $secret = self::$config->public_platform_app_secret; $grant_type = self::$config->public_platform_grant_type; //判斷code if(empty($code)){ return ['message' => ['message'=>$this->code_not_exit], 'errorCode' => 1]; } //請求微信 $return = json_decode(file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=$app_id&secret=$secret&code=$code&grant_type=$grant_type"), true); //檢查返回值 if(!is_array($return) || empty($return)){ return ['message' => ['message'=>$this->access_token_return_empty_error],'errorCode' => 1]; } //設定授權物件 $this->weCharAuthorize->setAccessToken($return['access_token']); $this->weCharAuthorize->setOpenId($return['openid']); return ['message' => ['message'=>$this->success], 'errorCode' => 0]; } /** * 授權地址獲取 * * @param $redirect_url * @param int $scope_type * @param string $state * @return string */ public function getAuthorizationRoute($redirect_url, $scope_type = 1, $state = '') { //確定應用授權作用域 if($scope_type == 1){ $scope = 'snsapi_base'; }else{ $scope = 'snsapi_userinfo'; } //授權後重定向的回撥連結地址 $redirect_url = urlencode($redirect_url); //設定第三方授權介面引數 $appid = self::$config->public_platform_app_id; $response_type = 'code'; //throw new Exception("https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_url&response_type=$response_type&scope=$scope&state=$state#wechat_redirect"); //拼接url,跳轉到授權頁面 return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_url&response_type=$response_type&scope=$scope&state=$state#wechat_redirect"; } /** * 設定微信使用者物件 * * @param $openid * @param $access_token * @return array */ public function setUserInfo($openid, $access_token) { //資料判斷 if(empty($access_token)){ return ['message' => ['message'=>$this->access_token_not_exit], 'errorCode' => 1]; } if(empty($openid)){ return ['message' => ['message'=>$this->open_id_not_exit], 'errorCode' => 1]; } //請求微信 $url_info = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid"; //連結平臺 得到使用者的基本資訊 $user_info = json_decode(file_get_contents($url_info), true); //資料判斷 if(!is_array($user_info) || empty($user_info)){ return ['message' => ['message'=>$this->user_info_not_exit], 'errorCode' => 1]; } //物件封裝 $weCharUserInfoTool = new WeCharUserInfoTool(); $this->weCharUserInfo = $weCharUserInfoTool->weCharInfoArr2weCharUserInfoObj($user_info); return ['message' => ['message'=>$this->success], 'errorCode' => 0]; } public function getWeCharUserInfo() { return $this->weCharUserInfo; } /** * @return WeCharAuthorize */ public function getWeCharAuthorize() { return $this->weCharAuthorize; } /** * 授權 * * @param $redirect_url * @param int $scope_type 為2時方可獲取使用者資訊 * @param string $state * @return array|WeCharAuthorize */ public function publicPlatformAuthorize($redirect_url, $scope_type = 1, $state = '') { //設定code與state $code_res = $this->setWeCharAuthorizeCodeAndState(); if($code_res['errorCode'] != 0) { $url = $this->getAuthorizationRoute($redirect_url, $scope_type, $state); Header("Location:{$url}");exit; } //設定access_token與openId $code = $this->weCharAuthorize->getCode(); $res = $this->setWeCharAuthorizeAccessTokenAndOpenId($code); if($res['errorCode'] != 0){ return $res; } //$scope_type為2則獲取使用者資訊 if($scope_type != 1){ $open_id = $this->weCharAuthorize->getOpenId(); $access_token = $this->weCharAuthorize->getAccessToken(); $res = $this->setUserInfo($open_id, $access_token); if($res['errorCode'] != 0){ return $res; } } return ['message' => ['message'=>$this->success], 'errorCode' => 0]; } //TODO function returnRewrite($result) { // TODO: Implement returnRewrite() method. } }
二:git地址如下
git@gitee.com:redunicorn/ThirdPartyAuthorizeBundle.git
轉載時請註明出處及相應連結,本文永久地址:https://blog.yayuanzi.com/24702.html
微信打賞
支付寶打賞
感謝您對作者Alex的打賞,我們會更加努力! 如果您想成為作者,請點我