完成登录操作
This commit is contained in:
parent
72c07d2a09
commit
d4dce97afe
@ -1,7 +1,5 @@
|
|||||||
package me.xiaoyan.point.api.pojo;
|
package me.xiaoyan.point.api.pojo;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.IdType;
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.experimental.Accessors;
|
import lombok.experimental.Accessors;
|
||||||
@ -13,9 +11,9 @@ import java.io.Serializable;
|
|||||||
@Builder
|
@Builder
|
||||||
public class Point implements Serializable {
|
public class Point implements Serializable {
|
||||||
private Integer uid;
|
private Integer uid;
|
||||||
private Integer total_point;
|
private Integer totalPoint;
|
||||||
private Integer valid_point;
|
private Integer validPoint;
|
||||||
private Integer expire_point;
|
private Integer expirePoint;
|
||||||
private Integer expire_time;
|
private Integer expireTime;
|
||||||
private Integer update_time;
|
private Integer updateTime;
|
||||||
}
|
}
|
||||||
|
@ -18,5 +18,5 @@ public class SignRecord implements Serializable {
|
|||||||
private Integer uid;
|
private Integer uid;
|
||||||
private Integer point;
|
private Integer point;
|
||||||
private String ip;
|
private String ip;
|
||||||
private Date create_time;
|
private Date createTime;
|
||||||
}
|
}
|
||||||
|
@ -15,8 +15,17 @@ import java.io.Serializable;
|
|||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class ApiResult implements Serializable {
|
public class ApiResult implements Serializable {
|
||||||
|
/**
|
||||||
|
* 响应码,0为正常,其他均不正确
|
||||||
|
*/
|
||||||
private int code;
|
private int code;
|
||||||
|
/**
|
||||||
|
* 响应消息,code不为0时的具体错误消息
|
||||||
|
*/
|
||||||
private String message;
|
private String message;
|
||||||
|
/**
|
||||||
|
* 具体的响应数据
|
||||||
|
*/
|
||||||
private Object data;
|
private Object data;
|
||||||
private long traceId;
|
private long traceId;
|
||||||
|
|
||||||
|
@ -5,4 +5,12 @@ import me.xiaoyan.point.api.pojo.PointRecord;
|
|||||||
import me.xiaoyan.point.api.pojo.UserInfo;
|
import me.xiaoyan.point.api.pojo.UserInfo;
|
||||||
|
|
||||||
public interface PointRecordService extends IService<PointRecord> {
|
public interface PointRecordService extends IService<PointRecord> {
|
||||||
|
/**
|
||||||
|
* 记录积分的变化
|
||||||
|
* @param uid
|
||||||
|
* @param point 积分变化值,正数为增加积分,负数为减少积分
|
||||||
|
* @param reason
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public PointRecord record(Integer uid,Integer point,String reason);
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package me.xiaoyan.point.api.service;
|
|||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
import me.xiaoyan.point.api.pojo.Point;
|
import me.xiaoyan.point.api.pojo.Point;
|
||||||
|
import me.xiaoyan.point.api.pojo.PointRecord;
|
||||||
import me.xiaoyan.point.api.pojo.UserInfo;
|
import me.xiaoyan.point.api.pojo.UserInfo;
|
||||||
|
|
||||||
public interface PointService extends IService<Point> {
|
public interface PointService extends IService<Point> {
|
||||||
|
@ -1,10 +1,44 @@
|
|||||||
package me.xiaoyan.point.api.service.impl;
|
package me.xiaoyan.point.api.service.impl;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
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.mapper.PointRecordMapper;
|
||||||
|
import me.xiaoyan.point.api.pojo.Point;
|
||||||
import me.xiaoyan.point.api.pojo.PointRecord;
|
import me.xiaoyan.point.api.pojo.PointRecord;
|
||||||
import me.xiaoyan.point.api.service.PointRecordService;
|
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>
|
public class PointRecordServiceImpl extends ServiceImpl<PointRecordMapper, PointRecord>
|
||||||
implements PointRecordService {
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,4 +7,5 @@ import me.xiaoyan.point.api.service.PointService;
|
|||||||
|
|
||||||
public class PointServiceImpl extends ServiceImpl<PointMapper, Point>
|
public class PointServiceImpl extends ServiceImpl<PointMapper, Point>
|
||||||
implements PointService {
|
implements PointService {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package me.xiaoyan.point.api.service.impl;
|
|||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.google.gson.Gson;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import me.xiaoyan.point.api.util.WechatDecryptDataUtil;
|
import me.xiaoyan.point.api.util.WechatDecryptDataUtil;
|
||||||
@ -53,12 +54,17 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
|
|||||||
// 新增积分信息
|
// 新增积分信息
|
||||||
Point point = Point.builder()
|
Point point = Point.builder()
|
||||||
.uid(info.getId())
|
.uid(info.getId())
|
||||||
.expire_point(0)
|
.expirePoint(0)
|
||||||
.total_point(firstLoginPoint)
|
.totalPoint(firstLoginPoint)
|
||||||
.valid_point(firstLoginPoint)
|
.validPoint(firstLoginPoint)
|
||||||
.build();
|
.build();
|
||||||
pointService.save(point);
|
pointService.save(point);
|
||||||
// 新增积分记录
|
// 新增积分记录
|
||||||
|
pointRecordService.record(
|
||||||
|
info.getId(),
|
||||||
|
firstLoginPoint,
|
||||||
|
"第一次登录赠送"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@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
|
@SneakyThrows
|
||||||
private CodeSessionData getOpenIdByCode(String code) {
|
private CodeSessionData getOpenIdByCode(String code) {
|
||||||
String url = CODE2SESSION + "?appid=" + app_id + "&secret=" + app_secret
|
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()) {
|
try (CloseableHttpClient client = HttpClients.createDefault()) {
|
||||||
HttpGet get = new HttpGet(url);
|
HttpGet get = new HttpGet(url);
|
||||||
final CloseableHttpResponse response = (CloseableHttpResponse) client.execute(get);
|
final CloseableHttpResponse response = (CloseableHttpResponse) client.execute(get);
|
||||||
|
// 获取到session的json数据
|
||||||
final String content = EntityUtils.toString(response.getEntity());
|
final String content = EntityUtils.toString(response.getEntity());
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
Gson gson = new Gson();
|
||||||
final CodeSessionData data = mapper.convertValue(content, CodeSessionData.class);
|
final CodeSessionData data = gson.fromJson(content, CodeSessionData.class);
|
||||||
if (data.getErrcode() != 0) {
|
if (data.getErrcode() != 0) {
|
||||||
throw BizException.create(1101, "换取code失败(" + data.getErrmsg() + ")");
|
throw BizException.create(1101, "换取session失败(" + data.getErrmsg() + ")");
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,13 @@ import java.util.Base64;
|
|||||||
*/
|
*/
|
||||||
public class WechatDecryptDataUtil {
|
public class WechatDecryptDataUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param encryptDataB64 加密的数据 从wx.getUserProfile而来
|
||||||
|
* @param sessionKeyB64 解密密钥,从code2Session而来
|
||||||
|
* @param ivB64
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public static WechatUserInfo decryptData(String encryptDataB64, String sessionKeyB64, String ivB64) {
|
public static WechatUserInfo decryptData(String encryptDataB64, String sessionKeyB64, String ivB64) {
|
||||||
String result = new String(
|
String result = new String(
|
||||||
decryptOfDiyIV(
|
decryptOfDiyIV(
|
||||||
|
File diff suppressed because one or more lines are too long
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 21 KiB |
Loading…
x
Reference in New Issue
Block a user