public function jingmo(){
//配置參數的數組
$CONF = array(
'__APPID__' =>'appid',
'__SERECT__' =>'serect',
'__CALL_URL__' =>'http://m.tenghuo.cn/Home/Accounts/weixincallback' //當前頁地址
);json
//沒有傳遞code的狀況下,先登陸一下
if(!isset($_GET['code']) || empty($_GET['code'])){api
$getCodeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize".
"?appid=" . $CONF['__APPID__'] .
"&redirect_uri=" . $CONF['__CALL_URL__'] .
"&response_type=code".
"&scope=snsapi_base". #!!!scope設置爲snsapi_base !!!
"&state=1";數組
//跳轉微信獲取code值,去登錄
header('Location:' . $getCodeUrl);
exit;
}微信
$code = trim($_GET['code']);
//使用code,拼湊連接獲取用戶openid
$getTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$CONF['__APPID__']."&secret=".$CONF['__SERECT__']."&code=".$code."&grant_type=authorization_code";app
//拿到openid,下面就能夠繼續調起支付啦
$openid = $token_get_all->openid;
}
public function weixincallback() {
$code = $_GET['code'];
$state = $_GET['state'];
$appid = 'wx610e783826ad389f';
$appsecret = 'a0395af84a7a7e1ea28fd8483fca125e';
if (empty($code)) {
$this->error('受權失敗');
}
//獲取access_token
$token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $code . '&grant_type=authorization_code';
$token = json_decode(file_get_contents($token_url));
if (isset($token->errcode)) {
echo '<h1>錯誤:</h1>' . $token->errcode;
echo '<br/><h2>錯誤信息:</h2>' . $token->errmsg;
exit;
}
$access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=' . $appid . '&grant_type=refresh_token&refresh_token=' . $token->refresh_token;
//認證令牌合法性
$access_token = json_decode(file_get_contents($access_token_url));
if (isset($access_token->errcode)) {
echo '<h1>錯誤:</h1>' . $access_token->errcode;
echo '<br/><h2>錯誤信息:</h2>' . $access_token->errmsg;
exit;
}
$is = A('Hezuoaccount')->login($access_token->openid);
if ($is == 2) {
A('Hezuoaccount')->registers($access_token->openid);
}
this