2020import java .util .HashMap ;
2121import java .util .Map ;
2222import java .util .Optional ;
23+ import java .util .concurrent .atomic .AtomicLong ;
2324
2425import static com .getui .gtps .config .GtSDKConstants .CommandPreValue .BySha1 ;
2526import static com .getui .gtps .manufacturer .constant .ManufacturerConstants .MANUFACTURER_NAME_OPPO ;
@@ -37,6 +38,8 @@ public class OppoService extends BaseManufacturer {
3738
3839 public final static String name = MANUFACTURER_NAME_OPPO ;
3940
41+ private final AtomicLong authLock = new AtomicLong (System .currentTimeMillis ());
42+
4043 public OppoService (String appId , String appKey , String appSecret , String masterSecret ) {
4144 super (appId , appKey , appSecret , masterSecret );
4245 }
@@ -48,10 +51,7 @@ public String getName() {
4851
4952 @ Override
5053 protected void auth () throws AuthFailedException {
51- if (!this .needAuth ()) {
52- return ;
53- }
54- synchronized (this ) {
54+ synchronized (authLock ) {
5555 // 可能并发操作已鉴权
5656 if (!this .needAuth ()) {
5757 return ;
@@ -69,16 +69,19 @@ protected void auth() throws AuthFailedException {
6969 LOGGER .info ("OppoService auth result: {}" , result .toString ());
7070 if (result .success ()) {
7171 JsonNode jsonNode = new ObjectMapper ().readTree (result .getContent ());
72- if (jsonNode .get ("code" ).intValue () == 0 ) {
72+ if (OppoConstants . ReturnCode . Success . getCode () == jsonNode .get ("code" ).intValue ()) {
7373 String authToken = jsonNode .get ("data" ).get ("auth_token" ).textValue ();
74- this .cacheMap .put (AUTH_TOKEN , CacheServiceFactory .getCacheService (CaffeineCacheService .class ).set (name , authToken , 24 * 3600 ));
74+ // OPPO说明:鉴权令牌有效期是24小时,在有效期内多次申请均返回相同的令牌,令牌超过有效期后申请将返回新令牌。令牌过期失效后有10分钟过渡期,过渡期间新旧两个auth_token均可使用,超过过渡期后只有新令牌可使用。
75+ // TTL说明:为了防止24小时临界点调用接口时,op可能返回旧的token被我们换成24小时,这里+5分钟进行缓存
76+ this .cacheMap .put (AUTH_TOKEN , CacheServiceFactory .getCacheService (CaffeineCacheService .class ).set (name , authToken , 24 * 3600 + 5 * 60 ));
77+ authLock .compareAndSet (authLock .get (), System .currentTimeMillis ());
7578 }
7679 }
7780 } catch (JsonProcessingException ex ) {
7881 e = ex ;
7982 }
80- } while (needAuth ( ) && i ++ < 2 );
81- if (needAuth ( )) {
83+ } while (isEmptyToken ( getAuthTokenFromCache () ) && i ++ < 2 );
84+ if (isEmptyToken ( getAuthTokenFromCache () )) {
8285 throw new AuthFailedException (this .getName (), e );
8386 }
8487 }
@@ -91,21 +94,44 @@ private static String getSign(String appKey, long timestamp, String masterSecret
9194
9295 @ Override
9396 protected boolean needAuth () {
94- Object cache = this .cacheMap .get (AUTH_TOKEN );
95- return cache == null || CacheServiceFactory .getCacheService (CaffeineCacheService .class ).get (cache , name ) == null ;
97+ return isEmptyToken (getAuthTokenFromCache ()) || System .currentTimeMillis () - authLock .get () >= 1000 ;
9698 }
9799
98100 @ Override
99101 protected String getAuthToken () {
100- return (String ) CacheServiceFactory .getCacheService (CaffeineCacheService .class ).get (this .cacheMap .get (AUTH_TOKEN ), name );
102+ String token = getAuthTokenFromCache ();
103+ if (isEmptyToken (token )) {
104+ auth ();
105+ token = getAuthTokenFromCache ();
106+ }
107+ return token ;
108+ }
109+
110+ private String getAuthTokenFromCache () {
111+ Object cache = this .cacheMap .get (AUTH_TOKEN );
112+ String token = null ;
113+ if (cache != null ) {
114+ token = (String ) CacheServiceFactory .getCacheService (CaffeineCacheService .class ).get (cache , name );
115+ }
116+ return token ;
117+ }
118+
119+ private boolean isEmptyToken (String token ) {
120+ return token == null || token .length () == 0 ;
101121 }
102122
103123 @ Override
104124 public Result uploadIcon (File file ) throws AuthFailedException {
105- auth ();
106125 String cacheKey = BySha1 .equals (CommonConfig .sameFileJudgePattern ) ? FileUtils .sha1 (file ) : file .getName ();
107126 Optional <Result > cacheResult = getCacheResult (ICON_URL , cacheKey );
108- return cacheResult .orElseGet (() -> uploadIcon (file , cacheKey ));
127+ return cacheResult .orElseGet (() -> {
128+ Result result = uploadIcon (file , cacheKey );
129+ if (Result .invalidAuthToken ().getCode () == result .getCode ()) {
130+ auth ();
131+ result = uploadIcon (file , cacheKey );
132+ }
133+ return result ;
134+ });
109135 }
110136
111137 private Result uploadIcon (File file , String cacheKey ) {
@@ -117,10 +143,13 @@ private Result uploadIcon(File file, String cacheKey) {
117143 if (httpResponse .success ()) {
118144 try {
119145 JsonNode jsonNode = new ObjectMapper ().readTree (httpResponse .getContent ());
120- if (0 == jsonNode .get ("code" ).intValue ()) {
146+ int returnCode = jsonNode .get ("code" ).intValue ();
147+ if (OppoConstants .ReturnCode .Success .getCode () == returnCode ) {
121148 String url = jsonNode .get ("data" ).get ("small_picture_id" ).textValue ();
122149 this .cacheMap .put (ICON_URL , CacheServiceFactory .getCacheService (CaffeineCacheService .class ).set (cacheKey , url ));
123150 return Result .success (url );
151+ } else if (OppoConstants .ReturnCode .InvalidAuthCode .getCode () == returnCode ) {
152+ return Result .invalidAuthToken ();
124153 } else {
125154 return Result .fail (jsonNode .get ("message" ).textValue ());
126155 }
@@ -133,10 +162,16 @@ private Result uploadIcon(File file, String cacheKey) {
133162
134163 @ Override
135164 public Result uploadPic (File file ) {
136- auth ();
137165 String cacheKey = BySha1 .equals (CommonConfig .sameFileJudgePattern ) ? FileUtils .sha1 (file ) : file .getName ();
138166 Optional <Result > cacheResult = getCacheResult (PIC_URL , cacheKey );
139- return cacheResult .orElseGet (() -> uploadPic (file , cacheKey ));
167+ return cacheResult .orElseGet (() -> {
168+ Result result = uploadPic (file , cacheKey );
169+ if (Result .invalidAuthToken ().getCode () == result .getCode ()) {
170+ auth ();
171+ result = uploadPic (file , cacheKey );
172+ }
173+ return result ;
174+ });
140175 }
141176
142177 private Result uploadPic (File file , String cacheKey ) {
@@ -148,10 +183,13 @@ private Result uploadPic(File file, String cacheKey) {
148183 if (httpResponse .success ()) {
149184 try {
150185 JsonNode jsonNode = new ObjectMapper ().readTree (httpResponse .getContent ());
151- if (0 == jsonNode .get ("code" ).intValue ()) {
186+ int returnCode = jsonNode .get ("code" ).intValue ();
187+ if (OppoConstants .ReturnCode .Success .getCode () == returnCode ) {
152188 String url = jsonNode .get ("data" ).get ("big_picture_id" ).textValue ();
153189 this .cacheMap .put (PIC_URL , CacheServiceFactory .getCacheService (CaffeineCacheService .class ).set (cacheKey , url ));
154190 return Result .success (url );
191+ } else if (OppoConstants .ReturnCode .InvalidAuthCode .getCode () == returnCode ) {
192+ return Result .invalidAuthToken ();
155193 } else {
156194 return Result .fail (jsonNode .get ("message" ).textValue ());
157195 }
0 commit comments