微信小程序实现微信登录详解(JAVA后台)

在开发之前我们先看一下官方提供的时序图,了解一下我们的大致开发流程:

大致了解流程之后,我们便可以着手开发了。

1. 前提

2. 开发流程

从时序图我们可以了解到流程大致分为两步:

2.1 小程序端

2.2 Java后端接口

后端的流程我将其大致分为如下几点:

获取openId

请求的地址:

通过GET方式访问,其中的参数分别是:

    appid:小程序的appid secret:小程序的appsecret js:小程序前端传来的code grant_type:这个不用修改,表示授权的类型

请求工具类代码如下:(APPID自行替换)

HTTP工具类如下:

需要添加相关依赖。

<!-- http请求工具包依赖 -->
<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpclient</artifactId>
	<version>4.5.2</version>
</dependency>
public class HttpClientUtil {
          
   

    public static String doGet(String url, Map<String, String> param) {
          
   

        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();

        String resultString = "";
        CloseableHttpResponse response = null;
        try {
          
   
            // 创建uri
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
          
   
                for (String key : param.keySet()) {
          
   
                    builder.addParameter(key, param.get(key));
                }
            }
            URI uri = builder.build();

            // 创建http GET请求
            HttpGet httpGet = new HttpGet(uri);

            // 执行请求
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
          
   
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
          
   
            e.printStackTrace();
        } finally {
          
   
            try {
          
   
                if (response != null) {
          
   
                    response.close();
                }
                httpclient.close();
            } catch (IOException e) {
          
   
                e.printStackTrace();
            }
        }
        return resultString;
    }

    public static String doGet(String url) {
          
   
        return doGet(url, null);
    }

    public static String doPost(String url, Map<String, String> param) {
          
   
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
          
   
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            // 创建参数列表
            if (param != null) {
          
   
                List<NameValuePair> paramList = new ArrayList<>();
                for (String key : param.keySet()) {
          
   
                    paramList.add(new BasicNameValuePair(key, param.get(key)));
                }
                // 模拟表单
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
                httpPost.setEntity(entity);
            }
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
          
   
            e.printStackTrace();
        } finally {
          
   
            try {
          
   
                response.close();
            } catch (IOException e) {
          
   
                e.printStackTrace();
            }
        }

        return resultString;
    }

    public static String doPost(String url) {
          
   
        return doPost(url, null);
    }

    public static String doPostJson(String url, String json) {
          
   
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
          
   
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            // 创建请求内容
            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
          
   
            e.printStackTrace();
        } finally {
          
   
            try {
          
   
                response.close();
            } catch (IOException e) {
          
   
                e.printStackTrace();
            }
        }

        return resultString;
    }
}

接口代码

具体代码如下所示:

判断用户是否存在后的代码根据自己的业务逻辑进行修改即可。

经验分享 程序员 微信小程序 职场和发展