deason vor 7 Jahren
Ursprung
Commit
9c105d2f60

+ 2 - 13
src/main/java/cn/com/qmth/examcloud/app/service/ExamAdminService.java

@@ -9,15 +9,11 @@ package cn.com.qmth.examcloud.app.service;
 
 import cn.com.qmth.examcloud.app.model.Result;
 import cn.com.qmth.examcloud.app.utils.HttpUtils;
-import okhttp3.Request;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import static cn.com.qmth.examcloud.app.model.Constants.PARAM_KEY;
-import static cn.com.qmth.examcloud.app.model.Constants.PARAM_TOKEN;
-
 /**
  * 考务业务服务接口
  */
@@ -28,16 +24,9 @@ public class ExamAdminService {
     private PropertyService propertyService;
 
     public Result getPracticeExamList(String key, String token, String studentId) throws Exception {
-        final String requestUrl = String.format(propertyService.getExamAdminUrl() + "/api/ecs_exam_work/exam_student/query?student_id=%s", studentId);
         //封装请求参数
-        Request request = new Request.Builder()
-                .url(requestUrl)
-                .get()
-                .addHeader(PARAM_KEY, key)
-                .addHeader(PARAM_TOKEN, token)
-                .build();
-        //执行请求
-        return HttpUtils.call(request);
+        final String requestUrl = String.format(propertyService.getExamAdminUrl() + "/api/ecs_exam_work/exam_student/query?student_id=%s", studentId);
+        return HttpUtils.doGet(requestUrl, key, token);
     }
 
     public Result getExamInfo(String key, String token, Long examId) throws Exception {

+ 20 - 68
src/main/java/cn/com/qmth/examcloud/app/service/NetExamService.java

@@ -13,7 +13,6 @@ import cn.com.qmth.examcloud.app.utils.DateUtils;
 import cn.com.qmth.examcloud.app.utils.HttpUtils;
 import okhttp3.MediaType;
 import okhttp3.MultipartBody;
-import okhttp3.Request;
 import okhttp3.RequestBody;
 import org.apache.commons.io.FilenameUtils;
 import org.slf4j.Logger;
@@ -22,7 +21,8 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.util.Assert;
 
-import static cn.com.qmth.examcloud.app.model.Constants.*;
+import static cn.com.qmth.examcloud.app.model.Constants.TYPE_PDF;
+import static cn.com.qmth.examcloud.app.model.Constants.TYPE_ZIP;
 
 /**
  * 网考业务服务接口
@@ -34,16 +34,9 @@ public class NetExamService {
     private PropertyService propertyService;
 
     public Result getCurrentTime(String key, String token) throws Exception {
-        final String requestUrl = propertyService.getNetExamUrl() + "/api/online_exam_course/currentTime";
         //封装请求参数
-        Request request = new Request.Builder()
-                .url(requestUrl)
-                .get()
-                .addHeader(PARAM_KEY, key)
-                .addHeader(PARAM_TOKEN, token)
-                .build();
-        //执行请求
-        Result<String> result = HttpUtils.call(request);
+        final String requestUrl = propertyService.getNetExamUrl() + "/api/online_exam_course/currentTime";
+        Result<String> result = HttpUtils.doGet(requestUrl, key, token);
         if (result.isSuccess()) {
             //转换日期格式
             result.setData(DateUtils.format(result.getData()));
@@ -52,29 +45,15 @@ public class NetExamService {
     }
 
     public Result getOfflineExamCourseList(String key, String token) throws Exception {
-        final String requestUrl = propertyService.getNetExamUrl() + "/api/offline_exam/getOfflineCourse";
         //封装请求参数
-        Request request = new Request.Builder()
-                .url(requestUrl)
-                .get()
-                .addHeader(PARAM_KEY, key)
-                .addHeader(PARAM_TOKEN, token)
-                .build();
-        //执行请求
-        return HttpUtils.call(request);
+        final String requestUrl = propertyService.getNetExamUrl() + "/api/offline_exam/getOfflineCourse";
+        return HttpUtils.doGet(requestUrl, key, token);
     }
 
     public Result startOfflineExamRecord(String key, String token, String examStudentId) throws Exception {
-        final String requestUrl = String.format(propertyService.getNetExamUrl() + "/api/offline_exam/start?examStudentId=%s", examStudentId);
         //封装请求参数
-        Request request = new Request.Builder()
-                .url(requestUrl)
-                .get()
-                .addHeader(PARAM_KEY, key)
-                .addHeader(PARAM_TOKEN, token)
-                .build();
-        //执行请求
-        return HttpUtils.call(request);
+        final String requestUrl = String.format(propertyService.getNetExamUrl() + "/api/offline_exam/start?examStudentId=%s", examStudentId);
+        return HttpUtils.doGet(requestUrl, key, token);
     }
 
     public Result uploadPaperAnswer(String key, String token, String examRecordId, byte[] fileBytes, String fileName, String md5) throws Exception {
@@ -87,59 +66,32 @@ public class NetExamService {
         if (fileBytes.length == 0) {
             throw new ApiException("File must be not empty.");
         }
+        //封装请求参数
         final String requestUrl = String.format(propertyService.getNetExamUrl() + "/api/offline_exam/%s/submit", examRecordId);
-        MultipartBody.Builder formBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
+        MultipartBody.Builder form = new MultipartBody.Builder().setType(MultipartBody.FORM);
         RequestBody body = RequestBody.create(MediaType.parse("application/octet-stream"), fileBytes);
-        formBody.addFormDataPart("file", fileName, body);
-        formBody.addFormDataPart("fileType", fileType);
-        formBody.addFormDataPart("md5", md5);
-        Request request = new Request.Builder()
-                .url(requestUrl)
-                .post(formBody.build())
-                .addHeader(PARAM_KEY, key)
-                .addHeader(PARAM_TOKEN, token)
-                .build();
-        //执行请求
-        return HttpUtils.call(request);
+        form.addFormDataPart("file", fileName, body);
+        form.addFormDataPart("fileType", fileType);
+        form.addFormDataPart("md5", md5);
+        return HttpUtils.doPost(requestUrl, form.build(), key, token);
     }
 
     public Result getPracticeExamCourseList(String key, String token, String examId) throws Exception {
-        final String requestUrl = String.format(propertyService.getNetExamUrl() + "/api/practice_course?examId=%s", examId);
         //封装请求参数
-        Request request = new Request.Builder()
-                .url(requestUrl)
-                .get()
-                .addHeader(PARAM_KEY, key)
-                .addHeader(PARAM_TOKEN, token)
-                .build();
-        //执行请求
-        return HttpUtils.call(request);
+        final String requestUrl = String.format(propertyService.getNetExamUrl() + "/api/practice_course?examId=%s", examId);
+        return HttpUtils.doGet(requestUrl, key, token);
     }
 
     public Result getExamRecordHeartbeat(String key, String token, String examRecordId) throws Exception {
-        final String requestUrl = String.format(propertyService.getNetExamUrl() + "/api/exam_control/heartbeat?examRecordId=%s", examRecordId);
         //封装请求参数
-        Request request = new Request.Builder()
-                .url(requestUrl)
-                .get()
-                .addHeader(PARAM_KEY, key)
-                .addHeader(PARAM_TOKEN, token)
-                .build();
-        //执行请求
-        return HttpUtils.call(request);
+        final String requestUrl = String.format(propertyService.getNetExamUrl() + "/api/exam_control/heartbeat?examRecordId=%s", examRecordId);
+        return HttpUtils.doGet(requestUrl, key, token);
     }
 
     public Result startPracticeExamRecord(String key, String token, String examStudentId) throws Exception {
-        final String requestUrl = String.format(propertyService.getNetExamUrl() + "/api/exam_control/start?stu_exam_info_id=%s", examStudentId);
         //封装请求参数
-        Request request = new Request.Builder()
-                .url(requestUrl)
-                .get()
-                .addHeader(PARAM_KEY, key)
-                .addHeader(PARAM_TOKEN, token)
-                .build();
-        //执行请求
-        return HttpUtils.call(request);
+        final String requestUrl = String.format(propertyService.getNetExamUrl() + "/api/exam_control/start?stu_exam_info_id=%s", examStudentId);
+        return HttpUtils.doGet(requestUrl, key, token);
     }
 
     public Result getExamRecordPaperStructList(String key, String token, String examRecordId) throws Exception {

+ 2 - 13
src/main/java/cn/com/qmth/examcloud/app/service/QuestionPoolService.java

@@ -9,15 +9,11 @@ package cn.com.qmth.examcloud.app.service;
 
 import cn.com.qmth.examcloud.app.model.Result;
 import cn.com.qmth.examcloud.app.utils.HttpUtils;
-import okhttp3.Request;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import static cn.com.qmth.examcloud.app.model.Constants.PARAM_KEY;
-import static cn.com.qmth.examcloud.app.model.Constants.PARAM_TOKEN;
-
 /**
  * 题库业务服务接口
  */
@@ -33,16 +29,9 @@ public class QuestionPoolService {
     }
 
     public Result getPaperDetail(String key, String token, String paperId) throws Exception {
-        final String requestUrl = String.format(propertyService.getQuestionPoolUrl() + "/api/ecs_ques/paper/%s", paperId);
         //封装请求参数
-        Request request = new Request.Builder()
-                .url(requestUrl)
-                .get()
-                .addHeader(PARAM_KEY, key)
-                .addHeader(PARAM_TOKEN, token)
-                .build();
-        //执行请求
-        return HttpUtils.call(request);
+        final String requestUrl = String.format(propertyService.getQuestionPoolUrl() + "/api/ecs_ques/paper/%s", paperId);
+        return HttpUtils.doGet(requestUrl, key, token);
     }
 
 }

+ 8 - 28
src/main/java/cn/com/qmth/examcloud/app/service/UserAuthService.java

@@ -34,8 +34,8 @@ public class UserAuthService {
     private PropertyService propertyService;
 
     public Result login(String account, String password, Long rootOrgId) throws Exception {
-        final String requestUrl = propertyService.getUserAuthUrl() + "/api/ecs_core/auth/login";
         //封装请求参数
+        final String requestUrl = propertyService.getUserAuthUrl() + "/api/ecs_core/auth/login";
         Map<String, String> params = new HashMap<>();
         params.put("accountValue", account);
         params.put("password", password);
@@ -54,7 +54,7 @@ public class UserAuthService {
         if (response.isSuccessful()) {
             return new Result().success(bodyStr);
         } else {
-            log.debug(bodyStr);
+            log.warn("Http response is " + bodyStr);
             ResBody body = new JsonMapper().fromJson(bodyStr, ResBody.class);
             if (body != null && body.getCode() != null) {
                 return new Result().error(body.getDesc());
@@ -64,53 +64,33 @@ public class UserAuthService {
     }
 
     public Result logout(String key, String token) throws Exception {
+        //封装请求参数
         final String requestUrl = propertyService.getUserAuthUrl() + "/api/ecs_core/auth/logout";
         RequestBody formBody = new FormBody.Builder()
                 .add(PARAM_KEY, key)
                 .add(PARAM_TOKEN, token)
                 .build();
-        Request request = new Request.Builder()
-                .url(requestUrl)
-                .post(formBody)
-                .addHeader(PARAM_KEY, key)
-                .addHeader(PARAM_TOKEN, token)
-                .build();
-        //执行请求
-        return HttpUtils.call(request);
+        return HttpUtils.doPost(requestUrl, formBody, key, token);
     }
 
-
     public Result getUserInfo(String key, String token) throws Exception {
+        //封装请求参数
         final String requestUrl = propertyService.getUserAuthUrl() + "/api/ecs_core/auth/getLoginUser";
         RequestBody formBody = new FormBody.Builder()
                 .add(PARAM_KEY, key)
                 .add(PARAM_TOKEN, token)
                 .build();
-        Request request = new Request.Builder()
-                .url(requestUrl)
-                .post(formBody)
-                .addHeader(PARAM_KEY, key)
-                .addHeader(PARAM_TOKEN, token)
-                .build();
-        //执行请求
-        return HttpUtils.call(request);
+        return HttpUtils.doPost(requestUrl, formBody, key, token);
     }
 
     public Result updatePassword(String key, String token, Long userId, String password) throws Exception {
-        final String requestUrl = propertyService.getUserAuthUrl() + "/api/ecs_core/user/password";
         //封装请求参数
+        final String requestUrl = propertyService.getUserAuthUrl() + "/api/ecs_core/user/password";
         RequestBody formBody = new FormBody.Builder()
                 .add("userId", String.valueOf(userId))
                 .add("password", password)
                 .build();
-        Request request = new Request.Builder()
-                .url(requestUrl)
-                .put(formBody)
-                .addHeader(PARAM_KEY, key)
-                .addHeader(PARAM_TOKEN, token)
-                .build();
-        //执行请求
-        return HttpUtils.call(request);
+        return HttpUtils.doPut(requestUrl, formBody, key, token);
     }
 
 }

+ 40 - 0
src/main/java/cn/com/qmth/examcloud/app/utils/HttpUtils.java

@@ -11,13 +11,53 @@ import cn.com.qmth.examcloud.app.model.Constants;
 import cn.com.qmth.examcloud.app.model.ResBody;
 import cn.com.qmth.examcloud.app.model.Result;
 import okhttp3.Request;
+import okhttp3.RequestBody;
 import okhttp3.Response;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static cn.com.qmth.examcloud.app.model.Constants.PARAM_KEY;
+import static cn.com.qmth.examcloud.app.model.Constants.PARAM_TOKEN;
+
 public class HttpUtils {
     private static Logger log = LoggerFactory.getLogger(HttpUtils.class);
 
+    public static Result<String> doGet(String requestUrl, String key, String token) throws Exception {
+        //封装请求参数
+        Request request = new Request.Builder()
+                .url(requestUrl)
+                .get()
+                .addHeader(PARAM_KEY, key)
+                .addHeader(PARAM_TOKEN, token)
+                .build();
+        //执行请求
+        return call(request);
+    }
+
+    public static Result<String> doPost(String requestUrl, RequestBody formBody, String key, String token) throws Exception {
+        //封装请求参数
+        Request request = new Request.Builder()
+                .url(requestUrl)
+                .post(formBody)
+                .addHeader(PARAM_KEY, key)
+                .addHeader(PARAM_TOKEN, token)
+                .build();
+        //执行请求
+        return call(request);
+    }
+
+    public static Result<String> doPut(String requestUrl, RequestBody formBody, String key, String token) throws Exception {
+        //封装请求参数
+        Request request = new Request.Builder()
+                .url(requestUrl)
+                .put(formBody)
+                .addHeader(PARAM_KEY, key)
+                .addHeader(PARAM_TOKEN, token)
+                .build();
+        //执行请求
+        return call(request);
+    }
+
     public static Result<String> call(Request request) throws Exception {
         Response response = HttpBuilder.client.getInstance().newCall(request).execute();
         String bodyStr = response.body().string();