获取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,16 +6,16 @@ spring:
druid:
# 主库数据源
master:
url: jdbc:mysql://117.50.163.143:3306/autosprout?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: autosprout
password: 2fT5hHLbj8Nis6fD
url: jdbc:mysql://localhost:3306/autosprout?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: 123456
# 从库数据源
slave:
# 从数据源开关/默认关闭
enabled: false
url:
username:
password:
url:
username:
password:
# 初始连接数
initialSize: 5
# 最小连接池数量
@ -39,7 +39,7 @@ spring:
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
webStatFilter:
webStatFilter:
enabled: true
statViewServlet:
enabled: true
@ -58,4 +58,4 @@ spring:
merge-sql: true
wall:
config:
multi-statement-allow: true
multi-statement-allow: true

View File

@ -31,6 +31,12 @@ watering:
daysToExpire: 100
# 推送消息token
token: tVpNdGKrAFHfKZNgpIWQfZukrcYHNfFM
# 百度植物识别
baidu:
tokenUrl: https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials
apiKey: 9TBnBZUDR4iSkBTHOK3GApvZ
secretKey: IAHhV9BqLQnrBXqwx5WsNCRpK2nDdwQ3
# 开发环境配置
server:
# 服务器的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()
// 对于登录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("/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));
}
/**
* 植物识别
*/
@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

@ -13,7 +13,7 @@ import java.util.List;
/**
* 浇水记录Service业务层处理
*
*
* @author qiuzhenzhao
* @date 2023-11-14
*/
@ -28,7 +28,7 @@ public class AsWateringRecordServiceImpl extends ServiceImpl<AsWateringRecordMap
/**
* 查询浇水记录
*
*
* @param deviceId 浇水记录主键
* @return 浇水记录
*/
@ -40,7 +40,7 @@ public class AsWateringRecordServiceImpl extends ServiceImpl<AsWateringRecordMap
/**
* 查询浇水记录列表
*
*
* @param asWateringRecord 浇水记录
* @return 浇水记录
*/
@ -56,8 +56,11 @@ public class AsWateringRecordServiceImpl extends ServiceImpl<AsWateringRecordMap
}else {
stringBuilder.append("普通模式浇水");
}
Integer sprayingTime = record.getSprayingTime() / 60;
stringBuilder.append(sprayingTime+"分钟");
Integer sprayingTime;
if(record.getSprayingTime()!=null){
sprayingTime = record.getSprayingTime() / 60;
stringBuilder.append(sprayingTime+"分钟");
}
record.setWateringDesc(stringBuilder.toString());
// record.setWaterTimeStr(DateUtils.getYYYY_MM_DD(record.getWaterTime()));//时间格式化
}
@ -66,7 +69,7 @@ public class AsWateringRecordServiceImpl extends ServiceImpl<AsWateringRecordMap
/**
* 新增浇水记录
*
*
* @param asWateringRecord 浇水记录
* @return 结果
*/
@ -78,7 +81,7 @@ public class AsWateringRecordServiceImpl extends ServiceImpl<AsWateringRecordMap
/**
* 修改浇水记录
*
*
* @param asWateringRecord 浇水记录
* @return 结果
*/
@ -90,7 +93,7 @@ public class AsWateringRecordServiceImpl extends ServiceImpl<AsWateringRecordMap
/**
* 批量删除浇水记录
*
*
* @param deviceIds 需要删除的浇水记录主键
* @return 结果
*/
@ -102,7 +105,7 @@ public class AsWateringRecordServiceImpl extends ServiceImpl<AsWateringRecordMap
/**
* 删除浇水记录信息
*
*
* @param deviceId 浇水记录主键
* @return 结果
*/