feat: 微信公众号扫码登录

This commit is contained in:
landaiqing
2024-06-20 23:51:09 +08:00
parent 91b27e9205
commit 97dbec7e25
34 changed files with 512 additions and 65 deletions

View File

@@ -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);
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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("登录失败");
}
}
}

View File

@@ -93,5 +93,7 @@ public interface SchisandraAuthUserDomainService {
Object deleteById(Long id);
SchisandraAuthUser queryByPhone(String phone);
Boolean wechatLogin(String openId);
}

View File

@@ -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();
}

View File

@@ -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)

View File

@@ -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;
/**

View File

@@ -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;
/**

View File

@@ -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;
/**

View File

@@ -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;
/**

View File

@@ -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);
}
/**

View File

@@ -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());
}

View File

@@ -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);
}
/**

View File

@@ -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;
}
/**