WechatWebApi.class.php 6.55 KB
Newer Older
章建武's avatar
章建武 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
<?php
namespace Common\Logic\Wechat;

use Think\Log;

/**
 * 微信网页接口类
 * 
 * 微信网页开发的接口
 * 
 * @see https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
 */
class WechatWebApi
{
    
    /**
     * 网页授权的地址
     * @var string
     */ 
    const OAUTH2_URL = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect';
    
    /**
     * 网页授权获取access_token的地址
     * @var string
     */
    const OAUTH2_ACCESS_URL = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code';
    
    /**
     * 网页授权后获取用户信息的地址
     * @var unknown
     */
    const USER_INFO_URL = 'https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN ';
    
    /**
     * 获取token的url
     * @var string
     */
    const TOKEN_URL = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s';
    
    /**
     * 获取js ticket的请求url
     * @var string
     */
    const JS_TICKET_URL = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi';
    
    /**
     * 实例
     * @var object
     */
    protected static $instance;
    
    /**
     * @var string
     */
    protected $appId = '';
    
    /**
     * @var string
     */
    protected $appSecret = '';
    
    /**
     * 错误信息
     * @var array
     */
    protected $error = array();

    /**
     * __construct
     */
    protected function __construct()
    {
        $this->appId        = C('APPID');
        $this->appSecret    = C('APP_SECRET');
    }
    
    /**
     * 获取实例
     * @return \Common\Logic\Wechat\WechatApi
     */
    public static function getInstance()
    {
        return self::$instance ? self::$instance : self::$instance = new WechatWebApi();
    }
    
    public function getAppId()
    {
        return $this->appId;
    }
    
    /**
     * 获取js ticket的请求url
     * @param string $token
     * @return string
     */
    public function getJsTicketUrl($token)
    {
        return sprintf(self::JS_TICKET_URL, $token);
    }
    
    /**
     * 获取js ticket
     * @param string $token
     * @return boolean|array
     */
    public function getJsTicket($token)
    {
        $url = $this->getJsTicketUrl($token);
        
        $curl = new Curl();
        $rst = $curl->setUrl($url)->setHttps()->request()->result;
        $rst_arr = json_decode($rst, true);
        
        if (!is_array($rst_arr) || empty($rst_arr) || !array_key_exists('ticket', $rst_arr)) {
            $this->setError('get js ticket error:'.$rst);
            return false;
        }
        
        return $rst_arr;
    }
    
    /**
     * 获取token的请求url
     * @return string
     */
    public function getTokenUrl()
    {
        return sprintf(self::TOKEN_URL, $this->appId, $this->appSecret);
    }
    
    public function getToken()
    {
        $url = $this->getTokenUrl();
        
        $curl = new Curl();
        $rst = $curl->setUrl($url)->setHttps()->request()->result;
        $rst_arr = json_decode($rst, true);
        
        if (!is_array($rst_arr) || empty($rst_arr) || !array_key_exists('access_token', $rst_arr)) {
            $this->setError('get token error:'.$rst);
            return false;
        }
        
        return $rst_arr;
    }
    
    /**
     * 获取授权地址
     * 
     * @param string 授权后返回的url
     * @param string $scope
     * @param string $state
     * 
     * @return string|boolean 失败返回false否则返回url
     */
    public function getOauthUrl($url, $scope = 'snsapi_base', $state = '')
    {
        if (empty($this->appId)) {
            $this->setError('appId is empty');
            return false;
        }
        
        return sprintf(self::OAUTH2_URL, $this->appId, urlencode($url), $scope, $state);
    }
    
    /**
     * 获取code换取access_token的地址
     *
     * @param string 授权后返回的url
     * @param string $scope
     * @param string $state
     *
     * @return string|boolean 失败返回false否则返回url
     */
    public function getAccessTokenUrl($code)
    {
        if (empty($this->appId) || empty($this->appSecret)) {
            $this->setError('appId or appSecret is empty');
            return false;
        }
    
        return sprintf(self::OAUTH2_ACCESS_URL, $this->appId, $this->appSecret, $code);
    }
    
    /**
     * 获取授权access_token
     * @param string code
     * @return array|boolean 失败返回false,否则返回access_token数组
     */
    public function getOauthAccess($code)
    {
        $url = $this->getAccessTokenUrl($code);
        if (!$url) return false;
        
        $curl = new Curl();
        $rst = $curl->setUrl($url)->setHttps()->request()->result;
        $rst_arr = json_decode($rst, true);
        
        if (!is_array($rst_arr) || empty($rst_arr) || !array_key_exists('access_token', $rst_arr)) {
            $this->setError('get access token error:'.$rst);
            return false;
        }
        
        return $rst_arr;
    }
    
    /**
     * 获取错误信息
     * @return array
     */
    public function getError()
    {
        return $this->error;
    }
    
    /**
     * 设置错误信息
     * @param string $msg
     */
    protected function setError($msg)
    {
        $this->error[] = $msg;
        Log::write($msg);
    }
    
    /**
     * 获取用户信息的地址
     * @param string access_token
     * @param string openid
     * @return boolean|string
     */
    public function userInfoUrl($access_token, $openid)
    {
        if (empty($access_token) || empty($openid)) {
            $this->setError('access token or openid is empty');
            return false;
        }
        
        return sprintf(self::USER_INFO_URL, $access_token, $openid);
    }
    
    /**
     * 获取用户信息
     * @param string access_token
     * @param string openid
     * @return boolean|array
     */
    public function getUserInfo($access_token, $openid)
    {
        $url = $this->userInfoUrl($access_token, $openid);
        if (!$url) return false;
        
        $curl = new Curl();
        $rst = $curl->setUrl($url)->setHttps()->request()->result;
        $rst_arr = json_decode($rst, true);
        
        if (!is_array($rst_arr) || empty($rst_arr) || !array_key_exists('openid', $rst_arr)) {
            $this->setError('get user info error:'.var_export($rst, true));
            return false;
        }
        
        return $rst_arr;
    }
}