新增redisUtil+CorpMpUtil+HttpServletRequestUtil
| | |
| | | <artifactId>spring-boot-starter-forest</artifactId> |
| | | <version>1.5.0-RC2</version> |
| | | </dependency> |
| | | |
| | | <!-- redis --> |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-data-redis</artifactId> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | <build> |
New file |
| | |
| | | package com.hx.redis; |
| | | |
| | | import com.fasterxml.jackson.annotation.JsonAutoDetect; |
| | | import com.fasterxml.jackson.annotation.PropertyAccessor; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import org.springframework.cache.annotation.EnableCaching; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.data.redis.connection.RedisConnectionFactory; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; |
| | | import org.springframework.data.redis.serializer.StringRedisSerializer; |
| | | |
| | | @Configuration |
| | | @EnableCaching |
| | | public class RedisConfig { |
| | | @Bean |
| | | public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { |
| | | RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); |
| | | template.setConnectionFactory(factory); |
| | | Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); |
| | | ObjectMapper om = new ObjectMapper(); |
| | | om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); |
| | | om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); |
| | | jackson2JsonRedisSerializer.setObjectMapper(om); |
| | | StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); |
| | | template.setKeySerializer(stringRedisSerializer); |
| | | template.setHashKeySerializer(stringRedisSerializer); |
| | | template.setValueSerializer(jackson2JsonRedisSerializer); |
| | | template.setHashValueSerializer(jackson2JsonRedisSerializer); |
| | | template.afterPropertiesSet(); |
| | | return template; |
| | | } |
| | | } |
New file |
| | |
| | | package com.hx.redis; |
| | | |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | |
| | | /** 文件处理工具 |
| | | * @author wangrenhuang |
| | | * @Date 2021-10-19 |
| | | */ |
| | | @Component |
| | | public class RedisUtil { |
| | | /** |
| | | * [redis] |
| | | */ |
| | | @Resource |
| | | private RedisTemplate<String, Object> redisTemplate; |
| | | |
| | | |
| | | |
| | | /** |
| | | * [判断key是否存在] |
| | | * @param key 键 |
| | | * @return true 存在 false不存在 |
| | | */ |
| | | public boolean hasKey(String key) { |
| | | try { |
| | | return redisTemplate.hasKey(key); |
| | | } catch (Exception e) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * [普通缓存获取] |
| | | * @param key 键 |
| | | * @return 值 |
| | | */ |
| | | public Object get(String key) { |
| | | return key == null ? null : redisTemplate.opsForValue().get(key); |
| | | } |
| | | /** |
| | | * [普通缓存删除] |
| | | * @param key 键 |
| | | * @return 值 |
| | | */ |
| | | public boolean delete(String key) { |
| | | try { |
| | | Boolean aBoolean = redisTemplate.hasKey(key); |
| | | return aBoolean == false ? true : redisTemplate.delete(key); |
| | | }catch (Exception e){ |
| | | return false; |
| | | } |
| | | } |
| | | /** |
| | | * [普通缓存放入] |
| | | * @param key 键 |
| | | * @param value 值 |
| | | * @return true成功 false失败 |
| | | */ |
| | | public boolean set(String key, Object value) { |
| | | try { |
| | | redisTemplate.opsForValue().set(key, value); |
| | | return true; |
| | | } catch (Exception e) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 普通缓存放入并设置时间 |
| | | * @param key 键 |
| | | * @param value 值 |
| | | * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期 |
| | | * @return true成功 false 失败 |
| | | */ |
| | | public boolean set(String key, Object value, long time) { |
| | | try { |
| | | if (time > 0) { |
| | | redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); |
| | | } else { |
| | | set(key, value); |
| | | } |
| | | return true; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | |
| | | public static final String ERROR_LOGIN="603"; |
| | | /*系统异常*/ |
| | | public static final String ERROR_SYSTEM = "999"; |
| | | /*签名错误*/ |
| | | public static final String ERROR_SIGN = "201"; |
| | | |
| | | |
| | | |
New file |
| | |
| | | package com.hx.util; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.io.BufferedReader; |
| | | import java.io.IOException; |
| | | import java.util.Enumeration; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** HttpServletRequestUtil 获取请求中的body |
| | | * @author wangrenhuang |
| | | * @Date 2021-10-19 |
| | | */ |
| | | public class HttpServletRequestUtil { |
| | | |
| | | /* |
| | | * 获取bady |
| | | * */ |
| | | public static String getBody(HttpServletRequest request) { |
| | | BufferedReader br = null; |
| | | StringBuilder sb = new StringBuilder(""); |
| | | String str ; |
| | | try { |
| | | br = request.getReader(); |
| | | while((str = br.readLine()) != null){ |
| | | sb.append(str); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | }finally { |
| | | try { |
| | | if (null != br){ |
| | | br.close(); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | |
| | | /* |
| | | * |
| | | * 获取请求头 |
| | | * */ |
| | | public static Map<String,String> getHeader(HttpServletRequest request) { |
| | | Enumeration<String> enumeration = request.getHeaderNames(); |
| | | Map<String,String> map = new HashMap<>(16); |
| | | StringBuffer headers = new StringBuffer(); |
| | | while (enumeration.hasMoreElements()) { |
| | | String name = enumeration.nextElement(); |
| | | String value = request.getHeader(name); |
| | | map.put(name,value); |
| | | } |
| | | |
| | | return map; |
| | | } |
| | | } |
New file |
| | |
| | | package com.hx.util.corp; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.hx.exception.TipsException; |
| | | import com.hx.util.HttpMethodUtil; |
| | | import com.hx.util.StringUtils; |
| | | import com.hx.util.corp.entity.WeiXinInfo; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** 企业微信工具类 |
| | | * @author wangrenhuang |
| | | * @Date 2021-10-19 |
| | | */ |
| | | public class CorpMpUtil { |
| | | |
| | | //log4j日志 |
| | | private static Logger logger = LoggerFactory.getLogger(CorpMpUtil.class.getName()); |
| | | |
| | | /**链接-获取应用accessToken*/ |
| | | public static final String URL_GET_USER_INFO= "https://qyapi.weixin.qq.com/cgi-bin/user/get"; |
| | | |
| | | /** |
| | | * 获取企业微信员工信息 |
| | | * @param userId 用户id |
| | | * @return 返回 |
| | | */ |
| | | public static WeiXinInfo userInfo(String userId, String token) { |
| | | logger.info("userId:"+userId); |
| | | logger.info("token:"+token); |
| | | Map<String,Object> map=new HashMap<>(); |
| | | map.put("access_token",token); |
| | | map.put("userid",userId); |
| | | String datas = HttpMethodUtil.HttpURLUtilJson(URL_GET_USER_INFO, null, map, null, "GET"); |
| | | logger.info("datas:"+datas); |
| | | WeiXinInfo weiXinInfo = JSONObject.parseObject(datas, WeiXinInfo.class); |
| | | return weiXinInfo; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.hx.util.corp.entity; |
| | | |
| | | import java.io.Serializable; |
| | | |
| | | public class WeiXinInfo implements Serializable { |
| | | |
| | | //返回码 |
| | | private Integer errcode; |
| | | //对返回码的文本描述内容 |
| | | private String errmsg; |
| | | //成员UserID。对应管理端的帐号,企业内必须唯一。不区分大小写,长度为1~64个字节 |
| | | private String userid; |
| | | //成员名称;第三方不可获取,调用时返回userid以代替name;代开发自建应用需要管理员授权才返回;对于非第三方创建的成员,第三方通讯录应用也不可获取;未返回name的情况需要通过通讯录展示组件来展示名字 |
| | | private String name; |
| | | //手机号码, |
| | | private String mobile; |
| | | //职务信息; |
| | | private String position; |
| | | //性别。0表示未定义,1表示男性,2表示女性 |
| | | private Integer gender; |
| | | //邮箱, |
| | | private String email; |
| | | // 激活状态: 1=已激活,2=已禁用,4=未激活,5=退出企业。 |
| | | private Integer status; |
| | | |
| | | |
| | | public Integer getErrcode() { |
| | | return errcode; |
| | | } |
| | | |
| | | public void setErrcode(Integer errcode) { |
| | | this.errcode = errcode; |
| | | } |
| | | |
| | | public String getErrmsg() { |
| | | return errmsg; |
| | | } |
| | | |
| | | public void setErrmsg(String errmsg) { |
| | | this.errmsg = errmsg; |
| | | } |
| | | |
| | | public String getUserid() { |
| | | return userid; |
| | | } |
| | | |
| | | public void setUserid(String userid) { |
| | | this.userid = userid; |
| | | } |
| | | |
| | | public String getName() { |
| | | return name; |
| | | } |
| | | |
| | | public void setName(String name) { |
| | | this.name = name; |
| | | } |
| | | |
| | | public String getMobile() { |
| | | return mobile; |
| | | } |
| | | |
| | | public void setMobile(String mobile) { |
| | | this.mobile = mobile; |
| | | } |
| | | |
| | | public String getPosition() { |
| | | return position; |
| | | } |
| | | |
| | | public void setPosition(String position) { |
| | | this.position = position; |
| | | } |
| | | |
| | | public Integer getGender() { |
| | | return gender; |
| | | } |
| | | |
| | | public void setGender(Integer gender) { |
| | | this.gender = gender; |
| | | } |
| | | |
| | | public String getEmail() { |
| | | return email; |
| | | } |
| | | |
| | | public void setEmail(String email) { |
| | | this.email = email; |
| | | } |
| | | |
| | | public Integer getStatus() { |
| | | return status; |
| | | } |
| | | |
| | | public void setStatus(Integer status) { |
| | | this.status = status; |
| | | } |
| | | |
| | | @Override |
| | | public String toString() { |
| | | return "WeiXinInfo{" + |
| | | "errcode=" + errcode + |
| | | ", errmsg='" + errmsg + '\'' + |
| | | ", userid='" + userid + '\'' + |
| | | ", name='" + name + '\'' + |
| | | ", mobile='" + mobile + '\'' + |
| | | ", position='" + position + '\'' + |
| | | ", gender=" + gender + |
| | | ", email='" + email + '\'' + |
| | | ", status=" + status + |
| | | '}'; |
| | | } |
| | | } |