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

相关内容

热门资讯

必看攻略!wepoker怎么开... 必看攻略!wepoker怎么开辅助,wepoker有辅助功能吗,技巧教程(确实有挂)能透视中分为三种...
近日!红龙poker辅助工具,... 近日!红龙poker辅助工具,wpk透视辅助,竟然确实有挂(真的有挂)1、近日!红龙poker辅助工...
推荐一下!边锋干瞪眼辅助器,小... 推荐一下!边锋干瞪眼辅助器,小唐家乐园辅助,其实真的是有挂(哔哩哔哩)1、在边锋干瞪眼辅助器插件功能...
揭秘关于!wpk真的有透视嘛,... 揭秘关于!wpk真的有透视嘛,hhpoker有没有辅助,攻略教程(今日头条)1、完成辅助器v3.3的...
现有说明如下!约局吧德州可以透... 现有说明如下!约局吧德州可以透视吗,如何下载wpk透视版,果然存在有挂(有挂技术)暗藏猫腻,小编详细...
解迷一下!科乐天天踢有没有作比... 解迷一下!科乐天天踢有没有作比,家乡大二辅助免费,切实真的是有挂(哔哩哔哩)1、首先打开科乐天天踢有...
实测教程!hhpoker视频巡... 实测教程!hhpoker视频巡查真的假的,hhpoker德州有挂吗,技巧教程(有挂工具)1、免费辅助...
此事引发网友热议!wepoke... 此事引发网友热议!wepoker透视苹果系统,哈糖大菠萝助手,原来真的有挂(有挂神器)1、下载好we...
必备一下!jj斗地主麻将外卦挂... 必备一下!jj斗地主麻将外卦挂,九游破解辅助插件,其实是真的有挂(哔哩哔哩)1、游戏颠覆性的策略玩法...
必看攻略!hh poker插件... 必看攻略!hh poker插件下载,hhpoker辅助挂,插件教程(有挂总结)1、用户打开应用后不用...