小程序用户信息修改接口

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

@@ -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;
}
}

View File

@@ -1,78 +0,0 @@
package com.czg.service.account.util;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.czg.exception.ApiNotPrintException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* 验证码工具类
*
* @author Administrator
*/
@Component
@Slf4j
public class SmsUtil {
/**
* 阿里云key
*/
@Value("${alipay.sms.key}")
private String key;
/**
* 阿里云secret
*/
@Value("${alipay.sms.secret}")
private String secret;
/**
* 短信模板id
*/
@Value("${alipay.sms.templateCode}")
private String templateCode;
public Client createClient() throws Exception {
Config config = new Config();
config.accessKeyId = key;
config.accessKeySecret = secret;
return new Client(config);
}
public void sendCode(String phone, String checkCode) {
try {
Client client = createClient();
// 1.发送短信
com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
.setSignName("银收客")
.setTemplateCode(templateCode)
.setTemplateParam("{\"code\":" + "'" + checkCode + "'" + "}")
.setPhoneNumbers(phone);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
log.info("短信发送请求参数: 手机号: {}, 短信模板: {}, 短信内容: {}", phone, templateCode, checkCode);
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
if (sendSmsResponse.getStatusCode() != 200) {
throw new ApiNotPrintException("短信发送失败");
}
}catch (Exception e) {
log.info("发送短信失败", e);
}
}
public static void main(String[] args) throws Exception {
Config config = new Config();
config.accessKeyId = "LTAI5tPdEfYSZcqHbjCrtPRD";
config.accessKeySecret = "DZjyHBq3nTujF0NMLxnZgsecU8ZCvy";
Client client = new Client(config);
// 1.发送短信
com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
.setSignName("银收客")
.setTemplateCode("SMS_244665149")
.setTemplateParam("{\"code\":" + "'" + "23123" + "'" + "}")
.setPhoneNumbers("19502966242");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
System.out.println(sendSmsResponse);
}
}