1 package com.shanxilgong.linxi.controllers;
2
3 import com.alibaba.fastjson.JSON;
4 import com.alibaba.fastjson.JSONObject;
5 import com.shanxilgong.linxi.listener.MyTimerTask;
6 import org.apache.catalina.servlet4preview.http.HttpServletRequest;
7 import org.apache.http.HttpEntity;
8 import org.apache.http.HttpResponse;
9 import org.apache.http.client.HttpClient;
10 import org.apache.http.client.methods.HttpGet;
11 import org.apache.http.impl.client.DefaultHttpClient;
12 import org.apache.http.util.EntityUtils;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 import org.springframework.beans.factory.annotation.Value;
16 import org.springframework.web.bind.annotation.RequestMapping;
17 import org.springframework.web.bind.annotation.RequestMethod;
18 import org.springframework.web.bind.annotation.RequestParam;
19 import org.springframework.web.bind.annotation.RestController;
20
21 import javax.servlet.http.HttpServletResponse;
22 import javax.servlet.http.HttpSession;
23 import java.io.IOException;
24 import java.util.Map;
25
26 import static com.shanxilgong.linxi.tokenManager.TokenManager.tokenManager;
27
28 /**
29 * 该接口用于换取openId,进行用户状态维护
30 * 1、在进行参数绑定时,默认是必填参数。
31 * 2、微信小程序侧在传递参数时,使用Get方法,
32 * 将wx.login()获取到的临时凭证拼接值url后面
33 *
34 * 小程序当前实现逻辑:
35 * 小程序页签每次进入到“我的”页签都会自动登录。
36 *
37 * 返回值: {"session_key":"JHdZspvydvulDWF8BnfUew==","expires_in":7200,"openid":"oGo8D0RMEW4KK55RMOpQR2XaJQA8"}
38 * 过期时间,单位为秒,1小时=1*60*60 过期时间是2小时
39 */
40 @RestController
41 @RequestMapping(value = {"/login"},method = RequestMethod.GET)
42 public class LoginGetInfo
43 {
44 // Spring boot 里面本身已经集成了 log4j 的jar相关
45 private static Logger logger = LoggerFactory.getLogger(LoginGetInfo.class);
46
47 @Value("${appid}")
48 private String appid;
49
50 @Value("${secret}")
51 private String secret;
52
53 private static final String URL =
54 "https://api.weixin.qq.com/sns/jscode2session?" +
55 "appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
56
57 @RequestMapping(value = {"/getInfo"})
58 public JSONObject getInfo(@RequestParam(value = "data") String js_code,
59 HttpServletRequest request, HttpServletResponse response)
60 {
61
62 JSONObject result = new JSONObject();
63 HttpClient httpClient=new DefaultHttpClient();
64 HttpGet httpGet = new HttpGet(
65 URL.replace("APPID",appid)
66 .replace("SECRET",secret)
67 .replace("JSCODE",js_code));
68 try
69 {
70 HttpResponse resp = httpClient.execute(httpGet);
71 if(200 != resp.getStatusLine().getStatusCode())
72 {
73 logger.error("execute request of get is error.");
74 result.put("code","-1");
75 result.put("msg","error");
76 return result;
77 }
78 HttpEntity entity=resp.getEntity();
79 String msg= EntityUtils.toString(entity,"utf-8");
80 JSONObject json = JSON.parseObject(msg);
81 String openId = json.getString("openid"); // 拿到当前用户在微信服务器的唯一凭证
82
83 // 在token缓存中去查看当前用户的凭证是否已经存在
84 if (!tokenManager.containsKey(openId))
85 {
86 // 添加用户凭证
87 tokenManager.put(openId,true);
88 }
89 }
90 catch (IOException e)
91 {
92 logger.error("execute request of get is error.");
93 result.put("code","-1");
94 result.put("msg","error");
95 return result;
96 }
97 result.put("code","0");
98 result.put("msg","success");
99 return result;
100 }
101 }