Ver Fonte

排课工具单点登录推送

dzx há 1 ano atrás
pai
commit
32b98798f9

+ 35 - 17
src/main/java/com/xjrsoft/module/schedule/controller/ScheduleController.java

@@ -4,17 +4,15 @@ import cn.dev33.satoken.annotation.SaCheckPermission;
 import cn.dev33.satoken.stp.StpUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
-import cn.hutool.db.Db;
 import com.alibaba.excel.support.ExcelTypeEnum;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
-import com.xjrsoft.common.constant.GlobalConstant;
 import com.xjrsoft.common.enums.DeleteMark;
 import com.xjrsoft.common.enums.EnabledMark;
 import com.xjrsoft.common.model.result.RT;
-import com.xjrsoft.common.utils.DatasourceUtil;
 import com.xjrsoft.module.courseTable.entity.CourseTable;
 import com.xjrsoft.module.courseTable.service.ICourseTableService;
 import com.xjrsoft.module.schedule.dto.CourseTableAdjustDto;
@@ -22,39 +20,34 @@ import com.xjrsoft.module.schedule.dto.CourseTableDto;
 import com.xjrsoft.module.schedule.dto.CourseTablePreCheckDto;
 import com.xjrsoft.module.schedule.dto.ScheduleWeekExportQueryDto;
 import com.xjrsoft.module.schedule.entity.CourseReceiveMsg;
-import com.xjrsoft.module.schedule.entity.JianyueData;
 import com.xjrsoft.module.schedule.service.ICourseReceiveMsgService;
-import com.xjrsoft.module.schedule.service.IJianyueDataService;
-import com.xjrsoft.module.schedule.util.DataUtil;
 import com.xjrsoft.module.schedule.util.ScheduleUtil;
 import com.xjrsoft.module.schedule.vo.CourseListVo;
 import com.xjrsoft.module.schedule.vo.CourseTableVo;
 import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
 import com.xjrsoft.module.student.service.impl.IBaseStudentSchoolRollService;
 import com.xjrsoft.module.teacher.entity.BaseTeacher;
+import com.xjrsoft.module.teacher.entity.XjrUser;
 import com.xjrsoft.module.teacher.service.IBaseTeacherService;
-import com.xjrsoft.module.textbook.dto.TextbookClaimExportQueryDto;
+import com.xjrsoft.module.teacher.service.ITeacherbaseManagerService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.AllArgsConstructor;
-import org.apache.commons.lang3.concurrent.BasicThreadFactory;
 import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.*;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
 
-import javax.sql.DataSource;
 import javax.validation.Valid;
 import java.io.ByteArrayOutputStream;
