微信小程序登录Java后台接口(详解,附示例代码)

时间:2024-03-09 16:44:35

首先看一下官方文档

地址:微信小程序官方文档API登录接口

 

我们先对官方给的时序图进行简单的分析

1.当小程序调用wx.login()时,会获得一个code(临时登录凭证),然后我们需要用wx.request()将code发送到自己的服务器.

2.在服务器的接口中,调用登录凭证校检接口,将appid(小程序唯一标识)+appsecret(小程序的app secret)+code发送到微信接口服务.然后微信服务器会返回session_key(会话秘钥)+openid(用户的唯一标识).

3.在服务器的接口中,已经得到微信用户的唯一标识openid,已经数据传输的session_key,接下来就写业务逻辑了.

4.返回给小程序自定义登录态,小程序将它存入storage中.接下来的wx.request()的业务请求,都会携带自定义登录态.

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
在不满足UnionID下发条件的情况下,返回参数
参数 说明
openid 用户唯一标识
session_key 会话密钥
在满足UnionID下发条件的情况下,返回参数
参数 说明
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;



}

}