首先看一下官方文档html
地址:微信小程序官方文档API登陆接口java
咱们先对官方给的时序图进行简单的分析web
1.当小程序调用wx.login()时,会得到一个code(临时登陆凭证),而后咱们须要用wx.request()将code发送到本身的服务器.spring
2.在服务器的接口中,调用登陆凭证校检接口,将appid(小程序惟一标识)+appsecret(小程序的app secret)+code发送到微信接口服务.而后微信服务器会返回session_key(会话秘钥)+openid(用户的惟一标识).小程序
3.在服务器的接口中,已经获得微信用户的惟一标识openid,已经数据传输的session_key,接下来就写业务逻辑了.微信小程序
4.返回给小程序自定义登陆态,小程序将它存入storage中.接下来的wx.request()的业务请求,都会携带自定义登陆态.api
5.在服务器的接口中经过自定义登陆态查询openid和session_key,而后返回业务数据.服务器
划一下重点微信
在服务器的接口中,须要进行一个http请求,将从小程序得到的code和接口中存储的appid和secret发送给微信接口服务,而后就能够得到session_key和openid.网络
接口地址
https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
参数 | 必填 | 说明 |
---|---|---|
appid | 是 | 小程序惟一标识 |
secret | 是 | 小程序的 app secret |
js_code | 是 | 登陆时获取的 code |
grant_type | 是 | 填写为 authorization_code |
参数 | 说明 |
---|---|
openid | 用户惟一标识 |
session_key | 会话密钥 |
参数 | 说明 |
---|---|
openid | 用户惟一标识 |
session_key | 会话密钥 |
unionid | 用户在开放平台的惟一标识符 |
//正常返回的JSON数据包 { "openid": "OPENID", "session_key": "SESSIONKEY", } //知足UnionID返回条件时,返回的JSON数据包 { "openid": "OPENID", "session_key": "SESSIONKEY", "unionid": "UNIONID" } //错误时返回JSON数据包(示例为Code无效) { "errcode": 40029, "errmsg": "invalid code" }
小程序登陆示例代码
//app.js App({ onLaunch: function() { wx.login({ success: function(res) { if (res.code) { //发起网络请求 wx.request({ url: 'https://test.com/onLogin', data: { code: res.code } }) } else { console.log('登陆失败!' + res.errMsg) } } }); } })
Java后台接口示例代码
package com.xx.action; import java.util.Map; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import com.google.gson.Gson; import com.hp.bean.WeChatAppLoginReq; import com.hp.bean.WeChatSession; import com.opensymphony.xwork2.ActionSupport; public class WeChatLogin extends ActionSupport{ /** *author 李俊标 *2018-4-19 */ private static final long serialVersionUID = 1L; private static final String APPID = "wx9xxxxxxxxxxx9b4"; private static final String SECRET = "685742***************84xs859"; private String code; public String getCode() { return code; } public void setCode(String code) { this.code = code; } //获取凭证校检接口 public String login() { //微信的接口 String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+APPID+ "&secret="+SECRET+"&js_code="+ code +"&grant_type=authorization_code"; RestTemplate restTemplate = new RestTemplate(); //进行网络请求,访问url接口 ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, String.class); //根据返回值进行后续操做 if(responseEntity != null && responseEntity.getStatusCode() == HttpStatus.OK) { String sessionData = responseEntity.getBody(); Gson gson = new Gson(); //解析从微信服务器得到的openid和session_key; WeChatSession weChatSession = gson.fromJson(sessionData,WeChatSession.class); //获取用户的惟一标识 String openid = weChatSession.getOpenid(); //获取会话秘钥 String session_key = weChatSession.getSession_key(); //下面就能够写本身的业务代码了 //最后要返回一个自定义的登陆态,用来作后续数据传输的验证 } return null; } }