网页授权获取用户基本信息

网页授权获取用户基本信息

OAuthWeixin

function OAuthWeixin($callback) {
    $isWeixinBrowser = isWeixinBrowser();
    if (empty($GLOBALS ['user'] ['appid'])) {
        $memberPublicInfo = M('member_public')->where(array('token'=>get_token()))->find();
        $GLOBALS ['user'] ['appid'] = $memberPublicInfo['appid'];
        $GLOBALS ['user'] ['secret'] = $memberPublicInfo['secret'];
    }
    if (!$isWeixinBrowser || empty($GLOBALS ['user'] ['appid'])) {
        redirect($callback . '&openid=-1');
    }
    $param ['appid'] = $GLOBALS ['user'] ['appid'];

    if (!isset($_GET ['getOpenId'])) {
        $param ['redirect_uri'] = $callback . '&getOpenId=1';
        $param ['response_type'] = 'code';
        $param ['scope'] = 'snsapi_base';
        $param ['state'] = 123;
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?' . http_build_query($param) . '#wechat_redirect';
        redirect($url);
    } elseif ($_GET ['state']) {
        $param ['secret'] = $GLOBALS ['user'] ['secret'];
        $param ['code'] = I('code');
        $param ['grant_type'] = 'authorization_code';

        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?' . http_build_query($param);
        $content = file_get_contents($url);
        $content = json_decode($content, true);
        redirect($callback . '&openid=' . $content ['openid']);
    }
}

getWeixinUserInfo

// 通过openid获取微信用户基本信息,此功能只有认证的服务号才能用
function getWeixinUserInfo($openid, $token) {
    if (empty($GLOBALS ['user'] ['appid'])) {
        $memberPublicInfo = M('member_public')->where(array('token'=>$token))->find();
        $GLOBALS ['user'] ['appid'] = $memberPublicInfo['appid'];
        $GLOBALS ['user'] ['secret'] = $memberPublicInfo['secret'];
    }
    $param ['appid'] = $GLOBALS ['user'] ['appid'];
    $param ['secret'] = $GLOBALS ['user'] ['secret'];
    $param ['grant_type'] = 'client_credential';

    $url = 'https://api.weixin.qq.com/cgi-bin/token?' . http_build_query($param);
    $content = file_get_contents($url);
    $content = json_decode($content, true);

    $param2 ['access_token'] = $content ['access_token'];
    $param2 ['openid'] = $openid;
    $param2 ['lang'] = 'zh_CN';

    $url = 'https://api.weixin.qq.com/cgi-bin/user/info?' . http_build_query($param2);
    $content = file_get_contents($url);
    $content = json_decode($content, true);
    session('wxUserInfo',$content);
    return $content;
}

积分添加

/**
 * 积分添加
 */
function integralSetInc(){
    $map['token'] = get_token();
    $map['openid'] = get_openid();
    $res = M('card_member')->where($map)->setInc('integral',10);
    if($res){
        $this->ajaxReturn('ok');
    }else{
        $this->ajaxReturn('error');
    }
}



回复列表



回复操作

正在加载验证码......

请先拖动验证码到相应位置