标签:load line _id on() cab method 实现 user 学习
登录工具类(用来模拟http请求以调用 auth.code2Session 接口)
/**
* 登录工具类
*/
public class LoginUtil
{
public final static String APP_ID="wxdb41305d2e277130";
public final static String SECRET="fe8f2cfceccab6110f2c4f0913a803e9";
public static String httpRequest(String requestUrl,String requestMethod,String output){
try{
URL url = new URL(requestUrl);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod(requestMethod);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
if(null != output){
OutputStream outputStream = connection.getOutputStream();
outputStream.write(output.getBytes("utf-8"));
outputStream.close();
}
// 从输入流读取返回内容
InputStream inputStream = connection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null){
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
connection.disconnect();
return buffer.toString();
}catch(Exception e){
e.printStackTrace();
}
return "";
}
}
auth.code2Session 接口返回结果类
/**
* 结果类
* @author 11313
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class LoginResult
{
/**
* 用户唯一标识
*/
private String openid;
/**
* 会话密钥
*/
private String session_key;
/**
* 用户在开放平台的唯一标识符
*/
private String unionid;
/**
* 错误码
*/
private Integer errcode;
/**
* 错误信息
*/
private String errmsg;
}
登录接口
@PostMapping("/loginRequest")
public String loginRequest(@RequestParam("code") String code, HttpSession session)
{
UserBO userBO=loginService.loginRequest(code);
session.setAttribute("user",userBO);
String token=TokenUtil.getToken(userBO);
PersonVO personVO=new PersonVO();
personVO.setId(userBO.getId());
personVO.setPortrait(userBO.getPortrait());
personVO.setQq(userBO.getQq());
personVO.setTel(userBO.getTelephone());
personVO.setUsername(userBO.getUsername());
CreditVO creditVO=new CreditVO();
if(userBO.getCredit()!=null)
{
creditVO.setCreditScore(userBO.getCredit().getCreditScore());
creditVO.setLikeNum(userBO.getCredit().getLikeNum());
creditVO.setDislikeNum(userBO.getCredit().getDislikeNum());
}
personVO.setCredit(creditVO);
return JSON.toJSONString(Result.successResult(new LoginVO(personVO,token)));
}
授权接口
@UserLoginToken
@PostMapping("/userAuth")
public String userAuth(@RequestParam("nickname") String wechatAccount,@RequestParam("avatar_url") String portrait,HttpSession session)
{
UserBO user= (UserBO) session.getAttribute("user");
UserBO newUser=loginService.userAuth(user.getId(),wechatAccount,portrait);
session.setAttribute("user",newUser);
PersonVO personVO=new PersonVO();
personVO.setId(newUser.getId());
personVO.setPortrait(newUser.getPortrait());
personVO.setQq(newUser.getQq());
personVO.setTel(newUser.getTelephone());
personVO.setUsername(newUser.getUsername());
CreditVO creditVO=new CreditVO();
if(newUser.getCredit()!=null)
{
creditVO.setCreditScore(newUser.getCredit().getCreditScore());
creditVO.setLikeNum(newUser.getCredit().getLikeNum());
creditVO.setDislikeNum(newUser.getCredit().getDislikeNum());
}
personVO.setCredit(creditVO);
return JSON.toJSONString(Result.successResult(personVO));
}
无
标签:load line _id on() cab method 实现 user 学习
原文地址:https://www.cnblogs.com/LebronZzhang/p/14944151.html