fanxp 1 year ago
parent
commit
57e3b2a4ac

+ 1 - 1
pom.xml

@@ -32,7 +32,7 @@
         <mybatisplus.join.version>1.4.2.2</mybatisplus.join.version>
         <mybatisplus.version>3.5.1</mybatisplus.version>
         <mybatisplus.generator.version>3.5.2</mybatisplus.generator.version>
-        <mybatisplus.dynamic.version>3.5.1</mybatisplus.dynamic.version>
+        <mybatisplus.dynamic.version>3.5.2</mybatisplus.dynamic.version>
         <druid.version>1.1.22</druid.version>
         <knife4j.version>2.0.7</knife4j.version>
         <freemarker.version>2.3.30</freemarker.version>

+ 31 - 0
src/main/java/com/xjrsoft/common/enums/WeChatType.java

@@ -0,0 +1,31 @@
+package com.xjrsoft.common.enums;
+
+/**
+ * 微信类型
+ */
+public enum WeChatType {
+    /**
+     * 公众号
+     */
+    WEWEB(0, "公众号"),
+    /**
+     * 小程序
+     */
+    WEMINI(1, "小程序");
+
+    final int code;
+    final String value;
+
+    public int getCode() {
+        return this.code;
+    }
+
+    public String getValue() {
+        return this.value;
+    }
+
+    WeChatType(final int code, final String message) {
+        this.code = code;
+        this.value = message;
+    }
+}

+ 28 - 23
src/main/java/com/xjrsoft/common/utils/WeChatUtil.java

@@ -1,12 +1,16 @@
 package com.xjrsoft.common.utils;
 
 
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.extra.spring.SpringUtil;
 import cn.hutool.http.HttpUtil;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.xjrsoft.common.enums.WeChatType;
 import com.xjrsoft.common.exception.MyException;
 import com.xjrsoft.module.organization.dto.WeChatDepartDto;
 import com.xjrsoft.module.organization.dto.WeChatUserDto;
+import com.xjrsoft.module.system.vo.WeChatUserInfo;
 import lombok.Data;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
