项目回顾-微信登录的实现

本文详细介绍了微信小程序登录的实现过程,包括Controller中的请求处理、Service中的微信接口调用和用户验证,以及UserMapper接口和XML文件中的数据库操作。
摘要由CSDN通过智能技术生成

一、具体流程

在这里插入图片描述
1.appid 是每个小程序都有的唯一id
2.appsecret 是密钥
3.利用HttpClientUtil发送请求 提交参数调用微信接口服务
4.之后微信接口返回openid

controller类流程

    @PostMapping("/login")
    @ApiOperation("微信登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO 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);
    }

Service流程

在这里插入图片描述
//微信服务接口
 public static final String WX_LOGIN = "https://api.weixin.qq.com/sns/jscode2session";
 public User wxLogin(UserLoginDTO userLoginDTO) {
        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){
        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); //利用HttpClient发送请求 微信接口返回字符串

        JSONObject jsonObject = JSON.parseObject(json);
        String openid = jsonObject.getString("openid");
        return openid;
    }

UserMapper接口

  /**
     * 根据openid查询用户
     * @param openid
     * @return
     */
    @Select("select  * from user where openid = #{openid}")
    User getByOpenid(String openid);
     /**
     * 插入数据
     * @param user
     */
    void insert(User user);
    @Select("select * from user where id = #{id}")
    User getById(Long userId);

UserMapper.xml

<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>
  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值