forked from rongchao/epmet-cloud-rizhao
52 changed files with 2247 additions and 37 deletions
@ -0,0 +1,19 @@ |
|||
dao层: |
|||
1、结果集是集合用selectListXXX |
|||
2、结果集为一个实例用selectOneXXX |
|||
3、更新:updateXXX |
|||
4、插入:insertXXX |
|||
5、删除:deleteXXX |
|||
6、获取统计值的方法用 selectCount |
|||
service层: |
|||
1、结果集是集合:listXXX |
|||
2、结果集是一个实例:getXXX |
|||
3、插入:saveXXX |
|||
5、修改:modifyXXX |
|||
4、删除:removeXXX |
|||
5、获取统计值的方法用 countXXX |
|||
|
|||
|
|||
数据传输对象:xxxDTO,xxx 为业务领域相关的名称 |
|||
|
|||
controller层根据接口定义来取名,接口定的什么就取什么 |
Binary file not shown.
@ -0,0 +1,125 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|||
<parent> |
|||
<artifactId>epmet-commons</artifactId> |
|||
<groupId>com.epmet</groupId> |
|||
<version>2.0.0</version> |
|||
</parent> |
|||
<modelVersion>4.0.0</modelVersion> |
|||
|
|||
<artifactId>epmet-common-clienttoken</artifactId> |
|||
<packaging>jar</packaging> |
|||
|
|||
<name>epmet-common-clienttoken</name> |
|||
<url>http://www.example.com</url> |
|||
<description>客户端token</description> |
|||
|
|||
<dependencies> |
|||
<dependency> |
|||
<groupId>com.epmet</groupId> |
|||
<artifactId>epmet-commons-tools</artifactId> |
|||
<version>${project.version}</version> |
|||
</dependency> |
|||
|
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-web</artifactId> |
|||
<scope>provided</scope> |
|||
</dependency> |
|||
|
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-aop</artifactId> |
|||
<scope>provided</scope> |
|||
</dependency> |
|||
|
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-configuration-processor</artifactId> |
|||
<optional>true</optional> |
|||
<exclusions> |
|||
<exclusion> |
|||
<groupId>com.vaadin.external.google</groupId> |
|||
<artifactId>android-json</artifactId> |
|||
</exclusion> |
|||
</exclusions> |
|||
</dependency> |
|||
|
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-autoconfigure</artifactId> |
|||
<scope>compile</scope> |
|||
</dependency> |
|||
|
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-autoconfigure-processor</artifactId> |
|||
<scope>compile</scope> |
|||
<optional>true</optional> |
|||
</dependency> |
|||
|
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-log4j2</artifactId> |
|||
<scope>provided</scope> |
|||
</dependency> |
|||
|
|||
<dependency> |
|||
<groupId>io.jsonwebtoken</groupId> |
|||
<artifactId>jjwt</artifactId> |
|||
<version>0.7.0</version> |
|||
</dependency> |
|||
</dependencies> |
|||
|
|||
<build> |
|||
<finalName>${project.artifactId}</finalName> |
|||
|
|||
<plugins> |
|||
<plugin> |
|||
<artifactId>maven-clean-plugin</artifactId> |
|||
</plugin> |
|||
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> |
|||
<plugin> |
|||
<artifactId>maven-resources-plugin</artifactId> |
|||
</plugin> |
|||
<plugin> |
|||
<artifactId>maven-compiler-plugin</artifactId> |
|||
<configuration> |
|||
<parameters>true</parameters> |
|||
<source>${maven.compiler.source}</source> <!-- 源代码使用的开发版本 --> |
|||
<target>${maven.compiler.target}</target> <!-- 需要生成的目标class文件的编译版本 --> |
|||
<encoding>${project.build.sourceEncoding}</encoding> |
|||
</configuration> |
|||
</plugin> |
|||
<plugin> |
|||
<artifactId>maven-surefire-plugin</artifactId> |
|||
</plugin> |
|||
<plugin> |
|||
<artifactId>maven-war-plugin</artifactId> |
|||
</plugin> |
|||
<plugin> |
|||
<artifactId>maven-install-plugin</artifactId> |
|||
</plugin> |
|||
<plugin> |
|||
<artifactId>maven-deploy-plugin</artifactId> |
|||
</plugin> |
|||
|
|||
<!-- jar插件 --> |
|||
<plugin> |
|||
<groupId>org.apache.maven.plugins</groupId> |
|||
<artifactId>maven-jar-plugin</artifactId> |
|||
<version>2.4</version> |
|||
<configuration> |
|||
<archive> |
|||
<manifest> |
|||
<addDefaultImplementationEntries>true</addDefaultImplementationEntries> |
|||
</manifest> |
|||
</archive> |
|||
</configuration> |
|||
</plugin> |
|||
</plugins> |
|||
|
|||
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory> |
|||
</build> |
|||
</project> |
@ -0,0 +1,31 @@ |
|||
/** |
|||
* Copyright 2018 人人开源 http://www.renren.io
|
|||
* <p> |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
|||
* use this file except in compliance with the License. You may obtain a copy of |
|||
* the License at |
|||
* <p> |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* <p> |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|||
* License for the specific language governing permissions and limitations under |
|||
* the License. |
|||
*/ |
|||
|
|||
package com.epmet.common.token.annotation; |
|||
|
|||
import java.lang.annotation.*; |
|||
|
|||
/** |
|||
* 登录效验 |
|||
* @author chenshun |
|||
* @email sunlightcs@gmail.com |
|||
* @date 2017/9/23 14:30 |
|||
*/ |
|||
@Target(ElementType.METHOD) |
|||
@Retention(RetentionPolicy.RUNTIME) |
|||
@Documented |
|||
public @interface Login { |
|||
} |
@ -0,0 +1,35 @@ |
|||
/** |
|||
* Copyright 2018 人人开源 http://www.renren.io
|
|||
* <p> |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
|||
* use this file except in compliance with the License. You may obtain a copy of |
|||
* the License at |
|||
* <p> |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* <p> |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|||
* License for the specific language governing permissions and limitations under |
|||
* the License. |
|||
*/ |
|||
|
|||
package com.epmet.common.token.annotation; |
|||
|
|||
import java.lang.annotation.ElementType; |
|||
import java.lang.annotation.Retention; |
|||
import java.lang.annotation.RetentionPolicy; |
|||
import java.lang.annotation.Target; |
|||
|
|||
/** |
|||
* 登录用户信息 |
|||
* |
|||
* @author chenshun |
|||
* @email sunlightcs@gmail.com |
|||
* @date 2017-03-23 20:39 |
|||
*/ |
|||
@Target(ElementType.PARAMETER) |
|||
@Retention(RetentionPolicy.RUNTIME) |
|||
public @interface LoginUser { |
|||
|
|||
} |
@ -0,0 +1,13 @@ |
|||
package com.epmet.common.token.annotation; |
|||
|
|||
import java.lang.annotation.ElementType; |
|||
import java.lang.annotation.Retention; |
|||
import java.lang.annotation.RetentionPolicy; |
|||
import java.lang.annotation.Target; |
|||
|
|||
@Retention(RetentionPolicy.CLASS)//生命注释保留时长,这里无需反射使用,使用CLASS级别
|
|||
@Target(ElementType.METHOD)//生命可以使用此注解的元素级别类型(如类、方法变量等)
|
|||
public @interface NeedClientToken { |
|||
|
|||
boolean value() default true; |
|||
} |
@ -0,0 +1,32 @@ |
|||
package com.epmet.common.token.dto; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import java.io.Serializable; |
|||
|
|||
/** |
|||
* |
|||
* 用户所有角色权限信息 |
|||
* |
|||
* @Author:liuchuang |
|||
* @Date:2019/11/19 23:48 |
|||
*/ |
|||
@Data |
|||
public class DeptDataScopeDTO implements Serializable { |
|||
private static final long serialVersionUID = -6319876948812713836L; |
|||
|
|||
/** |
|||
* 部门ID |
|||
*/ |
|||
private Long deptId; |
|||
|
|||
/** |
|||
* 部门名称 |
|||
*/ |
|||
private String deptName; |
|||
|
|||
/** |
|||
* 部门类型 |
|||
*/ |
|||
private String typeKey; |
|||
} |
@ -0,0 +1,52 @@ |
|||
package com.elink.esua.epdc.common.token.dto; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import java.io.Serializable; |
|||
|
|||
/** |
|||
* 用户token |
|||
* |
|||
* @author rongchao |
|||
* @Date 18-10-31 |
|||
*/ |
|||
@Data |
|||
public class TokenDto implements Serializable { |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
/** |
|||
* 用户ID |
|||
*/ |
|||
private String userId; |
|||
|
|||
/** |
|||
* 昵称 |
|||
*/ |
|||
private String nickname; |
|||
|
|||
/** |
|||
* 用户头像 |
|||
*/ |
|||
private String faceImg; |
|||
|
|||
/** |
|||
* 手机号 |
|||
*/ |
|||
private String mobile; |
|||
|
|||
/** |
|||
* 真实姓名 |
|||
*/ |
|||
private String realName; |
|||
|
|||
/** |
|||
* 网格ID |
|||
*/ |
|||
private Long gridId; |
|||
|
|||
/** |
|||
* 党员标识 0:否、1:是 |
|||
*/ |
|||
private String partyFlag; |
|||
} |
@ -0,0 +1,48 @@ |
|||
package com.epmet.common.token.dto; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import java.io.Serializable; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* |
|||
* 工作端用户授权信息 |
|||
* |
|||
* @Author:liuchuang |
|||
* @Date:2019/11/19 20:17 |
|||
*/ |
|||
@Data |
|||
public class WorkUserAuthorizationDTO implements Serializable { |
|||
private static final long serialVersionUID = -4230190448906007120L; |
|||
|
|||
/** |
|||
* 令牌 |
|||
*/ |
|||
private String token; |
|||
|
|||
/** |
|||
* 过期时长,单位秒 |
|||
*/ |
|||
private Integer expire; |
|||
|
|||
/** |
|||
* 部门名称 |
|||
*/ |
|||
private String deptName; |
|||
|
|||
/** |
|||
* 用户标签key |
|||
*/ |
|||
private String userTagKey; |
|||
|
|||
/** |
|||
* 注册状态 0-未注册,1-已注册 |
|||
*/ |
|||
private String registerState; |
|||
|
|||
/** |
|||
* 用户所有角色权限信息 |
|||
*/ |
|||
private List<DeptDataScopeDTO> deptDataScopeList; |
|||
} |
@ -0,0 +1,29 @@ |
|||
package com.epmet.common.token.dto; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import javax.validation.constraints.NotBlank; |
|||
import java.io.Serializable; |
|||
|
|||
/** |
|||
* |
|||
* 工作端用户登录信息 |
|||
* |
|||
* @Author:liuchuang |
|||
* @Date:2019/11/19 20:19 |
|||
*/ |
|||
@Data |
|||
public class WorkUserLoginDTO implements Serializable { |
|||
private static final long serialVersionUID = 1905641243346550379L; |
|||
|
|||
@NotBlank(message="用户名不能为空") |
|||
private String username; |
|||
|
|||
@NotBlank(message="密码不能为空") |
|||
private String password; |
|||
|
|||
@NotBlank(message="微信code不能为空") |
|||
private String wxCode; |
|||
|
|||
private String openId; |
|||
} |
@ -0,0 +1,25 @@ |
|||
package com.epmet.common.token.dto; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import javax.validation.constraints.NotBlank; |
|||
import javax.validation.constraints.NotNull; |
|||
import java.io.Serializable; |
|||
|
|||
/** |
|||
* |
|||
* 工作端获取token Form DTO |
|||
* |
|||
* @Author:liuchuang |
|||
* @Date:2019/11/19 20:35 |
|||
*/ |
|||
@Data |
|||
public class WorkUserTokenFormDTO implements Serializable { |
|||
private static final long serialVersionUID = -2239027109939769097L; |
|||
|
|||
@NotNull(message = "用户ID不能为空") |
|||
private Long id; |
|||
|
|||
@NotBlank(message = "用户名不能为空") |
|||
private String username; |
|||
} |
@ -0,0 +1,47 @@ |
|||
package com.epmet.common.token.enums; |
|||
|
|||
|
|||
import com.epmet.common.token.error.IErrorCode; |
|||
|
|||
/** |
|||
* client token错误码 |
|||
* |
|||
* @author rongchao |
|||
* @Date 18-11-24 |
|||
*/ |
|||
public enum ErrorCode implements IErrorCode { |
|||
|
|||
SUCCESS(0, "请求成功"), |
|||
|
|||
ERR10001(10001, "clientToken不合法或者已过期"), |
|||
ERR10002(10002, "无法获取当前用户的信息,无法生成clientToken。"), |
|||
ERR10003(10003, "clientToken生成失败,请重试。"), |
|||
ERR10004(10004, "返回的Object类型不是EsuaResponse,无法添加token!"), |
|||
ERR10005(10005, "clentToken不能为空"), |
|||
|
|||
ERR500(500, "Internal Server Error"), |
|||
ERR501(501, "参数绑定异常"), |
|||
|
|||
ERR(ErrorCode.COMMON_ERR_CODE, "其他异常"); |
|||
|
|||
private int code; |
|||
|
|||
private String msg; |
|||
|
|||
ErrorCode(final int code, final String msg) { |
|||
this.code = code; |
|||
this.msg = msg; |
|||
} |
|||
|
|||
public static final int COMMON_ERR_CODE = -1; |
|||
|
|||
@Override |
|||
public int getCode() { |
|||
return code; |
|||
} |
|||
|
|||
@Override |
|||
public String getMsg() { |
|||
return msg; |
|||
} |
|||
} |
@ -0,0 +1,11 @@ |
|||
package com.epmet.common.token.error; |
|||
|
|||
/** |
|||
* @author rongchao |
|||
* @Date 18-11-20 |
|||
*/ |
|||
public interface IErrorCode { |
|||
int getCode(); |
|||
|
|||
String getMsg(); |
|||
} |
@ -0,0 +1,23 @@ |
|||
package com.epmet.common.token.property; |
|||
|
|||
import org.springframework.boot.context.properties.ConfigurationProperties; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
/** |
|||
* @author rongchao |
|||
* @Date 18-12-3 |
|||
*/ |
|||
@Component |
|||
@ConfigurationProperties(prefix = "token") |
|||
public class TokenPropertise { |
|||
|
|||
private long expire = 7200L; |
|||
|
|||
public long getExpire() { |
|||
return expire; |
|||
} |
|||
|
|||
public void setExpire(long expire) { |
|||
this.expire = expire; |
|||
} |
|||
} |
@ -0,0 +1,94 @@ |
|||
/** |
|||
* Copyright (c) 2018 人人开源 All rights reserved. |
|||
* <p> |
|||
* https://www.renren.io
|
|||
* <p> |
|||
* 版权所有,侵权必究! |
|||
*/ |
|||
|
|||
package com.epmet.common.token.util; |
|||
|
|||
import cn.hutool.core.bean.BeanUtil; |
|||
import cn.hutool.core.map.MapUtil; |
|||
import com.elink.esua.epdc.common.token.dto.TokenDto; |
|||
import com.epmet.commons.tools.redis.RedisKeys; |
|||
import com.epmet.commons.tools.redis.RedisUtils; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* CP用户Redis |
|||
* |
|||
* @author rongchao |
|||
* @since 1.0.0 |
|||
*/ |
|||
@Component |
|||
public class CpUserDetailRedis { |
|||
|
|||
@Autowired |
|||
private RedisUtils redisUtils; |
|||
|
|||
public void set(TokenDto user, long expire) { |
|||
if (user == null) { |
|||
return; |
|||
} |
|||
String key = RedisKeys.getCpUserKey(user.getUserId()); |
|||
//bean to map
|
|||
Map<String, Object> map = BeanUtil.beanToMap(user, false, true); |
|||
redisUtils.hMSet(key, map, expire); |
|||
} |
|||
|
|||
/** |
|||
* 获取token信息 |
|||
* |
|||
* @param userId |
|||
* @return |
|||
*/ |
|||
public TokenDto get(String userId) { |
|||
String key = RedisKeys.getCpUserKey(userId); |
|||
|
|||
Map<String, Object> map = redisUtils.hGetAll(key); |
|||
if (MapUtil.isEmpty(map)) { |
|||
return null; |
|||
} |
|||
|
|||
//map to bean
|
|||
TokenDto user = BeanUtil.mapToBean(map, TokenDto.class, true); |
|||
|
|||
return user; |
|||
} |
|||
|
|||
/** |
|||
* 删除用户信息 |
|||
* |
|||
* @param userId |
|||
*/ |
|||
public void logout(String userId) { |
|||
redisUtils.delete(RedisKeys.getCpUserKey(userId)); |
|||
} |
|||
|
|||
/** |
|||
* 设置redis时间 |
|||
* |
|||
* @param userId |
|||
* @param expire |
|||
* @author rongchao |
|||
*/ |
|||
public boolean expire(String userId, long expire) { |
|||
return redisUtils.expire(RedisKeys.getCpUserKey(userId), expire); |
|||
} |
|||
|
|||
/** |
|||
* 查询token剩余时间 |
|||
* |
|||
* @param userId |
|||
* @return long |
|||
* @author yujintao |
|||
* @date 2019/9/9 14:18 |
|||
*/ |
|||
public long getExpire(String userId) { |
|||
return redisUtils.getExpire(RedisKeys.getCpUserKey(userId)); |
|||
} |
|||
} |
@ -0,0 +1,51 @@ |
|||
package com.epmet.common.token.util; |
|||
|
|||
import com.elink.esua.epdc.common.token.dto.TokenDto; |
|||
import com.epmet.common.token.property.TokenPropertise; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
/** |
|||
* token服务类 |
|||
* |
|||
* @author rongchao |
|||
* @Date 18-10-31 |
|||
*/ |
|||
@Component |
|||
public class TokenUtil { |
|||
|
|||
private Logger logger = LoggerFactory.getLogger(getClass()); |
|||
|
|||
@Autowired |
|||
private TokenPropertise tokenPropertise; |
|||
|
|||
@Autowired |
|||
private CpUserDetailRedis redisUtils; |
|||
|
|||
public TokenDto getTokenInfo(String userId) { |
|||
TokenDto tokenDto = redisUtils.get(userId); |
|||
return tokenDto; |
|||
} |
|||
|
|||
public void expireToken(String userId) { |
|||
redisUtils.logout(userId); |
|||
} |
|||
|
|||
public boolean delayToken(String userId) { |
|||
return redisUtils.expire(userId, tokenPropertise.getExpire()); |
|||
} |
|||
|
|||
/** |
|||
* 获取token过期时间 |
|||
* |
|||
* @param userId |
|||
* @return long |
|||
* @author yujintao |
|||
* @date 2019/9/9 14:19 |
|||
*/ |
|||
public long getExpire(String userId) { |
|||
return redisUtils.getExpire(userId); |
|||
} |
|||
} |
@ -0,0 +1,55 @@ |
|||
package com.epmet.common.token.util; |
|||
|
|||
import com.elink.esua.epdc.common.token.dto.TokenDto; |
|||
import com.epmet.commons.tools.constant.Constant; |
|||
import com.epmet.commons.tools.utils.WebUtil; |
|||
|
|||
/** |
|||
* 用户工具类 |
|||
* |
|||
* @author rongchao |
|||
* @Date 18-11-20 |
|||
*/ |
|||
public class UserUtil { |
|||
|
|||
/** |
|||
* 获取当前用户信息 |
|||
* |
|||
* @return |
|||
*/ |
|||
public static TokenDto getCurrentUser() { |
|||
return (TokenDto) WebUtil.getAttributesFromRequest(Constant.APP_USER_KEY); |
|||
} |
|||
|
|||
/** |
|||
* 获取当前用户信息 |
|||
* |
|||
* @return com.elink.esua.common.token.dto.UserTokenDto |
|||
* @author yujintao |
|||
* @date 2018/12/5 9:24 |
|||
*/ |
|||
public static TokenDto getCurrentUserInfo() { |
|||
TokenDto tokenDto = getCurrentUser(); |
|||
if (tokenDto == null) { |
|||
return null; |
|||
} |
|||
return tokenDto; |
|||
} |
|||
|
|||
/** |
|||
* 获取当前用户ID |
|||
* |
|||
* @return |
|||
*/ |
|||
public static String getCurrentUserId() { |
|||
TokenDto tokenDto = getCurrentUser(); |
|||
if (tokenDto == null) { |
|||
return null; |
|||
} |
|||
return tokenDto.getUserId(); |
|||
} |
|||
|
|||
public static void setCurrentUser(TokenDto user) { |
|||
WebUtil.setAttributesFromRequest(Constant.APP_USER_KEY, user); |
|||
} |
|||
} |
@ -0,0 +1,41 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|||
<parent> |
|||
<artifactId>epmet-commons</artifactId> |
|||
<groupId>com.epmet</groupId> |
|||
<version>2.0.0</version> |
|||
</parent> |
|||
<modelVersion>4.0.0</modelVersion> |
|||
|
|||
<artifactId>epmet-commons-tools-phone</artifactId> |
|||
<packaging>jar</packaging> |
|||
|
|||
<dependencies> |
|||
<dependency> |
|||
<groupId>com.googlecode.libphonenumber</groupId> |
|||
<artifactId>libphonenumber</artifactId> |
|||
<version>8.8.8</version> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>com.googlecode.libphonenumber</groupId> |
|||
<artifactId>carrier</artifactId> |
|||
<version>1.75</version> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>com.googlecode.libphonenumber</groupId> |
|||
<artifactId>geocoder</artifactId> |
|||
<version>2.85</version> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>com.googlecode.libphonenumber</groupId> |
|||
<artifactId>prefixmapper</artifactId> |
|||
<version>2.85</version> |
|||
</dependency> |
|||
</dependencies> |
|||
|
|||
<build> |
|||
<finalName>${project.artifactId}</finalName> |
|||
</build> |
|||
</project> |
@ -0,0 +1,79 @@ |
|||
package com.epmet.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; |
|||
} |
|||
} |
@ -0,0 +1,178 @@ |
|||
package com.epmet.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; |
|||
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.PhoneNumber pn = new Phonenumber.PhoneNumber(); |
|||
pn.setCountryCode(COUNTRY_CODE); |
|||
pn.setNationalNumber(phone); |
|||
return geocoder.getDescriptionForNumber(pn, Locale.CHINESE); |
|||
} |
|||
|
|||
/** |
|||
* 根据手机号 获取手机信息模型 |
|||
* |
|||
* <pre> |
|||
* 若返回值为null,则说明该号码无效 |
|||
* </pre> |
|||
* |
|||
* @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("该号码无效"); |
|||
} |
|||
} |
|||
|
|||
} |
@ -0,0 +1,34 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|||
<parent> |
|||
<artifactId>epmet-commons</artifactId> |
|||
<groupId>com.epmet</groupId> |
|||
<version>2.0.0</version> |
|||
</parent> |
|||
<modelVersion>4.0.0</modelVersion> |
|||
|
|||
<version>1.0.0</version> |
|||
<artifactId>epmet-commons-tools-wx-ma</artifactId> |
|||
<packaging>jar</packaging> |
|||
|
|||
<dependencies> |
|||
<dependency> |
|||
<groupId>org.projectlombok</groupId> |
|||
<artifactId>lombok</artifactId> |
|||
<scope>provided</scope> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-autoconfigure</artifactId> |
|||
<scope>compile</scope> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>com.github.binarywang</groupId> |
|||
<artifactId>weixin-java-miniapp</artifactId> |
|||
<version>3.6.0</version> |
|||
</dependency> |
|||
</dependencies> |
|||
|
|||
</project> |
@ -0,0 +1,63 @@ |
|||
package com.epmet.utils; |
|||
|
|||
import cn.binarywang.wx.miniapp.api.WxMaService; |
|||
import com.epmet.wx.ma.WxMaConfig; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
/** |
|||
* 获取小程序业务工具 |
|||
* |
|||
* @author work@yujt.net.cn |
|||
* @date 2019/11/25 13:29 |
|||
*/ |
|||
@Component |
|||
public class WxMaServiceUtils { |
|||
|
|||
@Value("${wx.ma.appId.resi}") |
|||
private String APPID_RESI; |
|||
|
|||
@Value("${wx.ma.appId.gov}") |
|||
private String APPID_GOV; |
|||
|
|||
@Value("${wx.ma.appId.oper}") |
|||
private String APPID_OPER; |
|||
|
|||
/** |
|||
* 获取党群e事通-居民端小程序配置 |
|||
* |
|||
* @return cn.binarywang.wx.miniapp.api.WxMaService |
|||
* @author yinzuomei |
|||
* @date 2020/03/13 10:44 |
|||
*/ |
|||
public final WxMaService resiWxMaService() { |
|||
final WxMaService wxMaService = WxMaConfig.getMaService(APPID_RESI); |
|||
return wxMaService; |
|||
} |
|||
|
|||
/** |
|||
* 获取党群e事通-政府端小程序配置 |
|||
* |
|||
* @return cn.binarywang.wx.miniapp.api.WxMaService |
|||
* @author yinzuomei |
|||
* @date 2020/03/13 10:44 |
|||
*/ |
|||
public final WxMaService govWxMaService() { |
|||
final WxMaService wxMaService = WxMaConfig.getMaService(APPID_GOV); |
|||
return wxMaService; |
|||
} |
|||
|
|||
/** |
|||
* |
|||
* 获取党群e事通-运营端小程序配置 |
|||
* |
|||
* @params [] |
|||
* @return cn.binarywang.wx.miniapp.api.WxMaService |
|||
* @author yinzuomei |
|||
* @since 2020/03/13 10:44 |
|||
*/ |
|||
public final WxMaService operWxMaService() { |
|||
final WxMaService wxMaService = WxMaConfig.getMaService(APPID_OPER); |
|||
return wxMaService; |
|||
} |
|||
} |
@ -0,0 +1,148 @@ |
|||
package com.epmet.wx.ma; |
|||
|
|||
import cn.binarywang.wx.miniapp.api.WxMaService; |
|||
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; |
|||
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage; |
|||
import cn.binarywang.wx.miniapp.bean.WxMaTemplateData; |
|||
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage; |
|||
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl; |
|||
import cn.binarywang.wx.miniapp.message.WxMaMessageHandler; |
|||
import cn.binarywang.wx.miniapp.message.WxMaMessageRouter; |
|||
import com.google.common.collect.Lists; |
|||
import com.google.common.collect.Maps; |
|||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; |
|||
import me.chanjar.weixin.common.error.WxErrorException; |
|||
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.io.File; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.stream.Collectors; |
|||
|
|||
/** |
|||
* @author <a href="https://github.com/binarywang">Binary Wang</a> |
|||
*/ |
|||
@Configuration |
|||
@EnableConfigurationProperties(WxMaProperties.class) |
|||
public class WxMaConfig { |
|||
|
|||
private WxMaProperties properties; |
|||
|
|||
private static Map<String, WxMaMessageRouter> routers = Maps.newHashMap(); |
|||
private static Map<String, WxMaService> maServices = Maps.newHashMap(); |
|||
|
|||
@Autowired |
|||
public WxMaConfig(WxMaProperties properties) { |
|||
this.properties = properties; |
|||
} |
|||
|
|||
public static WxMaService getMaService(String appid) { |
|||
WxMaService wxService = maServices.get(appid); |
|||
if (wxService == null) { |
|||
throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appid)); |
|||
} |
|||
|
|||
return wxService; |
|||
} |
|||
|
|||
public static WxMaMessageRouter getRouter(String appid) { |
|||
return routers.get(appid); |
|||
} |
|||
|
|||
@PostConstruct |
|||
public void init() { |
|||
List<WxMaProperties.Config> configs = this.properties.getConfigs(); |
|||
if (configs == null) { |
|||
throw new RuntimeException("大哥,拜托先看下项目首页的说明(readme文件),添加下相关配置,注意别配错了!"); |
|||
} |
|||
|
|||
maServices = configs.stream() |
|||
.map(a -> { |
|||
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); |
|||
config.setAppid(a.getAppid()); |
|||
config.setSecret(a.getSecret()); |
|||
config.setToken(a.getToken()); |
|||
config.setAesKey(a.getAesKey()); |
|||
config.setMsgDataFormat(a.getMsgDataFormat()); |
|||
|
|||
WxMaService service = new WxMaServiceImpl(); |
|||
service.setWxMaConfig(config); |
|||
routers.put(a.getAppid(), this.newRouter(service)); |
|||
return service; |
|||
}).collect(Collectors.toMap(s -> s.getWxMaConfig().getAppid(), a -> a)); |
|||
} |
|||
|
|||
private WxMaMessageRouter newRouter(WxMaService service) { |
|||
final WxMaMessageRouter router = new WxMaMessageRouter(service); |
|||
router |
|||
.rule().handler(logHandler).next() |
|||
.rule().async(false).content("模板").handler(templateMsgHandler).end() |
|||
.rule().async(false).content("文本").handler(textHandler).end() |
|||
.rule().async(false).content("图片").handler(picHandler).end() |
|||
.rule().async(false).content("二维码").handler(qrcodeHandler).end(); |
|||
return router; |
|||
} |
|||
|
|||
private final WxMaMessageHandler templateMsgHandler = (wxMessage, context, service, sessionManager) -> { |
|||
service.getMsgService().sendTemplateMsg(WxMaTemplateMessage.builder() |
|||
.templateId("此处更换为自己的模板id") |
|||
.formId("自己替换可用的formid") |
|||
.data(Lists.newArrayList( |
|||
new WxMaTemplateData("keyword1", "339208499", "#173177"))) |
|||
.toUser(wxMessage.getFromUser()) |
|||
.build()); |
|||
return null; |
|||
}; |
|||
|
|||
private final WxMaMessageHandler logHandler = (wxMessage, context, service, sessionManager) -> { |
|||
System.out.println("收到消息:" + wxMessage.toString()); |
|||
service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("收到信息为:" + wxMessage.toJson()) |
|||
.toUser(wxMessage.getFromUser()).build()); |
|||
return null; |
|||
}; |
|||
|
|||
private final WxMaMessageHandler textHandler = (wxMessage, context, service, sessionManager) -> { |
|||
service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("回复文本消息") |
|||
.toUser(wxMessage.getFromUser()).build()); |
|||
return null; |
|||
}; |
|||
|
|||
private final WxMaMessageHandler picHandler = (wxMessage, context, service, sessionManager) -> { |
|||
try { |
|||
WxMediaUploadResult uploadResult = service.getMediaService() |
|||
.uploadMedia("image", "png", |
|||
ClassLoader.getSystemResourceAsStream("tmp.png")); |
|||
service.getMsgService().sendKefuMsg( |
|||
WxMaKefuMessage |
|||
.newImageBuilder() |
|||
.mediaId(uploadResult.getMediaId()) |
|||
.toUser(wxMessage.getFromUser()) |
|||
.build()); |
|||
} catch (WxErrorException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
|
|||
return null; |
|||
}; |
|||
|
|||
private final WxMaMessageHandler qrcodeHandler = (wxMessage, context, service, sessionManager) -> { |
|||
try { |
|||
final File file = service.getQrcodeService().createQrcode("123", 430); |
|||
WxMediaUploadResult uploadResult = service.getMediaService().uploadMedia("image", file); |
|||
service.getMsgService().sendKefuMsg( |
|||
WxMaKefuMessage |
|||
.newImageBuilder() |
|||
.mediaId(uploadResult.getMediaId()) |
|||
.toUser(wxMessage.getFromUser()) |
|||
.build()); |
|||
} catch (WxErrorException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
|
|||
return null; |
|||
}; |
|||
|
|||
} |
@ -0,0 +1,48 @@ |
|||
package com.epmet.wx.ma; |
|||
|
|||
import lombok.Data; |
|||
import org.springframework.boot.context.properties.ConfigurationProperties; |
|||
|
|||
import java.util.List; |
|||
|
|||
/** |
|||
* 微信小程序框架配置类 |
|||
* |
|||
* @author rongchao |
|||
* @Date 19-5-13 |
|||
*/ |
|||
@Data |
|||
@ConfigurationProperties(prefix = "wx.ma") |
|||
public class WxMaProperties { |
|||
|
|||
private List<Config> configs; |
|||
|
|||
@Data |
|||
public static class Config { |
|||
/** |
|||
* 设置微信小程序的appid |
|||
*/ |
|||
private String appid; |
|||
|
|||
/** |
|||
* 设置微信小程序的Secret |
|||
*/ |
|||
private String secret; |
|||
|
|||
/** |
|||
* 设置微信小程序消息服务器配置的token |
|||
*/ |
|||
private String token; |
|||
|
|||
/** |
|||
* 设置微信小程序消息服务器配置的EncodingAESKey |
|||
*/ |
|||
private String aesKey; |
|||
|
|||
/** |
|||
* 消息格式,XML或者JSON |
|||
*/ |
|||
private String msgDataFormat; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,14 @@ |
|||
# EditorConfig: http://editorconfig.org/ |
|||
|
|||
root = true |
|||
|
|||
[*] |
|||
indent_style = space |
|||
indent_size = 4 |
|||
end_of_line = lf |
|||
charset = utf-8 |
|||
trim_trailing_whitespace = true |
|||
insert_final_newline = true |
|||
|
|||
[*.md] |
|||
trim_trailing_whitespace = false |
@ -0,0 +1,13 @@ |
|||
language: java |
|||
jdk: |
|||
- openjdk8 |
|||
|
|||
script: "mvn clean package -Dmaven.test.skip=true" |
|||
|
|||
branches: |
|||
only: |
|||
- master |
|||
|
|||
notifications: |
|||
email: |
|||
- binarywang@vip.qq.com |
@ -0,0 +1,58 @@ |
|||
[](https://gitee.com/binary/weixin-java-mp-demo-springboot) |
|||
[](https://github.com/binarywang/weixin-java-mp-demo-springboot) |
|||
[](https://travis-ci.org/binarywang/weixin-java-mp-demo-springboot) |
|||
----------------------- |
|||
|
|||
### 本Demo基于Spring Boot构建,实现微信公众号后端开发功能。 |
|||
### 本项目为WxJava的Demo演示程序,更多Demo请[查阅此处](https://github.com/Wechat-Group/WxJava/blob/master/demo.md)。 |
|||
#### 如有问题请[【在此提问】](https://github.com/binarywang/weixin-java-mp-demo-springboot/issues),谢谢配合。 |
|||
|
|||
<table border="0"> |
|||
<tbody> |
|||
<tr> |
|||
<td align="left" valign="middle"> |
|||
<a href="http://mp.weixin.qq.com/mp/homepage?__biz=MzI3MzAwMzk4OA==&hid=1&sn=f31af3bf562b116b061c9ab4edf70b61&scene=18#wechat_redirect" target="_blank"> |
|||
<img height="120" src="https://gitee.com/binary/weixin-java-tools/raw/master/images/qrcodes/mp.png"> |
|||
</a> |
|||
</td> |
|||
<td align="center" valign="middle"> |
|||
<a href="https://cloud.tencent.com/redirect.php?redirect=1014&cps_key=a4c06ffe004dbcda44036daa1bf8f876&from=console" target="_blank"> |
|||
<img height="120" src="https://gitee.com/binary/weixin-java-tools/raw/master/images/banners/tcloud.jpg"> |
|||
</a> |
|||
</td> |
|||
<td align="right" valign="middle"> |
|||
<a href="https://coding.net/?utm_source=WxJava" target="_blank"> |
|||
<img height="120" src="https://gitee.com/binary/weixin-java-tools/raw/master/images/banners/coding.jpg"> |
|||
</a> |
|||
</td> |
|||
<td align="center" valign="middle"> |
|||
<a href="https://promotion.aliyun.com/ntms/act/qwbk.html?userCode=7makzf5h" target="_blank"> |
|||
<img height="120" src="https://gitee.com/binary/weixin-java-tools/raw/master/images/banners/aliyun.jpg"> |
|||
</a> |
|||
</td> |
|||
</tr> |
|||
</tbody> |
|||
</table> |
|||
|
|||
## 使用步骤: |
|||
1. 请注意,本demo为简化代码编译时加入了lombok支持,如果不了解lombok的话,请先学习下相关知识,比如可以阅读[此文章](https://mp.weixin.qq.com/s/cUc-bUcprycADfNepnSwZQ); |
|||
1. 另外,新手遇到问题,请务必先阅读[【开发文档首页】](https://github.com/Wechat-Group/WxJava/wiki)的常见问题部分,可以少走很多弯路,节省不少时间。 |
|||
1. 配置:复制 `/src/main/resources/application.yml.template` 或修改其扩展名生成 `application.yml` 文件,根据自己需要填写相关配置(需要注意的是:yml文件内的属性冒号后面的文字之前需要加空格,可参考已有配置,否则属性会设置不成功); |
|||
2. 主要配置说明如下: |
|||
``` |
|||
wx: |
|||
mp: |
|||
configs: |
|||
- appId: 1111 (一个公众号的appid) |
|||
secret: 1111(公众号的appsecret) |
|||
token: 111 (接口配置里的Token值) |
|||
aesKey: 111 (接口配置里的EncodingAESKey值) |
|||
- appId: 2222 (另一个公众号的appid,以下同上) |
|||
secret: 1111 |
|||
token: 111 |
|||
aesKey: 111 |
|||
``` |
|||
3. 运行Java程序:`WxMpDemoApplication`; |
|||
4. 配置微信公众号中的接口地址:http://公网可访问域名/wx/portal/xxxxx (注意,xxxxx为对应公众号的appid值); |
|||
5. 根据自己需要修改各个handler的实现,加入自己的业务逻辑。 |
|||
|
@ -0,0 +1,60 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" |
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|||
<modelVersion>4.0.0</modelVersion> |
|||
|
|||
<parent> |
|||
<groupId>com.epmet</groupId> |
|||
<artifactId>epmet-commons</artifactId> |
|||
<version>2.0.0</version> |
|||
</parent> |
|||
|
|||
<version>1.0.0</version> |
|||
<artifactId>epmet-commons-tools-wx-mp</artifactId> |
|||
<packaging>jar</packaging> |
|||
|
|||
<!-- <name>Wechat mp demo with Spring Boot and WxJava</name>--> |
|||
<!-- <description>基于 WxJava 和 Spring Boot 实现的微信公众号后端开发演示项目</description>--> |
|||
|
|||
<properties> |
|||
<weixin-java-mp.version>3.6.0</weixin-java-mp.version> |
|||
<maven.compiler.source>1.8</maven.compiler.source> |
|||
<maven.compiler.target>1.8</maven.compiler.target> |
|||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding> |
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
|||
<project.build.locales>zh_CN</project.build.locales> |
|||
<docker.image.prefix>wechat-mp-demo</docker.image.prefix> |
|||
</properties> |
|||
|
|||
<dependencies> |
|||
<dependency> |
|||
<groupId>com.github.binarywang</groupId> |
|||
<artifactId>weixin-java-mp</artifactId> |
|||
<version>${weixin-java-mp.version}</version> |
|||
</dependency> |
|||
|
|||
<dependency> |
|||
<groupId>org.projectlombok</groupId> |
|||
<artifactId>lombok</artifactId> |
|||
<scope>provided</scope> |
|||
</dependency> |
|||
|
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-autoconfigure</artifactId> |
|||
<scope>compile</scope> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-configuration-processor</artifactId> |
|||
<scope>compile</scope> |
|||
<optional>true</optional> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-autoconfigure-processor</artifactId> |
|||
<scope>compile</scope> |
|||
<optional>true</optional> |
|||
</dependency> |
|||
</dependencies> |
|||
</project> |
@ -0,0 +1,17 @@ |
|||
package com.epmet.wx.mp.builder; |
|||
|
|||
import me.chanjar.weixin.mp.api.WxMpService; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
|
|||
/** |
|||
* @author Binary Wang(https://github.com/binarywang)
|
|||
*/ |
|||
public abstract class AbstractBuilder { |
|||
protected final Logger logger = LoggerFactory.getLogger(getClass()); |
|||
|
|||
public abstract WxMpXmlOutMessage build(String content, |
|||
WxMpXmlMessage wxMessage, WxMpService service); |
|||
} |
@ -0,0 +1,24 @@ |
|||
package com.epmet.wx.mp.builder; |
|||
|
|||
import me.chanjar.weixin.mp.api.WxMpService; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutImageMessage; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; |
|||
|
|||
/** |
|||
* @author Binary Wang(https://github.com/binarywang)
|
|||
*/ |
|||
public class ImageBuilder extends AbstractBuilder { |
|||
|
|||
@Override |
|||
public WxMpXmlOutMessage build(String content, WxMpXmlMessage wxMessage, |
|||
WxMpService service) { |
|||
|
|||
WxMpXmlOutImageMessage m = WxMpXmlOutMessage.IMAGE().mediaId(content) |
|||
.fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()) |
|||
.build(); |
|||
|
|||
return m; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,22 @@ |
|||
package com.epmet.wx.mp.builder; |
|||
|
|||
import me.chanjar.weixin.mp.api.WxMpService; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutTextMessage; |
|||
|
|||
/** |
|||
* @author Binary Wang(https://github.com/binarywang)
|
|||
*/ |
|||
public class TextBuilder extends AbstractBuilder { |
|||
|
|||
@Override |
|||
public WxMpXmlOutMessage build(String content, WxMpXmlMessage wxMessage, |
|||
WxMpService service) { |
|||
WxMpXmlOutTextMessage m = WxMpXmlOutMessage.TEXT().content(content) |
|||
.fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()) |
|||
.build(); |
|||
return m; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,113 @@ |
|||
package com.epmet.wx.mp.config; |
|||
|
|||
import com.epmet.wx.mp.handler.*; |
|||
import lombok.AllArgsConstructor; |
|||
import me.chanjar.weixin.mp.api.WxMpMessageRouter; |
|||
import me.chanjar.weixin.mp.api.WxMpService; |
|||
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; |
|||
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; |
|||
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
|||
import org.springframework.context.annotation.Bean; |
|||
import org.springframework.context.annotation.Configuration; |
|||
|
|||
import java.util.List; |
|||
import java.util.stream.Collectors; |
|||
|
|||
import static me.chanjar.weixin.common.api.WxConsts.EventType; |
|||
import static me.chanjar.weixin.common.api.WxConsts.EventType.SUBSCRIBE; |
|||
import static me.chanjar.weixin.common.api.WxConsts.EventType.UNSUBSCRIBE; |
|||
import static me.chanjar.weixin.common.api.WxConsts.MenuButtonType.CLICK; |
|||
import static me.chanjar.weixin.common.api.WxConsts.MenuButtonType.VIEW; |
|||
import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType; |
|||
import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType.EVENT; |
|||
import static me.chanjar.weixin.mp.constant.WxMpEventConstants.CustomerService.*; |
|||
import static me.chanjar.weixin.mp.constant.WxMpEventConstants.POI_CHECK_NOTIFY; |
|||
|
|||
/** |
|||
* wechat mp configuration |
|||
* |
|||
* @author Binary Wang(https://github.com/binarywang)
|
|||
*/ |
|||
@AllArgsConstructor |
|||
@Configuration |
|||
@EnableConfigurationProperties(WxMpProperties.class) |
|||
public class WxMpConfiguration { |
|||
private final LogHandler logHandler; |
|||
private final NullHandler nullHandler; |
|||
private final KfSessionHandler kfSessionHandler; |
|||
private final StoreCheckNotifyHandler storeCheckNotifyHandler; |
|||
private final LocationHandler locationHandler; |
|||
private final MenuHandler menuHandler; |
|||
private final MsgHandler msgHandler; |
|||
private final UnsubscribeHandler unsubscribeHandler; |
|||
private final SubscribeHandler subscribeHandler; |
|||
private final ScanHandler scanHandler; |
|||
private final WxMpProperties properties; |
|||
|
|||
@Bean |
|||
public WxMpService wxMpService() { |
|||
// 代码里 getConfigs()处报错的同学,请注意仔细阅读项目说明,你的IDE需要引入lombok插件!!!!
|
|||
final List<WxMpProperties.MpConfig> configs = this.properties.getConfigs(); |
|||
if (configs == null) { |
|||
throw new RuntimeException("大哥,拜托先看下项目首页的说明(readme文件),添加下相关配置,注意别配错了!"); |
|||
} |
|||
|
|||
WxMpService service = new WxMpServiceImpl(); |
|||
service.setMultiConfigStorages(configs |
|||
.stream().map(a -> { |
|||
WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl(); |
|||
configStorage.setAppId(a.getAppId()); |
|||
configStorage.setSecret(a.getSecret()); |
|||
configStorage.setToken(a.getToken()); |
|||
configStorage.setAesKey(a.getAesKey()); |
|||
return configStorage; |
|||
}).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a -> a, (o, n) -> o))); |
|||
return service; |
|||
} |
|||
|
|||
@Bean |
|||
public WxMpMessageRouter messageRouter(WxMpService wxMpService) { |
|||
final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService); |
|||
|
|||
// 记录所有事件的日志 (异步执行)
|
|||
newRouter.rule().handler(this.logHandler).next(); |
|||
|
|||
// 接收客服会话管理事件
|
|||
newRouter.rule().async(false).msgType(EVENT).event(KF_CREATE_SESSION) |
|||
.handler(this.kfSessionHandler).end(); |
|||
newRouter.rule().async(false).msgType(EVENT).event(KF_CLOSE_SESSION) |
|||
.handler(this.kfSessionHandler).end(); |
|||
newRouter.rule().async(false).msgType(EVENT).event(KF_SWITCH_SESSION) |
|||
.handler(this.kfSessionHandler).end(); |
|||
|
|||
// 门店审核事件
|
|||
newRouter.rule().async(false).msgType(EVENT).event(POI_CHECK_NOTIFY).handler(this.storeCheckNotifyHandler).end(); |
|||
|
|||
// 自定义菜单事件
|
|||
newRouter.rule().async(false).msgType(EVENT).event(CLICK).handler(this.menuHandler).end(); |
|||
|
|||
// 点击菜单连接事件
|
|||
newRouter.rule().async(false).msgType(EVENT).event(VIEW).handler(this.nullHandler).end(); |
|||
|
|||
// 关注事件
|
|||
newRouter.rule().async(false).msgType(EVENT).event(SUBSCRIBE).handler(this.subscribeHandler).end(); |
|||
|
|||
// 取消关注事件
|
|||
newRouter.rule().async(false).msgType(EVENT).event(UNSUBSCRIBE).handler(this.unsubscribeHandler).end(); |
|||
|
|||
// 上报地理位置事件
|
|||
newRouter.rule().async(false).msgType(EVENT).event(EventType.LOCATION).handler(this.locationHandler).end(); |
|||
|
|||
// 接收地理位置消息
|
|||
newRouter.rule().async(false).msgType(XmlMsgType.LOCATION).handler(this.locationHandler).end(); |
|||
|
|||
// 扫码事件
|
|||
newRouter.rule().async(false).msgType(EVENT).event(EventType.SCAN).handler(this.scanHandler).end(); |
|||
|
|||
// 默认
|
|||
newRouter.rule().async(false).handler(this.msgHandler).end(); |
|||
|
|||
return newRouter; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,46 @@ |
|||
package com.epmet.wx.mp.config; |
|||
|
|||
import com.epmet.wx.mp.utils.JsonUtils; |
|||
import lombok.Data; |
|||
import org.springframework.boot.context.properties.ConfigurationProperties; |
|||
|
|||
import java.util.List; |
|||
|
|||
/** |
|||
* wechat mp properties |
|||
* |
|||
* @author Binary Wang(https://github.com/binarywang)
|
|||
*/ |
|||
@Data |
|||
@ConfigurationProperties(prefix = "wx.mp") |
|||
public class WxMpProperties { |
|||
private List<MpConfig> configs; |
|||
|
|||
@Data |
|||
public static class MpConfig { |
|||
/** |
|||
* 设置微信公众号的appid |
|||
*/ |
|||
private String appId; |
|||
|
|||
/** |
|||
* 设置微信公众号的app secret |
|||
*/ |
|||
private String secret; |
|||
|
|||
/** |
|||
* 设置微信公众号的token |
|||
*/ |
|||
private String token; |
|||
|
|||
/** |
|||
* 设置微信公众号的EncodingAESKey |
|||
*/ |
|||
private String aesKey; |
|||
} |
|||
|
|||
@Override |
|||
public String toString() { |
|||
return JsonUtils.toJson(this); |
|||
} |
|||
} |
@ -0,0 +1,12 @@ |
|||
package com.epmet.wx.mp.handler; |
|||
|
|||
import me.chanjar.weixin.mp.api.WxMpMessageHandler; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
|
|||
/** |
|||
* @author Binary Wang(https://github.com/binarywang)
|
|||
*/ |
|||
public abstract class AbstractHandler implements WxMpMessageHandler { |
|||
protected Logger logger = LoggerFactory.getLogger(getClass()); |
|||
} |
@ -0,0 +1,25 @@ |
|||
package com.epmet.wx.mp.handler; |
|||
|
|||
import me.chanjar.weixin.common.session.WxSessionManager; |
|||
import me.chanjar.weixin.mp.api.WxMpService; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* @author Binary Wang(https://github.com/binarywang)
|
|||
*/ |
|||
@Component |
|||
public class KfSessionHandler extends AbstractHandler { |
|||
|
|||
@Override |
|||
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, |
|||
Map<String, Object> context, WxMpService wxMpService, |
|||
WxSessionManager sessionManager) { |
|||
//TODO 对会话做处理
|
|||
return null; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,44 @@ |
|||
package com.epmet.wx.mp.handler; |
|||
|
|||
import com.epmet.wx.mp.builder.TextBuilder; |
|||
import me.chanjar.weixin.common.session.WxSessionManager; |
|||
import me.chanjar.weixin.mp.api.WxMpService; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.Map; |
|||
|
|||
import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType; |
|||
|
|||
/** |
|||
* @author Binary Wang(https://github.com/binarywang)
|
|||
*/ |
|||
@Component |
|||
public class LocationHandler extends AbstractHandler { |
|||
|
|||
@Override |
|||
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, |
|||
Map<String, Object> context, WxMpService wxMpService, |
|||
WxSessionManager sessionManager) { |
|||
if (wxMessage.getMsgType().equals(XmlMsgType.LOCATION)) { |
|||
//TODO 接收处理用户发送的地理位置消息
|
|||
try { |
|||
String content = "感谢反馈,您的的地理位置已收到!"; |
|||
return new TextBuilder().build(content, wxMessage, null); |
|||
} catch (Exception e) { |
|||
this.logger.error("位置消息接收处理失败", e); |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
//上报地理位置事件
|
|||
this.logger.info("上报地理位置,纬度 : {},经度 : {},精度 : {}", |
|||
wxMessage.getLatitude(), wxMessage.getLongitude(), String.valueOf(wxMessage.getPrecision())); |
|||
|
|||
//TODO 可以将用户地理位置信息保存到本地数据库,以便以后使用
|
|||
|
|||
return null; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,25 @@ |
|||
package com.epmet.wx.mp.handler; |
|||
|
|||
import com.epmet.wx.mp.utils.JsonUtils; |
|||
import me.chanjar.weixin.common.session.WxSessionManager; |
|||
import me.chanjar.weixin.mp.api.WxMpService; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* @author Binary Wang(https://github.com/binarywang)
|
|||
*/ |
|||
@Component |
|||
public class LogHandler extends AbstractHandler { |
|||
@Override |
|||
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, |
|||
Map<String, Object> context, WxMpService wxMpService, |
|||
WxSessionManager sessionManager) { |
|||
this.logger.info("\n接收到请求消息,内容:{}", JsonUtils.toJson(wxMessage)); |
|||
return null; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,36 @@ |
|||
package com.epmet.wx.mp.handler; |
|||
|
|||
import me.chanjar.weixin.common.session.WxSessionManager; |
|||
import me.chanjar.weixin.mp.api.WxMpService; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.Map; |
|||
|
|||
import static me.chanjar.weixin.common.api.WxConsts.MenuButtonType; |
|||
|
|||
/** |
|||
* @author Binary Wang(https://github.com/binarywang)
|
|||
*/ |
|||
@Component |
|||
public class MenuHandler extends AbstractHandler { |
|||
|
|||
@Override |
|||
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, |
|||
Map<String, Object> context, WxMpService weixinService, |
|||
WxSessionManager sessionManager) { |
|||
|
|||
String msg = String.format("type:%s, event:%s, key:%s", |
|||
wxMessage.getMsgType(), wxMessage.getEvent(), |
|||
wxMessage.getEventKey()); |
|||
if (MenuButtonType.VIEW.equals(wxMessage.getEvent())) { |
|||
return null; |
|||
} |
|||
|
|||
return WxMpXmlOutMessage.TEXT().content(msg) |
|||
.fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()) |
|||
.build(); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,52 @@ |
|||
package com.epmet.wx.mp.handler; |
|||
|
|||
import com.epmet.wx.mp.builder.TextBuilder; |
|||
import com.epmet.wx.mp.utils.JsonUtils; |
|||
import me.chanjar.weixin.common.error.WxErrorException; |
|||
import me.chanjar.weixin.common.session.WxSessionManager; |
|||
import me.chanjar.weixin.mp.api.WxMpService; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.Map; |
|||
|
|||
import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType; |
|||
|
|||
/** |
|||
* @author Binary Wang(https://github.com/binarywang)
|
|||
*/ |
|||
@Component |
|||
public class MsgHandler extends AbstractHandler { |
|||
|
|||
@Override |
|||
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, |
|||
Map<String, Object> context, WxMpService weixinService, |
|||
WxSessionManager sessionManager) { |
|||
|
|||
if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) { |
|||
//TODO 可以选择将消息保存到本地
|
|||
} |
|||
|
|||
//当用户输入关键词如“你好”,“客服”等,并且有客服在线时,把消息转发给在线客服
|
|||
try { |
|||
if (StringUtils.startsWithAny(wxMessage.getContent(), "你好", "客服") |
|||
&& weixinService.getKefuService().kfOnlineList() |
|||
.getKfOnlineList().size() > 0) { |
|||
return WxMpXmlOutMessage.TRANSFER_CUSTOMER_SERVICE() |
|||
.fromUser(wxMessage.getToUser()) |
|||
.toUser(wxMessage.getFromUser()).build(); |
|||
} |
|||
} catch (WxErrorException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
|
|||
//TODO 组装回复消息
|
|||
String content = "收到信息内容:" + JsonUtils.toJson(wxMessage); |
|||
|
|||
return new TextBuilder().build(content, wxMessage, weixinService); |
|||
|
|||
} |
|||
|
|||
} |
@ -0,0 +1,24 @@ |
|||
package com.epmet.wx.mp.handler; |
|||
|
|||
import me.chanjar.weixin.common.session.WxSessionManager; |
|||
import me.chanjar.weixin.mp.api.WxMpService; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* @author Binary Wang(https://github.com/binarywang)
|
|||
*/ |
|||
@Component |
|||
public class NullHandler extends AbstractHandler { |
|||
|
|||
@Override |
|||
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, |
|||
Map<String, Object> context, WxMpService wxMpService, |
|||
WxSessionManager sessionManager) { |
|||
return null; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,25 @@ |
|||
package com.epmet.wx.mp.handler; |
|||
|
|||
import java.util.Map; |
|||
|
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import me.chanjar.weixin.common.error.WxErrorException; |
|||
import me.chanjar.weixin.common.session.WxSessionManager; |
|||
import me.chanjar.weixin.mp.api.WxMpService; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; |
|||
|
|||
/** |
|||
* @author Binary Wang(https://github.com/binarywang)
|
|||
*/ |
|||
@Component |
|||
public class ScanHandler extends AbstractHandler { |
|||
|
|||
@Override |
|||
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage, Map<String, Object> map, |
|||
WxMpService wxMpService, WxSessionManager wxSessionManager) throws WxErrorException { |
|||
// 扫码事件处理
|
|||
return null; |
|||
} |
|||
} |
@ -0,0 +1,27 @@ |
|||
package com.epmet.wx.mp.handler; |
|||
|
|||
import me.chanjar.weixin.common.session.WxSessionManager; |
|||
import me.chanjar.weixin.mp.api.WxMpService; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* 门店审核事件处理 |
|||
* |
|||
* @author Binary Wang(https://github.com/binarywang)
|
|||
*/ |
|||
@Component |
|||
public class StoreCheckNotifyHandler extends AbstractHandler { |
|||
|
|||
@Override |
|||
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, |
|||
Map<String, Object> context, WxMpService wxMpService, |
|||
WxSessionManager sessionManager) { |
|||
// TODO 处理门店审核事件
|
|||
return null; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,71 @@ |
|||
package com.epmet.wx.mp.handler; |
|||
|
|||
import java.util.Map; |
|||
|
|||
import com.epmet.wx.mp.builder.TextBuilder; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import me.chanjar.weixin.common.error.WxErrorException; |
|||
import me.chanjar.weixin.common.session.WxSessionManager; |
|||
import me.chanjar.weixin.mp.api.WxMpService; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; |
|||
import me.chanjar.weixin.mp.bean.result.WxMpUser; |
|||
|
|||
/** |
|||
* @author Binary Wang(https://github.com/binarywang)
|
|||
*/ |
|||
@Component |
|||
public class SubscribeHandler extends AbstractHandler { |
|||
|
|||
@Override |
|||
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, |
|||
Map<String, Object> context, WxMpService weixinService, |
|||
WxSessionManager sessionManager) throws WxErrorException { |
|||
|
|||
this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser()); |
|||
|
|||
// 获取微信用户基本信息
|
|||
try { |
|||
WxMpUser userWxInfo = weixinService.getUserService() |
|||
.userInfo(wxMessage.getFromUser(), null); |
|||
if (userWxInfo != null) { |
|||
// TODO 可以添加关注用户到本地数据库
|
|||
} |
|||
} catch (WxErrorException e) { |
|||
if (e.getError().getErrorCode() == 48001) { |
|||
this.logger.info("该公众号没有获取用户信息权限!"); |
|||
} |
|||
} |
|||
|
|||
|
|||
WxMpXmlOutMessage responseResult = null; |
|||
try { |
|||
responseResult = this.handleSpecial(wxMessage); |
|||
} catch (Exception e) { |
|||
this.logger.error(e.getMessage(), e); |
|||
} |
|||
|
|||
if (responseResult != null) { |
|||
return responseResult; |
|||
} |
|||
|
|||
try { |
|||
return new TextBuilder().build("感谢关注", wxMessage, weixinService); |
|||
} catch (Exception e) { |
|||
this.logger.error(e.getMessage(), e); |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
/** |
|||
* 处理特殊请求,比如如果是扫码进来的,可以做相应处理 |
|||
*/ |
|||
private WxMpXmlOutMessage handleSpecial(WxMpXmlMessage wxMessage) |
|||
throws Exception { |
|||
//TODO
|
|||
return null; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,27 @@ |
|||
package com.epmet.wx.mp.handler; |
|||
|
|||
import me.chanjar.weixin.common.session.WxSessionManager; |
|||
import me.chanjar.weixin.mp.api.WxMpService; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; |
|||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* @author Binary Wang(https://github.com/binarywang)
|
|||
*/ |
|||
@Component |
|||
public class UnsubscribeHandler extends AbstractHandler { |
|||
|
|||
@Override |
|||
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, |
|||
Map<String, Object> context, WxMpService wxMpService, |
|||
WxSessionManager sessionManager) { |
|||
String openId = wxMessage.getFromUser(); |
|||
this.logger.info("取消关注用户 OPENID: " + openId); |
|||
// TODO 可以更新本地数据库为取消关注状态
|
|||
return null; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,16 @@ |
|||
package com.epmet.wx.mp.utils; |
|||
|
|||
import com.google.gson.Gson; |
|||
import com.google.gson.GsonBuilder; |
|||
|
|||
/** |
|||
* @author Binary Wang(https://github.com/binarywang)
|
|||
*/ |
|||
public class JsonUtils { |
|||
public static String toJson(Object obj) { |
|||
Gson gson = new GsonBuilder() |
|||
.setPrettyPrinting() |
|||
.create(); |
|||
return gson.toJson(obj); |
|||
} |
|||
} |
@ -0,0 +1,66 @@ |
|||
package com.epmet.commons.tools.utils; |
|||
|
|||
import org.springframework.web.context.request.RequestAttributes; |
|||
import org.springframework.web.context.request.RequestContextHolder; |
|||
import org.springframework.web.context.request.ServletRequestAttributes; |
|||
|
|||
import javax.servlet.http.HttpServletRequest; |
|||
|
|||
/** |
|||
* Web工具类 |
|||
* |
|||
* @author rongchao |
|||
* @Date 18-11-20 |
|||
*/ |
|||
public class WebUtil { |
|||
|
|||
public static HttpServletRequest getHttpServletRequest() { |
|||
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
|||
HttpServletRequest request = requestAttributes.getRequest(); |
|||
return request; |
|||
} |
|||
|
|||
public static Object getAttributesFromRequest(String paramName) { |
|||
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
|||
return requestAttributes.getAttribute(paramName, RequestAttributes.SCOPE_REQUEST); |
|||
} |
|||
|
|||
public static void setAttributesFromRequest(String paramName, Object obj) { |
|||
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
|||
requestAttributes.setAttribute(paramName, obj, RequestAttributes.SCOPE_REQUEST); |
|||
} |
|||
|
|||
/** |
|||
* 获取用户真实IP地址,不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址, |
|||
* <p> |
|||
* 可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值,究竟哪个才是真正的用户端的真实IP呢? |
|||
* 答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。 |
|||
* <p> |
|||
* 如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130, |
|||
* 192.168.1.100 |
|||
* <p> |
|||
* 用户真实IP为: 192.168.1.110 |
|||
* |
|||
* @return |
|||
*/ |
|||
public static String getIpAddress() { |
|||
HttpServletRequest request = getHttpServletRequest(); |
|||
String ip = request.getHeader("x-forwarded-for"); |
|||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
|||
ip = request.getHeader("Proxy-Client-IP"); |
|||
} |
|||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
|||
ip = request.getHeader("WL-Proxy-Client-IP"); |
|||
} |
|||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
|||
ip = request.getHeader("HTTP_CLIENT_IP"); |
|||
} |
|||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
|||
ip = request.getHeader("HTTP_X_FORWARDED_FOR"); |
|||
} |
|||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
|||
ip = request.getRemoteAddr(); |
|||
} |
|||
return ip; |
|||
} |
|||
} |
Loading…
Reference in new issue