完成登录操作

This commit is contained in:
LittleBoy 2022-11-21 10:05:52 +08:00
parent 72c07d2a09
commit d4dce97afe
10 changed files with 84 additions and 16 deletions

View File

@ -1,7 +1,5 @@
package me.xiaoyan.point.api.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;
@ -13,9 +11,9 @@ import java.io.Serializable;
@Builder
public class Point implements Serializable {
private Integer uid;
private Integer total_point;
private Integer valid_point;
private Integer expire_point;
private Integer expire_time;
private Integer update_time;
private Integer totalPoint;
private Integer validPoint;
private Integer expirePoint;
private Integer expireTime;
private Integer updateTime;
}

View File

@ -18,5 +18,5 @@ public class SignRecord implements Serializable {
private Integer uid;
private Integer point;
private String ip;
private Date create_time;
private Date createTime;
}

View File

@ -15,8 +15,17 @@ import java.io.Serializable;
@AllArgsConstructor
@NoArgsConstructor
public class ApiResult implements Serializable {
/**
* 响应码,0为正常其他均不正确
*/
private int code;
/**
* 响应消息,code不为0时的具体错误消息
*/
private String message;
/**
* 具体的响应数据
*/
private Object data;
private long traceId;

View File

@ -5,4 +5,12 @@ import me.xiaoyan.point.api.pojo.PointRecord;
import me.xiaoyan.point.api.pojo.UserInfo;
public interface PointRecordService extends IService<PointRecord> {
/**
* 记录积分的变化
* @param uid
* @param point 积分变化值,正数为增加积分负数为减少积分
* @param reason
* @return
*/
public PointRecord record(Integer uid,Integer point,String reason);
}

View File

@ -2,6 +2,7 @@ package me.xiaoyan.point.api.service;
import com.baomidou.mybatisplus.extension.service.IService;
import me.xiaoyan.point.api.pojo.Point;
import me.xiaoyan.point.api.pojo.PointRecord;
import me.xiaoyan.point.api.pojo.UserInfo;
public interface PointService extends IService<Point> {

View File

@ -1,10 +1,44 @@
package me.xiaoyan.point.api.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.xiaoyan.point.api.error.BizException;
import me.xiaoyan.point.api.mapper.PointRecordMapper;
import me.xiaoyan.point.api.pojo.Point;
import me.xiaoyan.point.api.pojo.PointRecord;
import me.xiaoyan.point.api.service.PointRecordService;
import me.xiaoyan.point.api.service.PointService;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
public class PointRecordServiceImpl extends ServiceImpl<PointRecordMapper, PointRecord>
implements PointRecordService {
@Resource
private PointService pointService;
@Transactional
@Override
public PointRecord record(Integer uid, Integer point, String reason) {
if(point == 0 ){
throw BizException.create("积分值没有变化");
}
// 查询用户的积分信息
final Point pointInfo = pointService.getById(uid);
if(point < 0 && pointInfo.getTotalPoint() < Math.abs(point)){
throw BizException.create("积分不足");
}
final PointRecord record = PointRecord.builder()
.uid(uid)
.point(point)
.reason(reason)
.currentTotalPoint(pointInfo.getTotalPoint())
.build();
pointInfo.setTotalPoint(
pointInfo.getTotalPoint() + point
);
// 更新
pointService.updateById(pointInfo);
this.save(record);
return record;
}
}

View File

@ -7,4 +7,5 @@ import me.xiaoyan.point.api.service.PointService;
public class PointServiceImpl extends ServiceImpl<PointMapper, Point>
implements PointService {
}

View File

@ -2,6 +2,7 @@ package me.xiaoyan.point.api.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import me.xiaoyan.point.api.util.WechatDecryptDataUtil;
@ -53,12 +54,17 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
// 新增积分信息
Point point = Point.builder()
.uid(info.getId())
.expire_point(0)
.total_point(firstLoginPoint)
.valid_point(firstLoginPoint)
.expirePoint(0)
.totalPoint(firstLoginPoint)
.validPoint(firstLoginPoint)
.build();
pointService.save(point);
// 新增积分记录
pointRecordService.record(
info.getId(),
firstLoginPoint,
"第一次登录赠送"
);
}
@Transactional
@ -91,7 +97,10 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
}
}
// 使用code换取openId
/**
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
* 使用code换取openId
*/
@SneakyThrows
private CodeSessionData getOpenIdByCode(String code) {
String url = CODE2SESSION + "?appid=" + app_id + "&secret=" + app_secret
@ -99,11 +108,12 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpGet get = new HttpGet(url);
final CloseableHttpResponse response = (CloseableHttpResponse) client.execute(get);
// 获取到session的json数据
final String content = EntityUtils.toString(response.getEntity());
ObjectMapper mapper = new ObjectMapper();
final CodeSessionData data = mapper.convertValue(content, CodeSessionData.class);
Gson gson = new Gson();
final CodeSessionData data = gson.fromJson(content, CodeSessionData.class);
if (data.getErrcode() != 0) {
throw BizException.create(1101, "换取code失败(" + data.getErrmsg() + ")");
throw BizException.create(1101, "换取session失败(" + data.getErrmsg() + ")");
}
return data;
}

View File

@ -18,6 +18,13 @@ import java.util.Base64;
*/
public class WechatDecryptDataUtil {
/**
*
* @param encryptDataB64 加密的数据 从wx.getUserProfile而来
* @param sessionKeyB64 解密密钥,从code2Session而来
* @param ivB64
* @return
*/
public static WechatUserInfo decryptData(String encryptDataB64, String sessionKeyB64, String ivB64) {
String result = new String(
decryptOfDiyIV(

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB