Commit 0d1fda3844d0c87deb9baa0f1729bd7ab0b25d6e
1 parent
d7dac5d8
Exists in
master
微信分享
Showing
8 changed files
with
440 additions
and
4 deletions
Show diff stats
src/main/java/com/sincere/student/controller/CommonController.java
1 | 1 | package com.sincere.student.controller; |
2 | 2 | |
3 | -import com.alibaba.fastjson.JSON; | |
3 | +import com.alibaba.fastjson.JSONObject; | |
4 | +import com.sincere.student.dto.BaseDto; | |
4 | 5 | import com.sincere.student.dto.Province; |
6 | +import com.sincere.student.dto.WxSign; | |
7 | +import com.sincere.student.model.AccessToken; | |
5 | 8 | import com.sincere.student.model.Area; |
9 | +import com.sincere.student.model.Token; | |
6 | 10 | import com.sincere.student.service.CommonService; |
11 | +import com.sincere.student.service.ParameterService; | |
7 | 12 | import io.swagger.annotations.ApiOperation; |
8 | 13 | import org.springframework.beans.factory.annotation.Autowired; |
9 | 14 | import org.springframework.web.bind.annotation.RequestMapping; |
10 | 15 | import org.springframework.web.bind.annotation.RequestMethod; |
11 | 16 | import org.springframework.web.bind.annotation.RestController; |
12 | 17 | |
13 | -import java.util.ArrayList; | |
14 | -import java.util.Arrays; | |
15 | -import java.util.List; | |
18 | +import java.io.*; | |
19 | +import java.net.HttpURLConnection; | |
20 | +import java.net.MalformedURLException; | |
21 | +import java.net.URL; | |
22 | +import java.net.URLConnection; | |
23 | +import java.security.MessageDigest; | |
24 | +import java.security.NoSuchAlgorithmException; | |
25 | +import java.util.*; | |
16 | 26 | |
17 | 27 | @RestController |
18 | 28 | @RequestMapping("/common") |
... | ... | @@ -21,10 +31,65 @@ public class CommonController { |
21 | 31 | @Autowired |
22 | 32 | CommonService commonService ; |
23 | 33 | |
34 | + @Autowired | |
35 | + ParameterService parameterService ; | |
36 | + | |
24 | 37 | private static List<Area> list = new ArrayList<>(); |
25 | 38 | |
26 | 39 | private static List<Province> provinces = new ArrayList<>(); |
27 | 40 | |
41 | + @RequestMapping(value = "GetWxSign",method = RequestMethod.GET) | |
42 | + @ApiOperation(value = "获取微信分享签名") | |
43 | + public BaseDto<WxSign> GetWxSign(String url){ | |
44 | + try { | |
45 | + url = java.net.URLDecoder.decode(url, "UTF-8"); | |
46 | + } catch (UnsupportedEncodingException e) { | |
47 | + e.printStackTrace(); | |
48 | + } | |
49 | + url=url.replaceAll("\\+", "%2B"); | |
50 | + BaseDto<WxSign> result=new BaseDto<>(); | |
51 | +// String r=Get("http://114.55.30.100:1111/api/UserRegisterApp/GetWxShareSign","url="+url); | |
52 | +// JSONObject jsonObject= JSONObject.parseObject(r); | |
53 | +// String data = jsonObject.getString("result"); | |
54 | +// WxSign wxSign=JSON.parseObject(data,new TypeReference<WxSign>(){}); | |
55 | +// result.setData(wxSign); | |
56 | + | |
57 | + WxSign wxSign1=new WxSign(); | |
58 | + wxSign1.setTimestamp(Long.toString(System.currentTimeMillis())); | |
59 | + wxSign1.setNoncestr(UUID.randomUUID().toString()); | |
60 | + AccessToken accessToken=parameterService.getAccessToken(); | |
61 | + Token token=new Token(); | |
62 | + if(accessToken!=null) { | |
63 | + Calendar dateOne=Calendar.getInstance(); | |
64 | + Calendar dateTwo=Calendar.getInstance(); | |
65 | + dateOne.setTime(new Date()); | |
66 | + dateTwo.setTime(accessToken.getCreateTime()); | |
67 | + long timeOne=dateOne.getTimeInMillis(); | |
68 | + long timeTwo=dateTwo.getTimeInMillis(); | |
69 | + long minute=(timeOne-timeTwo)/(1000*60); | |
70 | + | |
71 | + if(minute<60) token.setAccess_token(accessToken.getAccessToken()); | |
72 | + else{ | |
73 | + token=getAccessToken("wx6078ff3f67524996","8a0465b8ad0f000f568f48853e2818c8"); | |
74 | + accessToken.setAccessToken(token.getAccess_token()); | |
75 | + parameterService.insertAccessToken(accessToken); | |
76 | + } | |
77 | + } | |
78 | + else { | |
79 | + token=getAccessToken("wx6078ff3f67524996","8a0465b8ad0f000f568f48853e2818c8"); | |
80 | + accessToken=new AccessToken(); | |
81 | + accessToken.setAccessToken(token.getAccess_token()); | |
82 | + parameterService.insertAccessToken(accessToken); | |
83 | + } | |
84 | + String js=Get("https://api.weixin.qq.com/cgi-bin/ticket/getticket","type=jsapi&access_token="+token.getAccess_token()); | |
85 | + JSONObject jsonObject= JSONObject.parseObject(js); | |
86 | + wxSign1.setJsapi_ticket(jsonObject.getString("ticket")); | |
87 | + String rawstring = "jsapi_ticket=" + wxSign1.getJsapi_ticket() + "&noncestr=" + wxSign1.getNoncestr() + "×tamp=" + wxSign1.getTimestamp() + "&url=" + url + ""; | |
88 | + wxSign1.setSignature(SHA1(rawstring)); | |
89 | + result.setData(wxSign1); | |
90 | + return result; | |
91 | + } | |
92 | + | |
28 | 93 | @ApiOperation("省份") |
29 | 94 | @RequestMapping(value = "getProvince",method = RequestMethod.GET) |
30 | 95 | public List<Area> getProvince(){ |
... | ... | @@ -115,4 +180,185 @@ public class CommonController { |
115 | 180 | List<String> list = Arrays.asList(array); |
116 | 181 | return list ; |
117 | 182 | } |
183 | + | |
184 | + public String Get(String url, String param) { | |
185 | + String result = ""; | |
186 | + BufferedReader in = null; | |
187 | + try { | |
188 | + String urlNameString = url + "?" + param; | |
189 | + URL realUrl = new URL(urlNameString); | |
190 | + // 打开和URL之间的连接 | |
191 | + URLConnection connection = realUrl.openConnection(); | |
192 | + // 设置通用的请求属性 | |
193 | + connection.setRequestProperty("accept", "*/*"); | |
194 | + connection.setRequestProperty("connection", "Keep-Alive"); | |
195 | + connection.setRequestProperty("user-agent", | |
196 | + "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); | |
197 | + // 建立实际的连接 | |
198 | + connection.connect(); | |
199 | + // 获取所有响应头字段 | |
200 | + Map<String, List<String>> map = connection.getHeaderFields(); | |
201 | + // 遍历所有的响应头字段 | |
202 | + for (String key : map.keySet()) { | |
203 | + System.out.println(key + "--->" + map.get(key)); | |
204 | + } | |
205 | + // 定义 BufferedReader输入流来读取URL的响应 | |
206 | + in = new BufferedReader(new InputStreamReader( | |
207 | + connection.getInputStream(),"UTF-8")); | |
208 | + String line; | |
209 | + while ((line = in.readLine()) != null) { | |
210 | + result += line; | |
211 | + } | |
212 | + } catch (Exception e) { | |
213 | + System.out.println("发送GET请求出现异常!" + e); | |
214 | + e.printStackTrace(); | |
215 | + } | |
216 | + // 使用finally块来关闭输入流 | |
217 | + finally { | |
218 | + try { | |
219 | + if (in != null) { | |
220 | + in.close(); | |
221 | + } | |
222 | + } catch (Exception e2) { | |
223 | + e2.printStackTrace(); | |
224 | + } | |
225 | + } | |
226 | + return result; | |
227 | + } | |
228 | + | |
229 | + public String Post(String httpUrl, String param) | |
230 | + { | |
231 | + HttpURLConnection connection = null; | |
232 | + InputStream is = null; | |
233 | + OutputStream os = null; | |
234 | + BufferedReader br = null; | |
235 | + String result = null; | |
236 | + try { | |
237 | + URL url = new URL(httpUrl); | |
238 | + // 通过远程url连接对象打开连接 | |
239 | + connection = (HttpURLConnection) url.openConnection(); | |
240 | + // 设置连接请求方式 | |
241 | + connection.setRequestMethod("POST"); | |
242 | + // 设置连接主机服务器超时时间:15000毫秒 | |
243 | + connection.setConnectTimeout(15000); | |
244 | + // 设置读取主机服务器返回数据超时时间:60000毫秒 | |
245 | + connection.setReadTimeout(60000); | |
246 | + | |
247 | + // 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true | |
248 | + connection.setDoOutput(true); | |
249 | + // 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无 | |
250 | + connection.setDoInput(true); | |
251 | + // 设置传入参数的格式:请求参数应该是 name1=value1&name2=value2 的形式。 | |
252 | + connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); | |
253 | + | |
254 | + // 设置鉴权信息:Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0 | |
255 | + connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0"); | |
256 | + | |
257 | + // 通过连接对象获取一个输出流 | |
258 | + os = connection.getOutputStream(); | |
259 | + // 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的 | |
260 | + os.write(param.getBytes()); | |
261 | + // 通过连接对象获取一个输入流,向远程读取 | |
262 | + if (connection.getResponseCode() == 200) { | |
263 | + | |
264 | + is = connection.getInputStream(); | |
265 | + // 对输入流对象进行包装:charset根据工作项目组的要求来设置 | |
266 | + br = new BufferedReader(new InputStreamReader(is, "UTF-8")); | |
267 | + | |
268 | + StringBuffer sbf = new StringBuffer(); | |
269 | + String temp = null; | |
270 | + // 循环遍历一行一行读取数据 | |
271 | + while ((temp = br.readLine()) != null) { | |
272 | + sbf.append(temp); | |
273 | + sbf.append("\r\n"); | |
274 | + } | |
275 | + result = sbf.toString(); | |
276 | + } | |
277 | + } catch (MalformedURLException e) { | |
278 | + e.printStackTrace(); | |
279 | + } catch (IOException e) { | |
280 | + e.printStackTrace(); | |
281 | + } finally { | |
282 | + // 关闭资源 | |
283 | + if (null != br) { | |
284 | + try { | |
285 | + br.close(); | |
286 | + } catch (IOException e) { | |
287 | + e.printStackTrace(); | |
288 | + } | |
289 | + } | |
290 | + if (null != os) { | |
291 | + try { | |
292 | + os.close(); | |
293 | + } catch (IOException e) { | |
294 | + e.printStackTrace(); | |
295 | + } | |
296 | + } | |
297 | + if (null != is) { | |
298 | + try { | |
299 | + is.close(); | |
300 | + } catch (IOException e) { | |
301 | + e.printStackTrace(); | |
302 | + } | |
303 | + } | |
304 | + // 断开与远程地址url的连接 | |
305 | + connection.disconnect(); | |
306 | + } | |
307 | + return result; | |
308 | + } | |
309 | + | |
310 | + public Token getAccessToken(String appID, String appScret) { | |
311 | + Token token = new Token(); | |
312 | + String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appID + "&secret=" | |
313 | + + appScret; | |
314 | + try { | |
315 | + URL getUrl=new URL(url); | |
316 | + HttpURLConnection http=(HttpURLConnection)getUrl.openConnection(); | |
317 | + http.setRequestMethod("GET"); | |
318 | + http.setRequestProperty("Content-Type", | |
319 | + "application/x-www-form-urlencoded"); | |
320 | + http.setDoOutput(true); | |
321 | + http.setDoInput(true); | |
322 | + | |
323 | + | |
324 | + http.connect(); | |
325 | + InputStream is = http.getInputStream(); | |
326 | + int size = is.available(); | |
327 | + byte[] b = new byte[size]; | |
328 | + is.read(b); | |
329 | + | |
330 | + String message = new String(b, "UTF-8"); | |
331 | + JSONObject json = JSONObject.parseObject(message); | |
332 | + token.setAccess_token(json.getString("access_token")); | |
333 | + System.out.println(message); | |
334 | + } catch (MalformedURLException e) { | |
335 | + e.printStackTrace(); | |
336 | + } catch (IOException e) { | |
337 | + e.printStackTrace(); | |
338 | + } | |
339 | + return token; | |
340 | + } | |
341 | + | |
342 | + public static String SHA1(String str) { | |
343 | + try { | |
344 | + MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1"); //如果是SHA加密只需要将"SHA-1"改成"SHA"即可 | |
345 | + digest.update(str.getBytes()); | |
346 | + byte messageDigest[] = digest.digest(); | |
347 | + // Create Hex String | |
348 | + StringBuffer hexStr = new StringBuffer(); | |
349 | + // 字节数组转换为 十六进制 数 | |
350 | + for (int i = 0; i < messageDigest.length; i++) { | |
351 | + String shaHex = Integer.toHexString(messageDigest[i] & 0xFF); | |
352 | + if (shaHex.length() < 2) { | |
353 | + hexStr.append(0); | |
354 | + } | |
355 | + hexStr.append(shaHex); | |
356 | + } | |
357 | + return hexStr.toString(); | |
358 | + | |
359 | + } catch (NoSuchAlgorithmException e) { | |
360 | + e.printStackTrace(); | |
361 | + } | |
362 | + return null; | |
363 | + } | |
118 | 364 | } | ... | ... |
... | ... | @@ -0,0 +1,68 @@ |
1 | +package com.sincere.student.dto; | |
2 | + | |
3 | +import io.swagger.annotations.ApiModelProperty; | |
4 | + | |
5 | +public class WxSign { | |
6 | + @ApiModelProperty(value = "时间戳") | |
7 | + private String timestamp; | |
8 | + | |
9 | + @ApiModelProperty(value = "随机码") | |
10 | + private String noncestr; | |
11 | + | |
12 | + @ApiModelProperty(value = "签名") | |
13 | + private String signature; | |
14 | + | |
15 | + private String jsapi_ticket; | |
16 | + | |
17 | + private String sign_before; | |
18 | + | |
19 | + private String basictoken; | |
20 | + | |
21 | + public String getTimestamp() { | |
22 | + return timestamp; | |
23 | + } | |
24 | + | |
25 | + public void setTimestamp(String timestamp) { | |
26 | + this.timestamp = timestamp; | |
27 | + } | |
28 | + | |
29 | + public String getNoncestr() { | |
30 | + return noncestr; | |
31 | + } | |
32 | + | |
33 | + public void setNoncestr(String noncestr) { | |
34 | + this.noncestr = noncestr; | |
35 | + } | |
36 | + | |
37 | + public String getSignature() { | |
38 | + return signature; | |
39 | + } | |
40 | + | |
41 | + public void setSignature(String signature) { | |
42 | + this.signature = signature; | |
43 | + } | |
44 | + | |
45 | + public String getJsapi_ticket() { | |
46 | + return jsapi_ticket; | |
47 | + } | |
48 | + | |
49 | + public void setJsapi_ticket(String jsapi_ticket) { | |
50 | + this.jsapi_ticket = jsapi_ticket; | |
51 | + } | |
52 | + | |
53 | + public String getSign_before() { | |
54 | + return sign_before; | |
55 | + } | |
56 | + | |
57 | + public void setSign_before(String sign_before) { | |
58 | + this.sign_before = sign_before; | |
59 | + } | |
60 | + | |
61 | + public String getBasictoken() { | |
62 | + return basictoken; | |
63 | + } | |
64 | + | |
65 | + public void setBasictoken(String basictoken) { | |
66 | + this.basictoken = basictoken; | |
67 | + } | |
68 | +} | ... | ... |
src/main/java/com/sincere/student/mapper/ParameterMapper.java
1 | 1 | package com.sincere.student.mapper; |
2 | 2 | |
3 | +import com.sincere.student.model.AccessToken; | |
3 | 4 | import com.sincere.student.model.Parameter; |
4 | 5 | |
5 | 6 | import java.util.List; |
... | ... | @@ -11,4 +12,9 @@ public interface ParameterMapper { |
11 | 12 | int create(Parameter parameter); |
12 | 13 | |
13 | 14 | int update(Parameter parameter); |
15 | + | |
16 | + | |
17 | + int insertAccessToken(AccessToken accessToken); | |
18 | + | |
19 | + AccessToken getAccessToken(); | |
14 | 20 | } | ... | ... |
src/main/java/com/sincere/student/model/AccessToken.java
0 → 100644
... | ... | @@ -0,0 +1,35 @@ |
1 | +package com.sincere.student.model; | |
2 | + | |
3 | +import java.util.Date; | |
4 | + | |
5 | +public class AccessToken { | |
6 | + private Integer id; | |
7 | + | |
8 | + private String accessToken; | |
9 | + | |
10 | + private Date createTime; | |
11 | + | |
12 | + public Integer getId() { | |
13 | + return id; | |
14 | + } | |
15 | + | |
16 | + public void setId(Integer id) { | |
17 | + this.id = id; | |
18 | + } | |
19 | + | |
20 | + public String getAccessToken() { | |
21 | + return accessToken; | |
22 | + } | |
23 | + | |
24 | + public void setAccessToken(String accessToken) { | |
25 | + this.accessToken = accessToken; | |
26 | + } | |
27 | + | |
28 | + public Date getCreateTime() { | |
29 | + return createTime; | |
30 | + } | |
31 | + | |
32 | + public void setCreateTime(Date createTime) { | |
33 | + this.createTime = createTime; | |
34 | + } | |
35 | +} | ... | ... |
... | ... | @@ -0,0 +1,53 @@ |
1 | +package com.sincere.student.model; | |
2 | + | |
3 | +public class Token { | |
4 | + public String access_token; | |
5 | + | |
6 | + public Integer expires_in; | |
7 | + | |
8 | + public String refresh_token; | |
9 | + | |
10 | + public String openid; | |
11 | + | |
12 | + public String scope; | |
13 | + | |
14 | + public String getAccess_token() { | |
15 | + return access_token; | |
16 | + } | |
17 | + | |
18 | + public void setAccess_token(String access_token) { | |
19 | + this.access_token = access_token; | |
20 | + } | |
21 | + | |
22 | + public Integer getExpires_in() { | |
23 | + return expires_in; | |
24 | + } | |
25 | + | |
26 | + public void setExpires_in(Integer expires_in) { | |
27 | + this.expires_in = expires_in; | |
28 | + } | |
29 | + | |
30 | + public String getRefresh_token() { | |
31 | + return refresh_token; | |
32 | + } | |
33 | + | |
34 | + public void setRefresh_token(String refresh_token) { | |
35 | + this.refresh_token = refresh_token; | |
36 | + } | |
37 | + | |
38 | + public String getOpenid() { | |
39 | + return openid; | |
40 | + } | |
41 | + | |
42 | + public void setOpenid(String openid) { | |
43 | + this.openid = openid; | |
44 | + } | |
45 | + | |
46 | + public String getScope() { | |
47 | + return scope; | |
48 | + } | |
49 | + | |
50 | + public void setScope(String scope) { | |
51 | + this.scope = scope; | |
52 | + } | |
53 | +} | ... | ... |
src/main/java/com/sincere/student/service/ParameterService.java
1 | 1 | package com.sincere.student.service; |
2 | 2 | |
3 | +import com.sincere.student.model.AccessToken; | |
3 | 4 | import com.sincere.student.model.Parameter; |
4 | 5 | |
5 | 6 | public interface ParameterService { |
... | ... | @@ -9,4 +10,8 @@ public interface ParameterService { |
9 | 10 | int create(Parameter parameter); |
10 | 11 | |
11 | 12 | int update(Parameter parameter); |
13 | + | |
14 | + int insertAccessToken(AccessToken accessToken); | |
15 | + | |
16 | + AccessToken getAccessToken(); | |
12 | 17 | } | ... | ... |
src/main/java/com/sincere/student/service/impl/ParameterServiceImpl.java
1 | 1 | package com.sincere.student.service.impl; |
2 | 2 | |
3 | 3 | import com.sincere.student.mapper.ParameterMapper; |
4 | +import com.sincere.student.model.AccessToken; | |
4 | 5 | import com.sincere.student.model.Parameter; |
5 | 6 | import com.sincere.student.service.ParameterService; |
6 | 7 | import org.springframework.beans.factory.annotation.Autowired; |
... | ... | @@ -32,4 +33,11 @@ public class ParameterServiceImpl implements ParameterService { |
32 | 33 | public int update(Parameter parameter) { |
33 | 34 | return parameterMapper.update(parameter); |
34 | 35 | } |
36 | + | |
37 | + @Override | |
38 | + public int insertAccessToken(AccessToken accessToken){return parameterMapper.insertAccessToken(accessToken);} | |
39 | + | |
40 | + @Override | |
41 | + public AccessToken getAccessToken(){return parameterMapper.getAccessToken();} | |
42 | + | |
35 | 43 | } | ... | ... |
src/main/resources/mapper/ParameterMapper.xml
... | ... | @@ -20,4 +20,19 @@ |
20 | 20 | where code = #{code} |
21 | 21 | </update> |
22 | 22 | |
23 | + <resultMap id="AccessTokenMap" type="com.sincere.student.model.AccessToken"> | |
24 | + <id column="id" jdbcType="INTEGER" property="id" /> | |
25 | + <result column="access_token" jdbcType="VARCHAR" property="accessToken" /> | |
26 | + <result column="create_time" jdbcType="TIMESTAMP" property="createTime" /> | |
27 | + </resultMap> | |
28 | + | |
29 | + <insert id="insertAccessToken" parameterType="com.sincere.student.model.AccessToken"> | |
30 | + insert into access_token (access_token) | |
31 | + values (#{accessToken,jdbcType=VARCHAR} | |
32 | + ) | |
33 | + </insert> | |
34 | + | |
35 | + <select id="getAccessToken" resultMap="AccessTokenMap"> | |
36 | + select top 1.* from access_token order by create_time desc | |
37 | + </select> | |
23 | 38 | </mapper> | ... | ... |