篆体字作品 | 篆体字知识 | 加入收藏 篆体字转换器软件可转换多种篆体字在线预览 网页版 V2.0
篆体字转换器

当前位置:篆体字网 > 知识库 >

qq微博登陆

时间:2023-12-30 09:15:46 编辑:篆字君 来源:篆体字网

package com.shishuo.studio.service;
import java.io.IOException;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shishuo.studio.constant.OAuthQQConstant;
import com.shishuo.studio.constant.StorageConstant;
import com.shishuo.studio.constant.SystemConstant;
import com.shishuo.studio.constant.UserConstant;
import com.shishuo.studio.entity.User;
import com.shishuo.studio.entity.vo.StorageVo;
import com.shishuo.studio.exception.AuthException;
import com.shishuo.studio.exception.LoginFailureException;
import com.shishuo.studio.exception.OpenIdNotFoundException;
import com.shishuo.studio.exception.UserNotFoundException;
import com.shishuo.studio.exception.notfound.StorageNotFoundException;
import com.shishuo.studio.util.HttpUtils;
@Service
public class OAuthQQService implements OAuthService {
@Autowired
private UserService userService;
@Autowired
private TeacherService teacherService;
@Autowired
private StorageService pictureService;
public String getCodeUrl() {
HashMapparam = new HashMap();
param.put("client_id", OAuthQQConstant.QQ_APPID);
param.put("client_secret", OAuthQQConstant.QQ_APPKEY);
param.put("redirect_uri", "http://www.shishuo.com/login/qq.htm");
return OAuthQQConstant.AUTH_CODE_URL + HttpUtils.getParam(param);
}
public String getTokenByCode(String code) {
HashMapparam = new HashMap();
param.put("grant_type", "authorization_code");
param.put("client_id", OAuthQQConstant.QQ_APPID);
param.put("client_secret", OAuthQQConstant.QQ_APPKEY);
param.put("code", code);
param.put("redirect_uri", "http://www.shishuo.com/login/qq.htm");
String[] tokens = HttpUtils.getUrl(OAuthQQConstant.TOKEN_URL, param)
.split("&");
String token = tokens[0].split("=")[1];
return token;
}
public String getOpenidByToken(String token) throws OpenIdNotFoundException {
String openid = "";
HashMapparam = new HashMap();
param.put("access_token", token);
param.put("format", SystemConstant.FORMAT);
String jsonp = HttpUtils.getUrl(OAuthQQConstant.OPENID_URL, param);
Matcher m = Pattern.compile("\"openid\"\\s*:\\s*\"(\\w+)\"").matcher(
jsonp);
if (m.find()) {
openid = m.group(1);
} else {
throw new OpenIdNotFoundException("");
}
return openid;
}
public JSONObject getUserByTokenAndOpenid(String token, String openid) {
HashMapparam = new HashMap();
param.put("access_token", token);
param.put("oauth_consumer_key", OAuthQQConstant.QQ_APPID);
param.put("openid", openid);
param.put("format", SystemConstant.FORMAT);
String jsonp = HttpUtils.getUrl(OAuthQQConstant.USERINFO_URL, param);
JSONObject object = JSONObject.fromObject(jsonp);
return object;
}
@Override
public User checkCode(String code) throws UserNotFoundException,
AuthException, ClientProtocolException, IOException,
StorageNotFoundException, IllegalStateException,
OpenIdNotFoundException, LoginFailureException {
String token = this.getTokenByCode(code);
String openId = this.getOpenidByToken(token);
JSONObject json = this.getUserByTokenAndOpenid(token, openId);
// ret==0 代表用户登陆成功
if (0 == Integer.parseInt(json.get("ret").toString())) {
if (userService.isUserByOpenIdAndType(openId, UserConstant.type.qq)) {
long userId = userService.updateTokenByOpenIdAndType(openId,
token, UserConstant.type.qq);
return userService.getUserById(userId);
} else {
User user = userService.addUserByThird(openId, token, "",
UserConstant.type.qq, "", "");
teacherService.updateNameByUserId(user.getUserId(),
json.getString("nickname"));
HttpEntity httpEntity = HttpUtils.download(json
.getString("figureurl_qq_2"));
StorageVo picture = pictureService.addPicture(
user.getUserId(),
user.getUserId(),
StorageConstant.kind.user,
httpEntity.getContentType().getValue()
.replaceAll("/", "."), httpEntity
.getContentType().getValue(), httpEntity
.getContent());
teacherService.updateTeacherStorageByUserId(user.getUserId(),
picture.getStorageId(), picture.getPath());
return user;
}
} else {
throw new LoginFailureException("登陆失败");
}
}
}

Copyright:2021-2023 篆体字转换器 www.dddtedu.com All rights reserved.