hz-zhhq-app-service/greenH5modul/.svn/pristine/52/525d1457cf54e94418bebf78736...

172 lines
6.6 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.nationalelectric.greenH5;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import com.aostarit.sgid.agent.EncryptHelper;
import com.nationalelectirc.Constant.Constant;
import com.nationalelectirc.utils.RestResult;
import com.nationalelectric.greenH5.utils.HttpClientUtil;
/**
* <b>概述</b><br>
*
* <p>
* <b>功能</b><br>
*
*
* @author chenweikang
*/
@Controller
@RequestMapping("/singleLogin")
public class SingleLoginController extends GreenBaseController {
/**
* 获取用户信息
*
* @param requestBody
* @return
* @throws Exception
*/
@RequestMapping(value="/getUserInfoByTicket", method = RequestMethod.POST)
@ResponseBody
public RestResult getUserInfoByTicket(@RequestBody Map<String,Object> map){
String serviceTicket = map.get("ticket").toString();
try {
Properties otherPro = PropertiesLoaderUtils.loadAllProperties("config/constant.properties");
String iscbaseUrl = otherPro.getProperty("isc.client.baseUrl");
String userInfoByTicketUrl = otherPro.getProperty("isc.client.userInfoByTicketUrl");
String iscappid = otherPro.getProperty("isc.client.appid");
String service = otherPro.getProperty("isc.client.service");
if (!StringUtils.isEmpty(serviceTicket)) {
// 封装请求body
String userJson = "{\"appId\":\"" + iscappid + "\"," + "\"service\":\"" + service + "\","
+ "\"ticket\":\"" + serviceTicket + "\"}";
// 获取签名
String userSign = paramSign(userJson.replace("\\", ""));
// 获取AccessToken
String userToken = getAccessToken(); // 获取userToken
if (userToken == null) {
saveErorrLog("获取AccessToken异常", Constant.INSERT, Constant.OPERATE_FAIL, "获取AccessToken失败1", "0",
"admin"); // 如果获取的usertoken是空则提示失败并记录日志
return new RestResult(Constant.FAILED, "获取用户信息失败");
}
System.out.println("userToken = " + userToken);
System.out.println("userUrl = " + iscbaseUrl + userInfoByTicketUrl);
System.out.println("userBody = " + userJson.replace("\\", ""));
System.out.println("userSign = " + userSign);
String userInfoTicketRep = HttpClientUtil.iscSend2(iscbaseUrl + userInfoByTicketUrl,
userJson.replace("\\", ""), userSign, userToken, iscappid); // 获取用户信息
System.out.println("userInfoRep = " + userInfoTicketRep);
if (StringUtils.isEmpty(userInfoTicketRep)) {
saveErorrLog("获取用户信息异常", Constant.INSERT, Constant.OPERATE_FAIL, "获取用户信息失败1", "0", "admin");
return new RestResult(Constant.FAILED, "获取用户信息失败");
}
JSONObject responseJson = JSONObject.parseObject(userInfoTicketRep);
String code = String.valueOf(responseJson.get("code"));
if (StringUtils.isNotEmpty(code) && "100001".equals(code)) {
String data = String.valueOf(responseJson.get("data"));
if (StringUtils.isNotEmpty(data)) {
RestResult rs = new RestResult(Constant.SUCCESS, "请求成功", data);
return rs;
}
saveErorrLog("获取用户信息异常", Constant.INSERT, Constant.OPERATE_FAIL, "获取用户信息失败2", "0", "admin");
return new RestResult(Constant.FAILED, "获取用户信息失败");
}
return new RestResult(Constant.FAILED, "获取用户信息失败");
}
saveErorrLog("获取用户信息异常", Constant.INSERT, Constant.OPERATE_FAIL, "获取用户信息失败3", "0", "admin");
return new RestResult(Constant.FAILED, "获取用户信息失败");
} catch (Exception e) {
saveErorrLog("获取用户信息异常", Constant.INSERT, Constant.OPERATE_FAIL, "获取用户信息失败4", "0", "admin");
return new RestResult(Constant.FAILED, "获取用户信息失败");
}
}
public static String getNewTicket() throws Exception{
String json = "";
Properties otherPro = PropertiesLoaderUtils.loadAllProperties("config/constant.properties");
String iscappid = otherPro.getProperty("isc.client.appid");
String isclogonserviceurl = otherPro.getProperty("isc.client.service");
String clientSecret = otherPro.getProperty("isc.client.clientSecret");
String sign = otherPro.getProperty("isc.client.sigKey");
json = "{\"iscappid\":\"" + iscappid + "\",\"isclogonserviceurl\":\"" + isclogonserviceurl + "\",\"clientSecret\":\"" + clientSecret + "\",\"sign\":\"" + sign + "\"}";
try{
}catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
}
public static String getAccessToken() throws IOException {
String json = "";
String at = "Client ";
Properties otherPro = PropertiesLoaderUtils.loadAllProperties("config/constant.properties");
String iscbaseUrl = otherPro.getProperty("isc.client.baseUrl");
String iscappid = otherPro.getProperty("isc.client.appid");
String accessTokenUrl = otherPro.getProperty("isc.client.accessTokenUrl");
String clientSecret = otherPro.getProperty("isc.client.clientSecret");
json = "{\"appId\":\"" + iscappid + "\",\"clientSecret\":\"" + clientSecret + "\"}";
String sig = "";
try {
// 获取签名
sig = paramSign(json.replace("\\", ""));
String response = HttpClientUtil.iscSend(iscbaseUrl + accessTokenUrl, json.replace("\\", ""), sig, iscappid);
if (!(StringUtils.isEmpty(response)) && response.startsWith("{")) {
JSONObject responseJson = JSONObject.parseObject(response);
String code = String.valueOf(responseJson.get("code"));
if ("100001".equals(code)) {
JSONObject dataJson = responseJson.getJSONObject("data");
String accessToken = at + dataJson.get("accessToken");
return accessToken;
} else {
return null;
}
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
}
/**
*
* @param jsonParam
* @return
* @throws IOException
*/
public static String paramSign(String jsonParam) {
try {
Properties otherPro = PropertiesLoaderUtils.loadAllProperties("config/constant.properties");
String signKey = otherPro.getProperty("isc.client.sigKey");
String key;
key = EncryptHelper.sign(signKey, jsonParam.replace("\\", ""));
System.out.println("key:" + key);
return key;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonParam;
}
}