|
@@ -0,0 +1,85 @@
|
|
|
+package com.bosshand.virgo.api.service;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.bosshand.virgo.exception.ServiceException;
|
|
|
+import okhttp3.*;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Base64;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class BimService {
|
|
|
+
|
|
|
+ public String accessToken() throws ServiceException {
|
|
|
+ Base64.Encoder encoder = Base64.getEncoder();
|
|
|
+ String key = "IwwzZlDPatrtN4iOwSgpZjObYobfPOjO" + ":" + "9PGvmj8Rh01lsNNuc1v4NYjvCowf4jaS";
|
|
|
+ OkHttpClient okHttpClient = new OkHttpClient();
|
|
|
+ RequestBody body = new FormBody.Builder().addEncoded("Content-Type", "application/json").build();
|
|
|
+ Request request = new Request.Builder().url("https://api.bimface.com/oauth2/token").post(body)
|
|
|
+ .addHeader("Authorization", "Basic" + " " + encoder.encodeToString(key.getBytes())).build();
|
|
|
+ Call call = okHttpClient.newCall(request);
|
|
|
+ Response response = null;
|
|
|
+ try {
|
|
|
+ response = call.execute();
|
|
|
+ JSONObject parse = JSON.parseObject(JSON.parseObject(response.body().string()).getString("data"));
|
|
|
+ return parse.getString("token");
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new ServiceException("Fail to get access token", e);
|
|
|
+ } finally {
|
|
|
+ if (response != null) {
|
|
|
+ response.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取viewToken
|
|
|
+ */
|
|
|
+ public String viewToken(String fileId) throws ServiceException {
|
|
|
+ OkHttpClient okHttpClient = new OkHttpClient();
|
|
|
+ Request request = new Request.Builder().url("https://api.bimface.com/view/token" + "?fileId=" + fileId).get()
|
|
|
+ .addHeader("Authorization", "Bearer" + " " + accessToken()).build();
|
|
|
+ Call call = okHttpClient.newCall(request);
|
|
|
+ okhttp3.Response response = null;
|
|
|
+ String data = null;
|
|
|
+ try {
|
|
|
+ response = call.execute();
|
|
|
+ data = response.body().string();
|
|
|
+ return JSON.parseObject(data).getString("data");
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new ServiceException("Fail to get view token", e);
|
|
|
+ } finally {
|
|
|
+ if (response != null) {
|
|
|
+ response.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取集成viewToken
|
|
|
+ */
|
|
|
+ public String viewTokenByIntegrateId(String integrateId) {
|
|
|
+ OkHttpClient okHttpClient = new OkHttpClient();
|
|
|
+ Request request = new Request.Builder().url("https://api.bimface.com/view/token" + "?integrateId=" + integrateId).get()
|
|
|
+ .addHeader("Authorization", "Bearer" + " " + accessToken()).build();
|
|
|
+ Call call = okHttpClient.newCall(request);
|
|
|
+ Response response = null;
|
|
|
+ String data = null;
|
|
|
+ try {
|
|
|
+ response = call.execute();
|
|
|
+ data = response.body().string();
|
|
|
+ return JSON.parseObject(data).getString("data");
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new ServiceException("Fail to get view token", e);
|
|
|
+ } finally {
|
|
|
+ if (response != null) {
|
|
|
+ response.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|