微信小程序登录

这段时间,一直闲,断断续续写了差不多10多篇博客了。今天下午研究了下微信小程序登录,以前没有做过这方面的,趁着有资源,玩一下小程序。

第一步: 去看微信开发文档接口,这个比什么资料都强,因为你是要调他们的接口。

第二步: 写Http请求工具类。

第三步: 配置账号AppID,AppSecret,与微信请求路径,也就是微信官方登录的请求路径,后面代码我会写上。

第四步: 接口,实现层,Controller写好。

第五步: 测试,联调。

其实调接口蛮简单的,只要你看懂了官方文档,无非是传参,业务处理,和异常处理。我就不啰嗦了,直接写代码把。

HttpClientUtil 请求工具类

 /**
  * dapeng
  */
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,"utf-8");
                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) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return resultString;
    }

    public static String doPost(String url, Map<String, String> param,String token) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
            if(token!=null){
                httpPost.addHeader("Authorization", "QBox "+token);
            }
            // 创建参数列表
            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,"utf-8");
                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) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return resultString;
    }

    public static String doPost(String url) {
        return doPost(url, null,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);
            System.out.println(httpPost);
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return resultString;
    }

    public static String doPostJson(String url, String json,String token) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            if(token!=null){
                httpPost.setHeader("Authorization","QBox <"+token+">");
            }
            // 创建请求内容
            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);
            System.out.println(httpPost);
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return resultString;
    }
}

WxConfig 参数配置,同学们,AppID与AppSecret你要写上你们公司账号的哈!

 public static final String AppID="AppID";
    public  static final String AppSecret="AppSecret";
    public  static final String CODE_SESSION = "https://api.weixin.qq.com/sns/jscode2session";

impl实现层,接口我就不显示了,因为我实现层都写出来了。

 @Override
    public WxLoginDto wxLogin(WxLoginParam wxLoginParam) {
        Map<String, String> param = new HashMap<>();
        param.put("appid", WxConfigVar.AppID);
        param.put("secret", WxConfigVar.AppSecret);
        param.put("js_code", wxLoginParam.getWxCode());
        param.put("grant_type", "authorization_code");
        String result = HttpClientUtil.doGet(WxConfigVar.CODE_SESSION, param);
        WxUserSession session = JSON.parseObject(result, WxUserSession.class);
        try {
            //这个里面是我公司的业务层,我直接去了,免得误导大家哈。
        } catch (Exception e) {
            e.printStackTrace();
            new WxUserException(ErrorCodeEnum.EDINSERTFAIL.code(), ErrorCodeEnum.EDINSERTFAIL.msg());
        }
    	//wxLoginDto  这个是我封装的一个类,用来返回前端需要的数据,你们自己随意。
        return wxLoginDto;
    }

小程序登录就这么简单,等会去看看微信登录,好像要申请应用。到时候我申请好了,发出来给大家看看,工作接口都写好了,等着联调了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值