用户联动微信小程序。相关的实现层代码
发送url请求。
/*** 登录验证** @param code 临时登录码* @return ·*/@Overridepublic Map<String, Object> checkLogin(String code) {String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + appsecret+ "&js_code=" + code + "&grant_type=authorization_code";String errmsg = "";String errcode = "";String session_key = "";String openid = "";String token = "";WeChatSessionModel weChatSessionModel;//向微信服务器发送请求,传出一些返回值ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, String.class);//校验发送是否成功if (responseEntity.getStatusCode() == HttpStatus.OK) {//如果成功则取出返回体的内容String sessionData = responseEntity.getBody();//取出微信返回的内容,并且使用gson工具类,将其封装进入wx模块weChatSessionModel = new Gson().fromJson(sessionData, WeChatSessionModel.class);openid = weChatSessionModel.getOpenid();session_key = weChatSessionModel.getSession_key();errcode = weChatSessionModel.getErrcode();errmsg = weChatSessionModel.getErrmsg();} else {//如果登录失败,则提取错误内容返回数据信息map.put("errmsg", errmsg);return map;}if ("".equals(openid) || openid == null) {//登录未失败,但是openid获取失败,也报错map.put("errmsg", errmsg);return map;} else {//查询openid在数据库是否存在LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(User::getOpenid, openid);User user = userMapper.selectOne(queryWrapper);//将session_key返回给前端map.put("session_key", session_key);if (user == null) {//当用户信息不存在时注册新用户。user = new User(UUID.randomUUID().toString(), openid, "微信用户", new Date(), "default.jpg");userMapper.insert(user);}//将数据返回给前端map.put("user", user);map.put("token", JWTUtils.createToken(user.getId().toString()));return map;}}