-import java.sql.SQLException;
 import java.time.DayOfWeek;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
 import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.CompletableFuture;
 
 /**
 * @title: 课表
@@ -70,10 +63,11 @@ public class ScheduleController {
 
 
     private final ICourseReceiveMsgService courseReceiveMsgService;
-    private final IJianyueDataService jianyueDataService;
+//    private final IJianyueDataService jianyueDataService;
     private final ICourseTableService courseTableService;
     private final IBaseStudentSchoolRollService baseStudentSchoolRollService;
     private final IBaseTeacherService baseTeacherService;
+    private final ITeacherbaseManagerService teacherService;
 
     @GetMapping(value = "/receive-msg")
     @ApiOperation(value="接收消息")
@@ -245,4 +239,28 @@ public class ScheduleController {
         ByteArrayOutputStream bot = courseTableService.listScheduleWeekExportQuery(dto);
         return RT.fileStream(bot.toByteArray(), "scheduleWeek" + ExcelTypeEnum.XLSX.getValue());
     }
+
+    @GetMapping(value = "/access-token")
+    @ApiOperation(value = "获取单点登录地址")
+    @SaCheckPermission("evaluateobject:detail")
+    public RT<String> getAccessToken(CourseTablePreCheckDto dto) throws Exception {
+        XjrUser xjrUser = teacherService.getById(StpUtil.getLoginIdAsLong());
+        long timeMillis = System.currentTimeMillis();
+        String sign = ScheduleUtil.createSign(timeMillis);
+
+        JsonObject jsonObject = new JsonObject();
+        jsonObject.addProperty("mobileNo", xjrUser.getMobile());
+
+        String url = ScheduleUtil.apiUrl + "auth/user/token";
+        try {
+            String result = ScheduleUtil.doPost(url, jsonObject.toString(), sign, timeMillis);
+            JsonParser parser = new JsonParser();
+            JsonObject resultJson = parser.parse(result).getAsJsonObject();
+            JsonObject dataJson = resultJson.get("data").getAsJsonObject();
+            String loginUrl = ScheduleUtil.hostUrl + "?access_token=" + dataJson.get("access_token").getAsString();
+            return RT.ok(loginUrl);
+        }catch (Exception e){
+            return RT.error(e.getMessage());
+        }
+    }
 }

+ 1 - 0
src/main/java/com/xjrsoft/module/schedule/util/ScheduleUtil.java

@@ -26,6 +26,7 @@ public class ScheduleUtil {
     public static final String apiUrl = "http://219.153.208.33:20000/api/v1/ScheduleFlowV2/OpenApi/";
     public static final String password = "@ak8To$xSHFoT6FoqsqYb3";
     public static final String secert = "UUXQ8G4W9IU";
+    public static final String hostUrl = "http://219.153.208.33:20000";
 //    private static JianyuekbConfig jianyuekbConfig;
 
 //    public ScheduleUtil(JianyuekbConfig jianyuekbConfig){

+ 188 - 0
src/test/java/com/xjrsoft/xjrsoftboot/ScheduleLoginTest.java

@@ -0,0 +1,188 @@
+package com.xjrsoft.xjrsoftboot;
+
+import cn.dev33.satoken.secure.SaSecureUtil;
+import com.google.gson.JsonObject;
+import com.xjrsoft.XjrSoftApplication;
+import com.xjrsoft.module.teacher.mapper.XjrUserMapper;
+import org.junit.jupiter.api.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+/**
+ * @author dzx
+ * @date 2024/2/27
+ */
+//@RunWith(SpringRunner.class)
+//@SpringBootTest(classes = XjrSoftApplication.class)
+public class ScheduleLoginTest {
+//    @Autowired
+//    private XjrUserMapper userMapper;
+
+    public static final String ALGORITHM = "HmacSHA256";
+    @Test
+    void test() throws Exception {
+        long timestamp = System.currentTimeMillis();
+        System.out.println("timestamp:" + timestamp);
+        String md5Str = SaSecureUtil.md5("@ak8To$xSHFoT6FoqsqYb3" + timestamp);
+
+
+        String sign = calculateHMac("UUXQ8G4W9IU", md5Str);
+        System.out.println("sign:" + sign);
+
+        String phone = "15334561180";
+
+        String url = "https://live.jianyuekb.com/api/v1/ScheduleFlowV2/OpenApi/auth/user/token";
+        JsonObject jsonObject = new JsonObject();
+        jsonObject.addProperty("mobileNo", phone);
+
+//        String result = doPost(url, jsonObject.toString(), sign, timestamp);
+//        System.out.println(result);
+    }
+
+    public static String calculateHMac(String key, String data) throws Exception {
+        Mac sha256_HMAC = Mac.getInstance(ALGORITHM);
+
+        SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), ALGORITHM);
+        sha256_HMAC.init(secret_key);
+
+        return byteArrayToHex(sha256_HMAC.doFinal(data.getBytes("UTF-8")));
+    }
+
+    public static String byteArrayToHex(byte[] a) {
+        StringBuilder sb = new StringBuilder(a.length * 2);
+        for (byte b : a)
+            sb.append(String.format("%02x", b));
+        return sb.toString();
+    }
+
+
+    /**
+     * 将加密后的字节数组转换成字符串
+     *
+     * @param b 字节数组
+     * @return 字符串
+     */
+    private static String byteArrayToHexString(byte[] b) {
+        StringBuilder hs = new StringBuilder();
+        String stmp;
+        for (int n = 0; b != null && n < b.length; n++) {
+            stmp = Integer.toHexString(b[n] & 0XFF);
+            if (stmp.length() == 1)
+                hs.append('0');
+            hs.append(stmp);
+        }
+        return hs.toString().toLowerCase();
+    }
+
+    public static String doPost(String httpUrl, String param, String sign, Long timestamp) {
+        HttpURLConnection connection = null;
+        InputStream is = null;
+        OutputStream os = null;
+        BufferedReader br = null;
+        String result = null;
+        OutputStreamWriter writer = null;
+        try {
+            URL url = new URL(httpUrl);
+            // 通过远程url连接对象打开连接
+            connection = (HttpURLConnection) url.openConnection();
+            connection.setRequestProperty("schoolId","UUFM5TID45X");
+            connection.setRequestProperty("sign",sign);
+            connection.setRequestProperty("timestamp",String.format("%d",timestamp));
+            // 设置连接请求方式
+            connection.setRequestMethod("POST");
+            // 设置连接主机服务器超时时间:15000毫秒
+            connection.setConnectTimeout(15000);
+            // 设置读取主机服务器返回数据超时时间:60000毫秒
+            connection.setReadTimeout(60000);
+            // 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
+            connection.setDoOutput(true);
+            // 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
+            connection.setDoInput(true);
+            // 设置传入参数的格式:请求参数应该是 name1=value1&name2=value2 的形式。
+            connection.setRequestProperty("Content-Type", "application/json");
+            // 设置鉴权信息:Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0
+//            connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
+            writer = new OutputStreamWriter(connection.getOutputStream(),"UTF-8");
+            //body参数放这里
+            writer.write(param);
+            writer.flush();
+
+            writer.close();
+            // 通过连接对象获取一个输出流
+//            os = connection.getOutputStream();
+//            // 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的
+//            os.write(param.getBytes());
+            // 通过连接对象获取一个输入流,向远程读取
+            if (connection.getResponseCode() == 200) {
+                is = connection.getInputStream();
+                // 对输入流对象进行包装:charset根据工作项目组的要求来设置
+                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
+                StringBuffer sbf = new StringBuffer();
+                String temp = null;
+                // 循环遍历一行一行读取数据
+                while ((temp = br.readLine()) != null) {
+                    sbf.append(temp);
+                    sbf.append("\r\n");
+                }
+                result = sbf.toString();
+            }else{
+                is = connection.getInputStream();
+                // 对输入流对象进行包装:charset根据工作项目组的要求来设置
+                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
+                StringBuffer sbf = new StringBuffer();
+                String temp = null;
+                // 循环遍历一行一行读取数据
+                while ((temp = br.readLine()) != null) {
+                    sbf.append(temp);
+                    sbf.append("\r\n");
+                }
+                System.out.println(sbf.toString());
+            }
+        } catch (MalformedURLException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            // 关闭资源
+            if (null != br) {
+                try {
+                    br.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (null != os) {
+                try {
+                    os.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (null != is) {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            // 断开与远程地址url的连接
+            connection.disconnect();
+        }
+        return result;
+    }
+
+}