微信小程序开发(三)

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

    @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=wx4fxxxx23&secret=c1ddc9fxxxx66efa798&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已经在表中则表示, 该用户以前登陆过。。 不用将数据存储到表中。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

饭酱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值