Browse Source

swjtu-getScore

deason 10 months ago
parent
commit
e849140b6b

+ 155 - 0
examcloud-exchange-api-provider/src/main/java/cn/com/qmth/examcloud/exchange/outer/api/controller/swjtu/SwjtuController.java

@@ -0,0 +1,155 @@
+package cn.com.qmth.examcloud.exchange.outer.api.controller.swjtu;
+
+import cn.com.qmth.examcloud.commons.exception.StatusException;
+import cn.com.qmth.examcloud.commons.util.JsonUtil;
+import cn.com.qmth.examcloud.commons.util.StringUtil;
+import cn.com.qmth.examcloud.core.oe.admin.api.ExamScoreDataCloudService;
+import cn.com.qmth.examcloud.core.oe.admin.api.bean.ScoreDataBean;
+import cn.com.qmth.examcloud.core.oe.admin.api.request.GetFinalScoreDataReq;
+import cn.com.qmth.examcloud.core.oe.admin.api.response.GetFinalScoreDataResp;
+import cn.com.qmth.examcloud.support.cache.CacheHelper;
+import cn.com.qmth.examcloud.support.cache.bean.OrgPropertyCacheBean;
+import cn.com.qmth.examcloud.web.support.ControllerSupport;
+import cn.com.qmth.examcloud.web.support.Naked;
+import com.google.common.collect.Maps;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.crypto.Cipher;
+import javax.crypto.spec.SecretKeySpec;
+import javax.servlet.http.HttpServletRequest;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 西南交通大学
+ * swjtu.ecs.qmth.com.cn
+ *
+ * @author wangwei
+ * @date 2019年10月16日 下午4:42
+ */
+@RestController
+@RequestMapping("/api/ecs_outlet/swjtu")
+public class SwjtuController extends ControllerSupport {
+
+    private static final Logger log = LoggerFactory.getLogger(SwjtuController.class);
+
+    private static final String ENTRY_SPLITE = ";";
+
+    private static final String KV_SPLITE = "=";
+
+    private static final String AES = "AES";
+
+    private static final String CRYPT_KEY = "ZwFOcwz6QtIQ983m";
+
+    @Autowired
+    private ExamScoreDataCloudService examScoreDataCloudService;
+
+    @Naked
+    @PostMapping("getScore")
+    public ResponseEntity<?> getScore(
+            @RequestParam String studentCode, @RequestParam String subjectCode,
+            @RequestParam(required = false) Long examId,
+            @RequestParam(required = false, defaultValue = "true") boolean encrypt,
+            @RequestParam(required = false, defaultValue = "false") boolean detail,
+            HttpServletRequest request) {
+
+        log.warn("[swjtu-getScore] studentCode:{} subjectCode:{} examId:{} encrypt:{} detail:{}", studentCode, subjectCode, examId, encrypt, detail);
+
+        String authInfo = request.getHeader("auth-info");
+        Map<String, String> infoMap = buildInfoMap(authInfo);
+        String loginname = infoMap.get("loginname");
+        String password = infoMap.get("password");
+
+        Long rootOrgId = 718L;
+        String loginnameConf = "swjtu";
+        String passwordConf = "ioweriwo2349ji0834j5n2j0rj";
+
+        if (null == examId) {
+            OrgPropertyCacheBean orgPropertyCacheBean = CacheHelper.getOrgProperty(rootOrgId, "THIRD_PARTY_API_DEFAULT_EXAM_ID");
+            String value = orgPropertyCacheBean.getValue();
+            if (StringUtils.isBlank(value)) {
+                throw new StatusException("001005", "examId is not configured");
+            }
+            examId = StringUtil.toLong(value);
+        }
+
+        if (!loginnameConf.equals(loginname) || !passwordConf.equals(password)) {
+            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
+        }
+
+        Map<String, Object> map = Maps.newHashMap();
+        try {
+            GetFinalScoreDataReq gfsdReq = new GetFinalScoreDataReq();
+            gfsdReq.setCourseCode(subjectCode);
+            gfsdReq.setExamId(examId);
+            gfsdReq.setRootOrgId(rootOrgId);
+            gfsdReq.setStudentCode(studentCode);
+            GetFinalScoreDataResp gfsdReqResp = examScoreDataCloudService.getFinalScoreData(gfsdReq);
+
+            ScoreDataBean scoreDataBean = gfsdReqResp.getScoreDataBean();
+            if (null == scoreDataBean) {
+                map.put("exist", false);
+            } else {
+                map.put("exist", true);
+                map.put("totalScore", String.valueOf(scoreDataBean.getTotalScore()));
+                map.put("examId", String.valueOf(scoreDataBean.getExamId()));
+                map.put("studentCode", scoreDataBean.getStudentCode());
+            }
+        } catch (Exception e) {
+            map.put("exception", "500");
+        }
+
+        String result = JsonUtil.toJson(map);
+        if (encrypt) {
+            result = encrypt(JsonUtil.toJson(result));
+        }
+
+        return ResponseEntity.status(HttpStatus.OK).body(result);
+    }
+
+    private Map<String, String> buildInfoMap(String infoString) {
+        Map<String, String> infoMap = new HashMap<>();
+
+        String[] values = StringUtils.split(infoString, ENTRY_SPLITE);
+        if (values != null && values.length > 0) {
+            for (String value : values) {
+                String[] pair = StringUtils.split(value, KV_SPLITE);
+                if (pair != null && pair.length == 2) {
+                    infoMap.put(pair[0].trim(), pair[1].trim());
+                }
+            }
+        }
+
+        return infoMap;
+    }
+
+    @SuppressWarnings("restriction")
+    public static String byte2base64(byte[] b) {
+        return new sun.misc.BASE64Encoder().encode(b);
+    }
+
+    public final static String encrypt(String data) {
+        try {
+            return byte2base64(encrypt(data.getBytes("ISO-8859-1"), CRYPT_KEY));
+        } catch (Exception e) {
+        }
+        return null;
+    }
+
+    public static byte[] encrypt(byte[] src, String key) throws Exception {
+        Cipher cipher = Cipher.getInstance(AES);
+        SecretKeySpec securekey = new SecretKeySpec(key.getBytes(), AES);
+        cipher.init(Cipher.ENCRYPT_MODE, securekey);// 设置密钥和加密形式
+        return cipher.doFinal(src);
+    }
+
+}