微信小程序开发(三)

获取到对应的参数后, 需要将用户的信息存储到对应的表中
表格如下:
在这里插入图片描述
然后将获取到的参数处理:

    @RequestMapping(value = "/decodeUserInfo",method = RequestMethod.POST)
    public R login(@RequestParam(value = "encryptedData",required = false) String encryptedData,@RequestParam(value = "iv",required = false) String iv,@RequestParam(value = "code",required = false) String code) {

        if(!StringUtils.isNotBlank(code)){
            return R.ok().put("未获取到用户凭证code",202);
        }
        System.out.println("code ==> " + code);
        String appid = "wxxxxxxxx23";
        String appSecret = "c1ddxxxxxxx98";
        String grantType = "authorization_code";
        String apiUrl = "https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+appSecret+"&js_code="+code+"&grant_type="+grantType+"&lang=zh_CN";

        // https://api.weixin.qq.com/sns/jscode2session?appid=wx4ff44fe0a3bd0723&secret=c1ddc9fc4d7a97c34df77c5666efa798&grant_type=authorization_code&lang=zh_CN

        String responseBody = HttpClientUtil.doGet(apiUrl);

        JSONObject jsonObject = JSON.parseObject(responseBody);
        if(StringUtils.isNotBlank(jsonObject.getString("openid")) && StringUtils.isNotBlank(jsonObject.getString("session_key"))){

            //解密获取用户信息
            JSONObject userInfoJSON = new WechatGetUserInfoUtil().getUserInfo(encryptedData,jsonObject.getString("session_key"),iv);

//            System.out.println("userInfoJSON ===>  " + userInfoJSON);

            if(userInfoJSON != null){
                //这步应该set进实体类
                Map<String,Object> userInfo = new HashMap();
                userInfo.put("openId", userInfoJSON.get("openId"));
                userInfo.put("nickName", userInfoJSON.get("nickName"));
                userInfo.put("gender", userInfoJSON.get("gender"));
                userInfo.put("city", userInfoJSON.get("city"));
                userInfo.put("province", userInfoJSON.get("province"));
                userInfo.put("country", userInfoJSON.get("country"));
                userInfo.put("avatarUrl", userInfoJSON.get("avatarUrl"));
                // 解密unionId & openId;
                if (userInfoJSON.get("unionId")!=null) {
                    userInfo.put("unionId", userInfoJSON.get("unionId"));
                }
                //然后根据openid去数据库判断有没有该用户信息,若没有则存入数据库,有则返回用户数据
                Map<String,Object> dataMap = new HashMap<>();
                dataMap.put("userInfo", userInfo);
                String uuid= UUID.randomUUID().toString();
                dataMap.put("WXTOKEN", uuid);
                wxuserService.saveUser(userInfo,dataMap);
                return R.ok().put("登陆成功",200).put("data",dataMap);
            }else{
                return R.ok().put("解密失败",202);
            }
        }else{
            return R.ok().put("未获取到用户openid 或 session  ==> ",202);
        }
    }
    public void saveUser(Map<String,Object> userInfo, Map<String,Object>dataMap) {

            WxUserEntity wxUser = new WxUserEntity();
            StringBuffer areas = new StringBuffer();
            for (Map.Entry<String, Object> map : dataMap.entrySet()) {
                if (map.getKey().equals("WXTOKEN")) {
                     System.out.println("wxTokenKey ===> " + map.getKey() + " , wxTokenValue ===> " + map.getValue());
                }
            }
            String userName = "";
            String opeId = "";
            String Img = "";
            Integer sex = 0;
            for (Map.Entry<String, Object> map2 : userInfo.entrySet()) {
                if(map2.getKey().equals("openId")){
                    opeId += map2.getValue().toString();
                }
                if(map2.getKey().equals("nickName")){
                    userName += map2.getValue().toString();
                }
                if(map2.getKey().equals("avatarUrl")){
                    Img += map2.getValue().toString();
                }
                if(map2.getKey().equals("gender")){
                    sex += Integer.parseInt(map2.getValue() + "");
                }
                if(map2.getKey().equals("country")){
                    areas.append(map2.getValue() + "/");
                }
                if(map2.getKey().equals("province")){
                    areas.append(map2.getValue() + "/");
                }
                if(map2.getKey().equals("city")){
                    areas.append(map2.getValue() + "");
                }
            }
            String login = baseMapper.selectopenId(opeId);
            if(login == null){
                wxUser.setUser_fill(1);
                wxUser.setUser_sex(sex);
                wxUser.setUser_photo(Img);
                wxUser.setOpen_id(opeId);
                wxUser.setUser_name(userName );
                wxUser.setUser_areas(areas + "");
                wxUser.setSubscribe_time(baseMapper.selectTime());
                baseMapper.insert(wxUser);
            }
    }

每个用户第一次登陆的时候会记录他的openId , 在用户第二次登陆的时候会先去表中查询现在获取的openId是否已经存在表中, 不在表中表示该用户为第一次登陆,就将用户对应的数据存储到表中,如果已经查询到了openId已经在表中则表示, 该用户以前登陆过。。 不用将数据存储到表中。。

猜你喜欢

转载自blog.csdn.net/qq_41164787/article/details/86680162