diff --git a/esua-epdc/epdc-commons/epdc-commons-tools-phone/pom.xml b/esua-epdc/epdc-commons/epdc-commons-tools-phone/pom.xml new file mode 100644 index 000000000..ee5c4f44c --- /dev/null +++ b/esua-epdc/epdc-commons/epdc-commons-tools-phone/pom.xml @@ -0,0 +1,41 @@ + + + + epdc-commons + com.esua.epdc + 1.0.0 + + 4.0.0 + + epdc-commons-tools-phone + jar + + + + com.googlecode.libphonenumber + libphonenumber + 8.8.8 + + + com.googlecode.libphonenumber + carrier + 1.75 + + + com.googlecode.libphonenumber + geocoder + 2.85 + + + com.googlecode.libphonenumber + prefixmapper + 2.85 + + + + + ${project.artifactId} + + \ No newline at end of file diff --git a/esua-epdc/epdc-commons/epdc-commons-tools-phone/src/main/java/com/elink/esua/epdc/commons/tools/utils/PhoneDto.java b/esua-epdc/epdc-commons/epdc-commons-tools-phone/src/main/java/com/elink/esua/epdc/commons/tools/utils/PhoneDto.java new file mode 100644 index 000000000..eb8f3fe2b --- /dev/null +++ b/esua-epdc/epdc-commons/epdc-commons-tools-phone/src/main/java/com/elink/esua/epdc/commons/tools/utils/PhoneDto.java @@ -0,0 +1,79 @@ +package com.elink.esua.epdc.commons.tools.utils; + +/** + * 电话DTO + * + * @author rongchao + * @Date 18-12-11 + */ +public class PhoneDto { + + /** + * 省份名称 + */ + private String provinceName; + + /** + * 城市名称 + */ + private String cityName; + + /** + * 运营商:移动/电信/联通 + */ + private String carrier; + + /** + * 省份名称 + * + * @return 获取provinceName属性值 + */ + public String getProvinceName() { + return provinceName; + } + + /** + * 省份名称 + * + * @param provinceName 设置 provinceName 属性值为参数值 provinceName + */ + public void setProvinceName(String provinceName) { + this.provinceName = provinceName; + } + + /** + * 城市名称 + * + * @return 获取cityName属性值 + */ + public String getCityName() { + return cityName; + } + + /** + * 城市名称 + * + * @param cityName 设置 cityName 属性值为参数值 cityName + */ + public void setCityName(String cityName) { + this.cityName = cityName; + } + + /** + * 运营商:移动/电信/联通 + * + * @return 获取carrier属性值 + */ + public String getCarrier() { + return carrier; + } + + /** + * 运营商:移动/电信/联通 + * + * @param carrier 设置 carrier 属性值为参数值 carrier + */ + public void setCarrier(String carrier) { + this.carrier = carrier; + } +} diff --git a/esua-epdc/epdc-commons/epdc-commons-tools-phone/src/main/java/com/elink/esua/epdc/commons/tools/utils/PhoneUtil.java b/esua-epdc/epdc-commons/epdc-commons-tools-phone/src/main/java/com/elink/esua/epdc/commons/tools/utils/PhoneUtil.java new file mode 100644 index 000000000..e9af526b4 --- /dev/null +++ b/esua-epdc/epdc-commons/epdc-commons-tools-phone/src/main/java/com/elink/esua/epdc/commons/tools/utils/PhoneUtil.java @@ -0,0 +1,175 @@ +package com.elink.esua.epdc.commons.tools.utils; + +/** + * @author rongchao + * @Date 18-12-11 + */ + +import com.google.i18n.phonenumbers.PhoneNumberToCarrierMapper; +import com.google.i18n.phonenumbers.PhoneNumberUtil; +import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; +import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder; + +import java.util.Locale; + +/** + * 手机号归属地查询 + * jar依赖:com.googlecode.libphonenumber(Libphonenumber、Geocoder、Prefixmapper + * 、Carrier) pom依赖:http://mvnrepository.com/search?q=libphonenumber + * 项目地址:https://github.com/googlei18n/libphonenumber + * + * @author rongchao + * @Date 18-12-11 + */ +public class PhoneUtil { + + /** + * 直辖市 + */ + private final static String[] MUNICIPALITY = {"北京市", "天津市", "上海市", "重庆市"}; + + /** + * 自治区 + */ + private final static String[] AUTONOMOUS_REGION = {"新疆", "内蒙古", "西藏", "宁夏", "广西"}; + + private static PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil + .getInstance(); + + /** + * 提供与电话号码相关的运营商信息 + */ + private static PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper + .getInstance(); + + /** + * 提供与电话号码有关的地理信息 + */ + private static PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder + .getInstance(); + + /** + * 中国大陆区区号 + */ + private final static int COUNTRY_CODE = 86; + + /** + * 根据手机号 判断手机号是否有效 + * + * @param phoneNumber 手机号码 + * @return true-有效 false-无效 + */ + public static boolean checkPhoneNumber(String phoneNumber) { + long phone = Long.parseLong(phoneNumber); + + PhoneNumber pn = new PhoneNumber(); + pn.setCountryCode(COUNTRY_CODE); + pn.setNationalNumber(phone); + + return phoneNumberUtil.isValidNumber(pn); + + } + + /** + * 根据手机号 判断手机运营商 + * + * @param phoneNumber 手机号码 + * @return 如:广东省广州市移动 + */ + public static String getCarrier(String phoneNumber) { + + long phone = Long.parseLong(phoneNumber); + + PhoneNumber pn = new PhoneNumber(); + pn.setCountryCode(COUNTRY_CODE); + pn.setNationalNumber(phone); + // 返回结果只有英文,自己转成成中文 + String carrierEn = carrierMapper.getNameForNumber(pn, Locale.ENGLISH); + String carrierZh = ""; + switch (carrierEn) { + case "China Mobile": + carrierZh += "移动"; + break; + case "China Unicom": + carrierZh += "联通"; + break; + case "China Telecom": + carrierZh += "电信"; + break; + default: + break; + } + return carrierZh; + } + + /** + * 根据手机号 获取手机归属地 + * + * @param phoneNumber 手机号码 + * @return 如:广东省广州市 + */ + public static String getGeo(String phoneNumber) { + long phone = Long.parseLong(phoneNumber); + + PhoneNumber pn = new PhoneNumber(); + pn.setCountryCode(COUNTRY_CODE); + pn.setNationalNumber(phone); + return geocoder.getDescriptionForNumber(pn, Locale.CHINESE); + } + + /** + * 根据手机号 获取手机信息模型 + * + *
+     * 若返回值为null,则说明该号码无效
+     * 
+ * + * @param phoneNumber 手机号码 + * @return 手机信息模型PhoneModel + */ + public static PhoneDto getPhoneDto(String phoneNumber) { + if (checkPhoneNumber(phoneNumber)) { + String geo = getGeo(phoneNumber); + PhoneDto phoneDto = new PhoneDto(); + String carrier = getCarrier(phoneNumber); + phoneDto.setCarrier(carrier); + // 直辖市 + for (String val : MUNICIPALITY) { + if (geo.equals(val)) { + phoneDto.setProvinceName(val.replace("市", "")); + phoneDto.setCityName(val); + return phoneDto; + } + } + // 自治区 + for (String val : AUTONOMOUS_REGION) { + if (geo.startsWith(val)) { + phoneDto.setProvinceName(val); + phoneDto.setCityName(geo.replace(val, "")); + return phoneDto; + } + } + + // 其它 + String[] splitArr = geo.split("省"); + if (splitArr != null && splitArr.length == 2) { + phoneDto.setProvinceName(splitArr[0]); + phoneDto.setCityName(splitArr[1]); + return phoneDto; + } + } + return null; + } + + public static void main(String[] args) { + PhoneDto phoneDto = PhoneUtil.getPhoneDto("13701001254"); + if (phoneDto != null) { + System.out.println(phoneDto.getProvinceName()); + System.out.println(phoneDto.getCityName()); + System.out.println(phoneDto.getCarrier()); + } else { + System.err.println("该号码无效"); + } + } + +} \ No newline at end of file diff --git a/esua-epdc/epdc-commons/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/RandomUtil.java b/esua-epdc/epdc-commons/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/RandomUtil.java new file mode 100644 index 000000000..bb1fc6faf --- /dev/null +++ b/esua-epdc/epdc-commons/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/RandomUtil.java @@ -0,0 +1,40 @@ +package com.elink.esua.epdc.commons.tools.utils; + +import java.util.Random; + +/** + * @author rongchao + * @Date 18-12-17 + */ +public class RandomUtil { + /** + * 根据需要生成指定长度的纯数字随机数,这个随机数的每一位都是从(0-9)这个产生的一位 + * + * @param codeLen + * @return + */ + public static String getNewRandomCode(int codeLen) { + Random randomCode = new Random(); + String strCode = ""; + while (codeLen > 0) { + int charCode = randomCode.nextInt(9); + strCode += charCode; + codeLen--; + } + return strCode; + } + + public static int getRandomNum(int min, int max) { + Random random = new Random(); + return random.nextInt(max - min + 1) + min; + } + + public static void main(String[] args) { + for (int i = 0; i < 10000; i++) { + int s = RandomUtil.getRandomNum(1, 10); + if (s < 1 || s > 10) { + System.out.println(s); + } + } + } +} \ No newline at end of file diff --git a/esua-epdc/epdc-commons/pom.xml b/esua-epdc/epdc-commons/pom.xml index 2755b9d7a..9709e2771 100644 --- a/esua-epdc/epdc-commons/pom.xml +++ b/esua-epdc/epdc-commons/pom.xml @@ -18,5 +18,6 @@ epdc-commons-mybatis epdc-commons-dynamic-datasource epdc-commons-api-version-control + epdc-commons-tools-phone diff --git a/esua-epdc/epdc-module/epdc-demo/epdc-demo-server/pom.xml b/esua-epdc/epdc-module/epdc-demo/epdc-demo-server/pom.xml index 0f98272b1..ad41b113f 100644 --- a/esua-epdc/epdc-module/epdc-demo/epdc-demo-server/pom.xml +++ b/esua-epdc/epdc-module/epdc-demo/epdc-demo-server/pom.xml @@ -41,6 +41,11 @@ spring-boot-admin-starter-client ${spring.boot.admin.version} + + com.esua.epdc + epdc-commons-tools-phone + 1.0.0 + diff --git a/esua-epdc/epdc-module/epdc-demo/epdc-demo-server/src/test/java/com/elink/esua/epdc/OrikaTest.java b/esua-epdc/epdc-module/epdc-demo/epdc-demo-server/src/test/java/com/elink/esua/epdc/OrikaTest.java index c28272ad7..59ef3f8a6 100644 --- a/esua-epdc/epdc-module/epdc-demo/epdc-demo-server/src/test/java/com/elink/esua/epdc/OrikaTest.java +++ b/esua-epdc/epdc-module/epdc-demo/epdc-demo-server/src/test/java/com/elink/esua/epdc/OrikaTest.java @@ -1,6 +1,8 @@ package com.elink.esua.epdc; import com.alibaba.fastjson.JSON; +import com.elink.esua.epdc.commons.tools.utils.PhoneDto; +import com.elink.esua.epdc.commons.tools.utils.PhoneUtil; import com.elink.esua.epdc.entity.DemoEntity; import com.elink.esua.epdc.entity.TransforDemoEntity; import ma.glasnost.orika.MapperFacade; @@ -35,4 +37,11 @@ public class OrikaTest { System.out.println(JSON.toJSONString(transforEntity)); } + @Test + public void test1(){ + PhoneDto phoneDto = PhoneUtil.getPhoneDto("18268115282"); + System.out.println(JSON.toJSONString(phoneDto)); + } + + } diff --git a/esua-epdc/epdc-module/epdc-user/epdc-user-server/pom.xml b/esua-epdc/epdc-module/epdc-user/epdc-user-server/pom.xml index bc8ceb206..5a89f4f99 100644 --- a/esua-epdc/epdc-module/epdc-user/epdc-user-server/pom.xml +++ b/esua-epdc/epdc-module/epdc-user/epdc-user-server/pom.xml @@ -16,17 +16,22 @@ com.esua.epdc epdc-user-client - 1.0.0 + ${project.version} + + + com.esua.epdc + epdc-commons-tools-phone + ${project.version} com.esua.epdc epdc-commons-tools - 1.0.0 + ${project.version} com.esua.epdc epdc-commons-mybatis - 1.0.0 + ${project.version} org.springframework.boot @@ -45,6 +50,18 @@ spring-boot-admin-starter-client ${spring.boot.admin.version} + + + com.github.binarywang + weixin-java-mp + 3.4.0 + + + + io.jsonwebtoken + jjwt + 0.9.0 + diff --git a/esua-epdc/epdc-module/epdc-user/epdc-user-server/src/main/java/com/elink/esua/epdc/config/WxMpConfig.java b/esua-epdc/epdc-module/epdc-user/epdc-user-server/src/main/java/com/elink/esua/epdc/config/WxMpConfig.java new file mode 100644 index 000000000..ec950c51e --- /dev/null +++ b/esua-epdc/epdc-module/epdc-user/epdc-user-server/src/main/java/com/elink/esua/epdc/config/WxMpConfig.java @@ -0,0 +1,45 @@ +package com.elink.esua.epdc.config; + +import com.elink.esua.epdc.config.property.WxMpProperties; +import me.chanjar.weixin.mp.api.WxMpConfigStorage; +import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage; +import me.chanjar.weixin.mp.api.WxMpService; +import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * 微信公众号配置 + * + * @author yujintao + * @date 2019/6/17 17:38 + */ +@Configuration +@EnableConfigurationProperties(WxMpProperties.class) +public class WxMpConfig { + + private WxMpProperties properties; + + @Autowired + public WxMpConfig(WxMpProperties properties) { + this.properties = properties; + } + + @Bean + public WxMpService wxMpService() { + WxMpService wxMpService = new WxMpServiceImpl(); + wxMpService.setWxMpConfigStorage(wxMpConfigStorage()); + return wxMpService; + } + + @Bean + public WxMpConfigStorage wxMpConfigStorage() { + WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage(); + wxMpConfigStorage.setAppId(properties.getAppid()); + wxMpConfigStorage.setSecret(properties.getSecret()); + return wxMpConfigStorage; + } + +} diff --git a/esua-epdc/epdc-module/epdc-user/epdc-user-server/src/main/java/com/elink/esua/epdc/config/property/WxMpProperties.java b/esua-epdc/epdc-module/epdc-user/epdc-user-server/src/main/java/com/elink/esua/epdc/config/property/WxMpProperties.java new file mode 100644 index 000000000..ca46e651a --- /dev/null +++ b/esua-epdc/epdc-module/epdc-user/epdc-user-server/src/main/java/com/elink/esua/epdc/config/property/WxMpProperties.java @@ -0,0 +1,26 @@ +package com.elink.esua.epdc.config.property; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * 微信公众号框架配置类 + * + * @author + * @Date + */ +@Data +@ConfigurationProperties(prefix = "wx.mp") +public class WxMpProperties { + + /** + * 设置微信公众号的appid + */ + private String appid; + + /** + * 设置微信公众号的Secret + */ + private String secret; + +} diff --git a/esua-epdc/epdc-module/epdc-user/epdc-user-server/src/main/java/com/elink/esua/epdc/jwt/JwtProperties.java b/esua-epdc/epdc-module/epdc-user/epdc-user-server/src/main/java/com/elink/esua/epdc/jwt/JwtProperties.java new file mode 100644 index 000000000..42f4d3b81 --- /dev/null +++ b/esua-epdc/epdc-module/epdc-user/epdc-user-server/src/main/java/com/elink/esua/epdc/jwt/JwtProperties.java @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2018 人人开源 All rights reserved. + * + * https://www.renren.io + * + * 版权所有,侵权必究! + */ + +package com.elink.esua.epdc.jwt; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +/** + * Jwt + * + * @author Mark sunlightcs@gmail.com + * @since 1.0.0 + */ +@Configuration +@ConfigurationProperties(prefix = "jwt") +public class JwtProperties { + + private String secret; + + private int expire; + + public String getSecret() { + return secret; + } + + public void setSecret(String secret) { + this.secret = secret; + } + + public int getExpire() { + return expire; + } + + public void setExpire(int expire) { + this.expire = expire; + } +} \ No newline at end of file diff --git a/esua-epdc/epdc-module/epdc-user/epdc-user-server/src/main/java/com/elink/esua/epdc/jwt/JwtUtils.java b/esua-epdc/epdc-module/epdc-user/epdc-user-server/src/main/java/com/elink/esua/epdc/jwt/JwtUtils.java new file mode 100644 index 000000000..fca511b85 --- /dev/null +++ b/esua-epdc/epdc-module/epdc-user/epdc-user-server/src/main/java/com/elink/esua/epdc/jwt/JwtUtils.java @@ -0,0 +1,80 @@ +/** + * Copyright (c) 2018 人人开源 All rights reserved. + *

+ * https://www.renren.io + *

+ * 版权所有,侵权必究! + */ + +package com.elink.esua.epdc.jwt; + +import io.jsonwebtoken.Claims; +import io.jsonwebtoken.Jwts; +import io.jsonwebtoken.SignatureAlgorithm; +import org.joda.time.DateTime; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Date; + +/** + * Jwt工具类 + * + * @author Mark sunlightcs@gmail.com + * @since 1.0.0 + */ +@Component +public class JwtUtils { + private static final Logger logger = LoggerFactory.getLogger(JwtUtils.class); + + @Autowired + private JwtProperties jwtProperties; + + /** + * 生成jwt token + */ + public String generateToken(Long userId) { + return Jwts.builder() + .setHeaderParam("typ", "JWT") + .setSubject(userId + "") + .setIssuedAt(new Date()) + .setExpiration(DateTime.now().plusSeconds(jwtProperties.getExpire()).toDate()) + .signWith(SignatureAlgorithm.HS512, jwtProperties.getSecret()) + .compact(); + } + + /** + * 生成jwt token + */ + public String generateToken(String userId) { + return Jwts.builder() + .setHeaderParam("typ", "JWT") + .setSubject(userId) + .setIssuedAt(new Date()) + .setExpiration(DateTime.now().plusSeconds(jwtProperties.getExpire()).toDate()) + .signWith(SignatureAlgorithm.HS512, jwtProperties.getSecret()) + .compact(); + } + + public Claims getClaimByToken(String token) { + try { + return Jwts.parser() + .setSigningKey(jwtProperties.getSecret()) + .parseClaimsJws(token) + .getBody(); + } catch (Exception e) { + logger.debug("validate is token error, token = " + token, e); + return null; + } + } + + /** + * token是否过期 + * @return true:过期 + */ + public boolean isTokenExpired(Date expiration) { + return expiration.before(new Date()); + } +} \ No newline at end of file diff --git a/esua-epdc/epdc-module/epdc-user/epdc-user-server/src/main/resources/application.yml b/esua-epdc/epdc-module/epdc-user/epdc-user-server/src/main/resources/application.yml index eec195699..444af3748 100644 --- a/esua-epdc/epdc-module/epdc-user/epdc-user-server/src/main/resources/application.yml +++ b/esua-epdc/epdc-module/epdc-user/epdc-user-server/src/main/resources/application.yml @@ -36,6 +36,17 @@ spring: username: @spring.datasource.druid.username@ password: @spring.datasource.druid.password@ +jwt: + #秘钥 + secret: 7016867071f0ebf1c46f123eaaf4b9d6[elink.epdc] + #token有效时长,默认7天,单位秒 + expire: 604800 + +wx: + # 公众号 + mp: + appid: + secret: management: endpoints: