获取token

This commit is contained in:
邱贞招 2024-01-24 12:02:48 +08:00
parent 65a15147d9
commit 41075254c7
7 changed files with 141 additions and 18 deletions

View File

@ -6,9 +6,9 @@ spring:
druid: druid:
# 主库数据源 # 主库数据源
master: master:
url: jdbc:mysql://117.50.163.143:3306/autosprout?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 url: jdbc:mysql://localhost:3306/autosprout?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: autosprout username: root
password: 2fT5hHLbj8Nis6fD password: 123456
# 从库数据源 # 从库数据源
slave: slave:
# 从数据源开关/默认关闭 # 从数据源开关/默认关闭

View File

@ -31,6 +31,12 @@ watering:
daysToExpire: 100 daysToExpire: 100
# 推送消息token # 推送消息token
token: tVpNdGKrAFHfKZNgpIWQfZukrcYHNfFM token: tVpNdGKrAFHfKZNgpIWQfZukrcYHNfFM
# 百度植物识别
baidu:
tokenUrl: https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials
apiKey: 9TBnBZUDR4iSkBTHOK3GApvZ
secretKey: IAHhV9BqLQnrBXqwx5WsNCRpK2nDdwQ3
# 开发环境配置 # 开发环境配置
server: server:
# 服务器的HTTP端口默认为8080 # 服务器的HTTP端口默认为8080

View File

@ -0,0 +1,59 @@
package com.ruoyi.common.utils.baidu;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
public class GetToken {
/** 缓存token */
private static String cachedAccessToken;
/** token过期时间 */
private static long tokenExpirationTime;
public static void main(String[] args) {
String token = getAccessToken();
}
private static String getAccessToken() {
/** 判断token是否过期如果不过期直接返回全局缓存token,如果过期重新获取token保存到全局缓存token中并更新过期时间*/
if (isTokenExpired()) {
try {
String tokenUrl = SpringUtils.getRequiredProperty("baidu.tokenUrl");
String apiKey = SpringUtils.getRequiredProperty("baidu.apiKey");
String secretKey = SpringUtils.getRequiredProperty("baidu.secretKey");
String url = tokenUrl+"&client_id="+apiKey+"&client_secret="+secretKey;
System.out.println("百度---tokenUrl"+url);
String res = HttpUtils.sendPost(url, null);
System.out.println("请求百度获取到accessToken========="+res);
// 创建 ObjectMapper 对象
ObjectMapper objectMapper = new ObjectMapper();
// JSON 字符串解析为 TokenInfo 对象
TokenInfo tokenInfo = objectMapper.readValue(res, TokenInfo.class);
String accessToken = tokenInfo.getAccessToken();
if(StringUtils.isNotEmpty(accessToken)){
return cachedAccessToken = accessToken;
}
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return "";
}
return "";
}
/**判断token是否过期*/
private static boolean isTokenExpired() {
return cachedAccessToken == null || System.currentTimeMillis() > tokenExpirationTime;
}
}

View File

@ -0,0 +1,25 @@
package com.ruoyi.common.utils.baidu;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
@Data
public class TokenInfo {
@JsonProperty("refresh_token")
private String refreshToken;
@JsonProperty("expires_in")
private int expiresIn;
@JsonProperty("session_key")
private String sessionKey;
@JsonProperty("access_token")
private String accessToken;
private String scope;
@JsonProperty("session_secret")
private String sessionSecret;
}

View File

@ -113,7 +113,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
// 过滤请求 // 过滤请求
.authorizeRequests() .authorizeRequests()
// 对于登录login 注册register 验证码captchaImage 允许匿名访问 // 对于登录login 注册register 验证码captchaImage 允许匿名访问
.antMatchers("/login", "/register", "/captchaImage","/common/receive","/appCaptcha","/appCodeLogin","/app/**").permitAll() .antMatchers("/login", "/register", "/captchaImage","/common/receive","/appCaptcha","/appCodeLogin","/app/**","/common/upload").permitAll()
// 静态资源可匿名访问 // 静态资源可匿名访问
.antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll() .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll() .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()

View File

@ -117,4 +117,34 @@ public class AppController extends BaseController
{ {
return toAjax(asDeviceService.deleteAsDeviceByDeviceIds(deviceIds)); return toAjax(asDeviceService.deleteAsDeviceByDeviceIds(deviceIds));
} }
/**
* 植物识别
*/
@Log(title = "植物识别", businessType = BusinessType.OTHER)
@PostMapping("/plant/identify")
public AjaxResult plant(String url)
{
/** 请求百度获取token*/
// HttpUtils.
// try
// {
// // 上传文件路径
// String filePath = RuoYiConfig.getUploadPath();
// // 上传并返回新文件名称
// String fileName = FileUploadUtils.upload(filePath, file);
// String url = serverConfig.getUrl() + fileName;
// AjaxResult ajax = AjaxResult.success();
// ajax.put("url", url);
// ajax.put("fileName", fileName);
// ajax.put("newFileName", FileUtils.getName(fileName));
// ajax.put("originalFilename", file.getOriginalFilename());
// return ajax;
// }
// catch (Exception e)
// {
// return AjaxResult.error(e.getMessage());
// }
return AjaxResult.success();
}
} }

View File

@ -56,8 +56,11 @@ public class AsWateringRecordServiceImpl extends ServiceImpl<AsWateringRecordMap
}else { }else {
stringBuilder.append("普通模式浇水"); stringBuilder.append("普通模式浇水");
} }
Integer sprayingTime = record.getSprayingTime() / 60; Integer sprayingTime;
stringBuilder.append(sprayingTime+"分钟"); if(record.getSprayingTime()!=null){
sprayingTime = record.getSprayingTime() / 60;
stringBuilder.append(sprayingTime+"分钟");
}
record.setWateringDesc(stringBuilder.toString()); record.setWateringDesc(stringBuilder.toString());
// record.setWaterTimeStr(DateUtils.getYYYY_MM_DD(record.getWaterTime()));//时间格式化 // record.setWaterTimeStr(DateUtils.getYYYY_MM_DD(record.getWaterTime()));//时间格式化
} }