天天看點

小程式開發--登入

登入流程

  技術一般水準有限,有什麼錯的地方,望大家指正。

  小程式的熱度散的快差不多了,記錄一下自己的開發中的坎坷。

  登入的照着官方的流程來即可:

小程式開發--登入

  首先建立一個請求方法來實作自己的伺服器和微信伺服器的一個通信:

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");