小程序用户信息修改接口

This commit is contained in:
张松 2025-02-27 16:50:21 +08:00
parent 3be9ca1710
commit b5e2f0c261
10 changed files with 173 additions and 12 deletions

View File

@ -54,9 +54,14 @@ public class UShopUserController {
return shopUserService.getCode(StpKit.USER.getLoginIdAsLong(), shopId == null ? StpKit.USER.getShopId() : shopId);
}
/**
* 加入会员
* @param shopUserAddDTO 会员信息
* @return 是否成功
*/
@PostMapping
public CzgResult<Boolean> join(@RequestBody @Validated ShopUserAddDTO shopUserAddDTO) {
return shopUserService.join(StpKit.USER.getShopId(), StpKit.USER.getLoginIdAsLong(), shopUserAddDTO);
return CzgResult.success(shopUserService.join(StpKit.USER.getShopId(), StpKit.USER.getLoginIdAsLong(), shopUserAddDTO));
}
}

View File

@ -38,7 +38,7 @@ public class UserAuthorizationController {
*/
@PostMapping("/test")
public CzgResult<String> login() {
StpKit.USER.login(1L, "2342", null, MyStpLogic.LoginType.USER, false);
StpKit.USER.login(103L, "2342", null, MyStpLogic.LoginType.USER, false);
return CzgResult.success(StpKit.USER.getTokenValue());
}
}

View File

@ -4,6 +4,7 @@ import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.czg.account.dto.user.userinfo.UserInfoDTO;
import com.czg.account.dto.user.userinfo.UserInfoEditDTO;
import com.czg.account.dto.user.userinfo.UserInfoPwdEditDTO;
import com.czg.account.entity.UserInfo;
import com.czg.account.service.UserInfoService;
import com.czg.resp.CzgResult;
@ -37,9 +38,28 @@ public class UserController {
*/
@PutMapping
public CzgResult<Boolean> update(@RequestBody UserInfoEditDTO userInfoEditDTO) {
return CzgResult.success(userInfoService.update(BeanUtil.copyProperties(userInfoEditDTO, UserInfo.class),
new QueryWrapper().eq(UserInfo::getId, StpKit.USER.getLoginIdAsLong())));
return CzgResult.success(userInfoService.updateInfo( StpKit.USER.getLoginIdAsLong(), userInfoEditDTO));
}
/**
* 用户密码修改
* @return 是否成功
*/
@PutMapping("/pwd")
public CzgResult<Boolean> updatePwd(@RequestBody UserInfoPwdEditDTO userInfoPwdEditDTO) {
return CzgResult.success(userInfoService.updatePwd(StpKit.USER.getLoginIdAsLong(), userInfoPwdEditDTO));
}
/**
* 验证码获取
* @return 是否获取成功
*/
@GetMapping("/code")
public CzgResult<Boolean> sendSmsCode() {
return CzgResult.success(userInfoService.getCode(StpKit.USER.getLoginIdAsLong(), "wxMiniPwd"));
}
}

View File

@ -0,0 +1,21 @@
package com.czg.account.dto.user.userinfo;
import jakarta.validation.constraints.NotEmpty;
import lombok.Data;
/**
* @author Administrator
*/
@Data
public class UserInfoPwdEditDTO {
/**
* 支付密码
*/
@NotEmpty(message = "支付密码不为空")
private String payPwd;
/**
* 验证码
*/
@NotEmpty(message = "验证码不为空")
private String code;
}

View File

@ -36,5 +36,5 @@ public interface ShopUserService extends IService<ShopUser> {
CzgResult<String> getCode(long userInfoId, long shopId);
CzgResult<Boolean> join(Long shopId, Long userId, ShopUserAddDTO shopUserAddDTO);
boolean join(Long shopId, Long userId, ShopUserAddDTO shopUserAddDTO);
}

View File

@ -1,6 +1,8 @@
package com.czg.account.service;
import com.czg.account.dto.user.userinfo.UserInfoDTO;
import com.czg.account.dto.user.userinfo.UserInfoEditDTO;
import com.czg.account.dto.user.userinfo.UserInfoPwdEditDTO;
import com.czg.account.entity.UserInfo;
import com.mybatisflex.core.service.IService;
@ -13,4 +15,10 @@ import com.mybatisflex.core.service.IService;
public interface UserInfoService extends IService<UserInfo> {
UserInfoDTO getInfo(long userInfoId);
Boolean updateInfo(long userId, UserInfoEditDTO userInfoEditDTO);
Boolean updatePwd(long userId, UserInfoPwdEditDTO userInfoPwdEditDTO);
Boolean getCode(Long userId, String type);
}

View File

@ -1,9 +1,10 @@
package com.czg.service.account.util;
package com.czg.utils;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.czg.exception.ApiNotPrintException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

View File

@ -1,6 +1,5 @@
package com.czg.service.account.service.impl;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.account.entity.SysUser;
@ -10,7 +9,7 @@ import com.czg.config.RedisCst;
import com.czg.exception.ApiNotPrintException;
import com.czg.sa.StpKit;
import com.czg.service.RedisService;
import com.czg.service.account.util.SmsUtil;
import com.czg.utils.SmsUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

View File

