完成微信登录、菜品浏览功能
This commit is contained in:
parent
45509ea96e
commit
25d44499d5
@ -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;
|
||||||
@ -30,6 +31,9 @@ public class WebMvcConfiguration extends WebMvcConfigurationSupport {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
|
private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private JwtTokenUserInterceptor jwtTokenUserInterceptor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注册自定义拦截器
|
* 注册自定义拦截器
|
||||||
*
|
*
|
||||||
@ -40,6 +44,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");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -67,7 +67,6 @@ public class EmployeeController {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 退出
|
* 退出
|
||||||
*
|
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@PostMapping("/logout")
|
@PostMapping("/logout")
|
||||||
|
@ -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.list(type);
|
||||||
|
return Result.success(list);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
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.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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据分类id查询菜品
|
||||||
|
*
|
||||||
|
* @param categoryId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/list")
|
||||||
|
@ApiOperation("根据分类id查询菜品")
|
||||||
|
public Result<List<DishVO>> list(Long categoryId) {
|
||||||
|
Dish dish = new Dish();
|
||||||
|
dish.setCategoryId(categoryId);
|
||||||
|
dish.setStatus(StatusConstant.ENABLE);//查询起售中的菜品
|
||||||
|
|
||||||
|
List<DishVO> list = dishService.listWithFlavor(dish);
|
||||||
|
|
||||||
|
return Result.success(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
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.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查询套餐")
|
||||||
|
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,62 @@
|
|||||||
|
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.Map;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/user/user")
|
||||||
|
@Api(tags = "C端用户相关接口")
|
||||||
|
@Slf4j
|
||||||
|
public class UserController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private JwtProperties jwtProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信登录
|
||||||
|
* @param userLoginDTO
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("/login")
|
||||||
|
@ApiOperation("微信登录")
|
||||||
|
public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
|
||||||
|
log.info("微信登录接口,参数:{}", userLoginDTO);
|
||||||
|
|
||||||
|
// 调用微信登录
|
||||||
|
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,62 @@
|
|||||||
|
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.getUserTokenName());
|
||||||
|
|
||||||
|
//2、校验令牌
|
||||||
|
try {
|
||||||
|
log.info("jwt校验:{}", token);
|
||||||
|
Claims claims = JwtUtil.parseJWT(jwtProperties.getUserSecretKey(), token);
|
||||||
|
Long userId = Long.valueOf(claims.get(JwtClaimsConstant.USER_ID).toString());
|
||||||
|
log.info("当前用户id:{}", userId);
|
||||||
|
//将操作者id放入threadlocal中
|
||||||
|
BaseContext.setCurrentId(userId);
|
||||||
|
//3、通过,放行
|
||||||
|
return true;
|
||||||
|
} catch (Exception ex) {
|
||||||
|
//4、不通过,响应401状态码
|
||||||
|
response.setStatus(401);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -7,6 +7,7 @@ import com.sky.entity.Setmeal;
|
|||||||
import com.sky.enumeration.OperationType;
|
import com.sky.enumeration.OperationType;
|
||||||
import com.sky.result.PageResult;
|
import com.sky.result.PageResult;
|
||||||
import com.sky.result.Result;
|
import com.sky.result.Result;
|
||||||
|
import com.sky.vo.DishItemVO;
|
||||||
import com.sky.vo.SetmealVO;
|
import com.sky.vo.SetmealVO;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
import org.apache.ibatis.annotations.Select;
|
import org.apache.ibatis.annotations.Select;
|
||||||
@ -67,4 +68,21 @@ public interface SetmealMapper {
|
|||||||
*/
|
*/
|
||||||
@AutoFill(OperationType.UPDATE)
|
@AutoFill(OperationType.UPDATE)
|
||||||
void update(Setmeal setmeal);
|
void update(Setmeal setmeal);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 动态条件查询套餐
|
||||||
|
* @param setmeal
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
List<Setmeal> list(Setmeal setmeal);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据套餐id查询菜品选项
|
||||||
|
* @param setmealId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@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 setmealId);
|
||||||
}
|
}
|
||||||
|
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);
|
||||||
|
}
|
@ -55,4 +55,11 @@ public interface DishService {
|
|||||||
* @param id
|
* @param id
|
||||||
*/
|
*/
|
||||||
void startOrStop(Integer status, Long id);
|
void startOrStop(Integer status, Long id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 条件查询菜品和口味
|
||||||
|
* @param dish
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
List<DishVO> listWithFlavor(Dish dish);
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,9 @@ 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;
|
import java.util.List;
|
||||||
@ -46,4 +48,18 @@ public interface SetmealService {
|
|||||||
* @param id
|
* @param id
|
||||||
*/
|
*/
|
||||||
void startOrStop(Integer status, Long id);
|
void startOrStop(Integer status, Long id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 条件查询
|
||||||
|
* @param setmeal
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
List<Setmeal> list(Setmeal setmeal);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id查询菜品选项
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
List<DishItemVO> getDishItemById(Long id);
|
||||||
}
|
}
|
||||||
|
14
sky-server/src/main/java/com/sky/service/UserService.java
Normal file
14
sky-server/src/main/java/com/sky/service/UserService.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package com.sky.service;
|
||||||
|
|
||||||
|
import com.sky.dto.UserLoginDTO;
|
||||||
|
import com.sky.entity.User;
|
||||||
|
|
||||||
|
public interface UserService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信登录
|
||||||
|
* @param userLoginDTO
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
User wxLogin(UserLoginDTO userLoginDTO);
|
||||||
|
}
|
@ -23,6 +23,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@ -193,4 +194,28 @@ public class DishServiceImpl implements DishService {
|
|||||||
.build();
|
.build();
|
||||||
dishMapper.update(dish);
|
dishMapper.update(dish);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 条件查询菜品和口味
|
||||||
|
* @param dish
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public List<DishVO> listWithFlavor(Dish dish) {
|
||||||
|
List<Dish> dishList = dishMapper.list(dish);
|
||||||
|
|
||||||
|
List<DishVO> dishVOList = new ArrayList<>();
|
||||||
|
|
||||||
|
for (Dish d : dishList) {
|
||||||
|
DishVO dishVO = new DishVO();
|
||||||
|
BeanUtils.copyProperties(d,dishVO);
|
||||||
|
|
||||||
|
//根据菜品id查询对应的口味
|
||||||
|
List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());
|
||||||
|
|
||||||
|
dishVO.setFlavors(flavors);
|
||||||
|
dishVOList.add(dishVO);
|
||||||
|
}
|
||||||
|
|
||||||
|
return dishVOList;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ import com.sky.mapper.SetmealDishMapper;
|
|||||||
import com.sky.mapper.SetmealMapper;
|
import com.sky.mapper.SetmealMapper;
|
||||||
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 lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
@ -175,4 +176,23 @@ public class SetmealServiceImpl implements SetmealService {
|
|||||||
.build();
|
.build();
|
||||||
setmealMapper.update(setmeal);
|
setmealMapper.update(setmeal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 条件查询
|
||||||
|
* @param setmeal
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public List<Setmeal> list(Setmeal setmeal) {
|
||||||
|
List<Setmeal> list = setmealMapper.list(setmeal);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id查询菜品选项
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public List<DishItemVO> getDishItemById(Long id) {
|
||||||
|
return setmealMapper.getDishItemBySetmealId(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,81 @@
|
|||||||
|
package com.sky.service.impl;
|
||||||
|
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
public User wxLogin(UserLoginDTO userLoginDTO) {
|
||||||
|
//调用微信接口服务,获取用户的openid
|
||||||
|
String openid = getOpenid(userLoginDTO.getCode());
|
||||||
|
|
||||||
|
//判断openid是否存在,如果不存在,则抛出异常
|
||||||
|
if (openid == null) {
|
||||||
|
log.error("微信登录失败,原因:openid为空");
|
||||||
|
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) {
|
||||||
|
//调用微信接口服务,获取用户的openid
|
||||||
|
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 = JSONObject.parseObject(json);
|
||||||
|
String openid = jsonObject.getString("openid");
|
||||||
|
return openid;
|
||||||
|
}
|
||||||
|
}
|
@ -15,3 +15,6 @@ sky:
|
|||||||
host: localhost
|
host: localhost
|
||||||
port: 6379
|
port: 6379
|
||||||
database: 1
|
database: 1
|
||||||
|
wechat:
|
||||||
|
appid: wx34fbbbace9943d4a
|
||||||
|
secret: 3e5f899be36fa508bb41dc5611bff0c7
|
||||||
|
@ -42,8 +42,18 @@ sky:
|
|||||||
admin-ttl: 720000000
|
admin-ttl: 720000000
|
||||||
# 设置前端传递过来的令牌名称
|
# 设置前端传递过来的令牌名称
|
||||||
admin-token-name: token
|
admin-token-name: token
|
||||||
|
# 用户端jwt签名加密时使用的秘钥
|
||||||
|
user-secret-key: itheima
|
||||||
|
# 用户端jwt过期时间
|
||||||
|
# user-ttl: 7200000
|
||||||
|
user-ttl: 720000000
|
||||||
|
# 用户端前端传递过来的令牌名称
|
||||||
|
user-token-name: authentication
|
||||||
alioss:
|
alioss:
|
||||||
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}
|
||||||
endpoint: ${sky.alioss.endpoint}
|
endpoint: ${sky.alioss.endpoint}
|
||||||
bucket-name: ${sky.alioss.bucket-name}
|
bucket-name: ${sky.alioss.bucket-name}
|
||||||
|
wechat:
|
||||||
|
appid: ${sky.wechat.appid}
|
||||||
|
secret: ${sky.wechat.secret}
|
||||||
|
@ -48,4 +48,19 @@
|
|||||||
</where>
|
</where>
|
||||||
order by s.create_time desc
|
order by s.create_time desc
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<select id="list" parameterType="Setmeal" resultType="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>
|
||||||
|
11
sky-server/src/main/resources/mapper/UserMapper.xml
Normal file
11
sky-server/src/main/resources/mapper/UserMapper.xml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?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…
Reference in New Issue
Block a user