Browse Source

Merge remote-tracking branch 'remotes/origin/dev_1.0.0' into release_1.0.0

xiatian 2 years ago
parent
commit
173e6f0eb9

+ 1 - 1
src/main/java/cn/com/qmth/mps/job/WxappAccessTokenJob.java

@@ -10,7 +10,7 @@ import cn.com.qmth.mps.service.AuthService;
 public class WxappAccessTokenJob {
 	@Autowired
 	private AuthService authService;
-	@Scheduled(cron = "0 * * * * ?")
+	@Scheduled(cron = "0/10 * * * * ?")
     public void modifyWxappAccessToken() {
 		authService.modifyWxappAccessToken();
     }

+ 4 - 1
src/main/java/cn/com/qmth/mps/service/WxappAccessTokenService.java

@@ -1,11 +1,14 @@
 package cn.com.qmth.mps.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.qmth.boot.core.solar.model.WxappAccessToken;
 
 import cn.com.qmth.mps.entity.WxappAccessTokenEntity;
 
 public interface WxappAccessTokenService  extends IService<WxappAccessTokenEntity> {
 
-	WxappAccessTokenEntity getWxappAccessToken();
+	WxappAccessToken getWxappAccessToken();
+
+	void saveWxappAccessToken(String accessToken, Long expiresTime);
 
 }

+ 37 - 23
src/main/java/cn/com/qmth/mps/service/impl/AuthServiceImpl.java

@@ -15,6 +15,8 @@ import org.springframework.transaction.annotation.Transactional;
 import com.qmth.boot.core.exception.StatusException;
 import com.qmth.boot.core.security.annotation.AuthorizationComponent;
 import com.qmth.boot.core.security.service.AuthorizationService;
+import com.qmth.boot.core.solar.model.WxappAccessToken;
+import com.qmth.boot.core.solar.service.SolarService;
 import com.qmth.boot.tools.signature.SignatureType;
 import com.qmth.boot.tools.uuid.FastUUID;
 
@@ -22,7 +24,6 @@ import cn.com.qmth.mps.bean.User;
 import cn.com.qmth.mps.config.SysProperty;
 import cn.com.qmth.mps.entity.SchoolEntity;
 import cn.com.qmth.mps.entity.UserEntity;
-import cn.com.qmth.mps.entity.WxappAccessTokenEntity;
 import cn.com.qmth.mps.entity.WxappInfoEntity;
 import cn.com.qmth.mps.enums.Role;
 import cn.com.qmth.mps.exception.ParameterExceptions;
@@ -54,7 +55,10 @@ public class AuthServiceImpl implements AuthorizationService<User>, AuthService
 	private SysProperty sysProperty;
 	@Autowired
 	private WxappInfoService wxappInfoService;
+	@Autowired
+	private SolarService solarService;
 
+	
 	@Override
 	public AdminLoginVo loginWxAppCode(String loginCode) {
 		JSONObject auth=getAuthorization(loginCode);
@@ -150,33 +154,43 @@ public class AuthServiceImpl implements AuthorizationService<User>, AuthService
 		sessionService.updateUserSession(user);
 		return true;
 	}
-
+	
 	@Override
 	public void modifyWxappAccessToken() {
-		WxappAccessTokenEntity token = wxappAccessTokenService.getWxappAccessToken();
+		WxappAccessToken token = wxappAccessTokenService.getWxappAccessToken();
 		long now = System.currentTimeMillis();
-		if (StringUtils.isEmpty(token.getAccessToken()) || token.getExpiresTime() - now <= 15 * 60 * 1000) {
-			Map<String, String> params = new HashMap<>();
-			params.put("appid", sysProperty.getWxappAppid());
-			params.put("secret", sysProperty.getWxappSecret());
-			params.put("grant_type", "client_credential");
-			String ret;
-			try {
-				ret = HttpUtil.httpsActionGet("https://api.weixin.qq.com/cgi-bin/token", null, params);
-			} catch (Exception e) {
-				throw new StatusException("获取失败", e);
-			}
-			JSONObject jo = JSONObject.fromObject(ret);
-			if (jo.containsKey("errcode")) {
-				throw new StatusException("获取失败," + jo.getString("errmsg"));
-			}
-			int ex = jo.getInt("expires_in");
-			String at = jo.getString("access_token");
-			token.setAccessToken(at);
-			token.setExpiresTime(now + ex * 1000);
-			wxappAccessTokenService.updateById(token);
+		if (StringUtils.isEmpty(token.getAccessToken()) || token.getExpireTime() - now <= 0) {
+			WxappAccessToken wt=solarService.getWxappAccessToken(sysProperty.getWxappAppid());
+			wxappAccessTokenService.saveWxappAccessToken(wt.getAccessToken(),wt.getExpireTime());
 		}
 	}
+	
+//	@Override
+//	public void modifyWxappAccessToken() {
+//		WxappAccessTokenEntity token = wxappAccessTokenService.getWxappAccessToken();
+//		long now = System.currentTimeMillis();
+//		if (StringUtils.isEmpty(token.getAccessToken()) || token.getExpiresTime() - now <= 15 * 60 * 1000) {
+//			Map<String, String> params = new HashMap<>();
+//			params.put("appid", sysProperty.getWxappAppid());
+//			params.put("secret", sysProperty.getWxappSecret());
+//			params.put("grant_type", "client_credential");
+//			String ret;
+//			try {
+//				ret = HttpUtil.httpsActionGet("https://api.weixin.qq.com/cgi-bin/token", null, params);
+//			} catch (Exception e) {
+//				throw new StatusException("获取失败", e);
+//			}
+//			JSONObject jo = JSONObject.fromObject(ret);
+//			if (jo.containsKey("errcode")) {
+//				throw new StatusException("获取失败," + jo.getString("errmsg"));
+//			}
+//			int ex = jo.getInt("expires_in");
+//			String at = jo.getString("access_token");
+//			token.setAccessToken(at);
+//			token.setExpiresTime(now + ex * 1000);
+//			wxappAccessTokenService.updateById(token);
+//		}
+//	}
 
 	@Transactional
 	@Override

+ 2 - 2
src/main/java/cn/com/qmth/mps/service/impl/SchoolServiceImpl.java

@@ -36,9 +36,9 @@ public class SchoolServiceImpl extends ServiceImpl<SchoolDao, SchoolEntity> impl
 	private String host;
 	@Value("${qmth.solar.org.uri}")
 	private String uri;
-	@Value("${access-key}")
+	@Value("${com.qmth.solar.access-key}")
 	private String accessKey;
-	@Value("${access-secret}")
+	@Value("${com.qmth.solar.access-secret}")
 	private String accessSecret;
 	@Value("${wxapp-url}")
 	private String wxappUrl;

+ 25 - 3
src/main/java/cn/com/qmth/mps/service/impl/WxappAccessTokenServiceImpl.java

@@ -1,8 +1,10 @@
 package cn.com.qmth.mps.service.impl;
 
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.qmth.boot.core.solar.model.WxappAccessToken;
 
 import cn.com.qmth.mps.dao.WxappAccessTokenDao;
 import cn.com.qmth.mps.entity.WxappAccessTokenEntity;
@@ -10,10 +12,30 @@ import cn.com.qmth.mps.service.WxappAccessTokenService;
 
 @Service
 public class WxappAccessTokenServiceImpl extends ServiceImpl<WxappAccessTokenDao, WxappAccessTokenEntity> implements WxappAccessTokenService {
-
+	private static WxappAccessToken wt;
+	
+	
+	@Override
+	public WxappAccessToken getWxappAccessToken() {
+		if(wt!=null) {
+			return wt;
+		}
+		WxappAccessTokenEntity e=this.getById(1L);
+		wt=new WxappAccessToken();
+		wt.setAccessToken(e.getAccessToken());
+		wt.setExpireTime(e.getExpiresTime());
+		return wt;
+	}
+	
+	@Transactional
 	@Override
-	public WxappAccessTokenEntity getWxappAccessToken() {
-		return this.getById(1L);
+	public void saveWxappAccessToken(String accessToken,Long expiresTime) {
+		wt.setAccessToken(accessToken);
+		wt.setExpireTime(expiresTime);
+		WxappAccessTokenEntity e=this.getById(1L);
+		e.setAccessToken(accessToken);
+		e.setExpiresTime(expiresTime);
+		this.updateById(e);
 	}
 
 }

+ 2 - 2
src/main/resources/application-test.properties

@@ -36,5 +36,5 @@ wxapp-secret=0bcbea00b9283890f3b6b0a672d5cda5
 markingcloud.server=http://192.168.10.224:8080
 qmth.solar.host=https://solar.qmth.com.cn
 qmth.solar.org.uri=/api/open/org/query
-qmth.solar.access-key=e5d4ccee19d34fcf99246a770ce62293
-qmth.solar.access-secret=Zfqq3nXAGLUGevDdJV1PieeKWJXCIKxG
+com.qmth.solar.access-key=e5d4ccee19d34fcf99246a770ce62293
+com.qmth.solar.access-secret=Zfqq3nXAGLUGevDdJV1PieeKWJXCIKxG

+ 2 - 2
src/main/resources/application.properties

@@ -36,5 +36,5 @@ wxapp-secret=0bcbea00b9283890f3b6b0a672d5cda5
 markingcloud.server=http://192.168.10.224:8080
 qmth.solar.host=https://solar.qmth.com.cn
 qmth.solar.org.uri=/api/open/org/query
-access-key=e5d4ccee19d34fcf99246a770ce62293
-access-secret=Zfqq3nXAGLUGevDdJV1PieeKWJXCIKxG
+com.qmth.solar.access-key=e5d4ccee19d34fcf99246a770ce62293
+com.qmth.solar.access-secret=Zfqq3nXAGLUGevDdJV1PieeKWJXCIKxG