微信小程序登录

1.开发流程 配置工作

2.编写代码

   /**
     * 微信授权时录入用户数据
     */
    @RequestMapping("/appLogin")
    @Transactional
    public Object insert(@RequestParam("code")String code, HttpSession session) throws ClientProtocolException, IOException {
        System.err.println("微信授权登录");
        System.err.println("code值: "+code);
        String appid = ConfigUtil.APPID; //自己的APPID
        String secret = ConfigUtil.APP_SECRECT; //自己小程序的SECRET

        //发起授权登录请求
        String loginUrl="https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code";
        try {
            CloseableHttpClient client = null;
            CloseableHttpResponse response = null;
            try {
                // 创建http GET请求
                HttpGet httpGet = new HttpGet(loginUrl);
                client = HttpClients.createDefault();
                // 执行请求
                response = client.execute(httpGet);

                //得到微信返回数据
                HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString(entity);

                System.err.println("微信返回的结果"+result);

                JSONObject json_test = JSONObject.parseObject(result);


                //openid
                String wxOpenid = json_test.getString("openid");
                //会话秘钥
                String sessionKey = json_test.getString("session_key");
                System.err.println("openid值: "+wxOpenid);//得到微信openID
                System.err.println("sessionKey值: "+sessionKey);





                //自己的逻辑,查用户对象
                //根据id数据库数据查询
                TaskUser taskUser = new TaskUser();

                taskUser.setOpenid(wxOpenid);
                List<TaskUser> taskUsers = userService.selectTaskUserList(taskUser);

                //如果没拿到openid
                if(StringUtils.isEmpty(wxOpenid)){

                    return  JsonResult.error(500,"未获取到openid",null);
                }

                //如果用户为空表示第一次,保存一下返回给前端
                if (taskUsers.isEmpty()){//如果user等于null说明该用户第一次登录,数据库没有该用户信息。
                    TaskUser userarg = new TaskUser();
                    userarg.setOpenid(wxOpenid);
                    insertUser(userarg);
                    //将用户放到redis中 得到根据用户相对应的token
                    String token = userRedisService.setToken(userarg);
                    Map<String,Object> map = new HashMap<>();
                    map.put("token",token);
                    map.put("user",userarg);
                    map.put("sessionKey",sessionKey);
                    map.put("openId",wxOpenid);
                    return JsonResult.success(map);

                }else {

                    //将用户放到redis中 得到根据用户相对应的token
                    String token = userRedisService.setToken(taskUsers.get(0));
                    Map<String,Object> map = new HashMap<>();
                    map.put("token",token);
                    map.put("user",taskUsers.get(0));
                    map.put("sessionKey",sessionKey);
                    map.put("openId",wxOpenid);
                    return JsonResult.success(map);


                }

            } finally {
                if (response != null) {
                    response.close();
                }
                if (client != null) {
                    client.close();
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  JsonResult.error(500,"微信返回异常",null);
    }

3. 业务流程

1.前端对微信发起请求 拿到code值
2.后端接受以后带着code和appid和appSECRECT去访问微信的url申请授权登录
   //发起授权登录请求
        String loginUrl="https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code"; 
3.拿到微信请求的结果
4.做自己的业务逻辑处理 
5.最后把前端需要的值和openid返回给前端 流程结束    
原文地址:https://www.cnblogs.com/wp980327/p/14430025.html