Controller层
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.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 login(@RequestBody UserLoginDTO userLoginDTO){ log.info("微信用户登录{}",userLoginDTO.getCode()); //微信登录 User user = userService.wxlogin(userLoginDTO); //为微信用户生成JWT令牌 Map claims=new HashMap<>(); claims.put(JwtClaimsConstant.USER_ID,user); String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims); //返回VO数据 UserLoginVO userLoginVO = new UserLoginVO(); userLoginVO.setId(user.getId()); userLoginVO.setOpenid(user.getOpenid()); userLoginVO.setToken(token); log.info("end={}",userLoginVO); return Result.success(userLoginVO); } }
service层
package com.sky.service; import com.sky.dto.UserLoginDTO; import com.sky.entity.User; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Service; public interface UserService { /** * 微信登录 * @param userLoginDTO * @return */ User wxlogin(UserLoginDTO userLoginDTO); }
servicelmpl
package com.sky.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.annotation.JsonFormat; import com.google.gson.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.apache.http.client.HttpClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.HashMap; import java.util.Map; @Service @Slf4j public class UserServicelmpl implements UserService { //微信服务接口地址,获取openid 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) { //调用微信接口服务 Map map= new HashMap<>(); map.put("appid",weChatProperties.getAppid()); map.put("secret",weChatProperties.getSecret()); map.put("js_code", userLoginDTO.getCode()); map.put("grant_type","authorization_code"); String json = HttpClientUtil.doGet(WX_LOGIN, map); log.info("获取到{}",json); JSONObject jsonObject = JSON.parseObject(json); String openid = jsonObject.getString("openid"); //判断OPenId是否为空 if (openid==null){ throw new LoginFailedException(MessageConstant.LOGIN_FAILED); } //当前用户是否为新用户 User user = userMapper.getByOpenid(openid); //如果是新用户,自动完成注册 if (user==null){ User nuser=new User(); nuser.setOpenid(openid); nuser.setCreateTime(LocalDateTime.now()); userMapper.insert(nuser); return nuser; } //返回用户对象 return user; } }
上一篇:ipc指的是什么
下一篇:Spring Security