Skip to content

Commit

Permalink
加入QQ登录
Browse files Browse the repository at this point in the history
  • Loading branch information
newpanjing committed Jul 14, 2017
1 parent 7d1a737 commit 5695584
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 2 deletions.
109 changes: 109 additions & 0 deletions core/auth/oauth_qq.js
@@ -0,0 +1,109 @@
var config = require("../common/config");
var qq = config.oauth.qq;
var http_request = require("../common/http_request");
var qs = require("querystring");
/**
* 认证地址
* @param callback
*/
exports.authorize = function (callback) {
var url = qq.api + "/authorize?";
var param = {
response_type: 'code',
client_id: qq.appId,
redirect_uri: qq.redirect_uri,
state: new Date().getTime()
};

url += qs.stringify(param);

callback(url);
}

/**
* 获取token
* @param code
* @param callback
*/
exports.accessToken = function (code, callback) {
var url = qq.api + "/token";
var param = {
grant_type: 'authorization_code',
client_id: qq.appId,
client_secret: qq.appKey,
code: code,
redirect_uri: qq.redirect_uri
};

http_request.httpsGet(url, param, function (json) {
var data = getAccessToken(json);
callback(data);
})
};
/**
* url参 转为json
* @param str
* @returns {{}}
*/
function getAccessToken(str) {
var data = {};
var array = str.split("&");
for (var i in array) {
var items = array[i].split("=");
data[items[0]] = items[1];
}

return data;
}

/**
* 获取用户
* @param accessToken
* @param callback
*/
exports.getUserInfo = function (accessToken, cb) {

//处理jsonp
function callback(data) {
console.log(data)
//获取用户信息
var url = qq.api2 + "/user/get_user_info";
var param = {
access_token: accessToken,
oauth_consumer_key: qq.appId,
openid: data.openid
};
http_request.httpsGet(url, param, function (json) {
var userInfo = JSON.parse(json);
userInfo.openId = data.openid;
cb(userInfo);
})

}

var url = qq.api + "/me";
var param = {
access_token: accessToken
};

http_request.httpsGet(url, param, function (str) {
eval(str);
})
}

/*var dd = getAccessToken("access_token=9CBBF1A737CE0713D47B476B62DB4734&expires_in=7776000&refresh_token=1C8C556AA8A928A8D560BBE972B7035E");
console.log(dd)*/
/*
this.authorize(function (url) {
console.log(url);
});
this.accessToken('4B0B31BD633DC586FF19CDD6AA220115',function (json) {
console.log(json)
})
*/

/*this.getUserInfo('9CBBF1A737CE0713D47B476B62DB4734', function (data) {
console.log(data)
});*/
7 changes: 7 additions & 0 deletions core/common/config.js
Expand Up @@ -17,6 +17,13 @@ var config = {
api: 'https://api.weibo.com/oauth2',//接口地址
api2: "https://api.weibo.com/2",//接口地址2
redirectUri: 'http://gobang.88cto.com/oauth/weibo/callback' //授权成功回调地址
},
qq:{
appId:'101409125',
appKey:'3ac4c01094df2fd58fecf3c972578f6b',
redirect_uri: 'http://gobang.88cto.com/auth/qq/callback',
api: 'https://graph.qq.com/oauth2.0',
api2: 'https://graph.qq.com'
}
},
redis: {
Expand Down
1 change: 1 addition & 0 deletions core/common/http_request.js
Expand Up @@ -244,6 +244,7 @@ var httpsGet = function (str, data, callback, headers) {
str += buffer.toString();
});
res.on("end", function () {
console.log("str:" + str);
callback(str);
})
}
Expand Down
69 changes: 69 additions & 0 deletions core/controllers/oauth.js
Expand Up @@ -3,6 +3,7 @@
*/

var oauth_weibo = require('../auth/oauth_weibo');
var oauth_qq = require('../auth/oauth_qq');
var models = require('../models');
var User = models.User;

Expand Down Expand Up @@ -78,4 +79,72 @@ exports.weiboCallback = function (req, res, next) {

});

}

exports.qqAuth = function (req, res, next) {
oauth_qq.authorize(function (url) {
res.redirect(url);
})
}