@ -17,6 +17,9 @@ import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.service.RedisService;
import com.czg.service.account.mapper.ShopUserMapper;
import com.czg.system.dto.SysParamsDTO;
import com.czg.system.entity.SysParams;
import com.czg.system.service.SysParamsService;
import com.czg.utils.AssertUtil;
import com.czg.utils.JoinQueryWrapper;
import com.czg.utils.PageUtil;
@ -24,6 +27,7 @@ import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -41,6 +45,9 @@ import static com.mybatisflex.core.query.QueryMethods.column;
@DubboService
@Service
public class ShopUserServiceImpl extends ServiceImpl<ShopUserMapper, ShopUser> implements ShopUserService {
@DubboReference
private SysParamsService sysParamsService;
@Resource
private ShopUserFlowService shopUserFlowService;
@Resource
@ -183,8 +190,54 @@ public class ShopUserServiceImpl extends ServiceImpl<ShopUserMapper, ShopUser> i
return date + platformNumber + shopIdLastTwoDigits + randomPart;
}
public String generateCode(long shopId) {
String code = "shop_user_code_val%d".formatted(shopId);
SysParams sysParam = sysParamsService.getOne(new QueryWrapper().eq(SysParams::getParamCode, code));
if (sysParam == null) {
SysParams sysParams = new SysParams()
.setParamValue("1")
.setParamType(1)
.setCreateTime(DateUtil.date().toLocalDateTime())
.setParamCode(code);
sysParamsService.save(sysParams);
return generateRandomCode(1);
}
long parseLong = Long.parseLong(sysParam.getParamValue());
long l = ++parseLong;
sysParam.setParamValue(String.valueOf(l));
sysParamsService.updateById(sysParam);
return generateRandomCode(l);
}
// 使用Hutool生成十六进制代码并随机填充字母
private String generateRandomCode(long value) {
// 生成十六进制代码确保为10位
String hexCode = String.format("%010X", value);
// 计算需要补充的字母数量
int missingLength = 10 - hexCode.length();
StringBuilder codeBuilder = new StringBuilder(hexCode);
for (int i = 0; i < missingLength; i++) {
// 生成随机字母
char randomChar = RandomUtil.randomChar("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
codeBuilder.append(randomChar);
}
return codeBuilder.toString();
}
@Override
public CzgResult<Boolean> join(Long shopId, Long userId, ShopUserAddDTO shopUserAddDTO) {
public boolean join(Long shopId, Long userId, ShopUserAddDTO shopUserAddDTO) {
UserInfo userInfo = userInfoService.getById(userId);
userInfo.setPhone(shopUserAddDTO.getPhone());
userInfoService.updateById(userInfo);
ShopUser shopUser = getOne(new QueryWrapper().eq(ShopUser::getShopId, shopId).eq(ShopUser::getUserId, userId));
if (shopUser != null) {
throw new ApiNotPrintException("您已加入店铺会员");
@ -192,8 +245,10 @@ public class ShopUserServiceImpl extends ServiceImpl<ShopUserMapper, ShopUser> i
shopUser = BeanUtil.copyProperties(shopUserAddDTO, ShopUser.class);
shopUser.setIsVip(1);
// shopUser.setCode();
return null;
shopUser.setCode(generateCode(shopId));
shopUser.setJoinTime(DateUtil.date().toLocalDateTime());
shopUser.setShopId(shopId);
shopUser.setUserId(userId);
return save(shopUser);
}
}

View File

@ -1,13 +1,25 @@
package com.czg.service.account.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.czg.account.dto.user.userinfo.UserInfoAssetsSummaryDTO;
import com.czg.account.dto.user.userinfo.UserInfoDTO;
import com.czg.account.dto.user.userinfo.UserInfoEditDTO;
import com.czg.account.dto.user.userinfo.UserInfoPwdEditDTO;
import com.czg.account.entity.ShopUser;
import com.czg.account.entity.SysUser;
import com.czg.account.entity.UserInfo;
import com.czg.account.service.UserInfoService;
import com.czg.config.RedisCst;
import com.czg.exception.ApiNotPrintException;
import com.czg.sa.StpKit;
import com.czg.service.RedisService;
import com.czg.service.account.mapper.ShopUserMapper;
import com.czg.service.account.mapper.UserInfoMapper;
import com.czg.utils.SmsUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboService;
@ -22,6 +34,10 @@ import org.apache.dubbo.config.annotation.DubboService;
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService{
@Resource
private ShopUserMapper shopUserMapper;
@Resource
private RedisService redisService;
@Resource
private SmsUtil smsUtil;
@Override
public UserInfoDTO getInfo(long userInfoId) {
@ -35,4 +51,40 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
userInfoDTO.setAssetsSummary(assetsSummaryDTO);
return userInfoDTO;
}
@Override
public Boolean updateInfo(long userId, UserInfoEditDTO userInfoEditDTO) {
UserInfo userInfo = getById(userId);
BeanUtil.copyProperties(userInfoEditDTO, userInfo);
return save(userInfo);
}
@Override
public Boolean getCode(Long userId, String type) {
UserInfo userInfo = queryChain().eq(UserInfo::getId, userId).one();
if (StrUtil.isBlank(userInfo.getPhone())) {
throw new ApiNotPrintException("账号未绑定手机号");
}
int code = RandomUtil.randomInt(100000, 1000000);
redisService.set("%s%s:%s".formatted(RedisCst.SMS_CODE, userInfo.getPhone(), type), String.valueOf(code), 300);
smsUtil.sendCode(userInfo.getPhone(), String.valueOf(code));
return true;
}
@Override
public Boolean updatePwd(long userId, UserInfoPwdEditDTO userInfoPwdEditDTO) {
UserInfo userInfo = queryChain().eq(UserInfo::getId, userId).one();
String key = "%s%s:%s".formatted(RedisCst.SMS_CODE, userInfo.getPhone(), "wxMiniPwd");
Object val = redisService.get(key);
if (val instanceof String code && !userInfoPwdEditDTO.getCode().equals(code)) {
throw new ApiNotPrintException("验证码错误");
}
userInfo.setPayPwd(SecureUtil.md5(userInfo.getId() + userInfoPwdEditDTO.getPayPwd()));
boolean b = updateById(userInfo);
if (b) {
redisService.del(key);
}
return b;
}
}