diff --git a/src/main/java/com/kiisoo/ic/config/WxCpConfiguration.java b/src/main/java/com/kiisoo/ic/config/WxCpConfiguration.java new file mode 100644 index 0000000..a953cb4 --- /dev/null +++ b/src/main/java/com/kiisoo/ic/config/WxCpConfiguration.java @@ -0,0 +1,51 @@ +package com.kiisoo.ic.config; + +import com.google.common.collect.Maps; +import lombok.val; +import me.chanjar.weixin.common.api.WxConsts; +import me.chanjar.weixin.cp.api.WxCpService; +import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl; +import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl; +import me.chanjar.weixin.cp.constant.WxCpConsts; +import me.chanjar.weixin.cp.message.WxCpMessageRouter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +import javax.annotation.PostConstruct; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * @author Binary Wang(https://github.com/binarywang) + */ +@Configuration +@EnableConfigurationProperties(WxCpProperties.class) +public class WxCpConfiguration { + private WxCpProperties properties; + + private static Map routers = Maps.newHashMap(); + private static Map cpServices = Maps.newHashMap(); + + @Autowired + public WxCpConfiguration(WxCpProperties properties) { + this.properties = properties; + } + + public static WxCpService getCpService(Integer agentId) { + return cpServices.get(agentId); + } + + @PostConstruct + public void initServices() { + cpServices = this.properties.getAppConfigs().stream().map(a -> { + val configStorage = new WxCpDefaultConfigImpl(); + configStorage.setCorpId(this.properties.getCorpId()); + configStorage.setAgentId(a.getAgentId()); + configStorage.setCorpSecret(a.getSecret()); + val service = new WxCpServiceImpl(); + service.setWxCpConfigStorage(configStorage); + return service; + }).collect(Collectors.toMap(service -> service.getWxCpConfigStorage().getAgentId(), a -> a)); + } +} diff --git a/src/main/java/com/kiisoo/ic/config/WxCpProperties.java b/src/main/java/com/kiisoo/ic/config/WxCpProperties.java new file mode 100644 index 0000000..48d82d5 --- /dev/null +++ b/src/main/java/com/kiisoo/ic/config/WxCpProperties.java @@ -0,0 +1,53 @@ +package com.kiisoo.ic.config; + +import com.kiisoo.ic.utils.JsonUtils; +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; + +import java.util.List; + +/** + * @author Binary Wang(https://github.com/binarywang) + */ +@Getter +@Setter +@ConfigurationProperties(prefix = "wechat.cp") +public class WxCpProperties { + /** + * 设置微信企业号的corpId + */ + private String corpId; + + private List appConfigs; + + @Getter + @Setter + public static class AppConfig { + /** + * 设置微信企业应用的AgentId + */ + private Integer agentId; + + /** + * 设置微信企业应用的Secret + */ + private String secret; + + /** + * 设置微信企业号的token + */ + private String token; + + /** + * 设置微信企业号的EncodingAESKey + */ + private String aesKey; + + } + + @Override + public String toString() { + return JsonUtils.toJson(this); + } +} diff --git a/src/main/java/com/kiisoo/ic/department/controller/DepartmentConreoller.java b/src/main/java/com/kiisoo/ic/department/controller/DepartmentConreoller.java new file mode 100644 index 0000000..d8b2f16 --- /dev/null +++ b/src/main/java/com/kiisoo/ic/department/controller/DepartmentConreoller.java @@ -0,0 +1,39 @@ +package com.kiisoo.ic.department.controller; + +import com.kiisoo.ic.common.BaseController; +import com.kiisoo.ic.employee.entity.EmployeeDO; +import com.kiisoo.ic.employee.service.EmployeeService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import java.util.Map; + +/** + * @Description: 用户管理controller + * @Auther: yechenhao + * @Date: 2020/4/7 0002 10:06 + * @Version: v1 + */ +@Controller +@RequestMapping("/user") +@Slf4j +public class DepartmentConreoller extends BaseController { + + @Autowired + private EmployeeService employeeService; + + @RequestMapping(value = "add",method = RequestMethod.POST) + public Map addUser(@RequestBody EmployeeDO employee){ + try { + Boolean hasAdd = employeeService.addUser(employee); + return data(hasAdd); + }catch (Exception e){ + log.error("添加用户失败",e); + return fail(); + } + } +} diff --git a/src/main/java/com/kiisoo/ic/department/entity/DepartmentDO.java b/src/main/java/com/kiisoo/ic/department/entity/DepartmentDO.java new file mode 100644 index 0000000..28ab741 --- /dev/null +++ b/src/main/java/com/kiisoo/ic/department/entity/DepartmentDO.java @@ -0,0 +1,21 @@ +package com.kiisoo.ic.department.entity; + +import lombok.Data; +import me.chanjar.weixin.cp.bean.Gender; + +/** + * @Description: 员工对象 + * @Auther: yechenhao + * @Date: 2020/4/7 0002 10:06 + * @Version: v1 + */ +@Data +public class DepartmentDO { + /** + * 数据库实体id和企业微信部门id + */ + private Long id; + private String name; + private Long parentId; + private Long order; +} diff --git a/src/main/java/com/kiisoo/ic/department/service/EmployeeService.java b/src/main/java/com/kiisoo/ic/department/service/EmployeeService.java new file mode 100644 index 0000000..e615ff1 --- /dev/null +++ b/src/main/java/com/kiisoo/ic/department/service/EmployeeService.java @@ -0,0 +1,33 @@ +package com.kiisoo.ic.department.service; + +import com.kiisoo.ic.employee.entity.EmployeeDO; +import com.kiisoo.ic.wx.service.QWMailListManageService; +import me.chanjar.weixin.common.error.WxErrorException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * @Description: 用户管理service + * @Auther: yechenhao + * @Date: 2020/4/7 0002 10:06 + * @Version: v1 + */ +@Service +public class EmployeeService { + + @Autowired + private QWMailListManageService qwMailListManageService; + + @Transactional(rollbackFor = Exception.class) + public Boolean addUser(EmployeeDO employee) throws WxErrorException { + //数据库创建用户 + + //数据库创建登录account + + //成功则添加用户到企业微信 + qwMailListManageService.addUser(employee); + + return true; + } +} diff --git a/src/main/java/com/kiisoo/ic/employee/controller/EmployeeConreoller.java b/src/main/java/com/kiisoo/ic/employee/controller/EmployeeConreoller.java new file mode 100644 index 0000000..eebb02a --- /dev/null +++ b/src/main/java/com/kiisoo/ic/employee/controller/EmployeeConreoller.java @@ -0,0 +1,39 @@ +package com.kiisoo.ic.employee.controller; + +import com.kiisoo.ic.common.BaseController; +import com.kiisoo.ic.employee.entity.EmployeeDO; +import com.kiisoo.ic.employee.service.EmployeeService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import java.util.Map; + +/** + * @Description: 用户管理controller + * @Auther: yechenhao + * @Date: 2020/4/7 0002 10:06 + * @Version: v1 + */ +@Controller +@RequestMapping("/user") +@Slf4j +public class EmployeeConreoller extends BaseController { + + @Autowired + private EmployeeService employeeService; + + @RequestMapping(value = "add",method = RequestMethod.POST) + public Map addUser(@RequestBody EmployeeDO employee){ + try { + Boolean hasAdd = employeeService.addUser(employee); + return data(hasAdd); + }catch (Exception e){ + log.error("添加用户失败",e); + return fail(); + } + } +} diff --git a/src/main/java/com/kiisoo/ic/employee/entity/EmployeeDO.java b/src/main/java/com/kiisoo/ic/employee/entity/EmployeeDO.java new file mode 100644 index 0000000..8ea600a --- /dev/null +++ b/src/main/java/com/kiisoo/ic/employee/entity/EmployeeDO.java @@ -0,0 +1,89 @@ +package com.kiisoo.ic.employee.entity; + +import lombok.Data; +import me.chanjar.weixin.cp.bean.Gender; + +/** + * @Description: 员工对象 + * @Auther: yechenhao + * @Date: 2020/4/7 0002 10:06 + * @Version: v1 + */ +@Data +public class EmployeeDO { + /** + * 数据库实体id + */ + private Long id; + /** + * 员工企业微信员工id + * 对应管理端的帐号,企业内必须唯一。不区分大小写,长度为1~64个字节。只能由数字、字母和“_-@.”四种字符组成,且第一个字符必须是数字或字母。 + */ + private String userId; + /** + * 员工名 + */ + private String name; + /** + * 成员所属部门id列表,不超过20个(企业微信部门id,非数据库部门id) + */ + private Long[] departIds; + /** + * 部门内的排序值,默认为0,成员次序以创建时间从小到大排列。数量必须和department一致,数值越大排序越前面。有效的值范围是[0, 2^32) + */ + private Integer[] orders; + /** + * 职务信息。长度为0~128个字符 + */ + private String position; + /** + * 手机号码。企业内必须唯一,mobile/email二者不能同时为空 + */ + private String mobile; + /** + * 性别。1表示男性,2表示女性 + */ + private Gender gender; + /** + * 邮箱。长度6~64个字节,且为有效的email格式。企业内必须唯一,mobile/email二者不能同时为空 + */ + private String email; + + /** + * 地址。长度最大128个字符 + */ + private String address; + /** + * 成员头像的mediaid,通过素材管理接口上传图片获得的mediaid + */ + private String avatarMediaId; + /** + * 数据库成员状态 + */ + private Integer status; + /** + * 启用/禁用成员。1表示启用成员,0表示禁用成员 + */ + private Integer enable; + /** + * 别名;第三方仅通讯录应用可获取 + */ + private String alias; + /** + * is_leader_in_dept. + * 个数必须和department一致,表示在所在的部门内是否为上级。1表示为上级,0表示非上级。在审批等应用里可以用来标识上级审批人 + */ + private Integer[] isLeaderInDept; + /** + * 座机。32字节以内,由纯数字或’-‘号组成。 + */ + private String telephone; + /** + * 员工邀请码 + */ + private String inviteQrCode; + /** + * 是否邀请该成员使用企业微信(将通过微信服务通知或短信或邮件下发邀请,每天自动下发一次,最多持续3个工作日),默认值为true。 + */ + private Boolean toInvite; +} diff --git a/src/main/java/com/kiisoo/ic/employee/service/EmployeeService.java b/src/main/java/com/kiisoo/ic/employee/service/EmployeeService.java new file mode 100644 index 0000000..5a7ca5a --- /dev/null +++ b/src/main/java/com/kiisoo/ic/employee/service/EmployeeService.java @@ -0,0 +1,33 @@ +package com.kiisoo.ic.employee.service; + +import com.kiisoo.ic.employee.entity.EmployeeDO; +import com.kiisoo.ic.wx.service.QWMailListManageService; +import me.chanjar.weixin.common.error.WxErrorException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * @Description: 用户管理service + * @Auther: yechenhao + * @Date: 2020/4/7 0002 10:06 + * @Version: v1 + */ +@Service +public class EmployeeService { + + @Autowired + private QWMailListManageService qwMailListManageService; + + @Transactional(rollbackFor = Exception.class) + public Boolean addUser(EmployeeDO employee) throws WxErrorException { + //数据库创建用户 + + //数据库创建登录account + + //成功则添加用户到企业微信 + qwMailListManageService.addUser(employee); + + return true; + } +} diff --git a/src/main/java/com/kiisoo/ic/utils/JsonUtils.java b/src/main/java/com/kiisoo/ic/utils/JsonUtils.java new file mode 100644 index 0000000..b2a0e06 --- /dev/null +++ b/src/main/java/com/kiisoo/ic/utils/JsonUtils.java @@ -0,0 +1,28 @@ +package com.kiisoo.ic.utils; + +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; + +/** + * @author Binary Wang(https://github.com/binarywang) + */ +public class JsonUtils { + private static final ObjectMapper JSON = new ObjectMapper(); + + static { + JSON.setSerializationInclusion(Include.NON_NULL); + JSON.configure(SerializationFeature.INDENT_OUTPUT, Boolean.TRUE); + } + + public static String toJson(Object obj) { + try { + return JSON.writeValueAsString(obj); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + + return null; + } +} diff --git a/src/main/java/com/kiisoo/ic/wx/service/QWMailListManageService.java b/src/main/java/com/kiisoo/ic/wx/service/QWMailListManageService.java new file mode 100644 index 0000000..dfaa710 --- /dev/null +++ b/src/main/java/com/kiisoo/ic/wx/service/QWMailListManageService.java @@ -0,0 +1,53 @@ +package com.kiisoo.ic.wx.service; + +import com.kiisoo.ic.config.WxCpConfiguration; +import com.kiisoo.ic.department.entity.DepartmentDO; +import com.kiisoo.ic.employee.entity.EmployeeDO; +import me.chanjar.weixin.common.error.WxErrorException; +import me.chanjar.weixin.cp.api.WxCpDepartmentService; +import me.chanjar.weixin.cp.api.WxCpService; +import me.chanjar.weixin.cp.api.WxCpUserService; +import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl; +import me.chanjar.weixin.cp.bean.WxCpDepart; +import me.chanjar.weixin.cp.bean.WxCpUser; +import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +/** + * @Description: 企业微信用户管理service + * @Auther: yechenhao + * @Date: 2020/4/7 0002 10:06 + * @Version: v1 + */ +@Service +public class QWMailListManageService { + + @Value("${qywx.applicationid}") + private Integer applicationid; + + private WxCpService wxCpService; + + /** + * 增加企业微信用户 + */ + public void addUser(EmployeeDO employeeDO) throws WxErrorException { + wxCpService = WxCpConfiguration.getCpService(applicationid); + WxCpUserService userService = wxCpService.getUserService(); + WxCpUser wxCpUser = new WxCpUser(); + BeanUtils.copyProperties(employeeDO,wxCpUser); + userService.create(wxCpUser); + } + + /** + * 增加企业微信用户 + */ + public void addDepartment(DepartmentDO departmentDO) throws WxErrorException { + wxCpService = WxCpConfiguration.getCpService(applicationid); + WxCpDepartmentService departmentService = wxCpService.getDepartmentService(); + WxCpDepart wxCpDepart = new WxCpDepart(); + BeanUtils.copyProperties(departmentDO,wxCpDepart); + departmentService.create(wxCpDepart); + } +}