80 lines
3.4 KiB
Java
80 lines
3.4 KiB
Java
package com.zcloud.util;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.zcloud.mapper.datasource.map.PersonLocationTokenMapper;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
/**
|
|
* 人员定位系统工具类
|
|
* @author zhangyue
|
|
* @date 2023/9/20/020 17:33
|
|
*/
|
|
@Component
|
|
public class PerLocUtil {
|
|
|
|
// @Value("${perLoc.url}")
|
|
// 线上地址
|
|
// public static String perLocUrl = "http://172.16.130.86/gateway-service";
|
|
// 线上外网映射
|
|
public static String perLocUrl = "http://221.195.199.13:7811/gateway-service";
|
|
// @Value("${perLoc.userName}")
|
|
// private String userName;
|
|
// @Value("${perLoc.pwd}")
|
|
private static String username = "qinan";
|
|
private static String password = "ZBo7pZ6UKDGLXq/xc87WB/J84jk0B7jpujsQZFcGbG4NrNN0CuWSiImfNln0nloVYC0BOp+9bnCq7MEFpzJXy11z2iYnlOA8B7MM5YDi2dpI+bHO0+lfL57eH6IXP7rrSjiKWCBDjNPgYGETkSgXNRVb8L9LnwX6qbHDri4ARTI=";
|
|
private static String userName = "n3Q3nVlzQ/8wVC0CObso2H+i8Y3g8q7k0DGZrZY4ofqnAv1wgfFst7AnlTYu7Rw1aJv36cPD/nrWARmNqP+UghX17K5iutwmKLVwC4EZaVmVABQGLjYdCekj2ePHpGjDFyHYaGQrIVJ651WxFZfKtdBexSL7b9sm7FRbpievNmA=";
|
|
private static String source = "API";
|
|
private static String code = "1";
|
|
// token
|
|
private static String perLocToken = "";
|
|
|
|
|
|
|
|
@Resource
|
|
private PersonLocationTokenMapper personLocationTokenMapper;
|
|
|
|
public static String getToken(){
|
|
// token 不为空。验证token是否失效
|
|
if (Tools.notEmpty(perLocToken)) {
|
|
JSONObject request = new JSONObject();
|
|
request.put("pageNum", 1);
|
|
request.put("pageSize", 10);
|
|
String httpResponseStr = HttpRequestUtil.doPost(perLocToken, perLocUrl + "/system/menu/menuTree",request.toJSONString());
|
|
JSONObject httpResponse = JSONObject.parseObject(httpResponseStr);
|
|
// token 有效
|
|
if (Tools.notEmpty(httpResponseStr) && httpResponse != null && httpResponse.get("code") != null && httpResponse.getInteger("code") == 200) {
|
|
return perLocToken;
|
|
} else { // token 失效 或者报错
|
|
perLocToken = goToLogin();
|
|
}
|
|
} else {
|
|
perLocToken = goToLogin();
|
|
}
|
|
return perLocToken;
|
|
}
|
|
|
|
|
|
private static String goToLogin(){
|
|
JSONObject request = new JSONObject();
|
|
request.put("username", username);
|
|
request.put("password", password);
|
|
request.put("userName", userName);
|
|
request.put("source", source);
|
|
request.put("code", code);
|
|
String loginResStr = HttpRequestUtil.doPost(perLocUrl + "/auth/encryptLogin", request.toJSONString());
|
|
JSONObject loginResponse = JSONObject.parseObject(loginResStr);
|
|
// 登录成功
|
|
if(Tools.notEmpty(loginResStr) && loginResponse != null
|
|
&& loginResponse.get("code") != null && loginResponse.getInteger("code") == 200
|
|
&& loginResponse.get("data") != null && loginResponse.getJSONObject("data").getString("token") != null){
|
|
// personLocationTokenMapper.edit(loginResponse.getJSONObject("data").getString("access_token"));
|
|
return loginResponse.getJSONObject("data").getString("token");
|
|
} else { // 登录失败
|
|
throw new RuntimeException("人员定位第三方登录失败");
|
|
}
|
|
}
|
|
}
|