CommonController.java
10.2 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
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 com.sincere.student.utils.HttpClientUtils;
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());
String token = getToken();
String js=Get("https://api.weixin.qq.com/cgi-bin/ticket/getticket","type=jsapi&access_token="+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 static String getToken() {
return HttpClientUtils.httpGet2("http://121.40.30.78:9903/getToken");
}
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 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"));
token.setExpires_in(new Integer(json.getString("expires_in")));
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;
}
}