| | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.mapper.EntityWrapper; |
| | | import com.core.common.Cools; |
| | | import com.core.exception.CoolException; |
| | | import com.zy.asrs.entity.Order; |
| | | import com.zy.asrs.entity.ErpSecret; |
| | | import com.zy.asrs.entity.WrkMast; |
| | | import com.zy.asrs.service.ApiLogService; |
| | | import com.zy.asrs.service.impl.ErpSecretServiceImpl; |
| | | import com.zy.asrs.task.AbstractHandler; |
| | | import com.zy.asrs.task.core.ReturnT; |
| | | import com.zy.common.utils.HttpHandler; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | |
| | | @Service |
| | |
| | | //登录地址 |
| | | private String loginaddress; |
| | | |
| | | @Value("${erp.login.acctID}") |
| | | //账号ID |
| | | private String acctID; |
| | | @Value("${erp.login.clientId}") |
| | | //WMS端ID |
| | | private String clientId; |
| | | |
| | | @Value("${erp.login.clientSecret}") |
| | | //账号密钥 |
| | | private String clientSecret; |
| | | |
| | | @Value("${erp.login.username}") |
| | | //账号 |
| | | private String username; |
| | | |
| | | @Value("${erp.login.password}") |
| | | //密码 |
| | | private String password; |
| | | @Value("${erp.login.accountId}") |
| | | private String accountId; |
| | | |
| | | @Value("${erp.login.lcid}") |
| | | // |
| | | private Integer lcid; |
| | | @Value("${erp.login.xAcfwIdentity}") |
| | | private String xAcfwIdentity; |
| | | |
| | | @Autowired |
| | | private ApiLogService apiLogService; |
| | | @Autowired |
| | | private ErpSecretServiceImpl erpSecretService; |
| | | |
| | | /** |
| | | * 45分钟登录一次金蝶系统 |
| | |
| | | */ |
| | | @Transactional |
| | | public synchronized ReturnT<String> start() { |
| | | ErpSecret erpSecret = erpSecretService.selectOne(new EntityWrapper<ErpSecret>().eq("account_id", accountId)); |
| | | String time = String.valueOf(System.currentTimeMillis()); |
| | | // 创建请求参数 |
| | | JSONObject requestPayload = new JSONObject(); |
| | | requestPayload.put("client_id", clientId); |
| | | requestPayload.put("client_secret", clientSecret); |
| | | requestPayload.put("username", username); |
| | | requestPayload.put("accountId", accountId); |
| | | requestPayload.put("nonce", erpSecret.getRefreshToken());//刷新token |
| | | requestPayload.put("timestamp", time); |
| | | requestPayload.put("language", "zh_CN"); |
| | | |
| | | LoginParam loginParam =new LoginParam(); |
| | | loginParam.setAcctID(acctID); |
| | | loginParam.setUsername(username); |
| | | loginParam.setPassword(password); |
| | | loginParam.setLcid(lcid); |
| | | //上报 |
| | | // 创建请求头 |
| | | Map<String, Object> headers = new HashMap<>(); |
| | | headers.put("x-acgw-identity", xAcfwIdentity); // 自定义请求头 |
| | | headers.put("Content-Type", "application/json;charset=UTF-8"); |
| | | String response = ""; |
| | | String cookie = ""; |
| | | boolean success = false; |
| | | String accessToken = ""; |
| | | try { |
| | | // 使用 HttpHandler 发起请求 |
| | | response = new HttpHandler.Builder() |
| | | .setUri(URL) |
| | | .setPath(loginaddress) |
| | | .setJson(JSON.toJSONString(loginParam)) |
| | | .setPath(loginaddress) // 登录接口路径 |
| | | .setJson(requestPayload.toJSONString()) // 请求体 |
| | | .setHeaders(headers) // 设置请求头 |
| | | .build() |
| | | .doPost(); |
| | | JSONObject jsonObject = JSON.parseObject(response); |
| | | Object sessionId = findValueByKey(JSON.parseObject(response), "SessionId"); |
| | | cookie="ASP.NET_SessionId="+sessionId+";"+"kdservice-sessionid="+jsonObject.getString("KDSVCSessionId"); |
| | | if (Cools.isEmpty(jsonObject.getString("Message"))) { |
| | | .doPost(); // 执行POST请求 |
| | | // 解析返回结果 |
| | | JSONObject jsonResponse = JSON.parseObject(response); |
| | | // 判断请求是否成功 |
| | | if (jsonResponse.getBooleanValue("status")) { |
| | | accessToken = jsonResponse.getJSONObject("data").getString("access_token"); |
| | | String refreshToken = jsonResponse.getJSONObject("data").getString("refresh_token"); |
| | | success = true; |
| | | erpSecret.setRefreshToken(refreshToken); |
| | | erpSecret.setAccessToken(accessToken); |
| | | if(erpSecretService.saveErpSecret(erpSecret)){ |
| | | log.info("获取更新Token成功: {}", accessToken); |
| | | } |
| | | } else { |
| | | callApiLogSaveLog(JSON.toJSONString(loginParam),false); |
| | | log.error("请求接口失败!!!url:{};request:{};response:{}", URL+loginaddress, JSON.toJSONString(loginParam), response); |
| | | // throw new CoolException("登录金蝶失败"); |
| | | log.error("获取Token失败,错误信息:{}", jsonResponse.getString("message")); |
| | | callApiLogSaveLog(requestPayload.toJSONString(), false); |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | log.error("fail", e); |
| | | callApiLogSaveLog(JSON.toJSONString(loginParam),false); |
| | | log.error("请求接口失败!!!url:{};request:{};response:{}", URL+loginaddress, JSON.toJSONString(loginParam), response); |
| | | // TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); |
| | | log.error("获取Token失败,异常信息:", e); |
| | | callApiLogSaveLog(requestPayload.toJSONString(), false); |
| | | return FAIL.setMsg(e.getMessage()); |
| | | } finally { |
| | | // try { |
| | | // // 保存接口日志 |
| | | // apiLogService.save( |
| | | // "登录金蝶", |
| | | // URL + loginaddress, |
| | | // null, |
| | | // "127.0.0.1", |
| | | // JSON.toJSONString(loginParam), |
| | | // response, |
| | | // success |
| | | // ); |
| | | // } catch (Exception e) { log.error("", e); } |
| | | } |
| | | return SUCCESS.setContent(cookie); |
| | | |
| | | if (!success) { |
| | | return FAIL.setMsg("Token获取失败"); |
| | | } |
| | | |
| | | return SUCCESS.setContent(accessToken); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 递归查找JSON中的指定字段 |
| | | */ |
| | | public static Object findValueByKey(JSONObject json, String key) { |
| | | Set<String> keySet = json.keySet(); |
| | | for (String k : keySet) { |
| | |
| | | return v; |
| | | } else if (v instanceof JSONArray) { |
| | | int size = ((JSONArray) v).size(); |
| | | for (int i = 0; i <= size - 1; i++) { |
| | | for (int i = 0; i < size; i++) { |
| | | Object result = findValueByKey((JSONObject) ((JSONArray) v).get(i), key); |
| | | if (result != null){ |
| | | return result; |
| | | } |
| | | } |
| | | } else if (v instanceof JSONObject){ |
| | | } else if (v instanceof JSONObject) { |
| | | Object result = findValueByKey((JSONObject) v, key); |
| | | if (result != null){ |
| | | if (result != null) { |
| | | return result; |
| | | } |
| | | } |
| | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 保存API日志 |
| | | */ |
| | | public void callApiLogSaveLog(String response, Boolean bool) { |
| | | apiLogService.save("登录金蝶失败", URL, "null", loginaddress, |
| | | "套账ID:"+acctID+"、账号:"+username+"、密码:"+password, |
| | | apiLogService.save("登录金蝶失败", URL, "端口", loginaddress, |
| | | "账号Id:"+accountId+"、用户端Id:"+clientId+"、用户密钥:"+clientSecret+"、username:"+username+"、refresh_token:", |
| | | response, bool); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | |