QQ登录官方JS SDK版

阅读更多

JS
1、把注册获得的APPID、redirect地址写入下面对应的地方,放到登录页JS位置



2、放置QQ登录按钮,把这段代码放到HTML页面中
-->
QQ登录


3、调用QQ登录API(JS SDK)

 

4、向后台发送请求,把请求URL和参数换成自己的就可以了,逻辑可以
/**
* QQ登录
* @author zhangbing
* @param s
* @param openId
* @param accessToken
* @date 2012-06-26
*/
function qq_Login(s,openId,accessToken){
//把openId,accessToken存入Cookie中
addCookie("qq_openId",openId,90);
addCookie("qq_accessToken",accessToken,90);

//判断性别
var sex=0;
if(s.data.gender=="女"){
sex=1;
}else{
sex=0;
}
//向后台发送数据
if(getCookie("qq_openId")=="" || getCookie("qq_openId")==null){ //如果Cookie里面没有保存openId,则传入新openId
$.post(
"login.htm",
{
'passed':'qqLogin',
'openId':openId,
'accessToKen':accessToken,
'nickname':s.data.nickname,
'figureurl_1':s.data.figureurl_1,
'figureurl_2':s.data.figureurl_2,
'sex':sex,
'vip':s.data.vip
},
function(result){
loginBackUrl(result);
});
}else{ //如有Cookie里面有openId,则直接引用Cookie里面的openId
$.post(
"login.htm",
{
'passed':'qqLogin',
'openId':getCookie("qq_openId"),
'accessToKen':getCookie("qq_accessToken"),
'nickname':s.data.nickname,
'figureurl_1':s.data.figureurl_1,
'figureurl_2':s.data.figureurl_2,
'sex':sex,
'vip':s.data.vip
},
function(result){
loginBackUrl(result);
});
}
}
//返回上一次操作URL
function loginBackUrl(data){
if(data.length>0&&data.length<=500){
if(data=="/"){
location.href="/";
return;
}
if(data=="/mypage.htm"){
location.href = "/mypage.htm";
}
if(data=="/regist.htm"){
location.href="/";
}
location.href=data;
}else{
alert("登录出错啦,请重新登陆!");
//QC.Login.signOut();
}
}

//完成请求回调
function qq_complete(c){
if(c.ret==0){
alert("完成请求回调!");
// window.parent.location.href   =   window.opener.location.href
// window.close();
}else{
alert("指定接口访问成功,但请求回调失败!");
}
}

function qq_error(f){
alert("出错了");
}

java
public void onPost(){
new BackurlUtil().setBackurl(hreq, "tag=spy");
try {
appDomain = wlUtil.getAppProperty("AppDomain");
if(passed.equals("userLogin")){//如果是淘必得登录
wisher = wlUtil.checkLi555User(emailaddress, loginpassword);
session.setAttribute("wisher", wisher);
if(wisher!=null){
//登录成功
try {
DESPlus des = new DESPlus("li555");
//登录成功后,加密,保存入cookie
helloLi555CK = new Cookie("Helloli555CK", des.encrypt(emailaddress +'#'+ loginpassword));
session.setAttribute("username",emailaddress);
helloLi555CK.setMaxAge(5184000);
helloLi555CK.setPath("/");
hrep.addCookie(helloLi555CK);
if (baseurl.indexOf("login.htm")>=0) {
baseurl = "/";
}
msg = "({'backurl':'" + baseurl+"'," +
"'susuidCK':'"+wisher.getSnsUid()+"'," +
"'result':'LOGIN_SUCCESS'," +
"'renren_uid':'"+wisher.getWisherid()+"'," +
"'snstypeCK':'"+wisher.getSnsType()+"'})";

} catch (Exception e) {
msg = "({'result':'LOGIN_FAIL"+e+"'})";
e.printStackTrace();
}
Json.outPut(hrep, msg);
}
} else if(passed.equals("qqLogin")){ //如果是QQ登录
QQUser qqUser=new QQUser();

qqUser.setQQUid(openId);
qqUser.setName(nickname);
qqUser.setSex(sex);
qqUser.setVip(vip);
qqUser.setTinyurl(figureurl_1);
qqUser.setMainurl(figureurl_2);
wisher=wlUtil.checkQQUser(qqUser,"");

wisherId = wisher.getWisherid();
session.setAttribute("wisher",wisher);
uidCK = new Cookie("qq_uid", wisherId);
uidCK.setMaxAge(5184000);
hrep.addCookie(uidCK);

//判断上一次请求的URL
BackurlUtil url=new BackurlUtil();
String backurl=url.getBackurl(hreq, "/");
backurl = (String) session.getAttribute("backurl");
if (backurl.indexOf("login.htm")>=0){
backurl = baseurl;
}else{
backurl = backurl.replaceAll(appDomain+"/", "");
}
if(backurl!=""){
Json.outPut(hrep, backurl);
}
}
} catch(Exception e){
}
}

 

 

你可能感兴趣的:(QQ登录,JS)