Bläddra i källkod

添加企业微信发送通知工具类

xuzuoyun 5 år sedan
förälder
incheckning
30cedf95d7

+ 1 - 0
jeecg-boot-module-system/src/main/java/org/jeecg/config/ShiroConfig.java

@@ -44,6 +44,7 @@ public class ShiroConfig {
 		// 配置不会被拦截的链接 顺序判断
         //登录接口排除
         filterChainDefinitionMap.put("/sys/login", "anon");
+		filterChainDefinitionMap.put("/sys/wxlogin", "anon");
 
         //获取加密串
         filterChainDefinitionMap.put("/sys/getEncryptedString", "anon");

+ 15 - 0
jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/controller/LoginController.java

@@ -1,5 +1,7 @@
 package org.jeecg.modules.system.controller;
 
+import cn.com.ctop.common.module.entity.CtopCorpWexinUser;
+import cn.com.ctop.common.module.service.ICtopCorpWexinUserService;
 import cn.com.ctop.common.module.utils.AesEncryptUtil;
 import cn.com.ctop.common.module.utils.EncryptedString;
 import cn.hutool.core.util.RandomUtil;
@@ -62,6 +64,8 @@ public class LoginController {
     private RedisUtil redisUtil;
     @Autowired
     private ISysDepartService sysDepartService;
+    @Autowired
+    private ICtopCorpWexinUserService ctopCorpWexinUserService;
 
     @RequestMapping(value = "/wxlogin", method = RequestMethod.POST)
     @ApiOperation("企业微信登录接口")
@@ -93,6 +97,17 @@ public class LoginController {
                 //用户登录信息
                 userInfo(sysUser, result);
                 sysBaseApi.addLog("用户名: " + email + ",登录成功!", CommonConstant.LOG_TYPE_1, null);
+                CtopCorpWexinUser ctopCorpWexinUser = ctopCorpWexinUserService.getById(sysUser.getId());
+                if(ctopCorpWexinUser == null){
+                    ctopCorpWexinUser = new CtopCorpWexinUser();
+                    ctopCorpWexinUser.setUserId(sysUser.getId());
+                    ctopCorpWexinUser.setWexinEmail(user.getEmail());
+                    ctopCorpWexinUser.setWexinGender(user.getGender().getGenderName());
+                    ctopCorpWexinUser.setWexinName(user.getName());
+                    ctopCorpWexinUser.setWexinId(user.getUserId());
+                    ctopCorpWexinUser.setWexinMobile(user.getMobile());
+                    ctopCorpWexinUserService.save(ctopCorpWexinUser);
+                }
                 return result;
             }
         } catch (WxErrorException e) {

+ 62 - 0
module-common/src/main/java/cn/com/ctop/common/module/entity/CtopCorpWexinUser.java

@@ -0,0 +1,62 @@
+package cn.com.ctop.common.module.entity;
+
+import java.io.Serializable;
+import java.util.Date;
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.baomidou.mybatisplus.annotation.TableField;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.experimental.Accessors;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import org.springframework.format.annotation.DateTimeFormat;
+import org.jeecgframework.poi.excel.annotation.Excel;
+
+/**
+ * 企业微信用户表
+ * @author jeecg-boot
+ * @date   2019-12-08
+ * @version V1.0
+ */
+@Data
+@TableName("ctop_corp_wexin_user")
+@EqualsAndHashCode(callSuper = false)
+@Accessors(chain = true)
+@ApiModel(value="ctop_corp_wexin_user对象", description="企业微信用户表")
+public class CtopCorpWexinUser {
+    
+	/**userId*/
+	@TableId(type = IdType.INPUT)
+	@Excel(name = "userId", width = 15)
+    @ApiModelProperty(value = "userId")
+	private String userId;
+	/**wexinId*/
+	@Excel(name = "wexinId", width = 15)
+    @ApiModelProperty(value = "wexinId")
+	private String wexinId;
+	/**wexinName*/
+	@Excel(name = "wexinName", width = 15)
+    @ApiModelProperty(value = "wexinName")
+	private String wexinName;
+	/**wexinMobile*/
+	@Excel(name = "wexinMobile", width = 15)
+    @ApiModelProperty(value = "wexinMobile")
+	private String wexinMobile;
+	/**wexinEmail*/
+	@Excel(name = "wexinEmail", width = 15)
+    @ApiModelProperty(value = "wexinEmail")
+	private String wexinEmail;
+	/**wexinGender*/
+	@Excel(name = "wexinGender", width = 15)
+    @ApiModelProperty(value = "wexinGender")
+	private String wexinGender;
+	/**createTime*/
+    @ApiModelProperty(value = "createTime")
+	private Date createTime;
+	/**updateTime*/
+    @ApiModelProperty(value = "updateTime")
+	private Date updateTime;
+}

+ 17 - 0
module-common/src/main/java/cn/com/ctop/common/module/mapper/CtopCorpWexinUserMapper.java

@@ -0,0 +1,17 @@
+package cn.com.ctop.common.module.mapper;
+
+import java.util.List;
+
+import cn.com.ctop.common.module.entity.CtopCorpWexinUser;
+import org.apache.ibatis.annotations.Param;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+ * 企业微信用户表
+ * @author: jeecg-boot
+ * @date:   2019-12-08
+ * @cersion: V1.0
+ */
+public interface CtopCorpWexinUserMapper extends BaseMapper<CtopCorpWexinUser> {
+
+}

+ 5 - 0
module-common/src/main/java/cn/com/ctop/common/module/mapper/xml/CtopCorpWexinUserMapper.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="cn.com.ctop.common.module.mapper.CtopCorpWexinUserMapper">
+
+</mapper>

+ 14 - 0
module-common/src/main/java/cn/com/ctop/common/module/service/ICtopCorpWexinUserService.java

@@ -0,0 +1,14 @@
+package cn.com.ctop.common.module.service;
+
+import cn.com.ctop.common.module.entity.CtopCorpWexinUser;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+ * 企业微信用户表
+ * @author jeecg-boot
+ * @date   2019-12-08
+ * @version V1.0
+ */
+public interface ICtopCorpWexinUserService extends IService<CtopCorpWexinUser> {
+
+}

+ 19 - 0
module-common/src/main/java/cn/com/ctop/common/module/service/impl/CtopCorpWexinUserServiceImpl.java

@@ -0,0 +1,19 @@
+package cn.com.ctop.common.module.service.impl;
+
+import cn.com.ctop.common.module.entity.CtopCorpWexinUser;
+import cn.com.ctop.common.module.mapper.CtopCorpWexinUserMapper;
+import cn.com.ctop.common.module.service.ICtopCorpWexinUserService;
+import org.springframework.stereotype.Service;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+
+/**
+ * 企业微信用户表
+ * @author jeecg-boot
+ * @date   2019-12-08
+ * @version V1.0
+ */
+@Service
+public class CtopCorpWexinUserServiceImpl extends ServiceImpl<CtopCorpWexinUserMapper, CtopCorpWexinUser> implements ICtopCorpWexinUserService {
+
+}

+ 46 - 0
module-common/src/main/java/cn/com/ctop/common/module/utils/CorpWexinUtils.java

@@ -0,0 +1,46 @@
+package cn.com.ctop.common.module.utils;
+
+import lombok.val;
+import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.cp.api.WxCpService;
+import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
+import me.chanjar.weixin.cp.bean.WxCpMessage;
+import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
+
+import java.util.List;
+
+public class CorpWexinUtils {
+    private static WxCpService wxCpService;
+    {
+        initService();
+    }
+    public static void initService(){
+        val configStorage = new WxCpDefaultConfigImpl();
+        configStorage.setCorpId("ww24b8a47826f5875f");
+        configStorage.setAgentId(1000002);
+        configStorage.setCorpSecret("MJIlySADGPlgvADnkFQPUpfZD4yV-4WN6066OgNnb0s");
+        configStorage.setToken("hcst2019");
+        configStorage.setAesKey("hcst2019");
+        wxCpService = new WxCpServiceImpl();
+        wxCpService.setWxCpConfigStorage(configStorage);
+    }
+    public static void sendMessage(List<String> wxUserList,String content){
+        if (wxCpService == null){
+            initService();
+        }
+        if (wxUserList != null && wxUserList.size() > 0){
+            for (String userId:wxUserList){
+                WxCpMessage message = new WxCpMessage();
+                message.setMsgType("text");
+                message.setContent(content);
+                message.setAgentId(1000002);
+                message.setToUser(userId);
+                try {
+                    wxCpService.messageSend(message);
+                } catch (WxErrorException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+}