From 9f0bef5f528d2ae94121bea1ec845f1bf10ba945 Mon Sep 17 00:00:00 2001 From: yijiegong Date: Tue, 29 Oct 2024 14:11:17 +0800 Subject: [PATCH] =?UTF-8?q?TbUserInfoController=20=E6=81=A2=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/TbUserInfoController.java | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 eladmin-system/src/main/java/cn/ysk/cashier/controller/TbUserInfoController.java diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/controller/TbUserInfoController.java b/eladmin-system/src/main/java/cn/ysk/cashier/controller/TbUserInfoController.java new file mode 100644 index 00000000..287817de --- /dev/null +++ b/eladmin-system/src/main/java/cn/ysk/cashier/controller/TbUserInfoController.java @@ -0,0 +1,127 @@ +package cn.ysk.cashier.controller; + +import cn.ysk.cashier.exception.BadRequestException; +import cn.ysk.cashier.pojo.shop.TbMerchantAccount; +import cn.ysk.cashier.repository.shop.TbMerchantAccountRepository; +import cn.ysk.cashier.utils.MD5Utils; +import cn.ysk.cashier.utils.RedisUtils; +import cn.ysk.cashier.utils.StringUtil; +import cn.ysk.cashier.utils.ValidateCodeUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import io.swagger.annotations.Api; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.bind.annotation.*; + +import java.util.Map; +import java.util.Objects; + +/** + * @website https://eladmin.vip + * @author lyf + * @date 2023-11-13 + **/ +@RestController +@RequiredArgsConstructor +@Api(tags = "/userInfo/list管理") +@RequestMapping("/api/tbUserInfo") +public class TbUserInfoController { + + @Autowired + ValidateCodeUtil validateCodeUtil; + + @Autowired + RedisUtils redisUtils; + + @GetMapping("sendMsg") + public Object sendMsg(){ + Object o= SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + if(Objects.isNull(o)){ + throw new BadRequestException("用户登录信息失效"); + } + JSONObject object=JSON.parseObject(JSON.toJSONString(o)); + if(Objects.isNull(object)){ + throw new BadRequestException("用户登录信息失效"); + } + String regex="^\\d{11}$"; + + if(!object.containsKey("username")||Objects.isNull(object.getString("username"))|| + !object.getString("username").matches(regex) + ){ + throw new BadRequestException("用户登录信息失效"); + } + String phone=object.getString("username"); + + String tempcode="SMS_244665149"; + + String random = StringUtil.random(6); + try { + validateCodeUtil.requestValidateCodeAli(phone, random,tempcode); + redisUtils.set(phone.concat("#").concat(tempcode),random,300L); + return "{\n" + + " \"code\": 0,\n" + + " \"msg\": \"成功\"\n" + + "}"; + } catch (Exception e) { + throw new RuntimeException(e.getMessage()); + } + + } + + + @Autowired + TbMerchantAccountRepository tbMerchantAccountRepository; + + + @RequestMapping(value = "modfiyUserInfo",method = RequestMethod.POST) + public ResponseEntity modfiyUserInfo(@RequestBody Map map){ + Object o= SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + if(Objects.isNull(o)){ + throw new BadRequestException("用户登录信息失效"); + } + JSONObject object=JSON.parseObject(JSON.toJSONString(o)); + if(Objects.isNull(object)){ + throw new BadRequestException("用户登录信息失效"); + } + String regex="^\\d{11}$"; + + if(!object.containsKey("username")||Objects.isNull(object.getString("username"))|| + !object.getString("username").matches(regex) + ){ + throw new BadRequestException("用户登录信息失效"); + } + + String tempcode="SMS_244665149"; + String phone=object.getString("username"); + Object redisCode= redisUtils.get(phone.concat("#").concat(tempcode)); + if(Objects.isNull(redisCode)){ + throw new BadRequestException("短信验证码已过期"); + } + + + String code= map.get("code"); + + if(!redisCode.toString().equals(code)){ + throw new BadRequestException("短信验证码错误"); + } + redisUtils.del(phone.concat("#").concat(tempcode)); + String pwd= map.get("pwd"); + + TbMerchantAccount account= tbMerchantAccountRepository.findByAccount(phone); + if(Objects.isNull(account)){ + throw new BadRequestException("账户不存在"); + } + + account.setPwd(MD5Utils.md5(pwd.concat(account.getAccount()).concat(account.getId().toString()))); + account.setUpdatedAt(System.currentTimeMillis()); + tbMerchantAccountRepository.save(account); + + + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + + } +}