项目地址:https://gitee.com/y_project/RuoYi-Vue
获取验证码 package com.ruoyi.web.controller.common;
先获取到一个成功的全局返回值 ajax
判断验证码是否打开 captchaOnOff
public AjaxResult getCode(HttpServletResponse response) throws IOException
{
AjaxResult ajax = AjaxResult.success();
boolean captchaOnOff = configService.selectCaptchaOnOff();
ajax.put("captchaOnOff", captchaOnOff);
if (!captchaOnOff)
{
return ajax;
}
// ...
}
selectCaptchaOnOff方法:
@Override
public boolean selectCaptchaOnOff()
{
String captchaOnOff = selectConfigByKey("sys.account.captchaOnOff");
if (StringUtils.isEmpty(captchaOnOff))
{
return true;
}
return Convert.toBool(captchaOnOff);
}
selectConfigByKey方法:
@Override
public String selectConfigByKey(String configKey)
{
String configValue = Convert.toStr(redisCache.getCacheObject(getCacheKey(configKey)));
if (StringUtils.isNotEmpty(configValue))
{
return configValue;
}
SysConfig config = new SysConfig();
config.setConfigKey(configKey);
SysConfig retConfig = configMapper.selectConfig(config);
if (StringUtils.isNotNull(retConfig))
{
redisCache.setCacheObject(getCacheKey(configKey), retConfig.getConfigValue());
return retConfig.getConfigValue();
}
return StringUtils.EMPTY;
}
getCacheKey方法:
private String getCacheKey(String configKey)
{
return CacheConstants.SYS_CONFIG_KEY + configKey;
}
CacheConstants.SYS_CONFIG_KEY
内的值是 sys_config:"
先从缓存中取 sys.account.captchaOnOff
标记,如果缓存中有标记就直接返回。
缓存中 sys.account.captchaOnOff 标记的来源
@Service
public class SysConfigServiceImpl implements ISysDictTypeService
{
@PostConstruct
public void init()
{
loadingConfigCache();
}
@Override
public void loadingConfigCache()
{
List<SysConfig> configsList = configMapper.selectConfigList(new SysConfig());
for (SysConfig config : configsList)
{
redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config