exports.qqCallback = function (req, res, next) {

var code = req.query.code;
oauth_qq.accessToken(code, function (accessToken) {
oauth_qq.getUserInfo(accessToken.access_token, function (userInfo) {


var uid = userInfo.openId;
var user = userInfo;
//查找数据
User.findOne({
uid: uid
}, function (err, rs) {

if (rs) {
//更新
User.update({uid: uid}, {
nickName: user.nickname, //昵称,
updateTime: new Date(), //更新时间
lastLoginTime: new Date(), //最后登录时间
sex: user.gender == '男' ? 'm' : 'f',
avatar: user.figureurl_qq_2, //头像1,
openType:1
}, function (err, rs) {

});

} else {
//不存在保存

var u = new User({
nickName: user.nickname, //昵称,
createTime: new Date(), //创建时间
updateTime: new Date(), //更新时间
lastLoginTime: new Date(), //最后登录时间
sex: user.gender == '男' ? 'm' : 'f',
avatar: user.figureurl_qq_2, //头像1,
uid: uid,//id
openType:1
});
u.save(function (err, doc) {
if (err) {
console.log(err);
}
});
}
});

//用户更新
var session = {
id: userInfo.openId,
name: userInfo.nickname
};
req.session.user = session;
res.redirect("/");
});
});
// res.end(req.query.code)


}
11 changes: 11 additions & 0 deletions core/models/test.js
@@ -0,0 +1,11 @@
var models = require("../models");
var User = models.User;

User.findOne({}).then(function (data) {
console.log(1)
})

User.findOne({}).then(function (data) {
console.log(2)
})
console.log("321")
5 changes: 5 additions & 0 deletions core/models/user.js
Expand Up @@ -31,6 +31,11 @@ var User = new Schema({
draw: {//平局
type: Number,
default: 0
},
//开发平台类型
openType:{
type:Number,
default:0 //0=微博,1=QQ
}
});

Expand Down
5 changes: 5 additions & 0 deletions core/rooms.js
Expand Up @@ -84,6 +84,11 @@ var rooms = {
var array = [];
for (var i in this._data) {
var item = this._data[i];

//掉线后更新棋盘
//更新战绩
//储存战绩

if (item.user1.uid != "") {
if (time - item.user1.lastTime >= 1000 * 20) {
if (item.user2.data) {
Expand Down
15 changes: 13 additions & 2 deletions public/js/game.js
Expand Up @@ -237,9 +237,15 @@ window.onload = function () {
html += '<span class="icon-white"></span>';
}

html += '<a href="https://weibo.com/' + user.uid + '" target="_blank"><span>' + user.nickName + '</span></a>';
if(user.openType==0){
html += '<a href="https://weibo.com/' + user.uid + '" target="_blank"><span>' + user.nickName + '</span></a>';
}else{
html += '<a href="javascript:void(0);" target="_blank"><span>' + user.nickName + '</span></a>';
}
html += '</div>';
html += '<div>' + user.location + '</div>';
if(user.location){
html += '<div>' + user.location + '</div>';
}
html += '<div>赢:' + user.victory + ' 输:' + user.failure + ' 和:' + user.draw + '</div>';
html += '<div>胜率:' + value + '%</div>';
// html += '<div>步时:02:00</div>';
Expand Down Expand Up @@ -287,6 +293,11 @@ window.onload = function () {
var dialog = new Dialog({
title: '您还没有登录!',
buttons: [{
text: 'QQ登录',
handler: function () {
window.location.href = "/oauth/qq";
}
},{
text: '微博登录',
handler: function () {
window.location.href = "/oauth/weibo";
Expand Down
4 changes: 4 additions & 0 deletions routes/index.js
Expand Up @@ -8,9 +8,13 @@ router.get('/', function (req, res, next) {
});

var oauth = require('../core/controllers/oauth');

router.get('/oauth/weibo', oauth.weiboAuth);
router.get('/oauth/weibo/callback', oauth.weiboCallback);

router.get('/oauth/qq', oauth.qqAuth);
router.get('/auth/qq/callback', oauth.qqCallback);


var websocket = require('../core/controllers/websocket');
websocket.start();
Expand Down

0 comments on commit 5695584

Please sign in to comment.