Commit a4f160d6 authored by Quxl's avatar Quxl

x

parent 72c9eece
package com.egolm.sso.services;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import com.egolm.sso.util.HttpUtil;
@Component
public class TraceService {
@Value("${systemId}")
private String systemId;
@Value("${dataTracking.appId}")
private String appId;
@Value("${dataTracking.appSecret}")
private String appSecret;
@Value("${dataTracking.username}")
private String username;
@Value("${dataTracking.baseUrl}")
private String baseUrl;
@Value("${dataTracking.uri.token}")
private String tokenUri;
@Value("${dataTracking.uri.insertOrderTracking}")
private String insertOrderTrackingUri;
@Value("${dataTracking.isOpen}")
private boolean isOpen;
@Value("${dataTracking.tokenExpireMinute}")
private Long tokenExpireMinute;
private static String token;
private static Date tokenCreateTime;
public String getToken() {
Date now = new Date();
if (token != null && tokenCreateTime != null
&& (tokenCreateTime.getTime() + ((tokenExpireMinute - 2) * 60L * 1000L)) > now.getTime()) {
return token;
}
String requestUrl = baseUrl + tokenUri;
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("appId", appId);
parameters.put("appSecret", appSecret);
parameters.put("username", username);
String resultText = HttpUtil.post(requestUrl, parameters, null);
JSONObject jsonObject = JSONObject.parseObject(resultText);
Boolean isSuccess = jsonObject.getBoolean("success");
if (isSuccess != null && isSuccess) {
tokenCreateTime = now;
token = jsonObject.getString("token");
return token;
} else {
throw new TrackingTokenException(jsonObject.getString("message"));
}
}
public String saveOrderTracking(String traceNo, String orderType, String orderNo, String disCode, String action,
String result, String message, String errorMessage) {
if (isOpen) {
String requestUrl = baseUrl + insertOrderTrackingUri;
String token = this.getToken();
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "Bearer " + token);
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("traceNo", traceNo);
parameters.put("orderType", orderType);
parameters.put("orderNo", orderNo);
parameters.put("disCode", disCode);
parameters.put("action", action);
parameters.put("result", result);
parameters.put("message", message);
parameters.put("errorMessage", errorMessage);
return HttpUtil.postRaw(requestUrl, parameters, headers);
}
return null;
}
public static class TrackingTokenException extends RuntimeException {
private static final long serialVersionUID = 1L;
public TrackingTokenException(String msg) {
super(msg);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment