最新消息:文章中包含代码时,请遵守代码高亮规范!

微信公共平台工具类集成[原创]

杂项 Alex 84浏览 0评论

项目中对于微信的接入是常见的,本次对微信进行了简单集成。

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 ThirdPartyAuthorizeController  implements 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


pay_weixin
pay_weixin
微信打赏
pay_weixin
支付宝打赏
感谢您对作者Alex的打赏,我们会更加努力!    如果您想成为作者,请点我

您必须 登录 才能发表评论!