Example #1
0
/**
 * 获取openid
 */
function getOpenId()
{
    $code = $_GET['code'];
    $state = $_GET['state'];
    $appid = Wx_C('wx_appid');
    $secret = Wx_C('wx_secret');
}
Example #2
0
/**
 * 微信通过code换取网页授权access_token并获取用户信息,登录
 */
function checkLogin($code)
{
    if (empty($code)) {
        getWxCode('snsapi_userinfo', U());
    } else {
        $code = trim($_GET['code']);
        $state = trim($_GET['state']);
        $appid = Wx_C('wx_appid');
        $secret = Wx_C('wx_secret');
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $secret . '&code=' . $code . '&grant_type=authorization_code';
        $info = json_decode(get_url($url));
        $token = $info->access_token;
        $openid = $dejson->openid;
        $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $token . '&openid=' . $openid . '&lang=zh_CN';
        $member = json_decode(get_url($url));
        $data = array('openid' => $member->openid, 'member_name' => $member->nickname, 'gender' => $member->sex, 'city' => $member->city, 'province' => $member->province, 'avatar' => $member->headimgurl);
        $where = array('openid' => $data['openid']);
        $res = M('member')->where($where)->find();
        if (!$res) {
            M('member')->add($data);
        } else {
            M('member')->where($where)->save($data);
        }
        session('openid', $data['openid']);
    }
}
 public function wx_auto_login()
 {
     $code = trim($_GET['code']);
     $state = trim($_GET['state']);
     if ($code && $state) {
         //通过code获取用户信息
         $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . Wx_C('wx_appid') . '&secret=' . Wx_C('wx_secret') . '&code=' . $code . '&grant_type=authorization_code';
         $info = json_decode(get_url($url));
         $web_token = $info->access_token;
         $refresh_token = $info->refresh_token;
         $openid = $info->openid;
         $unionid = $info->unionid;
         //检查此用户是否已经注册过
         $member_data = M('Member')->where('openid=\'' . $openid . '\'')->find();
         if (is_array($member_data) && !empty($member_data)) {
             //更新用户微信网页授权access_token
             M('Member')->where('member_id=' . $member_data['member_id'])->save(array('web_token' => $web_token, 'refresh_token' => $refresh_token));
             //授权
             session('member_id', $member_data['member_id']);
         } else {
             //未关注
             if ($state == 'STATEuserinfo') {
                 $get_userinfo_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $web_token . '&openid=' . $openid . '&lang=zh_CN';
                 $user = json_decode(get_url($get_userinfo_url));
             } else {
                 //已关注
                 $access_token = get_wx_AccessToken(1);
                 $get_user_url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';
                 $user = json_decode(get_url($get_user_url));
             }
             if ($user->nickname) {
                 $data = array();
                 $data['nickname'] = replaceEmoji($user->nickname);
                 //转义emoji
                 $data['openid'] = $user->openid;
                 $data['gender'] = $user->sex;
                 $data['country'] = $user->country;
                 $data['province'] = $user->province;
                 $data['city'] = $user->city;
                 $data['usercity'] = $user->city;
                 $data['avatar'] = $user->headimgurl;
                 $data['unionid'] = $user->unionid;
                 $data['web_token'] = $web_token;
                 $data['refresh_token'] = $refresh_token;
                 $data['register_time'] = NOW_TIME;
                 $return = M('Member')->add($data);
                 if ($return) {
                     session('member_id', $return);
                 }
             }
         }
     } else {
         $c_url = U('', $_GET, '', true);
         //当前地址  ERROR:该地址没有生成当前地址的参数项   导致授权之后跳转页面没有传参 已解决:2015-6-27 17:35:58
         $scope = 'snsapi_userinfo';
         $re_url = urlencode($c_url);
         $sq_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . Wx_C('wx_appid') . '&redirect_uri=' . $re_url . '&response_type=code&scope=' . $scope . '&state=STATEuserinfo#wechat_redirect';
         redirect($sq_url);
         //get_url($sq_url);
     }
 }
Example #4
0
function refresh_web_token($mid, $refresh_token)
{
    $url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=' . Wx_C('wx_appid') . '&grant_type=refresh_token&refresh_token=' . $refresh_token;
    $output = get_url($url);
    $output = json_decode($output, true);
    if ($output['errcode'] > 0) {
        return false;
    } else {
        $data = array();
        $data['web_token'] = $output['access_token'];
        $data['refresh_token'] = $output['refresh_token'];
        M('Member')->where('member_id=' . $mid)->save($data);
        unset($data);
        return $output;
    }
}
Example #5
0
/**
 * 
 */
function wx_key_url($url)
{
    $scope = 'snsapi_userinfo';
    $appId = Wx_C('wx_appid');
    $redirect_uri = urlencode($url);
    $response_type = 'code';
    $state = time();
    $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $appId . '&redirect_uri=' . $redirect_uri . '&response_type=' . $response_type . '&scope=' . $scope . '&state=' . $state . '#wechat_redirect';
    return $url;
}