@ApiOperation(value = "微信授权登录")@RequestMapping("doLoginByWechat")@ResponseBodypublic Object doLoginByWechat(HttpServletRequest request) {Map<String, Object> map = new HashMap<>();String code = Parameter("code");if (StringUtils.isEmpty(code) ) {map.put("code", Result.ERROR);map.put("msg", "请先进行授权!");return map;}String result = null;try {//获取access_tokenString url = "=client_credential&appid={0}&secret={1}";String replaceUrl = place("{0}", appid).replace("{1}", secret);String res = (replaceUrl);JSONObject json_token = JSON.parseObject(res);String access_token = String("access_token");//获取手机号String urlTwo = "={0}";String replaceUrlTwo = place("{0}",access_token);HashMap<String, Object> requestParam = new HashMap<>();// 手机号调用凭证requestParam.put("code", code);String jsonStr = JSONString(requestParam);HttpResponse response = HttpRequest.post(replaceUrlTwo).header(Header.CONTENT_ENCODING, "UTF-8")// 发送json数据需要设置contentType.header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded").body(jsonStr).execute();if (Status() == HttpStatus.HTTP_OK) {result = response.body();}Object phone_info=JSONObject.parseObject(result).get("phone_info");String purePhoneNumber=JSONObject.parseObject(String()).get("purePhoneNumber").toString();if(StringUtils.isBlank(purePhoneNumber)){map.put("code", Result.ERROR);map.put("msg", "当前用户没有绑定手机号!");return map;}else{CurriculumVitae curriculumVitae=new CurriculumVitae();curriculumVitae.setPhone(purePhoneNumber);//根据loginIphone查询List<CurriculumVitae> CurriculumVitaes = trialService.selectByIphone(curriculumVitae);if(CurriculumVitaes.size()==1) {//修改对应信息trialService.updateByWechat("1");}else{map.put("code", Result.ERROR);map.put("msg", "手机号有误!");return map;}}//生成tokenString token = ateToken(purePhoneNumber);map.put("code", Result.SUCCESS);map.put("msg", "登录成功");map.put("token", token);log.info(purePhoneNumber+"在"+new Date()+"登录后台成功");} catch (Exception e) {map.put("code", Result.WARN);map.put("msg", "获取失败!");return map;}return JSONObject.parseObject(result).get("phone_info");}
yml添加
#微信小程序
weixin:appid: wx836f5035d64898a5secret: eb96a3b31d3cc2aa16691b1d38ad7faa
本文发布于:2024-02-02 16:35:21,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170686292245052.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |