فهرست منبع

用户管理、学校管理

xiaof 3 سال پیش
والد
کامیت
4044f28c10

+ 22 - 0
src/main/java/cn/com/qmth/print/manage/config/DictionaryConfig.java

@@ -0,0 +1,22 @@
+package cn.com.qmth.print.manage.config;
+
+import cn.com.qmth.print.manage.config.domain.SolarDomain;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class DictionaryConfig {
+
+    /**
+     * 中心配置
+     *
+     * @return
+     */
+    @Bean
+    @ConfigurationProperties(prefix = "qmth.solar")
+    public SolarDomain solarDomain() {
+        return new SolarDomain();
+    }
+
+}

+ 46 - 0
src/main/java/cn/com/qmth/print/manage/config/domain/SolarDomain.java

@@ -0,0 +1,46 @@
+package cn.com.qmth.print.manage.config.domain;
+
+import java.io.Serializable;
+
+public class SolarDomain implements Serializable {
+
+    String accessKey;
+
+    String accessSecret;
+
+    String host;
+
+    String uri;
+
+    public String getAccessKey() {
+        return accessKey;
+    }
+
+    public void setAccessKey(String accessKey) {
+        this.accessKey = accessKey;
+    }
+
+    public String getAccessSecret() {
+        return accessSecret;
+    }
+
+    public void setAccessSecret(String accessSecret) {
+        this.accessSecret = accessSecret;
+    }
+
+    public String getHost() {
+        return host;
+    }
+
+    public void setHost(String host) {
+        this.host = host;
+    }
+
+    public String getUri() {
+        return uri;
+    }
+
+    public void setUri(String uri) {
+        this.uri = uri;
+    }
+}

+ 35 - 9
src/main/java/cn/com/qmth/print/manage/controller/OrgController.java

@@ -1,15 +1,14 @@
 package cn.com.qmth.print.manage.controller;
 
-import javax.annotation.Resource;
-
+import cn.com.qmth.print.manage.service.OrgService;
+import cn.com.qmth.print.manage.service.query.OrgQuery;
+import com.qmth.boot.api.constant.ApiConstant;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
 
-import cn.com.qmth.print.manage.service.OrgService;
-import cn.com.qmth.print.manage.service.query.OrgQuery;
-
-import com.qmth.boot.api.constant.ApiConstant;
+import javax.annotation.Resource;
 
 @RestController
 @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/org")
