目录
1. 套餐管理
1.1 新增套餐
1.2套餐信息分页查询
1.3 删除套餐(可批量)
1.4 修改套餐状态(可批量)
2.手机验证码登录
2.1 短信发送
2.2 手机验证码登录
1. 套餐管理
1.1 新增套餐
将新增的套餐信息添加到套餐表中,相应的菜品信息添加到套餐-菜品关联表中。
/*** 新增套餐* @param setmealDto* @return*/@Transactional@PostMappingpublic R<String> save(@RequestBody SetmealDto setmealDto){//保存套餐信息setmealService.save(setmealDto);//保存套餐内的菜品信息List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();for (SetmealDish setmealDish : setmealDishes) {//保存套餐idsetmealDish.setSetmealId(setmealDto.getId());}setmealDishService.saveBatch(setmealDishes);return R.success("保存成功");}
1.2套餐信息分页查询
/*** 套餐分页查询* @param page* @param pageSize* @param name* @return*/@GetMapping("/page")public R<Page> page(int page,int pageSize,String name){Page<Setmeal> setmealPage = new Page<>(page,pageSize);Page<SetmealDto> setmealDtoPage = new Page<>();//设置查询条件LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.like(name!=null,Setmeal::getName,name);queryWrapper.orderByDesc(Setmeal::getUpdateTime);setmealService.page(setmealPage,queryWrapper);List<Setmeal> setmeals = setmealPage.getRecords();List<SetmealDto> setmealDtos = new ArrayList<>();BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");for (Setmeal setmeal : setmeals) {String categoryName = categoryService.getById(setmeal.getCategoryId()).getName();SetmealDto setmealDto = new SetmealDto();BeanUtils.copyProperties(setmeal,setmealDto);setmealDto.setCategoryName(categoryName);setmealDtos.add(setmealDto);}setmealDtoPage.setRecords(setmealDtos);return R.success(setmealDtoPage);}
1.3 删除套餐(可批量)
注:使用for循环多次操作数据库效率低,使用mybatis-plus的批量处理方法效率更高。
/*** 删除套餐以及套餐和菜品的关联数据* @param ids* @return*/@Transactional@DeleteMappingpublic R<String> delete(@RequestParam List<Long> ids){//删除套餐:查询套餐状态,如果在售卖中,则不可删除,抛出自定义业务异常;如果是停售状态则可正常删除。//构造查询条件:select count(*) from setmeal where id in ids and status = 1LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<>();queryWrapper1.in(Setmeal::getId,ids);queryWrapper1.eq(Setmeal::getStatus,1);if(setmealService.count(queryWrapper1)!=0){throw new CustomException("套餐正在售卖中,不可删除!");}else {setmealService.removeByIds(ids);}//删除套餐菜品表中的关联数据:delete from setmealDish where id in ids;LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);setmealDishService.remove(lambdaQueryWrapper);return R.success("套餐删除成功");}
1.4 修改套餐状态(可批量)
/*** 修改套餐状态(可批量)* @param status* @param ids* @return*/@PostMapping("/status/{status}")public R<String> update(@PathVariable Integer status,@RequestParam List<Long> ids){// 构造更新语句:UPDATE setmeal SET status=?, update_time=?, update_user=? WHERE (id IN (?,?))LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();updateWrapper.in(Setmeal::getId,ids);Setmeal setmeal = new Setmeal();setmeal.setStatus(status);setmealService.update(setmeal,updateWrapper);return R.success("修改成功");}
2.手机验证码登录
2.1 短信发送
参照使用服务的官方文档即可。如果使用阿里云短信服务发送短信,开发步骤如下:
1. 导入maven坐标
<dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-dysmsapi</artifactId><version>2.1.0</version></dependency>
2. 调用api
/*** 发送短信* @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();}}}
2.2 手机验证码登录
1. 修改过滤器
放行发送短信的登录的请求
//定义不需要处理的请求路径String[] uris = new String[]{"/employee/login","/employee/logout","/backend/**","/front/**","/common/**","/user/sendMsg","/user/login"};
验证用户是否登录
//4-2、判断登录状态,如果已登录,则直接放行if(request.getSession().getAttribute("user") != null){log.info("用户已登录,用户id为:{}",request.getSession().getAttribute("user"));Long userId = (Long) request.getSession().getAttribute("user");BaseContext.setCurrentId(userId);filterChain.doFilter(request,response);return;}
2. 发送验证码
/*** 发送手机短信验证码* @param user* @return*/@PostMapping("/sendMsg")public R<String> sendMsg(@RequestBody User user, HttpSession session){//获取手机号String phone = user.getPhone();if(StringUtils.isNotEmpty(phone)){//生成随机的4位验证码String code = ValidateCodeUtils.generateValidateCode(4).toString();log.info("code={}",code);//调用阿里云提供的短信服务API完成发送短信//SMSUtils.sendMessage("瑞吉外卖","",phone,code);//需要将生成的验证码保存到Sessionsession.setAttribute(phone,code);return R.success("手机验证码短信发送成功");}return R.error("短信发送失败");}
3. 移动端用户登录
注:此处使用map集合而非User实体类来接收请求数据,因为请求数据中包含code信息,而user中不包含此字段
@PostMapping("/login")public R<User> login(@RequestBody Map map, HttpSession session){log.info(map.toString());//获取手机号String phone = map.get("phone").toString();//获取验证码String code = map.get("code").toString();//从Session中获取保存的验证码Object codeInSession = session.getAttribute("code");//进行验证码的比对(页面提交的验证码和Session中保存的验证码比对)if(codeInSession != null && codeInSession.equals(code)){//如果能够比对成功,说明登录成功LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(User::getPhone,phone);User user = userService.getOne(queryWrapper);if(user == null){//判断当前手机号对应的用户是否为新用户,如果是新用户就自动完成注册user = new User();user.setPhone(phone);user.setStatus(1);userService.save(user);}session.setAttribute("user",user.getId());return R.success(user);}return R.error("登录失败");}
如果觉得《黑马-瑞吉外卖 day5 套餐管理+手机验证码登录》对你有帮助,请点赞、收藏,并留下你的观点哦!