小程序开发--登录

登录流程

  技术一般水平有限,有什么错的地方,望大家指正。

  小程序的热度散的快差不多了,记录一下自己的开发中的坎坷。

  登录的照着官方的流程来即可:

  首先创建一个请求方法来实现自己的服务器和微信服务器的一个通信:

public static String GET(String url){
        String result = "";
        BufferedReader in = null;
        try {
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();
            conn.connect();
            Map<String, List<String>> map = conn.getHeaderFields();
            in = new BufferedReader(new InputStreamReader(
                    conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            
        }finally{
            try {
                if(in!=null){
                    in.close();
                }
            } catch (Exception e2) {
                //log中记录
            }
        }
        return result;
    }

   然后构建请求的url(把红圈变出的属性改成自己对应的数据):

  通过GET()方法和微信服务器来通信,如果请求正确我们就能够获取到session_key和openid,把这两个值存在session中:

Jedis jedis = new Jedis("localhost");             
String openid = openid;
String session_key = session_key;
String uid = UUID.randomUUID().toString();
StringBuffer sb = new StringBuffer();
sb.append(openid);
sb.append(","+session_key);
jedis.set(uid, sb.toString());

  把uid返给客户端,以后客户端的每一次请求都带上uid。

问题处理

  在处理过程中如果需要获取登录用户的用户名和头像,如果用户名有中文就会出现乱码,解决方法如下:

String nickNameDecode = new String(nickName.getBytes("ISO-8859-1"),"utf-8");
原文地址:https://www.cnblogs.com/shinhwazt/p/6437259.html