登录页面,当点击微信登录后,根据地址url生成一个二维码,让用户扫描。 在生成二维码的同时,我在数据库插入一条记录,然后将ID放在这个二维码中。 然后用户拿着微信去扫描这个二维码(地址),后面的就是公众号通过网页获取用户信息了.. 获取到用户信息后,根据地址中的ID,查找数据库,更新状态和用户信息 与此同时,在PC端二维码生成后,一直轮询向后台请求状态,根据状态获取对应的结果 大体思路就是这样,真实现起来.. 感觉也就微信公众号获取用户信息这段稍微麻烦点..毕竟得翻着API才能写.
代码是用nodejs实现的,后端的接口:
创建二维码 轮询状态 跳转到第三方地址(这个大家用的话,可能就是登录成功后跳转的地址了) 微信公众号跳转地址,用于获取code 微信公众号重定向地址,用户获取用户信息,并展示对应的信息 简单写下关于微信公众号获取用户信息的代码吧。
公众号获取用户信息代码部分
nodejs - thinkjs - controller/wechat.js
/***
module.exports = class extends Base{
/**
 * 微信扫描二维码进入该地址,附带地址信息。
 */
async indexAction(){
    let code = this.query('code');
    let redirectURI = this.config('site').domain.value+'/oauth/wechat/redirect';
    let appid = this.config('site').wechatappid.value;
    let scope = 'snsapi_userinfo';
    await this.model('lxxx').where({id : code}).update({
        status : 1//已扫描
    });
    let codeUrl =  `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirectURI}&response_type=code&scope=${scope}&state=${code}#wechat_redirect`;
    return this.redirect(codeUrl);
}
/**
 * 重定向后的地址,换取access_token
 */
async redirectAction(){
    let code = this.query('code');
    let state = this.query('state');
    let appid = this.config('site').wechatappid.value;
    let secret = this.config('site').wechatappsecret.value;
    let tokenUrl = `https://api.weixin.qq.com/sns/oauth2/access_token?appid=${appid}&secret=${secret}&code=${code}&grant_type=authorization_code`;
    let rs = await axios.get(tokenUrl).then(rs=>rs.data);
    console.log(rs);
    let access_token = rs.access_token;
    let openId = rs.openid;
    let userUrl = `https://api.weixin.qq.com/sns/userinfo?access_token=${access_token}&openid=${openId}&lang=zh_CN`
    let userInfo = await axios.get(userUrl).then(rs=>rs.data);
    console.log(userInfo);
    let {nickname,headimgurl} = userInfo;
    //查找state
    let record = await this.model('xxx').where({id : state}).find();
    console.log(record);
    if(!think.isEmpty(record) && openId && userInfo && !userInfo.errmsg){
        //获取用户信息成功
        console.log('登录成功,更新信息')
        await this.model('xxx').where({id : state}).update({
            json : JSON.stringify(userInfo),
            code : think.uuid().replace(/-/g,''),
            name : nickname,
            openid : openId,
            avatar : headimgurl,
            status : 2
        });
        this.assign('suc',true);
    }else{
        console.log('登录失败')
        await this.model('xxx').where({id : state}).update({status : 4});
        this.assign('suc',false);
    }
    //此处需要将获得的state ,然后查找对应的记录,进行数据更新。并提示关闭当前页面。
    return this.display('wechat/logintip');
}
/**
 * 扫描成功后,进入该地址,根据session进行页面地址跳转
 */
async wechatAction(){
    let id = this.query('id');
    let record = await this.model('xxx').where({id : id}).find();
    //获取对应的appid
    let loginInfo = await this.session('loginInfo');
    let clientCode = record.code;
    return this.redirect(loginInfo.redirect_uri+'?code='+clientCode+'&state='+loginInfo.state);
}}
转载请注明出处: http://sdxlp.cn/article/weixindengru.html