@@ -42,7 +46,7 @@ public class WeChatUtil {
     public String appletAppSecret;
 
     //公众号获取Openid
-    public  String[] getMpOpenid(String code) {
+    public WeChatUserInfo getMpOpenid(String code) {
         HashMap<String, Object> paramMap = new HashMap<>();
         paramMap.put("appid", mpAppKey);
         paramMap.put("secret", mpAppSecret);
@@ -54,17 +58,11 @@ public class WeChatUtil {
             //throw new MyException(result);
             return null;
         }
-        String openId = jsonObject.get("openid").toString();
-        String unionid = "";
-        if (jsonObject.containsKey("unionid")) {
-            unionid = jsonObject.get("unionid").toString();
-        }
-        String[] arrayRefVar = {openId, unionid};
-        return arrayRefVar;
+        return jsonObject.toJavaObject(WeChatUserInfo.class);
     }
 
     // 小程序
-    public  String[] getOpenid(String code) {
+    public  WeChatUserInfo getOpenid(String code) {
         HashMap<String, Object> paramMap = new HashMap<>();
         paramMap.put("appid", appletAppKey);
         paramMap.put("secret", appletAppSecret);
@@ -76,37 +74,44 @@ public class WeChatUtil {
             //throw new MyException(result);
             return null;
         }
-        String openId = jsonObject.get("openid").toString();
-        String unionid = "";
-        if (jsonObject.containsKey("unionid")) {
-            unionid = jsonObject.get("unionid").toString();
-        }
-        String[] arrayRefVar = {openId, unionid};
-        return arrayRefVar;
+        return jsonObject.toJavaObject(WeChatUserInfo.class);
     }
 
     //获取通讯录或发消息token
-    public String getToken(Integer type) {
+    public String getToken(WeChatType type) {
         HashMap<String, Object> paramMap = new HashMap<>();
         String url = "https://api.weixin.qq.com/cgi-bin/token";
-        if (type == 1) {
+        String key = "";
+        if (WeChatType.WEMINI == type) {
             url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
             paramMap.put("corpsecret", appSecret);
             paramMap.put("corpid", appKey);
-        } else if (type == 2) {
+            key = String.format("access_token_%s",appKey);
+        } else if (WeChatType.WEWEB == type) {
             paramMap.put("grant_type", "client_credential");
             paramMap.put("appid", mpAppKey);
             paramMap.put("secret", mpAppSecret);
+            key = String.format("access_token_%s",mpAppKey);
+        }
+        // 判断redis中token是否过期
+        RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
+        String accessToken = redisUtil.get(key);
+        if (StrUtil.isNotBlank(accessToken) && !accessToken.equals("null")) {
+            return accessToken;
         }
+
         String result = HttpUtil.get(url, paramMap);
         JSONObject jsonObject = JSONObject.parseObject(result);
         String token = jsonObject.get("access_token").toString();
+        // 缓存1小时
+        redisUtil.set(key,token,3600);
+
         return token;
     }
 
     //根据所有部门信息
     public List<WeChatDepartDto> getDepartmentList(Long dept_id) {
-        String token = this.getToken(1);
+        String token = this.getToken(WeChatType.WEMINI);
         HashMap<String, Object> paramMap = new HashMap<>();
         paramMap.put("access_token", token);
         if (dept_id != null) {
@@ -121,7 +126,7 @@ public class WeChatUtil {
 
     //获取部门的所有用户信息
     public List<WeChatUserDto> getDepartmentUser(Long dept_id) {
-        String token = this.getToken(1);
+        String token = this.getToken(WeChatType.WEMINI);
             HashMap<String, Object> paramMap = new HashMap<>();
             paramMap.put("access_token", token);
             paramMap.put("department_id", dept_id);
@@ -135,7 +140,7 @@ public class WeChatUtil {
 
     //公众号发送文本消息给用户
     public Boolean sendMessage(String content, String userId) {
-        String token = this.getToken(2);
+        String token = this.getToken(WeChatType.WEWEB);
         JSONObject object = new JSONObject();
         JSONArray touser=new JSONArray();
         touser.add(userId);
@@ -171,7 +176,7 @@ public class WeChatUtil {
     //           }
     //       }
     public Boolean sendTemplateMessage(JSONObject object){
-        String token = this.getToken(2);
+        String token = this.getToken(WeChatType.WEWEB);
         String result = HttpUtil.post("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token, JSONObject.toJSONString(object));
         System.out.printf(result);
         return true;

+ 1 - 28
src/main/java/com/xjrsoft/module/organization/controller/UserController.java

@@ -417,40 +417,13 @@ public class UserController {
     @PostMapping("/bind-openid")
     @ApiOperation(value = "绑定微信 Openid")
     public  R bindOpenid(@RequestBody BindOpenidDto dto) {
-//        User user = userService.getOne(Wrappers.<User>query().lambda().select(User::getId, User::getOpenId,User::getUnionId).eq(User::getId, dto.getId()), false);
-//        if (user != null) {
-//            if (StrUtil.isNotBlank(user.getOpenId())) {
-//                return R.error("该用户已经绑定微信!");
-//            }
-//
-//            long count = userService.count(Wrappers.<User>query().lambda().eq(User::getOpenId, dto.getOpenid()));
-//            if(count > 0){
-//                return R.error("该用户已经绑定微信!");
-//            }
-//
-//            User updateUser = new User();
-//            updateUser.setId(dto.getId());
-//            updateUser.setOpenId(dto.getOpenid());
-//            return R.ok(userService.updateById(updateUser));
-//        }else{
-//            long count = userService.count(Wrappers.<User>query().lambda().eq(User::getOpenId, dto.getOpenid()));
-//            if(count > 0){
-//                return R.error("该用户已经绑定微信!");
-//            }
-//        }
-//        return R.error("该用户不存在!");
-
         return R.ok(userService.bindOpenid(dto));
     }
 
     @GetMapping("/unbind-openid")
     @ApiOperation(value = "取消绑定微信 UnionId")
     public  R unbindOpenid(@RequestParam Long id) {
-        User updateUser = new User();
-        updateUser.setId(id);
-        updateUser.setOpenId("");
-        updateUser.setUnionId("");
-        return R.ok(userService.updateById(updateUser));
+        return R.ok(userService.unbindOpenid(id));
     }
 
     @PostMapping("/register")

+ 4 - 0
src/main/java/com/xjrsoft/module/organization/dto/BindOpenidDto.java

@@ -4,6 +4,10 @@ import lombok.Data;
 
 @Data
 public class BindOpenidDto {
+
     private Long id;
+
     private String openid;
+
+    private String unionid;
 }

+ 6 - 0
src/main/java/com/xjrsoft/module/organization/service/IUserService.java

@@ -49,6 +49,12 @@ public interface IUserService extends MPJBaseService<User> {
      */
     boolean bindOpenid(BindOpenidDto dto);
 
+    /**
+     * 取消绑定微信 UnionId
+     * @return
+     */
+    boolean unbindOpenid(Long id);
+
     /**
      * 批量获取用户信息
      *

+ 18 - 0
src/main/java/com/xjrsoft/module/organization/service/impl/UserServiceImpl.java

@@ -252,6 +252,7 @@ public class UserServiceImpl extends MPJBaseServiceImpl<UserMapper, User> implem
             User updateUser = new User();
             updateUser.setId(dto.getId());
             updateUser.setOpenId(dto.getOpenid());
+            updateUser.setUnionId(dto.getUnionid());
             boolean result = this.updateById(updateUser);
             // 用户绑定微信后,更新缓存
             if (result) {
@@ -270,6 +271,23 @@ public class UserServiceImpl extends MPJBaseServiceImpl<UserMapper, User> implem
         throw new MyException("该用户不存在!");
     }
 
+    @Override
+    public boolean unbindOpenid(Long id) {
+        User updateUser = new User();
+        updateUser.setId(id);
+        updateUser.setOpenId("");
+        updateUser.setUnionId("");
+        boolean result = updateById(updateUser);
+        // 用户绑定微信后,更新缓存
+        if (result) {
+            CompletableFuture.runAsync(() -> {
+                List<User> list = list();
+                redisUtil.set(GlobalConstant.USER_CACHE_KEY, list);
+            });
+        }
+        return result;
+    }
+
     @Override
     public List<UserInfoVo> getUsersInfo(String ids) {
 

+ 20 - 21
src/main/java/com/xjrsoft/module/system/service/impl/LoginServiceImpl.java

@@ -36,10 +36,7 @@ import com.xjrsoft.module.system.dto.LoginCaptchaDto;
 import com.xjrsoft.module.system.dto.LoginDto;
 import com.xjrsoft.module.system.service.IAuthorizeService;
 import com.xjrsoft.module.system.service.ILoginService;
-import com.xjrsoft.module.system.vo.CreateTokenVo;
-import com.xjrsoft.module.system.vo.ImgCaptchaVo;
-import com.xjrsoft.module.system.vo.LoginByCodeVo;
-import com.xjrsoft.module.system.vo.LoginVo;
+import com.xjrsoft.module.system.vo.*;
 import lombok.AllArgsConstructor;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -114,23 +111,23 @@ public class LoginServiceImpl implements ILoginService {
 
     @Override
     public LoginByCodeVo loginByCode(LoginByCodeDto dto) throws Exception {
-        LoginByCodeVo result = new LoginByCodeVo();
-        String[] ids;
+        WeChatUserInfo weChatUserInfo;
         if (dto.getType() == 0) {
-            ids = weChatUtil.getMpOpenid(dto.getCode());
+            weChatUserInfo = weChatUtil.getMpOpenid(dto.getCode());
         }else{
-            ids = weChatUtil.getOpenid(dto.getCode());
+            weChatUserInfo = weChatUtil.getOpenid(dto.getCode());
         }
-
-        if (ids == null) throw new MyException("code无效");
+        LoginByCodeVo result = new LoginByCodeVo(){{
+            setOpenId(weChatUserInfo.getOpenid());
+            setUnionId(weChatUserInfo.getUnionid());
+        }};
+        if (weChatUserInfo == null) throw new MyException("code无效");
 
         LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
         if (dto.getType() == 0) {
-            result.setOpenid(ids[0]);
-            queryWrapper.eq(User::getOpenId, ids[0]);
+            queryWrapper.eq(User::getOpenId, weChatUserInfo.getOpenid());
         } else {
-            result.setUnionId(ids[1]);
-            queryWrapper.eq(User::getUnionId, ids[1]);
+            queryWrapper.eq(User::getUnionId, weChatUserInfo.getUnionid());
         }
         User loginUser = userService.getOne(queryWrapper);
         if (loginUser == null) {
@@ -139,23 +136,25 @@ public class LoginServiceImpl implements ILoginService {
         LoginVo loginVo = getLoginInfo(loginUser, "WX-MP");
         result.setToken(loginVo.getToken());
         result.setUserType(loginVo.getUserType());
+
         return result;
     }
 
     @Override
     public LoginByCodeVo bindOpenid(LoginByCodeDto dto) throws Exception {
         LoginByCodeVo result = new LoginByCodeVo();
-        String[] ids = weChatUtil.getMpOpenid(dto.getCode());
-        if (ids == null) throw new MyException("code无效");
-        if (StrUtil.isEmpty(ids[1])) {
-            throw new MyException("无法获取Uid" + ids[0] + "-" + ids[1]);
+        WeChatUserInfo weChatUserInfo = weChatUtil.getMpOpenid(dto.getCode());
+        if (weChatUserInfo == null) throw new MyException("code无效");
+        if (StrUtil.isEmpty(weChatUserInfo.getUnionid())) {
+            throw new MyException("无法获取Uid" + weChatUserInfo.getOpenid() + "-" + weChatUserInfo.getUnionid());
         }
-        List<User> userList = userService.list(Wrappers.lambdaQuery(User.class).eq(User::getUnionId, ids[1]));
+        List<User> userList = userService.list(Wrappers.lambdaQuery(User.class).eq(User::getUnionId, weChatUserInfo.getUnionid()));
         if (userList == null || userList.isEmpty()) throw new MyException("code无效");
+
         User user = userList.get(0);
-        user.setOpenId(ids[0]);
+        user.setOpenId(weChatUserInfo.getOpenid());
         userService.updateById(user);
-        result.setToken(ids[0]);
+        result.setToken(weChatUserInfo.getOpenid());
         return result;
     }
 

+ 1 - 1
src/main/java/com/xjrsoft/module/system/vo/LoginByCodeVo.java

@@ -7,7 +7,7 @@ public class LoginByCodeVo extends LoginVo {
     /**
      * openid
      */
-    private String openid;
+    private String openId;
 
     private  String unionId;
 }

+ 13 - 0
src/main/java/com/xjrsoft/module/system/vo/WeChatUserInfo.java

@@ -0,0 +1,13 @@
+package com.xjrsoft.module.system.vo;
+
+import lombok.Data;
+
+@Data
+public class WeChatUserInfo {
+
+    private String openid;
+
+    private String unionid;
+
+    private String accessToken;
+}

+ 88 - 0
src/main/resources/sqlScript/20231213_sql.sql

@@ -0,0 +1,88 @@
+-- ------------------------------------------------------------------加班申请--------------------------------------------------------------------
+-- ----------------------------
+-- 加班申请-加班人
+-- ----------------------------
+DROP TABLE IF EXISTS wf_overtime_teacher;
+CREATE TABLE wf_overtime_teacher
+(
+    id BIGINT NOT NULL COMMENT '主键编号',
+    `create_user_id` BIGINT NULL DEFAULT NULL COMMENT '创建人',
+    `create_date` datetime NULL DEFAULT NULL COMMENT '创建时间',
+    `modify_user_id` BIGINT NULL DEFAULT NULL COMMENT '修改人',
+    `modify_date` datetime NULL DEFAULT NULL COMMENT '修改时间',
+    `delete_mark` INT NOT NULL COMMENT '删除标记',
+    `enabled_mark` INT NOT NULL COMMENT '有效标志',
+    `sort_code` INT NULL DEFAULT NULL COMMENT '序号',
+    `sex_cn` varchar(100) NULL DEFAULT NULL COMMENT '性别',
+    `teacher_no` varchar(100) NULL DEFAULT NULL COMMENT '工号',
+    `wf_overtime_id` bigint NULL DEFAULT NULL COMMENT '加班申请编号',
+    `teacher_user_id` bigint NULL DEFAULT NULL COMMENT '加班人编号',
+    `teacher_user_name` varchar(200) NULL DEFAULT NULL COMMENT '加班人姓名',
+    `department_id` bigint NULL DEFAULT NULL COMMENT '所属机构(xjr_department)',
+    `department_name` varchar(200) NULL DEFAULT NULL COMMENT '所属机构名称',
+    PRIMARY KEY (`id`)
+) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT '加班申请-加班人';
+-- ------------------------------------------------------------------加班申请--------------------------------------------------------------------
+
+
+-- ------------------------------------------------------------------教师获奖登记--------------------------------------------------------------------
+-- ----------------------------
+-- 教师获奖登记
+-- ----------------------------
+DROP TABLE IF EXISTS teacher_award;
+CREATE TABLE teacher_award
+(
+    id BIGINT NOT NULL COMMENT '主键编号',
+    `create_user_id` BIGINT NULL DEFAULT NULL COMMENT '创建人',
+    `create_date` datetime NULL DEFAULT NULL COMMENT '创建时间',
+    `modify_user_id` BIGINT NULL DEFAULT NULL COMMENT '修改人',
+    `modify_date` datetime NULL DEFAULT NULL COMMENT '修改时间',
+    `delete_mark` INT NOT NULL COMMENT '删除标记',
+    `enabled_mark` INT NOT NULL COMMENT '有效标志',
+    `sort_code` INT NULL DEFAULT NULL COMMENT '序号',
+    `applicant_user_id` bigint NULL DEFAULT NULL COMMENT '申请人',
+    `award_type` varchar(20) NULL DEFAULT NULL COMMENT '获奖类型(xjr_dictionary_item[award_type])',
+
+    `award_level` varchar(20) NULL DEFAULT NULL COMMENT '奖项级别(xjr_dictionary_item[honors_level])',
+    `competition_name`varchar(200) NULL DEFAULT NULL COMMENT '赛项名称',
+    `award_grade`varchar(200) NULL DEFAULT NULL COMMENT '获奖等级(一等奖、二等奖、三等奖、指导奖一等奖、指导奖二等奖、指导奖三等奖)',
+    `award_group`varchar(200) NULL DEFAULT NULL COMMENT '组别(职工、师生同赛)',
+
+    `paper_name`varchar(200) NULL DEFAULT NULL COMMENT '论文名称',
+    `journal_name`varchar(200) NULL DEFAULT NULL COMMENT '期刊名称',
+    `journal_number`varchar(200) NULL DEFAULT NULL COMMENT '期刊号',
+    `journal_level`varchar(200) NULL DEFAULT NULL COMMENT '期刊级别(国家级、省级)',
+    `issue_date` date NULL DEFAULT NULL COMMENT '发表时间',
+
+    `approval_user_id` bigint NULL DEFAULT NULL COMMENT '审核人',
+    `remark` varchar(1000) COMMENT '备注',
+    `file_id` BIGINT NULL DEFAULT NULL COMMENT '附件文件id',
+    `status` int not null default 0 COMMENT '状态(1:结束 0:未结束)',
+    PRIMARY KEY (`id`)
+) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT '教师获奖登记';
+
+-- ----------------------------
+-- 教师认定
+-- ----------------------------
+DROP TABLE IF EXISTS teacher_identify;
+CREATE TABLE teacher_identify
+(
+    id BIGINT NOT NULL COMMENT '主键编号',
+    `create_user_id` BIGINT NULL DEFAULT NULL COMMENT '创建人',
+    `create_date` datetime NULL DEFAULT NULL COMMENT '创建时间',
+    `modify_user_id` BIGINT NULL DEFAULT NULL COMMENT '修改人',
+    `modify_date` datetime NULL DEFAULT NULL COMMENT '修改时间',
+    `delete_mark` INT NOT NULL COMMENT '删除标记',
+    `enabled_mark` INT NOT NULL COMMENT '有效标志',
+    `sort_code` INT NULL DEFAULT NULL COMMENT '序号',
+    `applicant_user_id` bigint NULL DEFAULT NULL COMMENT '申请人',
+
+    `honors_type` varchar(200) NULL DEFAULT NULL COMMENT '荣誉类型(骨干老师、双师型教师)',
+    `identify_date` int NULL DEFAULT NULL COMMENT '认定时间(年)',
+    `certificate_file_id` BIGINT NULL DEFAULT NULL COMMENT '证书文件id',
+    `policy_file_id` BIGINT NULL DEFAULT NULL COMMENT '政策文件id',
+    `remark` varchar(1000) COMMENT '备注',
+    `status` int not null default 0 COMMENT '状态(1:结束 0:未结束)',
+    PRIMARY KEY (`id`)
+) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT '教师认定';
+-- ------------------------------------------------------------------教师获奖登记--------------------------------------------------------------------

+ 30 - 0
src/test/java/com/xjrsoft/xjrsoftboot/DataSourceTest.java

@@ -0,0 +1,30 @@
+package com.xjrsoft.xjrsoftboot;
+
+import com.alibaba.fastjson.JSON;
+import com.xjrsoft.XjrSoftApplication;
+import org.junit.jupiter.api.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import javax.sql.DataSource;
+import java.util.List;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = XjrSoftApplication.class)
+public class DataSourceTest {
+    @Test
+    public void DataSourceTest() {
+        // 获取配置的数据源
+//        DataSource dataSource = applicationContext.getBean(DataSource.class);
+//        // 查看配置数据源信息
+//        System.out.println(dataSource);
+//        System.out.println(dataSource.getClass().getName());
+//        System.out.println(dataSourceProperties);
+//        //执行SQL,输出查到的数据
+//        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
+//        List<?> resultList = jdbcTemplate.queryForList("select * from xjr_user");
+//        System.out.println("===>>>>>>>>>>>" + JSON.toJSONString(resultList));
+    }
+}