|
@@ -0,0 +1,70 @@
|
|
|
+package com.sxtvs.open.api.odata.service;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateField;
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.sxtvs.open.api.youmei.entity.YoumeiAccount;
|
|
|
+import com.sxtvs.open.api.youmei.mappser.YoumeiAccountMapper;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class YoumeiTokenService extends ServiceImpl<YoumeiAccountMapper, YoumeiAccount> {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ObjectMapper objectMapper;
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ public void refreshToken() {
|
|
|
+ Date expireTime = new DateTime()
|
|
|
+ .offset(DateField.HOUR, 2)
|
|
|
+ .toJdkDate();
|
|
|
+
|
|
|
+ //检测快过期的at
|
|
|
+ for (YoumeiAccount youmeiAccount : this.lambdaQuery()
|
|
|
+ .le(YoumeiAccount::getAtExpireTime, expireTime)
|
|
|
+ .list()) {
|
|
|
+
|
|
|
+ HashMap<String, Object> params = new HashMap<>();
|
|
|
+ params.put("appId", youmeiAccount.getOpenId());
|
|
|
+ params.put("responseType", "code");
|
|
|
+ params.put("state", "");
|
|
|
+ String body = HttpUtil.post("https://api-open.51wyq.cn/dataapp/api/oauth2/authorize", params);
|
|
|
+ String authorizeCode = objectMapper.readTree(body).get("authorizeCode").get("authorizeCode").asText();
|
|
|
+
|
|
|
+ params.clear();
|
|
|
+ params.put("appId", youmeiAccount.getOpenId());
|
|
|
+ params.put("grantType", "authorization_code");
|
|
|
+ params.put("appSecret", youmeiAccount.getSecret());
|
|
|
+ params.put("authorizeCode", authorizeCode);
|
|
|
+
|
|
|
+ body = HttpUtil.post("https://api-open.51wyq.cn/dataapp/api/oauth2/token", params);
|
|
|
+ JsonNode jsonNode = objectMapper.readTree(body).get("accessToken");
|
|
|
+
|
|
|
+ String accessToken = jsonNode.get("accessToken").asText();
|
|
|
+ long expireIn = jsonNode.get("expireIn").asLong();
|
|
|
+
|
|
|
+ youmeiAccount.setAccessToken(accessToken);
|
|
|
+ youmeiAccount.setAtExpireTime(new Date(expireIn));
|
|
|
+ youmeiAccount.setUpdateTime(new Date());
|
|
|
+
|
|
|
+ this.updateById(youmeiAccount);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ for (int i = 0; i < 30; i++) {
|
|
|
+ System.out.println(new DateTime()
|
|
|
+ .offset(DateField.HOUR, i * 2)
|
|
|
+ .toJdkDate());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|