select category
This commit is contained in:
@@ -5,6 +5,9 @@ import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
/**
|
||||
* @author SLHAF
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableTransactionManagement //开启注解方式的事务管理
|
||||
@Slf4j
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.sky.config;
|
||||
|
||||
import com.sky.interceptor.JwtTokenAdminInterceptor;
|
||||
import com.sky.interceptor.JwtTokenUserInterceptor;
|
||||
import com.sky.json.JacksonObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -22,6 +23,7 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* 配置类,注册web层相关组件
|
||||
* @author SLHAF
|
||||
*/
|
||||
@Configuration
|
||||
@Slf4j
|
||||
@@ -29,17 +31,25 @@ public class WebMvcConfiguration extends WebMvcConfigurationSupport {
|
||||
|
||||
@Autowired
|
||||
private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
|
||||
@Autowired
|
||||
private JwtTokenUserInterceptor jwtTokenUserInterceptor;
|
||||
|
||||
/**
|
||||
* 注册自定义拦截器
|
||||
*
|
||||
* @param registry
|
||||
*/
|
||||
@Override
|
||||
protected void addInterceptors(InterceptorRegistry registry) {
|
||||
log.info("开始注册自定义拦截器...");
|
||||
registry.addInterceptor(jwtTokenAdminInterceptor)
|
||||
.addPathPatterns("/admin/**")
|
||||
.excludePathPatterns("/admin/employee/login");
|
||||
|
||||
registry.addInterceptor(jwtTokenUserInterceptor)
|
||||
.addPathPatterns("/user/**")
|
||||
.excludePathPatterns("/user/user/login")
|
||||
.excludePathPatterns("/user/shop/status");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,6 +100,7 @@ public class WebMvcConfiguration extends WebMvcConfigurationSupport {
|
||||
* 设置静态资源映射
|
||||
* @param registry
|
||||
*/
|
||||
@Override
|
||||
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
log.info("开始设置静态资源映射...");
|
||||
registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
|
||||
|
||||
@@ -43,6 +43,7 @@ public class EmployeeController {
|
||||
@PostMapping("/login")
|
||||
@ApiOperation(value = "员工登录")
|
||||
public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
|
||||
// System.out.println(Thread.currentThread().getName());
|
||||
log.info("员工登录:{}", employeeLoginDTO);
|
||||
|
||||
Employee employee = employeeService.login(employeeLoginDTO);
|
||||
|
||||
@@ -10,8 +10,11 @@ import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author SLHAF
|
||||
*/
|
||||
@RestController("userShopController")
|
||||
@RequestMapping("/admin/user")
|
||||
@RequestMapping("/user/shop")
|
||||
@Api(tags = "店铺操作接口")
|
||||
@Slf4j
|
||||
public class ShopController {
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
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 lombok.extern.slf4j.Slf4j;
|
||||
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.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user/category")
|
||||
@Api(tags = "Category")
|
||||
@Slf4j
|
||||
public class UserCategoryController {
|
||||
@Autowired
|
||||
private CategoryService categoryService;
|
||||
|
||||
@GetMapping("/list")
|
||||
public Result<List<Category>> list(Integer type){
|
||||
log.info("user category list: {}",type);
|
||||
List<Category> list = categoryService.list(type);
|
||||
return Result.success(list);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
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.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author SLHAF
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/user/user")
|
||||
@Api(tags = "用户相关接口")
|
||||
@Slf4j
|
||||
public class UserController {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@Autowired
|
||||
private JwtProperties jwtProperties;
|
||||
|
||||
@PostMapping("/login")
|
||||
@ApiOperation("微信登录")
|
||||
public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
|
||||
// System.out.println(Thread.currentThread().getName());
|
||||
log.info("微信用户登录: {}",userLoginDTO.getCode());
|
||||
|
||||
//微信登录
|
||||
User user = userService.wxLogin(userLoginDTO);
|
||||
|
||||
//生成jwt
|
||||
HashMap<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);
|
||||
}
|
||||
}
|
||||
@@ -31,9 +31,9 @@ public class JwtTokenAdminInterceptor implements HandlerInterceptor {
|
||||
* @param response
|
||||
* @param handler
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||
//判断当前拦截到的是Controller的方法还是其他资源
|
||||
if (!(handler instanceof HandlerMethod)) {
|
||||
//当前拦截到的不是动态方法,直接放行
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||
//判断当前拦截到的是Controller的方法还是其他资源
|
||||
if (!(handler instanceof HandlerMethod)) {
|
||||
//当前拦截到的不是动态方法,直接放行
|
||||
return true;
|
||||
}
|
||||
|
||||
//1、从请求头中获取令牌
|
||||
String token = request.getHeader(jwtProperties.getUserTokenName());
|
||||
|
||||
//2、校验令牌
|
||||
try {
|
||||
log.info("jwt校验:{}", token);
|
||||
Claims claims = JwtUtil.parseJWT(jwtProperties.getUserSecretKey(), token);
|
||||
Long userId = Long.valueOf(claims.get(JwtClaimsConstant.USER_ID).toString());
|
||||
BaseContext.setCurrentId(userId);
|
||||
log.info("当前用户id:{}", userId);
|
||||
//3、通过,放行
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
//4、不通过,响应 401 状态码
|
||||
response.setStatus(401);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,4 +30,7 @@ public interface CategoryMapper {
|
||||
|
||||
@Select("select * from category where type = #{type}")
|
||||
List<Category> selectByType(Integer type);
|
||||
|
||||
@Select("select * from category")
|
||||
List<Category> selectCategory();
|
||||
}
|
||||
|
||||
23
sky-server/src/main/java/com/sky/mapper/UserMapper.java
Normal file
23
sky-server/src/main/java/com/sky/mapper/UserMapper.java
Normal file
@@ -0,0 +1,23 @@
|
||||
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 {
|
||||
|
||||
/**
|
||||
* 根据openid查询用户
|
||||
* @param openid
|
||||
* @return
|
||||
*/
|
||||
@Select("select * from user where openid = #{openid}")
|
||||
User getByOpenid(String openid);
|
||||
|
||||
/**
|
||||
* 插入数据
|
||||
* @param user
|
||||
*/
|
||||
void insert(User user);
|
||||
}
|
||||
19
sky-server/src/main/java/com/sky/service/UserService.java
Normal file
19
sky-server/src/main/java/com/sky/service/UserService.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.sky.service;
|
||||
|
||||
import com.sky.dto.UserLoginDTO;
|
||||
import com.sky.entity.User;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author SLHAF
|
||||
*/
|
||||
@Service
|
||||
public interface UserService {
|
||||
|
||||
/**
|
||||
* 微信登录
|
||||
* @param userLoginDTO
|
||||
* @return
|
||||
*/
|
||||
User wxLogin(UserLoginDTO userLoginDTO);
|
||||
}
|
||||
@@ -126,6 +126,12 @@ public class CategoryServiceImpl implements CategoryService {
|
||||
*/
|
||||
@Override
|
||||
public List<Category> list(Integer type) {
|
||||
return categoryMapper.selectByType(type);
|
||||
List<Category> categoryList;
|
||||
if (type == null){
|
||||
categoryList = categoryMapper.selectCategory();
|
||||
}else {
|
||||
categoryList = categoryMapper.selectByType(type);
|
||||
}
|
||||
return categoryList;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author SLHAF
|
||||
*/
|
||||
@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;
|
||||
|
||||
@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) {
|
||||
HashMap<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);
|
||||
return jsonObject.getString("openid");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user