From f3ad4beee9189e7dea24c69b19b8a7eaa2a92b6d Mon Sep 17 00:00:00 2001 From: cc <wangren_huang@163.com> Date: 星期二, 19 十月 2021 15:12:06 +0800 Subject: [PATCH] 新增redisUtil+CorpMpUtil+HttpServletRequestUtil --- src/main/java/com/hx/redis/RedisConfig.java | 34 +++++ src/main/java/com/hx/util/HttpServletRequestUtil.java | 59 +++++++++ src/main/java/com/hx/util/corp/entity/WeiXinInfo.java | 113 ++++++++++++++++++ pom.xml | 6 + src/main/java/com/hx/redis/RedisUtil.java | 96 ++++++++++++++++ src/main/java/com/hx/resultTool/ResponseCode.java | 2 src/main/java/com/hx/util/corp/CorpMpUtil.java | 43 +++++++ 7 files changed, 353 insertions(+), 0 deletions(-) diff --git a/pom.xml b/pom.xml index 852c3fe..0dec8eb 100644 --- a/pom.xml +++ b/pom.xml @@ -278,6 +278,12 @@ <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> diff --git a/src/main/java/com/hx/redis/RedisConfig.java b/src/main/java/com/hx/redis/RedisConfig.java new file mode 100644 index 0000000..834af2f --- /dev/null +++ b/src/main/java/com/hx/redis/RedisConfig.java @@ -0,0 +1,34 @@ +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; + } +} diff --git a/src/main/java/com/hx/redis/RedisUtil.java b/src/main/java/com/hx/redis/RedisUtil.java new file mode 100644 index 0000000..3b8a943 --- /dev/null +++ b/src/main/java/com/hx/redis/RedisUtil.java @@ -0,0 +1,96 @@ +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; + } + } + +} + diff --git a/src/main/java/com/hx/resultTool/ResponseCode.java b/src/main/java/com/hx/resultTool/ResponseCode.java index 303bf2d..9867837 100644 --- a/src/main/java/com/hx/resultTool/ResponseCode.java +++ b/src/main/java/com/hx/resultTool/ResponseCode.java @@ -20,6 +20,8 @@ public static final String ERROR_LOGIN="603"; /*绯荤粺寮傚父*/ public static final String ERROR_SYSTEM = "999"; + /*绛惧悕閿欒*/ + public static final String ERROR_SIGN = "201"; diff --git a/src/main/java/com/hx/util/HttpServletRequestUtil.java b/src/main/java/com/hx/util/HttpServletRequestUtil.java new file mode 100644 index 0000000..9449030 --- /dev/null +++ b/src/main/java/com/hx/util/HttpServletRequestUtil.java @@ -0,0 +1,59 @@ +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; + } +} diff --git a/src/main/java/com/hx/util/corp/CorpMpUtil.java b/src/main/java/com/hx/util/corp/CorpMpUtil.java new file mode 100644 index 0000000..98e460b --- /dev/null +++ b/src/main/java/com/hx/util/corp/CorpMpUtil.java @@ -0,0 +1,43 @@ +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; + } + +} diff --git a/src/main/java/com/hx/util/corp/entity/WeiXinInfo.java b/src/main/java/com/hx/util/corp/entity/WeiXinInfo.java new file mode 100644 index 0000000..702977c --- /dev/null +++ b/src/main/java/com/hx/util/corp/entity/WeiXinInfo.java @@ -0,0 +1,113 @@ +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浠ヤ唬鏇縩ame锛涗唬寮�鍙戣嚜寤哄簲鐢ㄩ渶瑕佺鐞嗗憳鎺堟潈鎵嶈繑鍥烇紱瀵逛簬闈炵涓夋柟鍒涘缓鐨勬垚鍛橈紝绗笁鏂归�氳褰曞簲鐢ㄤ篃涓嶅彲鑾峰彇锛涙湭杩斿洖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 + + '}'; + } +} -- Gitblit v1.8.0