缓存租户信息1天时间

This commit is contained in:
gxwebsoft
2024-04-22 12:38:20 +08:00
parent 1cc070040d
commit f6a4d20a73
2 changed files with 72 additions and 4 deletions

View File

@@ -0,0 +1,61 @@
package com.gxwebsoft.common.core.utils;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.gxwebsoft.common.system.entity.User;
import org.springframework.stereotype.Component;
@Component
public class RequestUtil {
private static final String host = "https://modules.gxwebsoft.com/api";
private static String ACCESS_TOKEN;
private static String TENANT_ID;
public void setTenantId(String tenantId){
TENANT_ID = tenantId;
}
public void setAccessToken(String token){
ACCESS_TOKEN = token;
}
public User getMerchantAccountByPhone(String phone){
String path = "/shop/merchant-account/getMerchantAccountByPhone/" + phone;
try {
// 链式构建请求
String result = HttpRequest.get(host.concat(path))
.header("Authorization", ACCESS_TOKEN)
.header("Tenantid", TENANT_ID)
.timeout(20000)//超时,毫秒
.execute().body();
JSONObject jsonObject = JSONObject.parseObject(result);
final String data = jsonObject.getString("data");
return JSONObject.parseObject(data, User.class);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public User getByUserId(Integer userId) {
String path = "/system/user/" + userId;
try {
// 链式构建请求
String result = HttpRequest.get(host.concat(path))
.header("Authorization", ACCESS_TOKEN)
.header("Tenantid", TENANT_ID)
.timeout(20000)//超时,毫秒
.execute().body();
JSONObject jsonObject = JSONObject.parseObject(result);
System.out.println("jsonObject = " + jsonObject);
final String data = jsonObject.getString("data");
return JSONObject.parseObject(data, User.class);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

View File

@@ -20,10 +20,7 @@ import com.gxwebsoft.common.core.config.ConfigProperties;
import com.gxwebsoft.common.core.exception.BusinessException; import com.gxwebsoft.common.core.exception.BusinessException;
import com.gxwebsoft.common.core.security.JwtSubject; import com.gxwebsoft.common.core.security.JwtSubject;
import com.gxwebsoft.common.core.security.JwtUtil; import com.gxwebsoft.common.core.security.JwtUtil;
import com.gxwebsoft.common.core.utils.CacheClient; import com.gxwebsoft.common.core.utils.*;
import com.gxwebsoft.common.core.utils.CommonUtil;
import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.core.utils.SignCheckUtil;
import com.gxwebsoft.common.core.web.ApiResult; import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController; import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.ExistenceParam; import com.gxwebsoft.common.core.web.ExistenceParam;
@@ -165,6 +162,13 @@ public class MainController extends BaseController {
if (tenant == null) { if (tenant == null) {
return fail("该租户不存在或已过期",null); return fail("该租户不存在或已过期",null);
} }
// 从缓存读取信息
String key = "TenantInfo:" + tenantId;
final String tenantInfo = redisUtil.get(key);
if(StrUtil.isNotBlank(tenantInfo)){
System.out.println("从缓存读取信息tenantInfo = " + tenantId);
return success(JSONObject.parseObject(tenantInfo,Company.class));
}
// 企业信息 // 企业信息
Company company = companyService.getByTenantIdRel(tenantId); Company company = companyService.getByTenantIdRel(tenantId);
if(company == null){ if(company == null){
@@ -173,11 +177,14 @@ public class MainController extends BaseController {
company.setBusinessEntity(null); company.setBusinessEntity(null);
company.setPhone(null); company.setPhone(null);
company.setCompanyCode(null); company.setCompanyCode(null);
// 配置信息 // 配置信息
HashMap<String, Object> config = new HashMap<>(); HashMap<String, Object> config = new HashMap<>();
config.put("LICENSE_CODE", "dk9mcwJyetRWQlxWRiojIzJCLi8mcQ5Wa4ojI0NWZqJWd6ICZpJCL0kjNwl1NnhENahnIvl2cyVmdiwiIiATMuEjI6IibQf0NW=="); config.put("LICENSE_CODE", "dk9mcwJyetRWQlxWRiojIzJCLi8mcQ5Wa4ojI0NWZqJWd6ICZpJCL0kjNwl1NnhENahnIvl2cyVmdiwiIiATMuEjI6IibQf0NW==");
config.put("MAP_KEY", "8191620da39a742c6f18f010c084c772"); config.put("MAP_KEY", "8191620da39a742c6f18f010c084c772");
company.setConfig(config); company.setConfig(config);
redisUtil.set(key,company,1L, TimeUnit.DAYS);
return success(company); return success(company);
} }