CommonController.java
14.3 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package com.sincere.student.controller;
import com.alibaba.fastjson.JSONObject;
import com.sincere.student.dto.BaseDto;
import com.sincere.student.dto.Province;
import com.sincere.student.dto.WxSign;
import com.sincere.student.model.AccessToken;
import com.sincere.student.model.Area;
import com.sincere.student.model.Token;
import com.sincere.student.service.CommonService;
import com.sincere.student.service.ParameterService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
@RestController
@RequestMapping("/common")
public class CommonController {
@Autowired
CommonService commonService ;
@Autowired
ParameterService parameterService ;
private static List<Area> list = new ArrayList<>();
private static List<Province> provinces = new ArrayList<>();
@RequestMapping(value = "GetWxSign",method = RequestMethod.GET)
@ApiOperation(value = "获取微信分享签名")
public BaseDto<WxSign> GetWxSign(String url){
try {
url = java.net.URLDecoder.decode(url, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
url=url.replaceAll("\\+", "%2B");
BaseDto<WxSign> result=new BaseDto<>();
// String r=Get("http://114.55.30.100:1111/api/UserRegisterApp/GetWxShareSign","url="+url);
// JSONObject jsonObject= JSONObject.parseObject(r);
// String data = jsonObject.getString("result");
// WxSign wxSign=JSON.parseObject(data,new TypeReference<WxSign>(){});
// result.setData(wxSign);
WxSign wxSign1=new WxSign();
wxSign1.setTimestamp(Long.toString(System.currentTimeMillis()));
wxSign1.setNoncestr(UUID.randomUUID().toString());
AccessToken accessToken=parameterService.getAccessToken();
Token token=new Token();
if(accessToken!=null) {
Calendar dateOne=Calendar.getInstance();
Calendar dateTwo=Calendar.getInstance();
dateOne.setTime(new Date());
dateTwo.setTime(accessToken.getCreateTime());
long timeOne=dateOne.getTimeInMillis();
long timeTwo=dateTwo.getTimeInMillis();
long minute=(timeOne-timeTwo)/(1000*60);
if(minute<60) token.setAccess_token(accessToken.getAccessToken());
else{
token=getAccessToken("wx6078ff3f67524996","8a0465b8ad0f000f568f48853e2818c8");
accessToken.setAccessToken(token.getAccess_token());
parameterService.insertAccessToken(accessToken);
}
}
else {
token=getAccessToken("wx6078ff3f67524996","8a0465b8ad0f000f568f48853e2818c8");
accessToken=new AccessToken();
accessToken.setAccessToken(token.getAccess_token());
parameterService.insertAccessToken(accessToken);
}
String js=Get("https://api.weixin.qq.com/cgi-bin/ticket/getticket","type=jsapi&access_token="+token.getAccess_token());
JSONObject jsonObject= JSONObject.parseObject(js);
wxSign1.setJsapi_ticket(jsonObject.getString("ticket"));
String rawstring = "jsapi_ticket=" + wxSign1.getJsapi_ticket() + "&noncestr=" + wxSign1.getNoncestr() + "×tamp=" + wxSign1.getTimestamp() + "&url=" + url + "";
wxSign1.setSignature(SHA1(rawstring));
result.setData(wxSign1);
return result;
}
@ApiOperation("省份")
@RequestMapping(value = "getProvince",method = RequestMethod.GET)
public List<Area> getProvince(){
if(list.size() == 0){
List<Area> areas = commonService.getProvince();
for(Area area : areas){
List<Area> cityList = commonService.getCity(area.getCode());
if(area.getName().contains("市")){
//获取全部
area.setList(cityList);
}else {
//获取4位的市
List<Area> trueCityList = new ArrayList<>();
for(Area city : cityList){
if(city.getCode().length() < 6){
trueCityList.add(city);
}
}
area.setList(trueCityList);
}
}
list = areas ;
}
return list ;
}
@ApiOperation("省份")
@RequestMapping(value = "getProvince2",method = RequestMethod.GET)
public List<Province> getProvince2(){
if(provinces.size() == 0){
if(list.size() == 0){
List<Area> areas = commonService.getProvince();
for(Area area : areas){
List<Area> cityList = commonService.getCity(area.getCode());
if(area.getName().contains("市")){
//获取全部
area.setList(cityList);
}else {
//获取4位的市
List<Area> trueCityList = new ArrayList<>();
for(Area city : cityList){
if(city.getCode().length() < 6){
trueCityList.add(city);
}
}
area.setList(trueCityList);
}
}
list = areas ;
}
for(Area area : list){
Province province = new Province() ;
province.setValue(area.getName());
province.setLabel(area.getName());
List<Province> cityList = new ArrayList<>();
for(Area city : area.getList()){
Province newCity = new Province();
newCity.setLabel(city.getName());
newCity.setValue(city.getName());
cityList.add(newCity);
}
province.setChildren(cityList);
provinces.add(province);
}
}
return provinces ;
}
@ApiOperation("市")
@RequestMapping(value = "getCity",method = RequestMethod.GET)
public List<Area> getCity(String code){
return commonService.getCity(code);
}
@ApiOperation("高校类型")
@RequestMapping(value = "getUniversityType",method = RequestMethod.GET)
public List<String> getUniversityType(){
String[] array = new String[]{"综合类","理工类","师范类","农林类","政法类","医药类"
,"财经类","民族类","语言类","艺术类","体育类","军事类","旅游类"};
List<String> list = Arrays.asList(array);
return list ;
}
@ApiOperation("高校教学层次")
@RequestMapping(value = "getUniversityLevel",method = RequestMethod.GET)
public List<String> getUniversityLevel(){
String[] array = new String[]{"本科","高职"};
List<String> list = Arrays.asList(array);
return list ;
}
public String Get(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
public String Post(String httpUrl, String param)
{
HttpURLConnection connection = null;
InputStream is = null;
OutputStream os = null;
BufferedReader br = null;
String result = null;
try {
URL url = new URL(httpUrl);
// 通过远程url连接对象打开连接
connection = (HttpURLConnection) url.openConnection();
// 设置连接请求方式
connection.setRequestMethod("POST");
// 设置连接主机服务器超时时间:15000毫秒
connection.setConnectTimeout(15000);
// 设置读取主机服务器返回数据超时时间:60000毫秒
connection.setReadTimeout(60000);
// 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
connection.setDoOutput(true);
// 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
connection.setDoInput(true);
// 设置传入参数的格式:请求参数应该是 name1=value1&name2=value2 的形式。
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// 设置鉴权信息:Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0
connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
// 通过连接对象获取一个输出流
os = connection.getOutputStream();
// 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的
os.write(param.getBytes());
// 通过连接对象获取一个输入流,向远程读取
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
// 对输入流对象进行包装:charset根据工作项目组的要求来设置
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuffer sbf = new StringBuffer();
String temp = null;
// 循环遍历一行一行读取数据
while ((temp = br.readLine()) != null) {
sbf.append(temp);
sbf.append("\r\n");
}
result = sbf.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭资源
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != os) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 断开与远程地址url的连接
connection.disconnect();
}
return result;
}
public Token getAccessToken(String appID, String appScret) {
Token token = new Token();
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appID + "&secret="
+ appScret;
try {
URL getUrl=new URL(url);
HttpURLConnection http=(HttpURLConnection)getUrl.openConnection();
http.setRequestMethod("GET");
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
http.connect();
InputStream is = http.getInputStream();
int size = is.available();
byte[] b = new byte[size];
is.read(b);
String message = new String(b, "UTF-8");
JSONObject json = JSONObject.parseObject(message);
token.setAccess_token(json.getString("access_token"));
System.out.println(message);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return token;
}
public static String SHA1(String str) {
try {
MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1"); //如果是SHA加密只需要将"SHA-1"改成"SHA"即可
digest.update(str.getBytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexStr = new StringBuffer();
// 字节数组转换为 十六进制 数
for (int i = 0; i < messageDigest.length; i++) {
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
if (shaHex.length() < 2) {
hexStr.append(0);
}
hexStr.append(shaHex);
}
return hexStr.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
}