尝试-尝试实现扫二维码登陆

TODO

方式一

生成二维码

要想二维码唯一,用于生成二维码的

长地址转短地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.util.LinkedList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/***
* 短链接转换工具类
*
* @author Administrator
*
*/
public class ShortUrlHelper {

public static CloseableHttpClient httpClient;

static {
httpClient = HttpClients.createDefault();
}


/**
* 将长链接转为短链接(调用的新浪的短网址API)
*
* @param url
* 需要转换的长链接url
* @return 返回转换后的短链接
*/
public static String convertSinaShortUrl(String url) {
try {
// 调用新浪API
HttpPost post = new HttpPost("http://api.t.sina.com.cn/short_url/shorten.json");
List<NameValuePair> params = new LinkedList<NameValuePair>();
// 必要的url长链接参数
params.add(new BasicNameValuePair("url_long", url));
// 必要的新浪key
params.add(new BasicNameValuePair("source", "3271760578"));
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
CloseableHttpResponse response = httpClient.execute(post);
// 得到调用新浪API后成功返回的json字符串
// url_short : 短链接地址 type:类型 url_long:原始长链接地址
String json = EntityUtils.toString(response.getEntity(), "utf-8");
JSONArray jsonArray = JSONArray.parseArray(json);
JSONObject object = (JSONObject) jsonArray.get(0);
return object.getString("url_short");
} catch (Exception e) {
e.printStackTrace();
return "";
}

}

/**
* 将长链接转为短链接(调用的百度短网址API)
*
* @param url
* 需要转换的长链接url
* @return 返回转换后的短链接
*/
public static String convertBaiDuShortUrl(String url) {
try {
// 调用百度API
HttpPost post = new HttpPost("http://www.dwz.cn/create.php");
List<NameValuePair> params = new LinkedList<NameValuePair>();
// 必要的url长链接参数
params.add(new BasicNameValuePair("url", url));
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
CloseableHttpResponse response = httpClient.execute(post);
// 得到调用百度API后成功返回的json字符串
// tinyurl : 短链接地址 status:0 表示转换成功 非0表示转换失败 longurl:原始长链接地址 err_msg:错误信息
String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");
JSONObject object = JSON.parseObject(jsonStr);
return object.getString("tinyurl");
} catch (Exception e) {
e.printStackTrace();
return "";
}

}

/**
* 测试
* @param args
*/
public static void main(String []args){
String tinyurl = convertBaiDuShortUrl("http://news.sina.com.cn/gov/xlxw/2018-09-05/doc-ihiixyeu3395739.shtml");
System.out.println(tinyurl);
}
}

轮询

判断二维码状态(服务器请求压力)

  • 未扫描
  • 扫描成功
  • 过时刷新

扫码

uid绑定用户(额外库)
稍后轮询会返回扫描成功

点击登陆

方式二

生成二维码

扫码

通知后台进行登陆

推送

  • 未扫码,不推送
  • 扫码成功,用户信息页

点击登陆