feat: 微信公众号扫码登录
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package com.schisandra.auth.api;
|
||||
|
||||
import com.schisandra.auth.entity.Result;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@FeignClient("schisandra-cloud-storage-auth")
|
||||
public interface SchisandraAuthFeignService {
|
||||
@PostMapping("/auth/user/wechatLogin")
|
||||
Result wechatLogin(@RequestParam(value = "openId") String openId);
|
||||
}
|
@@ -1 +0,0 @@
|
||||
api 对外接口
|
@@ -0,0 +1,50 @@
|
||||
package com.schisandra.auth.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Result<T> {
|
||||
|
||||
private Boolean success;
|
||||
|
||||
private Integer code;
|
||||
|
||||
private String message;
|
||||
|
||||
private T data;
|
||||
|
||||
public static Result ok(){
|
||||
Result result = new Result();
|
||||
result.setSuccess(true);
|
||||
result.setCode(ResultCodeEnum.SUCCESS.getCode());
|
||||
result.setMessage(ResultCodeEnum.SUCCESS.getDesc());
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> Result ok(T data){
|
||||
Result result = new Result();
|
||||
result.setSuccess(true);
|
||||
result.setCode(ResultCodeEnum.SUCCESS.getCode());
|
||||
result.setMessage(ResultCodeEnum.SUCCESS.getDesc());
|
||||
result.setData(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Result fail(){
|
||||
Result result = new Result();
|
||||
result.setSuccess(false);
|
||||
result.setCode(ResultCodeEnum.FAIL.getCode());
|
||||
result.setMessage(ResultCodeEnum.FAIL.getDesc());
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> Result fail(T data){
|
||||
Result result = new Result();
|
||||
result.setSuccess(false);
|
||||
result.setCode(ResultCodeEnum.FAIL.getCode());
|
||||
result.setMessage(ResultCodeEnum.FAIL.getDesc());
|
||||
result.setData(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
package com.schisandra.auth.entity;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum ResultCodeEnum {
|
||||
|
||||
SUCCESS(200,"成功"),
|
||||
FAIL(500,"失败");
|
||||
|
||||
public int code;
|
||||
|
||||
public String desc;
|
||||
|
||||
ResultCodeEnum(int code,String desc){
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public static ResultCodeEnum getByCode(int codeVal){
|
||||
for(ResultCodeEnum resultCodeEnum : ResultCodeEnum.values()){
|
||||
if(resultCodeEnum.code == codeVal){
|
||||
return resultCodeEnum;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@@ -1 +0,0 @@
|
||||
api 实体
|
@@ -298,5 +298,25 @@ public class SchisandraAuthUserController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 微信登录
|
||||
* @param: [authUser]
|
||||
* @return: com.schisandra.auth.common.entity.Result
|
||||
* @author: landaiqing
|
||||
* @date: 2024/6/20 下午9:27
|
||||
*/
|
||||
@PostMapping("wechatLogin")
|
||||
public Result wechatLogin(@RequestParam(value = "openId") String openId) {
|
||||
log.info("UserController.wechatLogin.openId:{}", openId);
|
||||
Preconditions.checkNotNull(openId, "openId 不能为空");
|
||||
Boolean result = schisandraAuthUserDomainService.wechatLogin(openId);
|
||||
if (result) {
|
||||
return Result.ok("登录成功");
|
||||
} else {
|
||||
return Result.fail("登录失败");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -93,5 +93,7 @@ public interface SchisandraAuthUserDomainService {
|
||||
Object deleteById(Long id);
|
||||
|
||||
SchisandraAuthUser queryByPhone(String phone);
|
||||
|
||||
Boolean wechatLogin(String openId);
|
||||
}
|
||||
|
||||
|
@@ -210,6 +210,57 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
|
||||
return schisandraAuthUserService.queryByPhone(phone);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 微信登陆
|
||||
* @param: [schisandraAuthUserBO]
|
||||
* @return: java.lang.Boolean
|
||||
* @author: landaiqing
|
||||
* @date: 2024/6/20 下午9:32
|
||||
*/
|
||||
@Override
|
||||
public Boolean wechatLogin(String openId) {
|
||||
SchisandraAuthUser schisandraAuthUser = schisandraAuthUserService.selectByUuidAndType(openId, OauthType.WECHAT.getType());
|
||||
if (ObjectUtils.isNotEmpty(schisandraAuthUser)) {
|
||||
Long userId = schisandraAuthUser.getId();
|
||||
// redis存储用户角色与权限信息
|
||||
userInfoPersistence(userId);
|
||||
StpUtil.login(userId);
|
||||
return true;
|
||||
} else {
|
||||
// 插入用户信息表
|
||||
SchisandraAuthUserBO schisandraAuthUserBO = new SchisandraAuthUserBO();
|
||||
schisandraAuthUserBO.setUuid(openId);
|
||||
schisandraAuthUserBO.setSource(OauthType.WECHAT.getType());
|
||||
schisandraAuthUserBO.setStatus(UserStatusEnum.NORMAL.getCode());
|
||||
SchisandraAuthUser AuthUser = SchisandraAuthUserBOConverter.INSTANCE.convertBOToEntity(schisandraAuthUserBO);
|
||||
int result = schisandraAuthUserService.insertAuthUserByOauth(AuthUser);
|
||||
if (result <= 0) {
|
||||
if (log.isInfoEnabled()) {
|
||||
log.error("insertAuthUserByOauth fail, param:{}", JSONObject.toJSONString(AuthUser));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Long authUserId = AuthUser.getId();
|
||||
// 建立用户与角色映射关系
|
||||
SchisandraAuthUserRoleBO schisandraAuthUserRoleBO = new SchisandraAuthUserRoleBO();
|
||||
schisandraAuthUserRoleBO.setUserId(authUserId);
|
||||
schisandraAuthUserRoleBO.setRoleId(UserRoleEnum.NORMAL_USER.getCode());
|
||||
schisandraAuthUserRoleBO.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
|
||||
SchisandraAuthUserRole schisandraAuthUserRole = SchisandraAuthUserRoleBOConverter.INSTANCE.convertBOToEntity(schisandraAuthUserRoleBO);
|
||||
int insert = schisandraAuthUserRoleService.insert(schisandraAuthUserRole);
|
||||
if (insert <= 0) {
|
||||
if (log.isInfoEnabled()) {
|
||||
log.error("insertUserRole fail, param:{}", JSONObject.toJSONString(schisandraAuthUserRole));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// redis存储用户角色与权限信息
|
||||
userInfoPersistence(authUserId);
|
||||
StpUtil.login(authUserId);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 根据第三方用户信息添加用户信息
|
||||
* @param: [data, type]
|
||||
@@ -228,6 +279,7 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
|
||||
// redis存储用户角色与权限信息
|
||||
userInfoPersistence(userId);
|
||||
StpUtil.login(userId, SaLoginConfig.setToken(token.getAccessToken()));
|
||||
return Result.ok();
|
||||
} else {
|
||||
// 插入用户信息表
|
||||
SchisandraAuthUserBO schisandraAuthUserBO = new SchisandraAuthUserBO();
|
||||
@@ -270,8 +322,8 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
|
||||
// redis存储用户角色与权限信息
|
||||
userInfoPersistence(authUserId);
|
||||
StpUtil.login(authUserId, SaLoginConfig.setToken(token.getAccessToken()));
|
||||
return Result.ok();
|
||||
}
|
||||
return Result.ok();
|
||||
|
||||
}
|
||||
|
||||
|
@@ -45,14 +45,19 @@ public class SchisandraAuthPermission implements Serializable {
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@Column(value = "created_time",onInsertValue = "now()")
|
||||
private Date createdTime;
|
||||
|
||||
/**
|
||||
* 更新人
|
||||
*/
|
||||
@Column("update_by")
|
||||
private String updateBy;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@Column(value = "update_time",onUpdateValue = "now()")
|
||||
private Date updateTime;
|
||||
|
||||
@Column(isLogicDelete = true)
|
||||
|
@@ -44,16 +44,19 @@ public class SchisandraAuthRole implements Serializable {
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@Column(value = "created_time",onInsertValue = "now()")
|
||||
private Date createdTime;
|
||||
|
||||
/**
|
||||
* 更新人
|
||||
*/
|
||||
@Column("update_by")
|
||||
private String updateBy;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@Column(value = "update_time",onUpdateValue = "now()")
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
|
@@ -47,7 +47,7 @@ public class SchisandraAuthRolePermission implements Serializable {
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@Column("created_time")
|
||||
@Column(value = "created_time",onInsertValue = "now()")
|
||||
private Date createdTime;
|
||||
|
||||
/**
|
||||
@@ -59,7 +59,7 @@ public class SchisandraAuthRolePermission implements Serializable {
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@Column("update_time")
|
||||
@Column(value = "update_time",onUpdateValue = "now()")
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
|
@@ -106,7 +106,7 @@ public class SchisandraAuthUser implements Serializable {
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@Column("created_time")
|
||||
@Column(value = "created_time",onInsertValue = "now()")
|
||||
private Date createdTime;
|
||||
|
||||
/**
|
||||
@@ -118,7 +118,7 @@ public class SchisandraAuthUser implements Serializable {
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@Column("update_time")
|
||||
@Column(value = "update_time",onUpdateValue = "now()")
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
|
@@ -48,7 +48,7 @@ public class SchisandraAuthUserRole implements Serializable {
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@Column("created_time")
|
||||
@Column(value = "created_time",onInsertValue = "now()")
|
||||
private Date createdTime;
|
||||
|
||||
/**
|
||||
@@ -60,7 +60,7 @@ public class SchisandraAuthUserRole implements Serializable {
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@Column("update_time")
|
||||
@Column(value = "update_time",onUpdateValue = "now()")
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
|
@@ -2,15 +2,10 @@ package com.schisandra.auth.infra.basic.service.impl;
|
||||
|
||||
import com.mybatisflex.spring.service.impl.CacheableServiceImpl;
|
||||
import com.schisandra.auth.infra.basic.dao.SchisandraAuthPermissionDao;
|
||||
import com.schisandra.auth.infra.basic.dao.SchisandraAuthRolePermissionDao;
|
||||
import com.schisandra.auth.infra.basic.entity.SchisandraAuthPermission;
|
||||
import com.schisandra.auth.infra.basic.service.SchisandraAuthPermissionService;
|
||||
import com.schisandra.auth.infra.basic.service.SchisandraAuthRolePermissionService;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@@ -49,7 +44,7 @@ public class SchisandraAuthPermissionServiceImpl extends CacheableServiceImpl<Sc
|
||||
*/
|
||||
@Override
|
||||
public SchisandraAuthPermission insert(SchisandraAuthPermission schisandraAuthPermission) {
|
||||
this.schisandraAuthPermissionDao.insert(schisandraAuthPermission);
|
||||
this.schisandraAuthPermissionDao.insert(schisandraAuthPermission,true);
|
||||
return schisandraAuthPermission;
|
||||
}
|
||||
|
||||
@@ -61,7 +56,7 @@ public class SchisandraAuthPermissionServiceImpl extends CacheableServiceImpl<Sc
|
||||
*/
|
||||
@Override
|
||||
public int update(SchisandraAuthPermission schisandraAuthPermission) {
|
||||
return this.schisandraAuthPermissionDao.update(schisandraAuthPermission);
|
||||
return this.schisandraAuthPermissionDao.update(schisandraAuthPermission,true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -41,7 +41,7 @@ public class SchisandraAuthRoleServiceImpl implements SchisandraAuthRoleService
|
||||
*/
|
||||
@Override
|
||||
public int insert(SchisandraAuthRole schisandraAuthRole) {
|
||||
return this.schisandraAuthRoleDao.insert(schisandraAuthRole);
|
||||
return this.schisandraAuthRoleDao.insert(schisandraAuthRole,true);
|
||||
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ public class SchisandraAuthRoleServiceImpl implements SchisandraAuthRoleService
|
||||
*/
|
||||
@Override
|
||||
public SchisandraAuthRole update(SchisandraAuthRole schisandraAuthRole) {
|
||||
this.schisandraAuthRoleDao.update(schisandraAuthRole);
|
||||
this.schisandraAuthRoleDao.update(schisandraAuthRole,true);
|
||||
return this.queryById(schisandraAuthRole.getId());
|
||||
}
|
||||
|
||||
|
@@ -7,7 +7,6 @@ import com.schisandra.auth.infra.basic.service.SchisandraAuthUserRoleService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Objects;
|
||||
|
||||
|
||||
/**
|
||||
@@ -41,7 +40,7 @@ public class SchisandraAuthUserRoleServiceImpl implements SchisandraAuthUserRole
|
||||
*/
|
||||
@Override
|
||||
public int insert(SchisandraAuthUserRole schisandraAuthUserRole) {
|
||||
return this.schisandraAuthUserRoleDao.insert(schisandraAuthUserRole);
|
||||
return this.schisandraAuthUserRoleDao.insert(schisandraAuthUserRole,true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -54,7 +54,7 @@ public class SchisandraAuthUserServiceImpl implements SchisandraAuthUserService
|
||||
@Override
|
||||
public Boolean insert(SchisandraAuthUser schisandraAuthUser) {
|
||||
|
||||
return this.schisandraAuthUserDao.insert(schisandraAuthUser) > 0;
|
||||
return this.schisandraAuthUserDao.insert(schisandraAuthUser,true) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -38,7 +38,7 @@ public class ${modelName}ServiceImpl implements ${modelName}Service {
|
||||
*/
|
||||
@Override
|
||||
public int insert(${modelName} ${_modelName}) {
|
||||
return this.${_modelName}Dao.insert(${_modelName},true);
|
||||
return this.${_modelName}Dao.insertSelective(${_modelName});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -245,13 +245,13 @@ public class SchisandraSmsConfig implements Serializable {
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@Column("created_time")
|
||||
@Column(value = "created_time",onInsertValue = "now()")
|
||||
private Date createdTime;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@Column("update_time")
|
||||
@Column(value = "update_time",onUpdateValue = "now()")
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
|
@@ -59,7 +59,7 @@ public class SchisandraSysConfig implements Serializable {
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@Column("create_date")
|
||||
@Column(value = "create_date",onInsertValue = "now()")
|
||||
private Date createDate;
|
||||
|
||||
/**
|
||||
@@ -71,7 +71,7 @@ public class SchisandraSysConfig implements Serializable {
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@Column("update_date")
|
||||
@Column(value = "update_date",onUpdateValue = "now()")
|
||||
private Date updateDate;
|
||||
|
||||
/**
|
||||
|
@@ -55,7 +55,7 @@ public class SchisandraSysLog implements Serializable {
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@Column("create_date")
|
||||
@Column(value = "create_date",onInsertValue = "now()")
|
||||
private Date createDate;
|
||||
|
||||
/**
|
||||
|
@@ -89,13 +89,13 @@ public class SchisandraSysOauth implements Serializable {
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@Column("created_time")
|
||||
@Column(value = "created_time",onInsertValue = "now()")
|
||||
private Date createdTime;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@Column("update_time")
|
||||
@Column(value = "update_time",onUpdateValue = "now()")
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
|
@@ -40,7 +40,7 @@ public class SchisandraSysConfigServiceImpl implements SchisandraSysConfigServic
|
||||
*/
|
||||
@Override
|
||||
public int insert(SchisandraSysConfig schisandraSysConfig) {
|
||||
return this.schisandraSysConfigDao.insert(schisandraSysConfig);
|
||||
return this.schisandraSysConfigDao.insert(schisandraSysConfig,true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -7,7 +7,6 @@ import com.schisandra.system.infra.basic.service.SchisandraSysLogService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 表服务实现类
|
||||
@@ -40,7 +39,7 @@ public class SchisandraSysLogServiceImpl implements SchisandraSysLogService {
|
||||
*/
|
||||
@Override
|
||||
public int insert(SchisandraSysLog schisandraSysLog) {
|
||||
return this.schisandraSysLogDao.insert(schisandraSysLog);
|
||||
return this.schisandraSysLogDao.insert(schisandraSysLog,true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -7,7 +7,6 @@ import com.schisandra.system.infra.basic.service.SchisandraSysOauthService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 表服务实现类
|
||||
@@ -40,7 +39,7 @@ public class SchisandraSysOauthServiceImpl implements SchisandraSysOauthService
|
||||
*/
|
||||
@Override
|
||||
public int insert(SchisandraSysOauth schisandraSysOauth) {
|
||||
return this.schisandraSysOauthDao.insert(schisandraSysOauth);
|
||||
return this.schisandraSysOauthDao.insert(schisandraSysOauth,true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -15,12 +15,15 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<spring-boot.version>2.4.2</spring-boot.version>
|
||||
<spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
|
||||
<spring-cloud.version>2020.0.6</spring-cloud.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>2.4.2</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>spring-boot-starter-logging</artifactId>
|
||||
@@ -28,11 +31,38 @@
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-bootstrap</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
<version>3.0.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-loadbalancer</artifactId>
|
||||
<version>3.0.6</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.24</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-log4j2</artifactId>
|
||||
@@ -57,11 +87,6 @@
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<version>2.12.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.12.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
@@ -74,12 +99,24 @@
|
||||
<dependency>
|
||||
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||
<artifactId>caffeine</artifactId>
|
||||
<version>3.1.8</version>
|
||||
<version>2.9.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.schisandra</groupId>
|
||||
<artifactId>schisandra-cloud-storage-auth-api</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
@@ -87,6 +124,13 @@
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring-cloud-alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
@@ -2,6 +2,7 @@ package com.schisandra.wechat;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
/**
|
||||
@@ -12,6 +13,7 @@ import org.springframework.context.annotation.ComponentScan;
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@ComponentScan("com.schisandra")
|
||||
@EnableFeignClients(basePackages = "com.schisandra")
|
||||
public class WeChatApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(WeChatApplication.class);
|
||||
|
@@ -31,11 +31,6 @@ public class CallBackController {
|
||||
@Value("${wechat.token}")
|
||||
private String token;
|
||||
|
||||
@RequestMapping("/test")
|
||||
public String test() {
|
||||
return "HELLO WORLD";
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 回调消息校验
|
||||
* @param: [signature, timestamp, nonce, echostr]
|
||||
@@ -65,11 +60,11 @@ public class CallBackController {
|
||||
@RequestParam("timestamp") String timestamp,
|
||||
@RequestParam("nonce") String nonce,
|
||||
@RequestParam(value = "msg_signature", required = false) String msgSignature) {
|
||||
log.info("接收到微信消息:requestBody:{}", requestBody);
|
||||
// log.info("接收到微信消息:requestBody:{}", requestBody);
|
||||
Map<String, String> messageMap = MessageUtil.parseXml(requestBody);
|
||||
String msgType = messageMap.get("MsgType");
|
||||
String event = messageMap.get("Event") == null ? "" : messageMap.get("Event");
|
||||
log.info("msgType:{},event:{}", msgType, event);
|
||||
// log.info("msgType:{},event:{}", msgType, event);
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(msgType);
|
||||
@@ -83,7 +78,7 @@ public class CallBackController {
|
||||
return "unknown";
|
||||
}
|
||||
String replyContent = weChatMsgHandler.dealMsg(messageMap);
|
||||
log.info("replyContent:{}", replyContent);
|
||||
// log.info("replyContent:{}", replyContent);
|
||||
return replyContent;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,50 @@
|
||||
package com.schisandra.wechat.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Result<T> {
|
||||
|
||||
private Boolean success;
|
||||
|
||||
private Integer code;
|
||||
|
||||
private String message;
|
||||
|
||||
private T data;
|
||||
|
||||
public static Result ok(){
|
||||
Result result = new Result();
|
||||
result.setSuccess(true);
|
||||
result.setCode(ResultCodeEnum.SUCCESS.getCode());
|
||||
result.setMessage(ResultCodeEnum.SUCCESS.getDesc());
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> Result ok(T data){
|
||||
Result result = new Result();
|
||||
result.setSuccess(true);
|
||||
result.setCode(ResultCodeEnum.SUCCESS.getCode());
|
||||
result.setMessage(ResultCodeEnum.SUCCESS.getDesc());
|
||||
result.setData(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Result fail(){
|
||||
Result result = new Result();
|
||||
result.setSuccess(false);
|
||||
result.setCode(ResultCodeEnum.FAIL.getCode());
|
||||
result.setMessage(ResultCodeEnum.FAIL.getDesc());
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> Result fail(T data){
|
||||
Result result = new Result();
|
||||
result.setSuccess(false);
|
||||
result.setCode(ResultCodeEnum.FAIL.getCode());
|
||||
result.setMessage(ResultCodeEnum.FAIL.getDesc());
|
||||
result.setData(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
package com.schisandra.wechat.entity;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum ResultCodeEnum {
|
||||
|
||||
SUCCESS(200,"成功"),
|
||||
FAIL(500,"失败");
|
||||
|
||||
public int code;
|
||||
|
||||
public String desc;
|
||||
|
||||
ResultCodeEnum(int code,String desc){
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public static ResultCodeEnum getByCode(int codeVal){
|
||||
for(ResultCodeEnum resultCodeEnum : ResultCodeEnum.values()){
|
||||
if(resultCodeEnum.code == codeVal){
|
||||
return resultCodeEnum;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@@ -1,6 +1,7 @@
|
||||
package com.schisandra.wechat.handler;
|
||||
|
||||
import com.schisandra.wechat.redis.RedisUtil;
|
||||
import com.schisandra.wechat.rpc.AuthRpc;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -14,12 +15,16 @@ import java.util.concurrent.TimeUnit;
|
||||
public class ReceiveTextMsgHandler implements WeChatMsgHandler {
|
||||
|
||||
private static final String KEY_WORD = "验证码";
|
||||
private static final String LOGIN_KEY_WORD = "登录";
|
||||
|
||||
private static final String LOGIN_PREFIX = "loginCode";
|
||||
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
@Resource
|
||||
private AuthRpc authRpc;
|
||||
|
||||
@Override
|
||||
public WeChatMsgTypeEnum getMsgType() {
|
||||
return WeChatMsgTypeEnum.TEXT_MSG;
|
||||
@@ -27,28 +32,53 @@ public class ReceiveTextMsgHandler implements WeChatMsgHandler {
|
||||
|
||||
@Override
|
||||
public String dealMsg(Map<String, String> messageMap) {
|
||||
log.info("接收到文本消息事件");
|
||||
String content = messageMap.get("Content");
|
||||
if (!KEY_WORD.equals(content)) {
|
||||
return "";
|
||||
}
|
||||
String fromUserName = messageMap.get("FromUserName");
|
||||
String toUserName = messageMap.get("ToUserName");
|
||||
|
||||
Random random = new Random();
|
||||
int num = random.nextInt(9000)+1000;
|
||||
String numKey = redisUtil.buildKey(LOGIN_PREFIX, String.valueOf(num));
|
||||
redisUtil.setNx(numKey, fromUserName, 1L, TimeUnit.MINUTES);
|
||||
String numContent = "您当前的验证码是:【" + num + "】 , 1分钟内有效";
|
||||
String replyContent = "<xml>\n" +
|
||||
" <ToUserName><![CDATA[" + fromUserName + "]]></ToUserName>\n" +
|
||||
" <FromUserName><![CDATA[" + toUserName + "]]></FromUserName>\n" +
|
||||
" <CreateTime>12345678</CreateTime>\n" +
|
||||
" <MsgType><![CDATA[text]]></MsgType>\n" +
|
||||
" <Content><![CDATA[" + numContent + "]]></Content>\n" +
|
||||
"</xml>";
|
||||
// 验证码
|
||||
if (KEY_WORD.equals(content)) {
|
||||
|
||||
Random random = new Random();
|
||||
int num = random.nextInt(9000) + 1000;
|
||||
String numKey = redisUtil.buildKey(LOGIN_PREFIX, String.valueOf(num));
|
||||
redisUtil.setNx(numKey, fromUserName, 1L, TimeUnit.MINUTES);
|
||||
String numContent = "您当前的验证码是:【" + num + "】 , 1分钟内有效";
|
||||
String replyContent = "<xml>\n" +
|
||||
" <ToUserName><![CDATA[" + fromUserName + "]]></ToUserName>\n" +
|
||||
" <FromUserName><![CDATA[" + toUserName + "]]></FromUserName>\n" +
|
||||
" <CreateTime>12345678</CreateTime>\n" +
|
||||
" <MsgType><![CDATA[text]]></MsgType>\n" +
|
||||
" <Content><![CDATA[" + numContent + "]]></Content>\n" +
|
||||
"</xml>";
|
||||
|
||||
return replyContent;
|
||||
}else if (LOGIN_KEY_WORD.equals(content)) {
|
||||
Boolean result = authRpc.wechatLogin(toUserName);
|
||||
if (result) {
|
||||
String replyContent = "<xml>\n" +
|
||||
" <ToUserName><![CDATA[" + fromUserName + "]]></ToUserName>\n" +
|
||||
" <FromUserName><![CDATA[" + toUserName + "]]></FromUserName>\n" +
|
||||
" <CreateTime>12345678</CreateTime>\n" +
|
||||
" <MsgType><![CDATA[text]]></MsgType>\n" +
|
||||
" <Content><![CDATA[" + "登录成功" + "]]></Content>\n" +
|
||||
"</xml>";
|
||||
return replyContent;
|
||||
} else {
|
||||
String replyContent = "<xml>\n" +
|
||||
" <ToUserName><![CDATA[" + fromUserName + "]]></ToUserName>\n" +
|
||||
" <FromUserName><![CDATA[" + toUserName + "]]></FromUserName>\n" +
|
||||
" <CreateTime>12345678</CreateTime>\n" +
|
||||
" <MsgType><![CDATA[text]]></MsgType>\n" +
|
||||
" <Content><![CDATA[" + "登录失败" + "]]></Content>\n" +
|
||||
"</xml>";
|
||||
return replyContent;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
return null;
|
||||
|
||||
return replyContent;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,31 @@
|
||||
package com.schisandra.wechat.rpc;
|
||||
|
||||
import com.schisandra.auth.api.SchisandraAuthFeignService;
|
||||
import com.schisandra.auth.entity.Result;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @Classname AuthRpc
|
||||
* @BelongsProject: schisandra-cloud-storage
|
||||
* @BelongsPackage: com.schisandra.wechat.rpc
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-06-20 22:59
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Component
|
||||
public class AuthRpc {
|
||||
@Resource
|
||||
private SchisandraAuthFeignService schisandraAuthFeignService;
|
||||
|
||||
public Boolean wechatLogin(String openId) {
|
||||
Result result = schisandraAuthFeignService.wechatLogin(openId);
|
||||
if (result.getSuccess()) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
spring:
|
||||
application:
|
||||
name: schisandra-cloud-storage-wechat
|
||||
cloud:
|
||||
nacos:
|
||||
config:
|
||||
server-addr: 1.95.0.111:8848
|
||||
prefix: ${spring.application.name}
|
||||
group: DEFAULT_GROUP
|
||||
namespace:
|
||||
file-extension: yaml
|
||||
timeout: 60000
|
||||
discovery:
|
||||
enabled: true
|
||||
server-addr: 1.95.0.111:8848
|
||||
|
@@ -0,0 +1,87 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--Configuration后面的status,这个用于设置log4j2自身内部的信息输出,可以不设置,当设置成trace时,你会看到log4j2内部各种详细输出-->
|
||||
<!--monitorInterval:Log4j能够自动检测修改配置 文件和重新配置本身,设置间隔秒数-->
|
||||
<configuration status="INFO" monitorInterval="5">
|
||||
<!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
|
||||
<!--变量配置-->
|
||||
<Properties>
|
||||
<!-- 格式化输出:%date表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度 %msg:日志消息,%n是换行符-->
|
||||
<!-- %logger{36} 表示 Logger 名字最长36个字符 -->
|
||||
<property name="LOG_PATTERN" value="%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%thread]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx" />
|
||||
<!-- 定义日志存储的路径 -->
|
||||
<property name="FILE_PATH" value="../log" />
|
||||
<property name="FILE_NAME" value="schisandra-cloud-storage-wechat.log" />
|
||||
</Properties>
|
||||
|
||||
<!--https://logging.apache.org/log4j/2.x/manual/appenders.html-->
|
||||
<appenders>
|
||||
|
||||
<console name="Console" target="SYSTEM_OUT">
|
||||
<!--输出日志的格式-->
|
||||
<PatternLayout pattern="${LOG_PATTERN}"/>
|
||||
<!--控制台只输出level及其以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
|
||||
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
|
||||
</console>
|
||||
|
||||
<!--文件会打印出所有信息,这个log每次运行程序会自动清空,由append属性决定,适合临时测试用-->
|
||||
<File name="fileLog" fileName="${FILE_PATH}/temp.log" append="false">
|
||||
<PatternLayout pattern="${LOG_PATTERN}"/>
|
||||
</File>
|
||||
|
||||
<!-- 这个会打印出所有的info及以下级别的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
|
||||
<RollingFile name="RollingFileInfo" fileName="${FILE_PATH}/info.log" filePattern="${FILE_PATH}/${FILE_NAME}-INFO-%d{yyyy-MM-dd}_%i.log.gz">
|
||||
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
|
||||
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
|
||||
<PatternLayout pattern="${LOG_PATTERN}"/>
|
||||
<Policies>
|
||||
<!--interval属性用来指定多久滚动一次,默认是1 hour-->
|
||||
<TimeBasedTriggeringPolicy interval="1"/>
|
||||
<SizeBasedTriggeringPolicy size="10MB"/>
|
||||
</Policies>
|
||||
<!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件开始覆盖-->
|
||||
<DefaultRolloverStrategy max="15"/>
|
||||
</RollingFile>
|
||||
|
||||
<!-- 这个会打印出所有的warn及以下级别的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
|
||||
<RollingFile name="RollingFileWarn" fileName="${FILE_PATH}/warn.log" filePattern="${FILE_PATH}/${FILE_NAME}-WARN-%d{yyyy-MM-dd}_%i.log.gz">
|
||||
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
|
||||
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
|
||||
<PatternLayout pattern="${LOG_PATTERN}"/>
|
||||
<Policies>
|
||||
<!--interval属性用来指定多久滚动一次,默认是1 hour-->
|
||||
<TimeBasedTriggeringPolicy interval="1"/>
|
||||
<SizeBasedTriggeringPolicy size="10MB"/>
|
||||
</Policies>
|
||||
<!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件开始覆盖-->
|
||||
<DefaultRolloverStrategy max="15"/>
|
||||
</RollingFile>
|
||||
|
||||
<!-- 这个会打印出所有的error及以下级别的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
|
||||
<RollingFile name="RollingFileError" fileName="${FILE_PATH}/error.log" filePattern="${FILE_PATH}/${FILE_NAME}-ERROR-%d{yyyy-MM-dd}_%i.log.gz">
|
||||
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
|
||||
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
|
||||
<PatternLayout pattern="${LOG_PATTERN}"/>
|
||||
<Policies>
|
||||
<!--interval属性用来指定多久滚动一次,默认是1 hour-->
|
||||
<TimeBasedTriggeringPolicy interval="1"/>
|
||||
<SizeBasedTriggeringPolicy size="10MB"/>
|
||||
</Policies>
|
||||
<!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件开始覆盖-->
|
||||
<DefaultRolloverStrategy max="15"/>
|
||||
</RollingFile>
|
||||
|
||||
</appenders>
|
||||
|
||||
<!--Logger节点用来单独指定日志的形式,比如要为指定包下的class指定不同的日志级别等。-->
|
||||
<!--然后定义loggers,只有定义了logger并引入的appender,appender才会生效-->
|
||||
<loggers>
|
||||
<root level="info">
|
||||
<appender-ref ref="Console"/>
|
||||
<appender-ref ref="RollingFileInfo"/>
|
||||
<appender-ref ref="RollingFileWarn"/>
|
||||
<appender-ref ref="RollingFileError"/>
|
||||
<appender-ref ref="fileLog"/>
|
||||
</root>
|
||||
</loggers>
|
||||
|
||||
</configuration>
|
Reference in New Issue
Block a user