Java 微信小程序登录
前言
服务端微信小程序登录流程.
1.微信小程序获取token
@Value("${wechat.mini.appid}")private String wechatAppId;@Value("${wechat.mini.secret}")private String wechatSecret;private String getWechatToken () {log.info("微信小程序获取token");String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wechatAppId +"&secret=" + wechatSecret;try {String rest = HttpRequestUtil.get(url);WeChatToken weChatToken = JSON.parseObject(rest, WeChatToken.class);if (weChatToken.getErrcode() == null || weChatToken.getErrcode().equals("0")) {return weChatToken.getAccess_token();}log.error("微信小程序获取token失败" + weChatToken.getErrmsg());return null;} catch (Exception e) {log.error("微信小程序获取token失败" + e.getMessage());return null;}}
2.获取手机号
// 前端通过code换取用户手机号// 每个code只能使用一次,code的有效期为5分钟@GetMapping("/getPhoneNumber")public Result<Authority> getPhoneNumber(@RequestParam("code") String code) {log.info("微信小程序获取手机号:" + code);String wechatToken = getWechatToken();if (StringUtils.isEmpty(wechatToken)) {return Result.error("服务异常,请重试");}String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + wechatToken;try {HashMap<String, String> map = new HashMap<>();map.put("code", code);String rest = HttpRequestUtil.post(url, JSON.toJSONString(map));WeChatSession weChatSession = JSON.parseObject(rest, WeChatSession.class);if (weChatSession.getErrcode() == null || weChatSession.getErrcode().equals("0")) {String phone_info = weChatSession.getPhone_info();WeChatPhoneInfo phoneInfo = JSON.parseObject(phone_info, WeChatPhoneInfo.class);return Result.success(phoneInfo);}return Result.error(weChatSession.getErrmsg());} catch (Exception e) {log.error(e.getMessage());return Result.error("服务异常,请重试");}}
@Data
public class WeChatSession {String openid;String session_key;String unionid;String errcode;String errmsg;String phone_info;}
3.小程序登录授权
// 前端通过code获取微信session信息@GetMapping("/code2Session")public Result<String> code2Session(@RequestParam("code") String code) {log.info("微信小程序登录:" + code);String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + wechatAppId + "&secret=" + wechatSecret + "&js_code=" + code + "&grant_type=authorization_code";try {String rest = HttpRequestUtil.get(url);WeChatSession weChatSession = JSON.parseObject(rest, WeChatSession.class);if (weChatSession.getErrcode() == null || weChatSession.getErrcode().equals("0")) {return Result.success(weChatSession);}return Result.error(weChatSession.getErrmsg());} catch (Exception e) {log.error(e.getMessage());return Result.error("服务异常,请重试");}}
4.微信小程序用户登录
// 校验用户是否注册,是否绑定// 未注册用户先注册// 未绑定用户@PostMapping("/login")public Result<Authority> login(@RequestParam(name = "appId") String appId, @RequestParam(name = "openId") String openId, @RequestParam(name = "mobile") String mobile) {return loginFacadeService.loginByOpenId(appId, openId, mobile);}
5.首次登录小程序的用户进行绑定
// 首次登录小程序的用户记录openId,下次使用openId直接登陆@PostMapping("/bind")public Result<Authority> bind(@RequestBody @Valid WeChatBind weChatBind) {return loginFacadeService.bindOpenId(weChatBind.getAppId(), weChatBind.getMobile(), weChatBind.getPswd(), weChatBind.getOpenId());}
Java 微信小程序登录相关推荐
- 微信小程序登录 + 基于token的身份验证
官方时序图如下: https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html 图里其实说的很清 ...
- java后端微信小程序登录与注册
java后端微信小程序登录与注册&微信登录授权 分析: 微信小程序用户表 的字段来源于微信服务器 , 必须想办法去获取到对应的用户信息 找到微信开放平台: 微信开放平台 以下是微信开放平台给出 ...
- 微信小程序登录Java后台接口
微信小程序登录java后台接口 首先看一下微信小程序的开发文档: 微信小程序开发文档 步骤: 小程序端向微信接口服务发送请求--wx.login():获取到登录临时凭证code 小程序端拿着获取到的c ...
- 不带框架的java后端实现微信小程序登录(三层)
前端参考[PHP做后端实现微信小程序登录](https://blog.csdn.net/weixin_44735933/article/details/106453785) LoginServlet ...
- java版微信小程序登录商城源码Spring Cloud+Redis+MQ+VR全景+b2b2c多商家入驻前后端分离商城源码
@源码地址来源: https://minglisoft.cn/honghu2/business.html 微信小程序登录代码: /*** Copyright © 2012-2017 <a hre ...
- java版微信小程序登录商城源码Spring Cloud+Spring Boot+mybatis+security+uniapp+Redis+MQ+VR全景+b2b2c多商家入驻前后端分离商城源码
@源码地址来源: https://minglisoft.cn/honghu2/business.html 微信小程序登录代码: /*** Copyright © 2012-2017 <a hre ...
- java版微信小程序登录商城源码MQ+VR全景+b2b2c多商家入驻前后端分离商城源码
@源码地址来源: https://minglisoft.cn/honghu2/business.html 微信小程序登录代码: /*** Copyright © 2012-2017 <a hre ...
- Java - 微信小程序授权手机号登录
最近做了一个关于商城的项目,B端选用若依的开源框架,C端还是vue前后端分离.其中C端主要是小程序的形式,所以想着来总结一下对接微信小程序登录中Java部分遇到的坑即代码分享! 废话不多说,直接上代码 ...
- java微信小程序接口openid过期_Java微信小程序登录接口获取openid
根据官方文档,wx.login()的回调函数中,需要我们传递生成的用户登录凭证到code2accessToken的接口中 小程序登录方法 code2accessToken的方法中要求传入如下参数 co ...
最新文章
- OpenVAS漏洞扫描基础教程之创建用户
- 介绍如何安装project 2016方法
- Mysql事务处理问题
- C/C++ 日期 时间 time_t与struct tm转换 收藏
- 下载并使用阿里图标库
- 程序员的思考--终于确定了自己的技术发展方向
- ModelCoder国产化解决方案已逐步代替国外软件Matlab/Simulink
- SQL Server编程(01)流程控制
- MineCraft note
- Python爬虫-博客and贴吧
- 2020 智能零售领域最具商业合作价值企业盘点
- MySql大批量生成测试数据
- 猪场超级管家 v4.0 免费
- word文档字不靠边_word怎么调整单元格文字边距表格文字紧靠边框怎么办
- 安卓App自启动,两种不同的方式!!!支持到安卓4.4
- 正交设计 python算法_人人都可以掌握的正交试验设计测试用例方法
- 客户关系维护注意事项 如何维护客户关系
- 计算机自考免考课程,自考能免考哪些课程?
- 001 变量与数据类型
- CHIL-ORACLE-修改密码