微信,作为三方接口的处理逻辑。以下是个人项目处理的逻辑,欢迎大家吐槽!
这边展示项目部分代码
/**
* 微信 登录与注册
* @param request 请求
* @param code 要解析的code,或 已解析的 openid
* @param nickName 昵称
* @return
*/
public Map<String,Object> wxLogin(HttpServletRequest request,String code,String nickName){
Map<String,Object> map=new HashMap<>();
//判断是否存在请求头
String key=CookieUtil.getKeyFromTokenCookie(request);
User user=null;
if (key ==""){ //请求头无值
JsCodeDto jsCodeDto= WeChatUtil.getCode(code);//解析 code
ThrowException.illegal(jsCodeDto.getOpenid()==null,"code解析失败");
//判断openid是否存在(是否注册过)
Third third = thirdMapper.queryThirdByOpenId(jsCodeDto.getOpenid());
if (third==null){
//不存在,做注册注册操作
user=new User();
user.setOrg(0);
user.setIsValid(0);
//时间处理
Date date=new Date();
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(date);
rightNow.add(Calendar.DAY_OF_YEAR,30);//日期加30天
Date addDate=rightNow.getTime();
//写入时间
user.setExpireDate(addDate);
user.setCreateDate(date);
user.setUpdateDate(date);
ThrowException.illegal(userMapper.insertSelective(user)<1,"添加用户表失败!");
//写入third
third=new Third();
third.setUserId(user.getId());
third.setNickname(nickName);
third.setOpenid(jsCodeDto.getOpenid());
third.setSessionKey(jsCodeDto.getSession_key());
int row= thirdMapper.insertSelective(third);
if (row<1){
userMapper.deleteByPrimaryKey(user.getId());
new ParamsException("添加三方表失败");
}
map.put("uuid",setRedis(user,nickName));//写入redis
map.put("openid",jsCodeDto.getOpenid());
}else{
//存在,做查询操作
user=userMapper.selectByPrimaryKey(third.getUserId());
map.put("uuid",setRedis(user,nickName));
map.put("openid",third.getOpenid());
}
}else{//有请求头,表明之前注册过
Long expire=redisUtil.getExpire(key);//判断是发存在redis
if (expire<=0){//时间已经过期
Third third = thirdMapper.queryThirdByOpenId(code);//code就是openid
user=userMapper.selectByPrimaryKey(third.getUserId());
map.put("uuid",setRedis(user,nickName));
map.put("openid",code);
}else{//存在
map.put("uuid",key);
map.put("openid",code);
}
}
return map;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/107118.html