dcs пре 1 година
родитељ
комит
8d3be47055

+ 44 - 0
virgo.api/src/main/java/com/bosshand/virgo/api/controller/BimController.java

@@ -0,0 +1,44 @@
+package com.bosshand.virgo.api.controller;
+
+
+import com.bosshand.virgo.api.service.BimService;
+import com.bosshand.virgo.core.response.Response;
+import com.bosshand.virgo.exception.Constant;
+import com.bosshand.virgo.exception.ServiceException;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping({"bim"})
+@Api(tags = {"bim"})
+public class BimController {
+
+    @Autowired
+    BimService bimService;
+
+    @ApiOperation("获取viewToken")
+    @RequestMapping(value = "/viewToken/{fileId}", method = RequestMethod.GET)
+    public Response viewToken(@PathVariable String fileId) {
+        try {
+            return Response.ok(bimService.viewToken(fileId));
+        } catch (ServiceException e) {
+            return Response.fail(Constant.CODE_BAD_REQUEST, Constant.RET_INPUT_ERROR);
+        }
+    }
+
+    @ApiOperation("获取集成viewToken")
+    @RequestMapping(value = "/viewToken/integrate/{integrateId}", method = RequestMethod.GET)
+    public Response viewTokenByIntegrateId(@PathVariable String integrateId) {
+        try {
+            return Response.ok(bimService.viewTokenByIntegrateId(integrateId));
+        } catch (ServiceException e) {
+            return Response.fail(Constant.CODE_BAD_REQUEST, Constant.RET_INPUT_ERROR);
+        }
+    }
+
+}

+ 85 - 0
virgo.api/src/main/java/com/bosshand/virgo/api/service/BimService.java

@@ -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();
+            }
+        }
+    }
+
+
+}