springboot中实现微信登录功能
创始人
2024-11-19 07:07:59
0

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

相关内容

热门资讯

黑科技攻略(wepoke真的有... WePoker透视辅助版本解析‌,黑科技攻略(wepoke真的有挂)wepower有辅助器吗(wep...
黑科技辅助!微扑克ai代打(透... 黑科技辅助!微扑克ai代打(透视)wepoke教程(2021已更新)(哔哩哔哩)1、微扑克ai代打a...
黑科技黑科技!德扑软件决策(透... 黑科技黑科技!德扑软件决策(透视)确实真的有挂(2023已更新)(哔哩哔哩)1、每一步都需要思考,不...
黑科技了解(wepoke透明黑... 黑科技了解(wepoke透明黑科技)wepoke透明挂辅助(wepOKE)好像存在有挂(了解有挂)-...
黑科技好牌!EV扑克辅助软件(... 黑科技好牌!EV扑克辅助软件(透视)果然是有挂(2024已更新)(哔哩哔哩);1、这是跨平台的EV扑...
黑科技辅助!来玩app德州扑克... 黑科技辅助!来玩app德州扑克(透视)必备教程(2025已更新)(哔哩哔哩)1、玩家可以在来玩app...
黑科技数据(wepoke智能a... 黑科技数据(wepoke智能ai)wepoke透明挂使用方法(wEpOke)果然真的有挂(有挂教学)...
黑科技游戏!微扑克怎么加入俱乐... 黑科技游戏!微扑克怎么加入俱乐部(透视)都是存在有挂(2020已更新)(哔哩哔哩);1、起透看视 微...
黑科技辅助!wepoke ap... 黑科技辅助!wepoke app(透视)新2025教程(2024已更新)(哔哩哔哩)1、在wepok...
黑科技ai(wepoke黑科技... 黑科技ai(wepoke黑科技)wepoke确实有挂(WepoKe)本然有挂(有人有挂)-哔哩哔哩;...