mirror of
https://gitee.com/youlaitech/youlai-mall.git
synced 2024-12-22 12:48:59 +08:00
fix:小程序用户认证接口调整,nacos问题修复
This commit is contained in:
parent
fc65748924
commit
05399119b9
@ -158,7 +158,7 @@ DROP TABLE IF EXISTS `oms_order_log`;
|
||||
CREATE TABLE `oms_order_log` (
|
||||
`id` bigint(0) NOT NULL AUTO_INCREMENT COMMENT 'id',
|
||||
`order_id` bigint(0) NOT NULL COMMENT '订单id',
|
||||
`OAuthUserDetails` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '操作人[用户;系统;后台管理员]',
|
||||
`user` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '操作人[用户;系统;后台管理员]',
|
||||
`detail` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '' COMMENT '操作详情',
|
||||
`order_status` int(0) DEFAULT NULL COMMENT '操作时订单状态',
|
||||
`remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '' COMMENT '备注',
|
||||
|
@ -11,10 +11,10 @@ import org.springframework.web.bind.annotation.*;
|
||||
public interface MemberFeignClient {
|
||||
|
||||
@PostMapping("/app-api/v1/members")
|
||||
Result add(@RequestBody UmsMember member);
|
||||
Result<UmsMember> add(@RequestBody UmsMember member);
|
||||
|
||||
|
||||
@PostMapping("/app-api/v1/members/{id}")
|
||||
@PutMapping("/app-api/v1/members/{id}")
|
||||
Result update(@PathVariable Long id,@RequestBody UmsMember member);
|
||||
|
||||
|
||||
|
@ -18,10 +18,6 @@ public class UmsMember extends BaseEntity {
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
private Integer gender;
|
||||
|
||||
private String nickName;
|
||||
|
@ -7,7 +7,9 @@ public class MemberDTO {
|
||||
|
||||
private Long id;
|
||||
|
||||
private String nickname;
|
||||
private String nickName;
|
||||
|
||||
private String avatarUrl;
|
||||
|
||||
private String mobile;
|
||||
|
||||
|
@ -103,6 +103,7 @@
|
||||
<dependency>
|
||||
<groupId>com.youlai</groupId>
|
||||
<artifactId>common-log</artifactId>
|
||||
<version>${youlai.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
@ -9,7 +9,7 @@ import com.youlai.common.constant.GlobalConstants;
|
||||
import com.youlai.common.enums.QueryModeEnum;
|
||||
import com.youlai.common.result.Result;
|
||||
import com.youlai.mall.ums.pojo.domain.UmsMember;
|
||||
import com.youlai.mall.ums.service.IUmsUserService;
|
||||
import com.youlai.mall.ums.service.IUmsMemberService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiImplicitParams;
|
||||
@ -27,7 +27,7 @@ import java.util.Arrays;
|
||||
@AllArgsConstructor
|
||||
public class MemberController {
|
||||
|
||||
private IUmsUserService iUmsUserService;
|
||||
private IUmsMemberService iUmsMemberService;
|
||||
|
||||
@ApiOperation(value = "列表分页")
|
||||
@ApiImplicitParams({
|
||||
@ -48,8 +48,8 @@ public class MemberController {
|
||||
queryWrapper.ne(UmsMember::getDeleted, GlobalConstants.DELETED_VALUE);
|
||||
switch (queryModeEnum) {
|
||||
default: // PAGE
|
||||
queryWrapper.like(StrUtil.isNotBlank(nickname), UmsMember::getNickname, nickname);
|
||||
IPage<UmsMember> result = iUmsUserService.list(new Page<>(page, limit), new UmsMember().setNickname(nickname));
|
||||
queryWrapper.like(StrUtil.isNotBlank(nickname), UmsMember::getNickName, nickname);
|
||||
IPage<UmsMember> result = iUmsMemberService.list(new Page<>(page, limit), new UmsMember().setNickName(nickname));
|
||||
return Result.success(result.getRecords(), result.getTotal());
|
||||
}
|
||||
}
|
||||
@ -60,7 +60,7 @@ public class MemberController {
|
||||
public Result getMemberById(
|
||||
@PathVariable Long id
|
||||
) {
|
||||
UmsMember user = iUmsUserService.getById(id);
|
||||
UmsMember user = iUmsMemberService.getById(id);
|
||||
return Result.success(user);
|
||||
}
|
||||
|
||||
@ -71,9 +71,9 @@ public class MemberController {
|
||||
})
|
||||
@PutMapping(value = "/{id}")
|
||||
public Result update(
|
||||
@PathVariable Integer id,
|
||||
@RequestBody UmsMember user) {
|
||||
boolean status = iUmsUserService.updateById(user);
|
||||
@PathVariable Long id,
|
||||
@RequestBody UmsMember member) {
|
||||
boolean status = iUmsMemberService.updateById(member);
|
||||
return Result.judge(status);
|
||||
}
|
||||
|
||||
@ -86,7 +86,7 @@ public class MemberController {
|
||||
public Result patch(@PathVariable Long id, @RequestBody UmsMember user) {
|
||||
LambdaUpdateWrapper<UmsMember> updateWrapper = new LambdaUpdateWrapper<UmsMember>().eq(UmsMember::getId, id);
|
||||
updateWrapper.set(user.getStatus() != null, UmsMember::getStatus, user.getStatus());
|
||||
boolean status = iUmsUserService.update(updateWrapper);
|
||||
boolean status = iUmsMemberService.update(updateWrapper);
|
||||
return Result.judge(status);
|
||||
}
|
||||
|
||||
@ -94,7 +94,7 @@ public class MemberController {
|
||||
@ApiImplicitParam(name = "ids", value = "id集合", required = true, paramType = "query", dataType = "String")
|
||||
@DeleteMapping("/{ids}")
|
||||
public Result delete(@PathVariable String ids) {
|
||||
boolean status = iUmsUserService.update(new LambdaUpdateWrapper<UmsMember>()
|
||||
boolean status = iUmsMemberService.update(new LambdaUpdateWrapper<UmsMember>()
|
||||
.in(UmsMember::getId, Arrays.asList(ids.split(",")))
|
||||
.set(UmsMember::getDeleted, GlobalConstants.DELETED_VALUE));
|
||||
return Result.judge(status);
|
||||
|
@ -6,7 +6,7 @@ import com.youlai.common.result.Result;
|
||||
import com.youlai.mall.ums.pojo.domain.UmsMember;
|
||||
import com.youlai.mall.ums.pojo.dto.RechargeDTO;
|
||||
import com.youlai.mall.ums.pojo.dto.ResultPayDTO;
|
||||
import com.youlai.mall.ums.service.IUmsUserService;
|
||||
import com.youlai.mall.ums.service.IUmsMemberService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -105,7 +105,7 @@ public class RechargeController {
|
||||
return Result.failed();
|
||||
}
|
||||
|
||||
private IUmsUserService iUmsUserService;
|
||||
private IUmsMemberService iUmsMemberService;
|
||||
|
||||
@PostMapping(value = "/callback")
|
||||
public void receiveCallBack(@RequestBody ResultPayDTO resultPay) {
|
||||
@ -113,10 +113,10 @@ public class RechargeController {
|
||||
//处理自己的业务逻辑
|
||||
//例如开通会员、用户充值等等。。。
|
||||
String thirduid = resultPay.getThirduid();
|
||||
UmsMember user = iUmsUserService.getById(thirduid);
|
||||
UmsMember user = iUmsMemberService.getById(thirduid);
|
||||
if (user != null) {
|
||||
user.setBalance((long) (user.getBalance() + Float.valueOf(resultPay.getPrice()) * 100 * 10000));
|
||||
}
|
||||
iUmsUserService.updateById(user);
|
||||
iUmsMemberService.updateById(user);
|
||||
}
|
||||
}
|
||||
|
@ -7,9 +7,8 @@ import com.youlai.common.result.Result;
|
||||
import com.youlai.common.result.ResultCode;
|
||||
import com.youlai.common.web.util.JwtUtils;
|
||||
import com.youlai.mall.ums.pojo.domain.UmsMember;
|
||||
import com.youlai.mall.ums.pojo.dto.AuthMemberDTO;
|
||||
import com.youlai.mall.ums.pojo.dto.MemberDTO;
|
||||
import com.youlai.mall.ums.service.IUmsUserService;
|
||||
import com.youlai.mall.ums.service.IUmsMemberService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiImplicitParams;
|
||||
@ -25,7 +24,7 @@ import org.springframework.web.bind.annotation.*;
|
||||
@AllArgsConstructor
|
||||
public class MemberController {
|
||||
|
||||
private IUmsUserService iUmsUserService;
|
||||
private IUmsMemberService iUmsMemberService;
|
||||
|
||||
@ApiOperation(value = "获取会员信息")
|
||||
@ApiImplicitParam(name = "id", value = "会员ID", required = true, paramType = "path", dataType = "Long")
|
||||
@ -34,7 +33,7 @@ public class MemberController {
|
||||
@PathVariable Long id
|
||||
) {
|
||||
MemberDTO memberDTO = new MemberDTO();
|
||||
UmsMember user = iUmsUserService.getOne(
|
||||
UmsMember user = iUmsMemberService.getOne(
|
||||
new LambdaQueryWrapper<UmsMember>()
|
||||
.select(UmsMember::getId, UmsMember::getNickName, UmsMember::getMobile, UmsMember::getBalance)
|
||||
.eq(UmsMember::getId, id)
|
||||
@ -51,7 +50,7 @@ public class MemberController {
|
||||
public Result getByOpenid(
|
||||
@PathVariable String openid
|
||||
) {
|
||||
UmsMember member = iUmsUserService.getOne(new LambdaQueryWrapper<UmsMember>()
|
||||
UmsMember member = iUmsMemberService.getOne(new LambdaQueryWrapper<UmsMember>()
|
||||
.eq(UmsMember::getOpenid, openid));
|
||||
if (member == null) {
|
||||
return Result.failed(ResultCode.USER_NOT_EXIST);
|
||||
@ -62,16 +61,20 @@ public class MemberController {
|
||||
@ApiOperation(value = "新增会员")
|
||||
@ApiImplicitParam(name = "member", value = "实体JSON对象", required = true, paramType = "body", dataType = "UmsMember")
|
||||
@PostMapping
|
||||
public Result add(@RequestBody UmsMember user) {
|
||||
boolean status = iUmsUserService.save(user);
|
||||
return Result.judge(status);
|
||||
public Result<UmsMember> add(@RequestBody UmsMember member) {
|
||||
boolean status = iUmsMemberService.save(member);
|
||||
if (status) {
|
||||
return Result.success(member);
|
||||
} else {
|
||||
return Result.failed();
|
||||
}
|
||||
}
|
||||
|
||||
@ApiOperation(value = "新增会员")
|
||||
@ApiOperation(value = "修改会员")
|
||||
@ApiImplicitParam(name = "member", value = "实体JSON对象", required = true, paramType = "body", dataType = "UmsMember")
|
||||
@PutMapping("/{id}")
|
||||
public Result add(@PathVariable Long id,@RequestBody UmsMember user) {
|
||||
boolean status = iUmsUserService.updateById(user);
|
||||
public Result add(@PathVariable Long id, @RequestBody UmsMember user) {
|
||||
boolean status = iUmsMemberService.updateById(user);
|
||||
return Result.judge(status);
|
||||
}
|
||||
|
||||
@ -79,12 +82,12 @@ public class MemberController {
|
||||
@GetMapping("/me")
|
||||
public Result getMemberInfo() {
|
||||
Long userId = JwtUtils.getUserId();
|
||||
UmsMember user = iUmsUserService.getById(userId);
|
||||
if (user == null) {
|
||||
UmsMember member = iUmsMemberService.getById(userId);
|
||||
if (member == null) {
|
||||
return Result.failed(ResultCode.USER_NOT_EXIST);
|
||||
}
|
||||
MemberDTO memberDTO = new MemberDTO();
|
||||
BeanUtil.copyProperties(user, memberDTO);
|
||||
BeanUtil.copyProperties(member, memberDTO);
|
||||
return Result.success(memberDTO);
|
||||
}
|
||||
|
||||
@ -96,9 +99,9 @@ public class MemberController {
|
||||
})
|
||||
@PutMapping("/{id}/points")
|
||||
public Result updatePoint(@PathVariable Long id, @RequestParam Integer num) {
|
||||
UmsMember user = iUmsUserService.getById(id);
|
||||
UmsMember user = iUmsMemberService.getById(id);
|
||||
user.setPoint(user.getPoint() + num);
|
||||
boolean result = iUmsUserService.updateById(user);
|
||||
boolean result = iUmsMemberService.updateById(user);
|
||||
return Result.judge(result);
|
||||
}
|
||||
|
||||
@ -109,7 +112,7 @@ public class MemberController {
|
||||
})
|
||||
@PutMapping("/{id}/deduct-balance")
|
||||
public Result updateBalance(@PathVariable Long id, @RequestParam Long balance) {
|
||||
boolean result = iUmsUserService.update(new LambdaUpdateWrapper<UmsMember>()
|
||||
boolean result = iUmsMemberService.update(new LambdaUpdateWrapper<UmsMember>()
|
||||
.setSql("balance = balance - " + balance)
|
||||
.eq(UmsMember::getId, id)
|
||||
);
|
||||
@ -121,7 +124,7 @@ public class MemberController {
|
||||
@GetMapping("/{id}/balance")
|
||||
public Result<Long> updateBalance(@PathVariable Long id) {
|
||||
Long balance = 0l;
|
||||
UmsMember user = iUmsUserService.getById(id);
|
||||
UmsMember user = iUmsMemberService.getById(id);
|
||||
if (user != null) {
|
||||
balance = user.getBalance();
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.youlai.mall.ums.pojo.domain.UmsMember;
|
||||
|
||||
public interface IUmsUserService extends IService<UmsMember> {
|
||||
public interface IUmsMemberService extends IService<UmsMember> {
|
||||
|
||||
IPage<UmsMember> list(Page<UmsMember> page, UmsMember user);
|
||||
}
|
@ -5,13 +5,13 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.youlai.mall.ums.pojo.domain.UmsMember;
|
||||
import com.youlai.mall.ums.mapper.UmsUserMapper;
|
||||
import com.youlai.mall.ums.service.IUmsUserService;
|
||||
import com.youlai.mall.ums.service.IUmsMemberService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class UmsUserServiceImpl extends ServiceImpl<UmsUserMapper, UmsMember> implements IUmsUserService {
|
||||
public class UmsMemberServiceImpl extends ServiceImpl<UmsUserMapper, UmsMember> implements IUmsMemberService {
|
||||
|
||||
|
||||
@Override
|
@ -3,20 +3,25 @@ package com.youlai.auth.service.impl;
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.youlai.auth.common.jwt.JwtGenerator;
|
||||
import com.youlai.auth.domain.UserInfo;
|
||||
import com.youlai.auth.service.IAuthService;
|
||||
import com.youlai.common.constant.AuthConstants;
|
||||
import com.youlai.common.result.Result;
|
||||
import com.youlai.common.result.ResultCode;
|
||||
import com.youlai.common.web.exception.BizException;
|
||||
import com.youlai.mall.ums.api.MemberFeignClient;
|
||||
import com.youlai.mall.ums.pojo.domain.UmsMember;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author haoxr
|
||||
@ -28,90 +33,70 @@ import java.util.Map;
|
||||
public class WeAppServiceImpl implements IAuthService {
|
||||
|
||||
private MemberFeignClient memberFeignClient;
|
||||
private PasswordEncoder passwordEncoder;
|
||||
private WxMaService wxMaService;
|
||||
private JwtGenerator jwtGenerator;
|
||||
|
||||
/**
|
||||
* @param parameters code=小程序授权code
|
||||
* encryptedData=包括敏感数据在内的完整用户信息的加密数据
|
||||
* iv=
|
||||
* rawData=不包括敏感信息的原始数据字符串,用于计算签名
|
||||
* signature=使用 sha1( rawData + sessionkey ) 得到字符串,用于校验用户信息,详见 用户数据的签名验证和加解密
|
||||
* @return
|
||||
*/
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public Map<String, Object> login(Map<String, String> parameters) {
|
||||
Map<String, Object> resultMap = new HashMap<>();
|
||||
|
||||
String code = parameters.get("code");
|
||||
String rawData = parameters.get("rawData");
|
||||
String signature = parameters.get("signature");
|
||||
WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
|
||||
String sessionKey = sessionInfo.getSessionKey();
|
||||
// 校验微信用户信息
|
||||
boolean checkResult = wxMaService.getUserService().checkUserInfo(sessionKey, rawData, signature);
|
||||
if (checkResult) {
|
||||
String openid = sessionInfo.getOpenid();
|
||||
Result<UmsMember> result = memberFeignClient.getByOpenid(openid);
|
||||
|
||||
UmsMember member = null;
|
||||
Result memberResult;
|
||||
Result memberOptResult = null;
|
||||
if (ResultCode.USER_NOT_EXIST.getCode().equals(result.getCode())) {
|
||||
// 用户不存在,注册成为新用户
|
||||
UserInfo userInfo = JSONUtil.toBean(rawData, UserInfo.class);
|
||||
member = new UmsMember();
|
||||
BeanUtil.copyProperties(userInfo, member);
|
||||
memberResult = memberFeignClient.add(member);
|
||||
member.setOpenid(openid);
|
||||
member.setSessionKey(sessionKey);
|
||||
memberOptResult = memberFeignClient.add(member);
|
||||
if (ResultCode.SUCCESS.getCode().equals(memberOptResult.getCode())) {
|
||||
member = (UmsMember) memberOptResult.getData();
|
||||
}
|
||||
} else if (ResultCode.SUCCESS.getCode().equals(result.getCode()) && result.getData() != null) {
|
||||
member = result.getData();
|
||||
UserInfo userInfo = JSONUtil.toBean(rawData, UserInfo.class);
|
||||
BeanUtil.copyProperties(userInfo, member);
|
||||
memberResult = memberFeignClient.update(member.getId(), member);
|
||||
member.setSessionKey(sessionKey);
|
||||
memberOptResult = memberFeignClient.update(member.getId(), member);
|
||||
}
|
||||
if (memberOptResult != null && ResultCode.SUCCESS.getCode().equals(memberOptResult.getCode())) {
|
||||
|
||||
// JWT授权,一般存放用户的角色标识,用于资源服务器(网关)鉴权
|
||||
Set<String> authorities = new HashSet<>();
|
||||
|
||||
}
|
||||
// JWT增强,携带用户ID等信息
|
||||
Map<String, String> additional = new HashMap<>();
|
||||
additional.put(AuthConstants.USER_ID_KEY, Convert.toStr(member.getId()));
|
||||
|
||||
String accessToken = jwtGenerator.createAccessToken(authorities, additional);
|
||||
String tokenType = "bearer";
|
||||
|
||||
// String userInfo = parameters.get("userInfo");
|
||||
/* if (StrUtil.isBlank(code)) {
|
||||
throw new BizException("code不能为空");
|
||||
}
|
||||
|
||||
WxMaJscode2SessionResult session;
|
||||
// 根据授权code获取微信用户信息
|
||||
session = wxMaService.getUserService().getSessionInfo(code);
|
||||
String openid = session.getOpenid();
|
||||
String sessionKey = session.getSessionKey();
|
||||
|
||||
Result<AuthMemberDTO> result = memberFeignClient.getUserByOpenid(openid);
|
||||
Long userId = result.getData().getId();
|
||||
|
||||
if (ResultCode.USER_NOT_EXIST.getCode().equals(result.getCode())) { // 微信授权登录 会员信息不存在时 注册会员
|
||||
String encryptedData = parameters.get("encryptedData");
|
||||
String iv = parameters.get("iv");
|
||||
|
||||
WxMaUserInfo userInfo = wxMaService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
|
||||
if (userInfo == null) {
|
||||
throw new BizException("获取用户信息失败");
|
||||
}
|
||||
UmsMember user = new UmsMember()
|
||||
.setNickname(userInfo.getNickName())
|
||||
.setAvatar(userInfo.getAvatarUrl())
|
||||
.setGender(Integer.valueOf(userInfo.getGender()))
|
||||
.setOpenid(openid)
|
||||
.setUsername(openid)
|
||||
.setPassword(passwordEncoder.encode(openid).replace(PasswordEncoderTypeEnum.BCRYPT.getPrefix(),
|
||||
Strings.EMPTY)) // 加密密码移除前缀加密方式 {bcrypt}
|
||||
.setStatus(GlobalConstants.STATUS_YES);
|
||||
|
||||
Result res = memberFeignClient.add(user);
|
||||
if (!ResultCode.SUCCESS.getCode().equals(res.getCode())) {
|
||||
throw new BizException("注册会员失败");
|
||||
resultMap.put("access_token", accessToken);
|
||||
resultMap.put("token_type", tokenType);
|
||||
return resultMap;
|
||||
}
|
||||
} else {
|
||||
throw new BizException("非法用户");
|
||||
}
|
||||
|
||||
HashSet<String> roles = new HashSet<>();
|
||||
HashMap<String, String> additional = new HashMap<>();
|
||||
additional.put("userId", String.valueOf(userId));*/
|
||||
// jwtGenerator.createAccessToken(openid, roles, additional);
|
||||
return null;
|
||||
throw new BizException("认证失败");
|
||||
}
|
||||
}
|
||||
|
@ -39,7 +39,6 @@ import java.util.Map;
|
||||
*/
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
|
||||
public class ElasticSearchService {
|
||||
|
||||
private RestHighLevelClient client;
|
||||
|
Loading…
Reference in New Issue
Block a user