缓存菜品套餐
This commit is contained in:
parent
743427a8a9
commit
773613c598
@ -44,7 +44,7 @@ public class HttpClientUtil {
|
|||||||
URIBuilder builder = new URIBuilder(url);
|
URIBuilder builder = new URIBuilder(url);
|
||||||
if(paramMap != null){
|
if(paramMap != null){
|
||||||
for (String key : paramMap.keySet()) {
|
for (String key : paramMap.keySet()) {
|
||||||
builder.addParameter(key,paramMap.get(key));
|
builder.addParameter(key,paramMap.get(key)); //将传入的参数 `paramMap` 中的每一个键值对转换为 URL 的查询字符串形式
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
URI uri = builder.build();
|
URI uri = builder.build();
|
||||||
|
@ -127,6 +127,11 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-cache</artifactId>
|
||||||
|
<version>2.7.3</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -3,11 +3,13 @@ package com.sky;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableTransactionManagement //开启注解方式的事务管理
|
@EnableTransactionManagement //开启注解方式的事务管理
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@EnableCaching
|
||||||
public class SkyApplication {
|
public class SkyApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(SkyApplication.class, args);
|
SpringApplication.run(SkyApplication.class, args);
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.sky.config;
|
package com.sky.config;
|
||||||
|
|
||||||
import com.sky.interceptor.JwtTokenAdminInterceptor;
|
import com.sky.interceptor.JwtTokenAdminInterceptor;
|
||||||
|
import com.sky.interceptor.JwtTokenUserInterceptor;
|
||||||
import com.sky.json.JacksonObjectMapper;
|
import com.sky.json.JacksonObjectMapper;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -29,6 +30,8 @@ public class WebMvcConfiguration extends WebMvcConfigurationSupport {
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
|
private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
|
||||||
|
@Autowired
|
||||||
|
private JwtTokenUserInterceptor jwtTokenUserInterceptor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注册自定义拦截器
|
* 注册自定义拦截器
|
||||||
@ -40,6 +43,11 @@ public class WebMvcConfiguration extends WebMvcConfigurationSupport {
|
|||||||
registry.addInterceptor(jwtTokenAdminInterceptor)
|
registry.addInterceptor(jwtTokenAdminInterceptor)
|
||||||
.addPathPatterns("/admin/**")
|
.addPathPatterns("/admin/**")
|
||||||
.excludePathPatterns("/admin/employee/login");
|
.excludePathPatterns("/admin/employee/login");
|
||||||
|
|
||||||
|
registry.addInterceptor(jwtTokenUserInterceptor)
|
||||||
|
.addPathPatterns("user/**")
|
||||||
|
.excludePathPatterns("/user/user/login")
|
||||||
|
.excludePathPatterns("/user/shop/status");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -13,7 +13,7 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@RestController
|
@RestController("adminCategoryController")
|
||||||
@RequestMapping("/admin/category")
|
@RequestMapping("/admin/category")
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class CategoryController {
|
public class CategoryController {
|
||||||
|
@ -10,22 +10,29 @@ import com.sky.vo.DishVO;
|
|||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
@RestController
|
@RestController("adminDishController")
|
||||||
@RequestMapping("/admin/dish")
|
@RequestMapping("/admin/dish")
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class DishController {
|
public class DishController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private DishService dishService;
|
private DishService dishService;
|
||||||
|
@Autowired
|
||||||
|
private RedisTemplate redisTemplate;
|
||||||
@PostMapping
|
@PostMapping
|
||||||
@ApiOperation("新增菜品")
|
@ApiOperation("新增菜品")
|
||||||
public Result addDish(@RequestBody DishDTO dishDTO){
|
public Result addDish(@RequestBody DishDTO dishDTO){
|
||||||
log.info("新增菜品");
|
log.info("新增菜品");
|
||||||
System.out.println("dishDTO:"+dishDTO);
|
|
||||||
dishService.addDish(dishDTO);
|
dishService.addDish(dishDTO);
|
||||||
|
|
||||||
|
//清理缓存数据
|
||||||
|
String key = "dish_" + dishDTO.getCategoryId();
|
||||||
|
cleanCache(key);
|
||||||
return Result.success();
|
return Result.success();
|
||||||
}
|
}
|
||||||
@GetMapping("/page")
|
@GetMapping("/page")
|
||||||
@ -39,6 +46,9 @@ public class DishController {
|
|||||||
public Result deleteDish(Long[] ids){
|
public Result deleteDish(Long[] ids){
|
||||||
log.info("批量删除菜品");
|
log.info("批量删除菜品");
|
||||||
dishService.deleteBatch(ids);
|
dishService.deleteBatch(ids);
|
||||||
|
|
||||||
|
//将所有的菜品缓存数据清理掉,所有以dish_开头的key
|
||||||
|
cleanCache("dish_*");
|
||||||
return Result.success();
|
return Result.success();
|
||||||
}
|
}
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
@ -50,20 +60,33 @@ public class DishController {
|
|||||||
@PutMapping
|
@PutMapping
|
||||||
@ApiOperation("更新菜品")
|
@ApiOperation("更新菜品")
|
||||||
public Result update(@RequestBody DishDTO dishDTO){
|
public Result update(@RequestBody DishDTO dishDTO){
|
||||||
|
log.info("修改菜品:{}", dishDTO);
|
||||||
dishService.update(dishDTO);
|
dishService.update(dishDTO);
|
||||||
|
|
||||||
|
//将所有的菜品缓存数据清理掉,所有以dish_开头的key
|
||||||
|
cleanCache("dish_*");
|
||||||
return Result.success();
|
return Result.success();
|
||||||
}
|
}
|
||||||
@PostMapping("/status/{status}")
|
@PostMapping("/status/{status}")
|
||||||
@ApiOperation("菜品起售/停售")
|
@ApiOperation("菜品起售/停售")
|
||||||
public Result startOrStop(@PathVariable("status") Integer status, Long id){
|
public Result startOrStop(@PathVariable("status") Integer status, Long id){
|
||||||
dishService.startOrStop(status,id);
|
dishService.startOrStop(status,id);
|
||||||
|
|
||||||
|
//将所有的菜品缓存数据清理掉,所有以dish_开头的key
|
||||||
|
cleanCache("dish_*");
|
||||||
return Result.success();
|
return Result.success();
|
||||||
}
|
}
|
||||||
@GetMapping("/list")
|
@GetMapping("/list")
|
||||||
@ApiOperation("根据分类id查询菜品")
|
@ApiOperation("根据分类id查询菜品")
|
||||||
public Result<List<Dish>> list(Long categoryId){
|
public Result<List<Dish>> list(Long categoryId){
|
||||||
List<Dish> dishList=dishService.list(categoryId);
|
Dish dish=new Dish();
|
||||||
|
dish.setCategoryId(categoryId);
|
||||||
|
List<Dish> dishList=dishService.list(dish);
|
||||||
return Result.success(dishList);
|
return Result.success(dishList);
|
||||||
}
|
}
|
||||||
|
private void cleanCache(String pattern){
|
||||||
|
Set keys = redisTemplate.keys(pattern);
|
||||||
|
redisTemplate.delete(keys);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,10 @@ import io.swagger.annotations.Api;
|
|||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.cache.annotation.CacheEvict;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@RestController
|
@RestController("adminSetmealController")
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Api("套餐相关接口")
|
@Api("套餐相关接口")
|
||||||
@RequestMapping("/admin/setmeal")
|
@RequestMapping("/admin/setmeal")
|
||||||
@ -22,6 +23,7 @@ public class SetmealController {
|
|||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
@ApiOperation("新增套餐")
|
@ApiOperation("新增套餐")
|
||||||
|
@CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")//key: setmealCache::100
|
||||||
public Result addSetmeal(@RequestBody SetmealDTO setmealDTO){
|
public Result addSetmeal(@RequestBody SetmealDTO setmealDTO){
|
||||||
log.info("新增套餐");
|
log.info("新增套餐");
|
||||||
setmealService.addSetmeal(setmealDTO);
|
setmealService.addSetmeal(setmealDTO);
|
||||||
@ -35,6 +37,7 @@ public class SetmealController {
|
|||||||
}
|
}
|
||||||
@DeleteMapping
|
@DeleteMapping
|
||||||
@ApiOperation("删除套餐")
|
@ApiOperation("删除套餐")
|
||||||
|
@CacheEvict(cacheNames = "setmealCache",allEntries = true)
|
||||||
public Result deleteByIds(@RequestParam Long[] ids){
|
public Result deleteByIds(@RequestParam Long[] ids){
|
||||||
log.info("删除套餐");
|
log.info("删除套餐");
|
||||||
setmealService.deleteBatch(ids);
|
setmealService.deleteBatch(ids);
|
||||||
@ -48,12 +51,14 @@ public class SetmealController {
|
|||||||
}
|
}
|
||||||
@PutMapping
|
@PutMapping
|
||||||
@ApiOperation("修改套餐")
|
@ApiOperation("修改套餐")
|
||||||
|
@CacheEvict(cacheNames = "setmealCache",allEntries = true)
|
||||||
public Result update(@RequestBody SetmealDTO setmealDTO) {
|
public Result update(@RequestBody SetmealDTO setmealDTO) {
|
||||||
setmealService.update(setmealDTO);
|
setmealService.update(setmealDTO);
|
||||||
return Result.success();
|
return Result.success();
|
||||||
}
|
}
|
||||||
@PostMapping("/status/{status}")
|
@PostMapping("/status/{status}")
|
||||||
@ApiOperation("套餐起售停售")
|
@ApiOperation("套餐起售停售")
|
||||||
|
@CacheEvict(cacheNames = "setmealCache",allEntries = true)
|
||||||
public Result startOrStop(@PathVariable Integer status, Long id) {
|
public Result startOrStop(@PathVariable Integer status, Long id) {
|
||||||
setmealService.startOrStop(status, id);
|
setmealService.startOrStop(status, id);
|
||||||
return Result.success();
|
return Result.success();
|
||||||
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.sky.controller.user;
|
||||||
|
|
||||||
|
import com.sky.entity.Category;
|
||||||
|
import com.sky.result.Result;
|
||||||
|
import com.sky.service.CategoryService;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController("userCategoryController")
|
||||||
|
@RequestMapping("/user/category")
|
||||||
|
@Api(tags = "C端-分类接口")
|
||||||
|
public class CategoryController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CategoryService categoryService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询分类
|
||||||
|
* @param type
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/list")
|
||||||
|
@ApiOperation("查询分类")
|
||||||
|
public Result<List<Category>> list(Integer type) {
|
||||||
|
List<Category> list = categoryService.listByType(type);
|
||||||
|
return Result.success(list);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,58 @@
|
|||||||
|
package com.sky.controller.user;
|
||||||
|
|
||||||
|
import com.sky.constant.StatusConstant;
|
||||||
|
import com.sky.entity.Dish;
|
||||||
|
import com.sky.result.Result;
|
||||||
|
import com.sky.service.DishService;
|
||||||
|
import com.sky.vo.DishVO;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController("userDishController")
|
||||||
|
@RequestMapping("/user/dish")
|
||||||
|
@Slf4j
|
||||||
|
@Api(tags = "C端-菜品浏览接口")
|
||||||
|
public class DishController {
|
||||||
|
@Autowired
|
||||||
|
private DishService dishService;
|
||||||
|
@Autowired
|
||||||
|
private RedisTemplate redisTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据分类id查询菜品
|
||||||
|
*
|
||||||
|
* @param categoryId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/list")
|
||||||
|
@ApiOperation("根据分类id查询菜品")
|
||||||
|
public Result<List<DishVO>> list(Long categoryId) {
|
||||||
|
//构造key:dish_分类id
|
||||||
|
String key="dish_"+categoryId;
|
||||||
|
|
||||||
|
//查询redis中是否存在菜品数据
|
||||||
|
List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);
|
||||||
|
if(list!=null &&list.size()>0)
|
||||||
|
{
|
||||||
|
//如果存在,直接返回,无须查询数据库
|
||||||
|
log.info("查缓存");
|
||||||
|
return Result.success(list);
|
||||||
|
}
|
||||||
|
//如果不存在,查询数据库并将数据放入redis中
|
||||||
|
Dish dish = new Dish();
|
||||||
|
dish.setCategoryId(categoryId);
|
||||||
|
dish.setStatus(StatusConstant.ENABLE);//查询起售中的菜品
|
||||||
|
|
||||||
|
list = dishService.listWithFlavor(dish);
|
||||||
|
redisTemplate.opsForValue().set(key,list);
|
||||||
|
return Result.success(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
package com.sky.controller.user;
|
||||||
|
|
||||||
|
import com.sky.constant.StatusConstant;
|
||||||
|
import com.sky.entity.Setmeal;
|
||||||
|
import com.sky.result.Result;
|
||||||
|
import com.sky.service.SetmealService;
|
||||||
|
import com.sky.vo.DishItemVO;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController("userSetmealController")
|
||||||
|
@RequestMapping("/user/setmeal")
|
||||||
|
@Api(tags = "C端-套餐浏览接口")
|
||||||
|
public class SetmealController {
|
||||||
|
@Autowired
|
||||||
|
private SetmealService setmealService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 条件查询
|
||||||
|
*
|
||||||
|
* @param categoryId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/list")
|
||||||
|
@ApiOperation("根据分类id查询套餐")
|
||||||
|
@Cacheable(cacheNames = "setmealCache",key = "#categoryId") //key: setmealCache::100
|
||||||
|
public Result<List<Setmeal>> list(Long categoryId) {
|
||||||
|
Setmeal setmeal = new Setmeal();
|
||||||
|
setmeal.setCategoryId(categoryId);
|
||||||
|
setmeal.setStatus(StatusConstant.ENABLE);
|
||||||
|
|
||||||
|
List<Setmeal> list = setmealService.list(setmeal);
|
||||||
|
return Result.success(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据套餐id查询包含的菜品列表
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/dish/{id}")
|
||||||
|
@ApiOperation("根据套餐id查询包含的菜品列表")
|
||||||
|
public Result<List<DishItemVO>> dishList(@PathVariable("id") Long id) {
|
||||||
|
List<DishItemVO> list = setmealService.getDishItemById(id);
|
||||||
|
return Result.success(list);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
package com.sky.controller.user;
|
||||||
|
|
||||||
|
import com.sky.constant.JwtClaimsConstant;
|
||||||
|
import com.sky.dto.UserLoginDTO;
|
||||||
|
import com.sky.entity.User;
|
||||||
|
import com.sky.properties.JwtProperties;
|
||||||
|
import com.sky.result.Result;
|
||||||
|
import com.sky.service.UserService;
|
||||||
|
import com.sky.utils.JwtUtil;
|
||||||
|
import com.sky.vo.UserLoginVO;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/user/user")
|
||||||
|
@Api(tags = "C端用户相关接口")
|
||||||
|
@Slf4j
|
||||||
|
public class UserController implements Serializable {
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
@Autowired
|
||||||
|
private JwtProperties jwtProperties;
|
||||||
|
@PostMapping("/login")
|
||||||
|
@ApiOperation("微信登录")
|
||||||
|
public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
|
||||||
|
log.info("微信用户登录:{}",userLoginDTO.getCode());
|
||||||
|
User user=userService.wxLogin(userLoginDTO);
|
||||||
|
//生成JWT令牌
|
||||||
|
Map<String,Object>claims=new HashMap<>();
|
||||||
|
claims.put(JwtClaimsConstant.USER_ID,user.getId());
|
||||||
|
String token=JwtUtil.createJWT(jwtProperties.getUserSecretKey(),jwtProperties.getUserTtl(),claims);
|
||||||
|
UserLoginVO userLoginVO = UserLoginVO.builder().id(user.getId()).openid(user.getOpenid()).token(token).build();
|
||||||
|
return Result.success(userLoginVO);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
package com.sky.interceptor;
|
||||||
|
|
||||||
|
import com.sky.constant.JwtClaimsConstant;
|
||||||
|
import com.sky.context.BaseContext;
|
||||||
|
import com.sky.properties.JwtProperties;
|
||||||
|
import com.sky.utils.JwtUtil;
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.method.HandlerMethod;
|
||||||
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* jwt令牌校验的拦截器
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class JwtTokenUserInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private JwtProperties jwtProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 校验jwt
|
||||||
|
*
|
||||||
|
* @param request
|
||||||
|
* @param response
|
||||||
|
* @param handler
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||||
|
//判断当前拦截到的是Controller的方法还是其他资源
|
||||||
|
if (!(handler instanceof HandlerMethod)) {
|
||||||
|
//当前拦截到的不是动态方法,直接放行
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//1、从请求头中获取令牌
|
||||||
|
String token = request.getHeader(jwtProperties.getAdminTokenName());
|
||||||
|
|
||||||
|
//2、校验令牌
|
||||||
|
try {
|
||||||
|
log.info("jwt校验:{}", token);
|
||||||
|
Claims claims = JwtUtil.parseJWT(jwtProperties.getAdminSecretKey(), token);
|
||||||
|
Long userId = Long.valueOf(claims.get(JwtClaimsConstant.USER_ID).toString());
|
||||||
|
log.info("当前员工id:", userId);
|
||||||
|
BaseContext.setCurrentId(userId);
|
||||||
|
//3、通过,放行
|
||||||
|
return true;
|
||||||
|
} catch (Exception ex) {
|
||||||
|
//4、不通过,响应401状态码
|
||||||
|
response.setStatus(401);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -24,7 +24,6 @@ public interface CategoryMapper {
|
|||||||
void deleteById(Integer id);
|
void deleteById(Integer id);
|
||||||
@AutoFill(OperationType.UPDATE)
|
@AutoFill(OperationType.UPDATE)
|
||||||
void update(Category category);
|
void update(Category category);
|
||||||
@Select("select * from category where type=#{type}")
|
|
||||||
List<Category> listByType(Integer type);
|
List<Category> listByType(Integer type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,8 +29,7 @@ public interface DishMapper {
|
|||||||
void deleteById(Long id);
|
void deleteById(Long id);
|
||||||
@AutoFill(OperationType.UPDATE)
|
@AutoFill(OperationType.UPDATE)
|
||||||
void update(Dish dish);
|
void update(Dish dish);
|
||||||
@Select("select * from dish where category_id=#{categoryId}")
|
List<Dish> listByType(Dish dish);
|
||||||
List<Dish> listByType(Long categoryId);
|
|
||||||
@Select("select a.* from dish a left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = #{setmealId}")
|
@Select("select a.* from dish a left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = #{setmealId}")
|
||||||
List<Dish> getBySetmealId(Long setmealId);
|
List<Dish> getBySetmealId(Long setmealId);
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import com.sky.annotation.AutoFill;
|
|||||||
import com.sky.dto.SetmealPageQueryDTO;
|
import com.sky.dto.SetmealPageQueryDTO;
|
||||||
import com.sky.entity.Setmeal;
|
import com.sky.entity.Setmeal;
|
||||||
import com.sky.enumeration.OperationType;
|
import com.sky.enumeration.OperationType;
|
||||||
|
import com.sky.vo.DishItemVO;
|
||||||
import com.sky.vo.SetmealVO;
|
import com.sky.vo.SetmealVO;
|
||||||
import org.apache.ibatis.annotations.Delete;
|
import org.apache.ibatis.annotations.Delete;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
@ -23,4 +24,14 @@ public interface SetmealMapper {
|
|||||||
void deleteById(Long id);
|
void deleteById(Long id);
|
||||||
@AutoFill(OperationType.UPDATE)
|
@AutoFill(OperationType.UPDATE)
|
||||||
void update(Setmeal setmeal);
|
void update(Setmeal setmeal);
|
||||||
|
/**
|
||||||
|
* 动态条件查询套餐
|
||||||
|
* @param setmeal
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
List<Setmeal> list(Setmeal setmeal);
|
||||||
|
@Select("select sd.name, sd.copies, d.image, d.description " +
|
||||||
|
"from setmeal_dish sd left join dish d on sd.dish_id = d.id " +
|
||||||
|
"where sd.setmeal_id = #{setmealId}")
|
||||||
|
List<DishItemVO> getDishItemBySetmealId(Long id);
|
||||||
}
|
}
|
||||||
|
17
sky-server/src/main/java/com/sky/mapper/UserMapper.java
Normal file
17
sky-server/src/main/java/com/sky/mapper/UserMapper.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package com.sky.mapper;
|
||||||
|
|
||||||
|
import com.sky.entity.User;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface UserMapper {
|
||||||
|
@Select("select * from user where openid=#{openid}")
|
||||||
|
User getByOpenid(String openid);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插入新用户
|
||||||
|
* @param user
|
||||||
|
*/
|
||||||
|
void insert(User user);
|
||||||
|
}
|
@ -21,5 +21,7 @@ public interface DishService {
|
|||||||
|
|
||||||
void startOrStop(Integer status, Long id);
|
void startOrStop(Integer status, Long id);
|
||||||
|
|
||||||
List<Dish> list(Long categoryId);
|
List<Dish> list(Dish dish);
|
||||||
|
|
||||||
|
List<DishVO> listWithFlavor(Dish dish);
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,13 @@ package com.sky.service;
|
|||||||
|
|
||||||
import com.sky.dto.SetmealDTO;
|
import com.sky.dto.SetmealDTO;
|
||||||
import com.sky.dto.SetmealPageQueryDTO;
|
import com.sky.dto.SetmealPageQueryDTO;
|
||||||
|
import com.sky.entity.Setmeal;
|
||||||
import com.sky.result.PageResult;
|
import com.sky.result.PageResult;
|
||||||
|
import com.sky.vo.DishItemVO;
|
||||||
import com.sky.vo.SetmealVO;
|
import com.sky.vo.SetmealVO;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface SetmealService {
|
public interface SetmealService {
|
||||||
void addSetmeal(SetmealDTO setmealDTO);
|
void addSetmeal(SetmealDTO setmealDTO);
|
||||||
|
|
||||||
@ -22,4 +26,8 @@ public interface SetmealService {
|
|||||||
void update(SetmealDTO setmealDTO);
|
void update(SetmealDTO setmealDTO);
|
||||||
|
|
||||||
void startOrStop(Integer status, Long id);
|
void startOrStop(Integer status, Long id);
|
||||||
|
|
||||||
|
List<Setmeal> list(Setmeal setmeal);
|
||||||
|
|
||||||
|
List<DishItemVO> getDishItemById(Long id);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.sky.service;
|
||||||
|
|
||||||
|
import com.sky.dto.UserLoginDTO;
|
||||||
|
import com.sky.entity.User;
|
||||||
|
|
||||||
|
public interface UserService {
|
||||||
|
User wxLogin(UserLoginDTO userLoginDTO);
|
||||||
|
}
|
@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@ -122,8 +123,35 @@ public class DishServiceImpl implements DishService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Dish> list(Long categoryId) {
|
public List<Dish> list(Dish dish) {
|
||||||
List<Dish> dishList=dishMapper.listByType(categoryId);
|
List<Dish> dishList=dishMapper.listByType(dish);
|
||||||
return dishList;
|
return dishList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
/**
|
||||||
|
* 条件查询菜品和口味
|
||||||
|
* @param dish
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public List<DishVO> listWithFlavor(Dish dish) {
|
||||||
|
//TODO 貌似还有问题
|
||||||
|
List<Dish> dishList = dishMapper.listByType(dish);
|
||||||
|
|
||||||
|
List<DishVO> dishVOList = new ArrayList<>();
|
||||||
|
|
||||||
|
for (Dish d : dishList) {
|
||||||
|
DishVO dishVO = new DishVO();
|
||||||
|
BeanUtils.copyProperties(d,dishVO);
|
||||||
|
|
||||||
|
//根据菜品id查询对应的口味
|
||||||
|
List<DishFlavor> flavors = dishFlavorMapper.queryByDishId(d.getId());
|
||||||
|
|
||||||
|
dishVO.setFlavors(flavors);
|
||||||
|
dishVOList.add(dishVO);
|
||||||
|
}
|
||||||
|
|
||||||
|
return dishVOList;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ import com.sky.mapper.SetmealMapper;
|
|||||||
import com.sky.mapper.Setmeal_dishMapper;
|
import com.sky.mapper.Setmeal_dishMapper;
|
||||||
import com.sky.result.PageResult;
|
import com.sky.result.PageResult;
|
||||||
import com.sky.service.SetmealService;
|
import com.sky.service.SetmealService;
|
||||||
|
import com.sky.vo.DishItemVO;
|
||||||
import com.sky.vo.SetmealVO;
|
import com.sky.vo.SetmealVO;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -126,4 +127,27 @@ public class SetmealServiceImpl implements SetmealService {
|
|||||||
.build();
|
.build();
|
||||||
setmealMapper.update(setmeal);
|
setmealMapper.update(setmeal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
/**
|
||||||
|
* 条件查询
|
||||||
|
* @param setmeal
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public List<Setmeal> list(Setmeal setmeal) {
|
||||||
|
List<Setmeal> list = setmealMapper.list(setmeal);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
/**
|
||||||
|
* 根据id查询菜品选项
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public List<DishItemVO> getDishItemById(Long id) {
|
||||||
|
return setmealMapper.getDishItemBySetmealId(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,72 @@
|
|||||||
|
package com.sky.service.impl;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.sky.constant.MessageConstant;
|
||||||
|
import com.sky.dto.UserLoginDTO;
|
||||||
|
import com.sky.entity.User;
|
||||||
|
import com.sky.exception.LoginFailedException;
|
||||||
|
import com.sky.mapper.UserMapper;
|
||||||
|
import com.sky.properties.WeChatProperties;
|
||||||
|
import com.sky.service.UserService;
|
||||||
|
import com.sky.utils.HttpClientUtil;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class UserServiceImpl implements UserService {
|
||||||
|
public static final String WX_LOGIN = "https://api.weixin.qq.com/sns/jscode2session";
|
||||||
|
@Autowired
|
||||||
|
private WeChatProperties weChatProperties;
|
||||||
|
@Autowired
|
||||||
|
private UserMapper userMapper;
|
||||||
|
/**
|
||||||
|
* 微信登录
|
||||||
|
* @param userLoginDTO
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public User wxLogin(UserLoginDTO userLoginDTO) {
|
||||||
|
//调用微信接口服务,获得当前微信用户的openid
|
||||||
|
String openid=getOpenid(userLoginDTO.getCode());
|
||||||
|
//判断openid是否为空,如果空则登录失败
|
||||||
|
if(openid==null)
|
||||||
|
throw new LoginFailedException(MessageConstant.LOGIN_FAILED);
|
||||||
|
|
||||||
|
//判断当前用户是否为新用户
|
||||||
|
User user = userMapper.getByOpenid(openid);
|
||||||
|
//如果是,则自动注册
|
||||||
|
if(user==null){
|
||||||
|
user=User.builder()
|
||||||
|
.openid(openid)
|
||||||
|
.createTime(LocalDateTime.now())
|
||||||
|
.build();
|
||||||
|
userMapper.insert(user);
|
||||||
|
}
|
||||||
|
//返回用户对象
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调用微信接口服务,获取微信用户的openid
|
||||||
|
* @param code
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private String getOpenid(String code){
|
||||||
|
Map<String,String> map=new HashMap<>();
|
||||||
|
map.put("appid",weChatProperties.getAppid());
|
||||||
|
map.put("secret",weChatProperties.getSecret());
|
||||||
|
map.put("js_code",code);
|
||||||
|
map.put("grant_type","authorization_code");
|
||||||
|
String json=HttpClientUtil.doGet(WX_LOGIN,map);
|
||||||
|
JSONObject jsonObject = JSON.parseObject(json);
|
||||||
|
String openid = jsonObject.getString("openid");
|
||||||
|
return openid;
|
||||||
|
}
|
||||||
|
}
|
@ -17,3 +17,7 @@ sky:
|
|||||||
access-key-secret: AJPJSYc5sdwiZoj8RWzsXtjKR3W8f0
|
access-key-secret: AJPJSYc5sdwiZoj8RWzsXtjKR3W8f0
|
||||||
endpoint: https://oss-cn-hangzhou.aliyuncs.com
|
endpoint: https://oss-cn-hangzhou.aliyuncs.com
|
||||||
bucket-name: zyjavaweb
|
bucket-name: zyjavaweb
|
||||||
|
|
||||||
|
wechat:
|
||||||
|
appid: wxa3b6f70e4ffb92cd
|
||||||
|
secret: ef08b2be0e89a4ff71105c28416e9f42
|
||||||
|
@ -18,6 +18,7 @@ spring:
|
|||||||
auth: ${sky.redis.auth}
|
auth: ${sky.redis.auth}
|
||||||
database: ${sky.redis.database}
|
database: ${sky.redis.database}
|
||||||
|
|
||||||
|
|
||||||
mybatis:
|
mybatis:
|
||||||
#mapper配置文件
|
#mapper配置文件
|
||||||
mapper-locations: classpath:mapper/*.xml
|
mapper-locations: classpath:mapper/*.xml
|
||||||
@ -36,6 +37,7 @@ logging:
|
|||||||
|
|
||||||
sky:
|
sky:
|
||||||
jwt:
|
jwt:
|
||||||
|
#管理端token
|
||||||
# 设置jwt签名加密时使用的秘钥
|
# 设置jwt签名加密时使用的秘钥
|
||||||
admin-secret-key: itcast
|
admin-secret-key: itcast
|
||||||
# 设置jwt过期时间
|
# 设置jwt过期时间
|
||||||
@ -43,11 +45,16 @@ sky:
|
|||||||
# 设置前端传递过来的令牌名称
|
# 设置前端传递过来的令牌名称
|
||||||
admin-token-name: token
|
admin-token-name: token
|
||||||
|
|
||||||
|
#用户端token
|
||||||
|
user-secret-key: itheima
|
||||||
|
user-ttl: 7200000
|
||||||
|
user-token-name: authentication
|
||||||
|
|
||||||
alioss:
|
alioss:
|
||||||
endpoint: {sky.alioss.endpoint}
|
endpoint: {sky.alioss.endpoint}
|
||||||
bucket-name: {sky.alioss.bucket-name}
|
bucket-name: {sky.alioss.bucket-name}
|
||||||
access-key-id: {sky.alioss.access-key-id}
|
access-key-id: {sky.alioss.access-key-id}
|
||||||
access-key-secret: {sky.alioss.access-key-secret}
|
access-key-secret: {sky.alioss.access-key-secret}
|
||||||
|
wechat:
|
||||||
|
appid: ${sky.wechat.appid}
|
||||||
|
secret: ${sky.wechat.secret}
|
||||||
|
@ -14,6 +14,12 @@
|
|||||||
</where>
|
</where>
|
||||||
order by sort
|
order by sort
|
||||||
</select>
|
</select>
|
||||||
|
<select id="listByType" resultType="com.sky.entity.Category">
|
||||||
|
select * from category
|
||||||
|
<where>
|
||||||
|
<if test="type!=null">type=#{type}</if>
|
||||||
|
</where>
|
||||||
|
</select>
|
||||||
<update id="update">
|
<update id="update">
|
||||||
update category
|
update category
|
||||||
<set>
|
<set>
|
||||||
|
@ -21,6 +21,13 @@
|
|||||||
</where>
|
</where>
|
||||||
order by d.create_time desc
|
order by d.create_time desc
|
||||||
</select>
|
</select>
|
||||||
|
<select id="listByType" resultType="com.sky.entity.Dish">
|
||||||
|
select * from dish
|
||||||
|
<where>
|
||||||
|
category_id=#{categoryId} and
|
||||||
|
<if test="status!=null">status=#{status}</if>
|
||||||
|
</where>
|
||||||
|
</select>
|
||||||
<update id="update">
|
<update id="update">
|
||||||
update dish
|
update dish
|
||||||
<set>
|
<set>
|
||||||
|
@ -29,4 +29,18 @@
|
|||||||
<if test="status!=null">and c.status=#{status}</if>
|
<if test="status!=null">and c.status=#{status}</if>
|
||||||
</where>
|
</where>
|
||||||
</select>
|
</select>
|
||||||
|
<select id="list" resultType="com.sky.entity.Setmeal">
|
||||||
|
select * from setmeal
|
||||||
|
<where>
|
||||||
|
<if test="name != null">
|
||||||
|
and name like concat('%',#{name},'%')
|
||||||
|
</if>
|
||||||
|
<if test="categoryId != null">
|
||||||
|
and category_id = #{categoryId}
|
||||||
|
</if>
|
||||||
|
<if test="status != null">
|
||||||
|
and status = #{status}
|
||||||
|
</if>
|
||||||
|
</where>
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
10
sky-server/src/main/resources/mapper/UserMapper.xml
Normal file
10
sky-server/src/main/resources/mapper/UserMapper.xml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||||
|
<mapper namespace="com.sky.mapper.UserMapper">
|
||||||
|
|
||||||
|
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
|
||||||
|
insert into user (openid, name, phone, sex, id_number, avatar, create_time)
|
||||||
|
values (#{openid}, #{name}, #{phone}, #{sex}, #{idNumber}, #{avatar}, #{createTime})
|
||||||
|
</insert>
|
||||||
|
</mapper>
|
Loading…
x
Reference in New Issue
Block a user