@@ -18,9 +17,36 @@ public class OrgController {
     @Resource
     private OrgService orgService;
 
-    @RequestMapping("/query")
-    public Object appInfo(@Validated OrgQuery query) {
-        return orgService.list();
+    /**
+     * 学校分页查询
+     *
+     * @param query
+     * @return
+     */
+    @RequestMapping(value = "/page", method = RequestMethod.POST)
+    public Object page(@Validated OrgQuery query) {
+        return orgService.pageQuery(query);
+    }
+
+    /**
+     * 学校查询
+     *
+     * @return
+     */
+    @RequestMapping(value = "/list", method = RequestMethod.POST)
+    public Object list() {
+        return orgService.listByEnable();
+    }
+
+    /**
+     * 同步学校
+     *
+     * @return
+     */
+    @RequestMapping(value = "/pull", method = RequestMethod.POST)
+    public Object pull() {
+        orgService.pull();
+        return true;
     }
 
 }

+ 65 - 0
src/main/java/cn/com/qmth/print/manage/controller/UserController.java

@@ -0,0 +1,65 @@
+package cn.com.qmth.print.manage.controller;
+
+import cn.com.qmth.print.manage.entity.UserEntity;
+import cn.com.qmth.print.manage.service.UserService;
+import cn.com.qmth.print.manage.service.query.UserQuery;
+import com.qmth.boot.api.constant.ApiConstant;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.Resource;
+
+/**
+ * @Date: 2021/11/16.
+ */
+@RestController
+@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/user")
+public class UserController {
+
+    @Resource
+    private UserService userService;
+
+    /**
+     * 用户分页查询
+     *
+     * @param query
+     * @return
+     */
+    @RequestMapping(value = "/page", method = RequestMethod.POST)
+    public Object page(@Validated UserQuery query) {
+        return userService.pageQuery(query);
+    }
+
+    /**
+     * 用户查询
+     *
+     * @return
+     */
+    @RequestMapping(value = "/list", method = RequestMethod.POST)
+    public Object list() {
+        return userService.listByEnable();
+    }
+
+
+    /**
+     * 新增/修改用户
+     *
+     * @param user
+     * @return
+     */
+    @RequestMapping(value = "/save", method = RequestMethod.POST)
+    public Object save(UserEntity user) {
+        if (user.getId() != null) {
+            UserEntity userEntity = userService.getById(user.getId());
+            if (userEntity == null) {
+                throw new RuntimeException("用户不存在");
+            }
+            if (!user.getLoginName().equals(userEntity.getLoginName())) {
+                throw new RuntimeException("用户名不能修改");
+            }
+        }
+        return userService.saveOrUpdate(user);
+    }
+}

+ 8 - 0
src/main/java/cn/com/qmth/print/manage/dao/UserDao.java

@@ -0,0 +1,8 @@
+package cn.com.qmth.print.manage.dao;
+
+import cn.com.qmth.print.manage.entity.UserEntity;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+public interface UserDao extends BaseMapper<UserEntity> {
+
+}

+ 9 - 0
src/main/java/cn/com/qmth/print/manage/service/OrgService.java

@@ -2,8 +2,17 @@ package cn.com.qmth.print.manage.service;
 
 import cn.com.qmth.print.manage.entity.OrgEntity;
 
+import cn.com.qmth.print.manage.service.query.OrgQuery;
+import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.service.IService;
 
+import java.util.List;
+
 public interface OrgService extends IService<OrgEntity> {
 
+    IPage<OrgEntity> pageQuery(OrgQuery query);
+
+    List<OrgEntity> listByEnable();
+
+    void pull();
 }

+ 15 - 0
src/main/java/cn/com/qmth/print/manage/service/UserService.java

@@ -0,0 +1,15 @@
+package cn.com.qmth.print.manage.service;
+
+import cn.com.qmth.print.manage.entity.UserEntity;
+import cn.com.qmth.print.manage.service.query.UserQuery;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+import java.util.List;
+
+public interface UserService extends IService<UserEntity> {
+
+    IPage<UserEntity> pageQuery(UserQuery query);
+
+    List<UserEntity> listByEnable();
+}

+ 94 - 3
src/main/java/cn/com/qmth/print/manage/service/impl/OrgServiceImpl.java

@@ -1,14 +1,105 @@
 package cn.com.qmth.print.manage.service.impl;
 
-import org.springframework.stereotype.Service;
-
 import cn.com.qmth.print.manage.dao.OrgDao;
 import cn.com.qmth.print.manage.entity.OrgEntity;
 import cn.com.qmth.print.manage.service.OrgService;
-
+import cn.com.qmth.print.manage.service.query.OrgQuery;
+import cn.com.qmth.print.manage.utils.SolarUtils;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.CollectionUtils;
+
+import javax.annotation.Resource;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.stream.Collectors;
 
 @Service
 public class OrgServiceImpl extends ServiceImpl<OrgDao, OrgEntity> implements OrgService {
 
+    @Resource
+    private OrgDao orgDao;
+
+    @Autowired
+    private SolarUtils solarUtils;
+
+    @Override
+    public IPage<OrgEntity> pageQuery(OrgQuery query) {
+        QueryWrapper<OrgEntity> queryWrapper = new QueryWrapper<>();
+        if (StringUtils.isNotEmpty(query.getCode())) {
+            queryWrapper.lambda().like(OrgEntity::getCode, query.getCode());
+        }
+        if (StringUtils.isNotEmpty(query.getName())) {
+            queryWrapper.lambda().like(OrgEntity::getName, query.getName());
+        }
+        queryWrapper.lambda().orderByAsc(OrgEntity::getId);
+        Page<OrgEntity> page = new Page<>(query.getPageNumber(), query.getPageSize());
+        return orgDao.selectPage(page, queryWrapper);
+    }
+
+    @Override
+    public List<OrgEntity> listByEnable() {
+        QueryWrapper<OrgEntity> queryWrapper = new QueryWrapper<>();
+        queryWrapper.lambda().eq(OrgEntity::getEnable, true).orderByAsc(OrgEntity::getId);
+        return orgDao.selectList(queryWrapper);
+    }
+
+    @Transactional
+    @Override
+    public void pull() {
+        List<Map> mapList = solarUtils.pullSchool();
+        List<OrgEntity> orgEntities = orgDao.selectList(null);
+        List<OrgEntity> notExist;
+        if (CollectionUtils.isEmpty(mapList)) {
+            notExist = orgEntities;
+        } else {
+            List<String> codeList = mapList.stream().map(m -> m.get("code").toString()).collect(Collectors.toList());
+            notExist = orgEntities.stream().map(m -> {
+                if (!codeList.contains(m.getCode())) {
+                    return m;
+                }
+                return null;
+            }).filter(Objects::nonNull).collect(Collectors.toList());
+
+            for (Map map : mapList) {
+                Long orgId = Long.valueOf(String.valueOf(map.get("id"))); // 机构中心的id
+                String code = String.valueOf(map.get("code"));
+                String name = String.valueOf(map.get("name"));
+                String logo = String.valueOf(map.get("logo"));
+                String accessKey = String.valueOf(map.get("accessKey"));
+                String accessSecret = String.valueOf(map.get("accessSecret"));
+
+                OrgEntity entity = orgEntities.stream().filter(m -> m.getCode().equals(code)).findFirst().get();
+                if (entity == null) {
+                    entity = new OrgEntity();
+                    // 中心的学校ID被占用,则使用自增ID
+                    OrgEntity orgById = orgDao.selectById(orgId);
+                    if (orgById == null) {
+                        entity.setId(orgId);
+                    }
+                }
+                entity.setCode(code);
+                entity.setName(name);
+                entity.setAccessKey(accessKey);
+                entity.setAccessSecret(accessSecret);
+                entity.setLogoUrl(logo);
+                entity.setEnable(true);
+                this.saveOrUpdate(entity);
+            }
+        }
+        // 不在中心的学校,本地全部禁用
+        if (!CollectionUtils.isEmpty(notExist)) {
+            for (OrgEntity orgEntity : notExist) {
+                orgEntity.setEnable(false);
+                orgDao.updateById(orgEntity);
+            }
+        }
+    }
 }

+ 46 - 0
src/main/java/cn/com/qmth/print/manage/service/impl/UserServiceImpl.java

@@ -0,0 +1,46 @@
+package cn.com.qmth.print.manage.service.impl;
+
+import cn.com.qmth.print.manage.dao.UserDao;
+import cn.com.qmth.print.manage.entity.UserEntity;
+import cn.com.qmth.print.manage.service.UserService;
+import cn.com.qmth.print.manage.service.query.UserQuery;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.List;
+
+/**
+ * @Date: 2021/11/16.
+ */
+@Service
+public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {
+
+    @Resource
+    private UserDao userDao;
+
+    @Override
+    public IPage<UserEntity> pageQuery(UserQuery query) {
+        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
+        if (StringUtils.isNotEmpty(query.getLoginName())) {
+            queryWrapper.lambda().like(UserEntity::getLoginName, query.getLoginName());
+        }
+        if (query.getRole() != null) {
+            queryWrapper.lambda().eq(UserEntity::getRole, query.getRole());
+        }
+        queryWrapper.lambda().eq(UserEntity::isEnable, query.isEnable()).orderByAsc(UserEntity::getId);
+        Page<UserEntity> page = new Page<>(query.getPageNumber(), query.getPageSize());
+        return userDao.selectPage(page, queryWrapper);
+    }
+
+    @Override
+    public List<UserEntity> listByEnable() {
+        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
+        queryWrapper.lambda().eq(UserEntity::isEnable, true);
+        return userDao.selectList(queryWrapper);
+    }
+}

+ 59 - 0
src/main/java/cn/com/qmth/print/manage/service/query/UserQuery.java

@@ -0,0 +1,59 @@
+package cn.com.qmth.print.manage.service.query;
+
+import cn.com.qmth.print.manage.entity.UserEntity;
+import cn.com.qmth.print.manage.enums.RoleMeta;
+import com.qmth.boot.mybatis.query.BaseQuery;
+import org.springframework.validation.annotation.Validated;
+
+import javax.validation.constraints.Max;
+import javax.validation.constraints.Min;
+import javax.validation.constraints.NotNull;
+
+@Validated
+public class UserQuery extends BaseQuery<UserEntity> {
+
+    private static final long serialVersionUID = 3260496142491163981L;
+
+    private String loginName;
+
+    private RoleMeta role;
+
+    @NotNull
+    private boolean enable;
+
+    public String getLoginName() {
+        return loginName;
+    }
+
+    public void setLoginName(String loginName) {
+        this.loginName = loginName;
+    }
+
+    public RoleMeta getRole() {
+        return role;
+    }
+
+    public void setRole(RoleMeta role) {
+        this.role = role;
+    }
+
+    public boolean isEnable() {
+        return enable;
+    }
+
+    public void setEnable(boolean enable) {
+        this.enable = enable;
+    }
+
+    @Min(1)
+    public long getPageNumber() {
+        return super.getPageNumber();
+    }
+
+    @Min(1)
+    @Max(100)
+    public long getPageSize() {
+        return super.getPageSize();
+    }
+
+}

+ 87 - 0
src/main/java/cn/com/qmth/print/manage/utils/HttpUtils.java

@@ -0,0 +1,87 @@
+package cn.com.qmth.print.manage.utils;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.Map;
+
+public class HttpUtils {
+
+    /**
+     * 向指定 URL 发送POST方法的请求
+     *
+     * @param url    发送请求的 URL
+     * @param params 请求的参数集合
+     * @return 远程资源的响应结果
+     */
+    @SuppressWarnings("unused")
+    public static String sendPost(String url, Map<String, String> params, Map<String, String> requestHeader) {
+        OutputStreamWriter out = null;
+        BufferedReader in = null;
+        StringBuilder result = new StringBuilder();
+        try {
+            URL realUrl = new URL(url);
+            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
+            // 发送POST请求必须设置如下两行
+            conn.setDoOutput(true);
+            conn.setDoInput(true);
+            // POST方法
+            conn.setRequestMethod("POST");
+            // 设置通用的请求属性
+            conn.setRequestProperty("accept", "*/*");
+            conn.setRequestProperty("connection", "Keep-Alive");
+            conn.setRequestProperty("user-agent",
+                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
+            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
+            if (requestHeader != null && requestHeader.size() > 0) {
+                for (Map.Entry<String, String> entry : requestHeader.entrySet()) {
+                    conn.setRequestProperty(entry.getKey(), entry.getValue());
+                }
+            }
+            conn.connect();
+            // 获取URLConnection对象对应的输出流
+            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
+            // 发送请求参数
+            if (params != null) {
+                StringBuilder param = new StringBuilder();
+                for (Map.Entry<String, String> entry : params.entrySet()) {
+                    if (param.length() > 0) {
+                        param.append("&");
+                    }
+                    param.append(entry.getKey());
+                    param.append("=");
+                    param.append(entry.getValue());
+                }
+                out.write(param.toString());
+            }
+            // flush输出流的缓冲
+            out.flush();
+            // 定义BufferedReader输入流来读取URL的响应
+            in = new BufferedReader(
+                    new InputStreamReader(conn.getInputStream(), "UTF-8"));
+            String line;
+            while ((line = in.readLine()) != null) {
+                result.append(line);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        //使用finally块来关闭输出流、输入流
+        finally {
+            try {
+                if (out != null) {
+                    out.close();
+                }
+                if (in != null) {
+                    in.close();
+                }
+            } catch (IOException ex) {
+                ex.printStackTrace();
+            }
+        }
+        return result.toString();
+    }
+
+}

+ 111 - 0
src/main/java/cn/com/qmth/print/manage/utils/SolarUtils.java

@@ -0,0 +1,111 @@
+package cn.com.qmth.print.manage.utils;
+
+import cn.com.qmth.print.manage.config.DictionaryConfig;
+import com.qmth.boot.tools.signature.SignatureEntity;
+import com.qmth.boot.tools.signature.SignatureType;
+import net.sf.json.JSONObject;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * 同步云阅卷接口工具类
+ * <p>
+ * Date: 2021/10/29.
+ */
+@Component
+public class SolarUtils {
+    private final static Logger log = LoggerFactory.getLogger(SolarUtils.class);
+
+    // 所有请求方法默认为"POST"
+    private static final String POST_METHOD = "POST";
+    private static final String HEADER_AUTHORIZATION = "Authorization";
+    private static final String HEADER_TIME = "time";
+
+    private final int DEFAULT_PAGE_NUMBER = 1;
+    private final int DEFAULT_PAGE_SIZE = 100;
+
+    @Autowired
+    private DictionaryConfig dictionaryConfig;
+
+    /**
+     * 同步学校
+     *
+     * @return
+     */
+    public List<Map> pullSchool() {
+        String host = dictionaryConfig.solarDomain().getHost();
+        String uri = dictionaryConfig.solarDomain().getUri();
+        validUrl(host, uri);
+        String postUrl = host.concat(uri);
+        List<Map> finalList = new ArrayList<>();
+        try {
+            AtomicInteger integer = new AtomicInteger(DEFAULT_PAGE_NUMBER);
+            while (true) {
+                //参数
+                Map<String, String> map = new HashMap<>();
+                map.put("pageNumber", String.valueOf(integer.getAndIncrement()));
+                map.put("pageSize", String.valueOf(DEFAULT_PAGE_SIZE));
+
+                String result = HttpUtils.sendPost(postUrl, map, getHeaders(uri));
+                JSONObject jsonObject = new JSONObject();
+                List<Map> mapList = jsonObject.getJSONArray(result);
+                if (mapList.size() == 0) {
+                    break;
+                }
+                finalList.addAll(mapList);
+            }
+        } catch (Exception e) {
+            throw new RuntimeException("请求异常:" + e.getMessage());
+        }
+        return finalList;
+    }
+
+    /**
+     * http请求头
+     *
+     * @param url 请求URL
+     */
+    private Map<String, String> getHeaders(String url) {
+        long time = System.currentTimeMillis();
+        Map<String, String> header = new HashMap<>();
+        header.put(HEADER_AUTHORIZATION, createSign(time, url));
+        header.put(HEADER_TIME, String.valueOf(time));
+        return header;
+    }
+
+    /**
+     * 签名
+     *
+     * @param time 时间戳
+     * @param url  请求URL
+     */
+    private String createSign(long time, String url) {
+        String accessKey = dictionaryConfig.solarDomain().getAccessKey();
+        String accessSecret = dictionaryConfig.solarDomain().getAccessSecret();
+        if (StringUtils.isAnyBlank(accessKey, accessSecret)) {
+            throw new RuntimeException("accessKey或者accessSecret未配置");
+        }
+        return SignatureEntity.build(SignatureType.SECRET, POST_METHOD, url, time, accessKey, accessSecret);
+    }
+
+    /**
+     * 校验url是否配置
+     *
+     * @param urls URL数组
+     */
+    private void validUrl(String... urls) {
+        if (StringUtils.isAnyBlank(urls)) {
+            throw new RuntimeException("请求URL未配置");
+        }
+    }
+
+}

+ 2 - 2
src/main/resources/application.properties

@@ -29,7 +29,7 @@ spring.datasource.hikari.maximum-pool-size=10
 #
 # ********** sys config **********
 #
-qmth.solar.accessKey=
-qmth.solar.accessSecret=
+qmth.solar.accessKey=274f823e5f59410f8b3bb6edcd8e2b6e
+qmth.solar.accessSecret=y7AO6W0TOdTF8HpWBwGHbp3wfIHsmUKr
 qmth.solar.host=https://solar.qmth.com.cn
 qmth.solar.uri=/api/open/org/query