黑马程序员——瑞吉外卖项目解析(5)

套餐

 /**
     * 新增套餐
     * @param setmealDto 用套餐Dto对象接收参数
     * @return
     */
    @PostMapping()
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result<String> saveSetmeal(@RequestBody SetmealDto setmealDto) {
        log.info(setmealDto.toString());
        //因为是两张表关联查询,所以MP直接查是不可以的,自己写一个,把两个信息关联起来存储
        setmealService.saveWithDish(setmealDto);
        return Result.success("保存成功");
    }

    /**
     * 套餐模块的分页查询,因为是多表查询,所以直接MP的分页是不行的
     * 所以这里自己写的Mapper文件,一个SQL+标签动态SQL解决的
     * @param page 查第几页
     * @param pageSize 每页条数
     * @param name 模糊查询
     * @return
     */
    @GetMapping("page")
    public Result<Page> pageList(int page, int pageSize, String name) {
        Page page1 = new Page<>();
        //传入是page是从0页开始的,所以要-1
        List<SetmealDish> resultList=setmealMapper.listSetmeal(page-1, pageSize, name);
        //将分页对象setRecords,不然的话前端不识别。
        page1.setRecords(resultList);
        return Result.success(page1);
    }

短信发送

使用阿里云平台

/**
 * 短信发送工具类
 */
public class SMSUtils {

	/**
	 * 发送短信
	 * @param signName 签名
	 * @param templateCode 模板
	 * @param phoneNumbers 手机号
	 * @param param 参数
	 */
	public static void sendMessage(String signName, String templateCode,String phoneNumbers,String param){
		DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "", "");
		IAcsClient client = new DefaultAcsClient(profile);

		SendSmsRequest request = new SendSmsRequest();
		request.setSysRegionId("cn-hangzhou");
		request.setPhoneNumbers(phoneNumbers);
		request.setSignName(signName);
		request.setTemplateCode(templateCode);
		request.setTemplateParam("{\"code\":\""+param+"\"}");
		try {
			SendSmsResponse response = client.getAcsResponse(request);
			System.out.println("短信发送成功");
		}catch (ClientException e) {
			e.printStackTrace();
		}
	}
}

手机验证码登录

 /**
     * 验证码发送
     * @param user 接收用户电话号码
     * @param session 把验证码存入session,后续登陆验证要用
     * @return
     */
    @PostMapping("/sendMsg")
    public Result<String> sendMsg(@RequestBody User user, HttpSession session) {
        //获取手机号
        String userPhone = user.getPhone();
        //判断手机号是否为空
        if (StringUtils.isNotEmpty(userPhone)) {
            //利用验证码生成类来生成验证码
            String code = ValidateCodeUtils.generateValidateCode4String(4);
            //这里不太可能去真的发验证码,所以把生成的验证码在后台看一眼就好
            log.info("手机号Phone:{}   验证码Code:{}",userPhone,code);
            //如果要发短信应该出现的代码
            //SMSUtils.sendMessage("外卖", "模板", userPhone, code);
            //把验证码存入Session,验证用,phone为Key,code为value
            //session.setAttribute(userPhone, code);
            //将验证码存入redis,并设置好失效时间为5分钟
            redisTemplate.opsForValue().set(userPhone, code, 5, TimeUnit.MINUTES);

            return Result.success("验证码发送成功,有效时间为5分钟");
        }

        return Result.error("验证码发送失败");
    }
/**
     * 前台登陆功能
     * @param userDto 对User类进行了扩展,原有user类没有code属性
     * @param codeInSession 从session中拿code(验证码),方便后需验证
     * @return
     */
    @PostMapping("/login")
    public Result<String> login(@RequestBody UserDto userDto, HttpSession codeInSession) {
        //拿到验证码和手机号
        String code = userDto.getCode();
        String phone = userDto.getPhone();
        //从session中拿到对应的验证码
        //String tempCode = (String) codeInSession.getAttribute(phone);

        //从Redis中拿验证
        String tempCode = (String) redisTemplate.opsForValue().get(phone);

        //验证码相等
        if (code.equals(tempCode) && codeInSession != null) {
            //是否为新用户,如果是新用户顺手注册了
            LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(User::getPhone, phone);
            //只能用getOne来匹配,不能用getById,因为没有Id给你匹配,都是空的
            User user = userService.getOne(lambdaQueryWrapper);
            if (user==null){
                //用户不存在,注册一下,注册完放行
                //用户的ID是有自动生成策略的,不用管
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            //把用户的ID存入Session,留给过滤器进行验证放行
            //codeInSession.setAttribute("user", user.getId());

            //此时已经登陆成功,向Redis中存入userId的信息留给过滤器进行验证放行
            redisTemplate.opsForValue().set("user", user.getId());
            //再删掉验证码
            redisTemplate.delete(phone);


            return Result.success("登陆成功,欢迎~");
        }
        return Result.error("验证码错误");
    }

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值