Commit f16f8f8f by levphon

微信小程序C端账号体系

1 parent a9c90d1c
package cn.com.glsx.app.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@Configuration
public class BeansConfig {
@Bean
public BCryptPasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}
}
......@@ -2,11 +2,10 @@ package cn.com.glsx.app.modules;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.com.glsx.app.common.enums.LoginType;
import cn.com.glsx.app.modules.entity.UserAuthRel;
import cn.com.glsx.app.modules.model.AppUser;
import cn.com.glsx.app.modules.model.QQLoginBO;
import cn.com.glsx.app.modules.model.QQOAuth2AccessToken;
import cn.com.glsx.app.modules.model.WechatLoginBO;
import cn.com.glsx.app.modules.service.LoginService;
import cn.com.glsx.app.modules.service.UserService;
import cn.hutool.json.JSONUtil;
......@@ -15,7 +14,9 @@ import com.glsx.plat.core.web.R;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.HashMap;
......@@ -41,65 +42,45 @@ public class ThirdLoginController {
private WxMaService wxMaService;
@NoLogin
@GetMapping(value = "/wechatAuth")
public R wechatAuth(@RequestParam String code) throws WxErrorException {
//获取openId、unionid、session_key
WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
log.info("微信code[{}]获取的Session信息为{}", code, JSONUtil.toJsonStr(sessionInfo));
UserAuthRel authRel = loginService.wechatAuth(sessionInfo);
Map<String, Object> rtnMap = thirdLogin(authRel);
return R.ok().data(rtnMap);
}
@NoLogin
@PostMapping(value = "/wechatLogin")
public R wechatLogin(@RequestBody WechatLoginBO loginBO) {
log.info("微信授权参数 {}", loginBO.toString());
UserAuthRel wxAuthRel = loginService.wechatLogin(loginBO);
Map<String, Object> rtnMap = thirdLogin(wxAuthRel);
return R.ok().data(rtnMap);
}
@NoLogin
@GetMapping(value = "/qqAuth")
public R qqAuth(@RequestParam String code) {
//获取openId、unionId等
QQOAuth2AccessToken accessToken = new QQOAuth2AccessToken();
log.info("QQ code[{}]获取的Session信息为{}", code, JSONUtil.toJsonStr(accessToken));
UserAuthRel authRel = loginService.qqAuth(accessToken);
Map<String, Object> rtnMap = thirdLogin(authRel);
Map<String, Object> rtnMap = thirdLogin(LoginType.QQ, authRel);
return R.ok().data(rtnMap);
}
@NoLogin
@PostMapping(value = "/qqLogin")
public R qqLogin(@RequestBody QQLoginBO loginBO) {
log.info("QQ授权参数 {}", loginBO.toString());
UserAuthRel qqAuthRel = loginService.qqLogin(loginBO);
@GetMapping(value = "/wechatAuth")
public R wechatAuth(@RequestParam String code) throws WxErrorException {
//获取openId、unionid、session_key
WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
log.info("微信code[{}]获取的Session信息为{}", code, JSONUtil.toJsonStr(sessionInfo));
UserAuthRel authRel = loginService.wechatAuth(sessionInfo);
Map<String, Object> rtnMap = thirdLogin(qqAuthRel);
Map<String, Object> rtnMap = thirdLogin(LoginType.WECHAT, authRel);
return R.ok().data(rtnMap);
}
private Map<String, Object> thirdLogin(UserAuthRel authRel) {
private Map<String, Object> thirdLogin(LoginType loginType, UserAuthRel authRel) {
Map<String, Object> rtnMap = new HashMap<>();
Optional<Long> optUserId = Optional.ofNullable(authRel.getUserId());
if (optUserId.isPresent()) {
//如果用户id存在,理论上User不为空
AppUser user = userService.getAppUserByUserId(optUserId.get());
//已绑定本地账号
AppUser user = userService.getAppUserByUserId(authRel.getUserId());
String token = userService.createToken(user);
rtnMap.put("token", token);
rtnMap.put("user", user);
rtnMap.put("token", token);
} else {
//没有绑定用户,跳转登录界面登录,回传此id
//未绑定本地账号,跳转登录界面登录,回传此id
rtnMap.put("authRelId", authRel.getId());
}
return rtnMap;
......
package cn.com.glsx.app.modules.entity;
import com.glsx.plat.mybatis.base.BaseEntity;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Table;
@Data
@Table(name = "t_cust_user")
public class User extends BaseEntity {
/**
* 登录token
*/
private String token;
/**
* token过期时间
*/
@Column(name = "expire_in")
private Long expireIn;
/**
* 登录失败次数
*/
@Column(name = "try_times")
private Integer tryTimes;
/**
* 用户账号/手机号
*/
private String account;
/**
* 状态:0白名单,临时封禁(blacklist>0,值为封禁天数),永久封禁(del_flag=-1)
*/
private Integer blacklist;
}
\ No newline at end of file
......@@ -11,9 +11,15 @@ import javax.persistence.Table;
public class UserLocalAuth extends BaseEntity {
/**
* 用户id
* 用户账号
*/
@Column(name = "user_id")
private Long userId;
@Column(name = "account")
private String account;
/**
* 密码
*/
@Column(name = "account")
private String password;
}
\ No newline at end of file
......@@ -10,16 +10,8 @@ import java.util.List;
@Mapper
public interface UserAuthRelMapper extends CommonBaseMapper<UserAuthRel> {
UserAuthRel selectById(@Param("id") Long id);
UserAuthRel selectLocalAuthRelByUserId(@Param("userId") Long userId);
UserAuthRel selectByAuthTypeAndAuthId(@Param("authType") String authType, @Param("authId") Long authId);
List<UserAuthRel> selectByAuthTypeAndUserId(@Param("authType") String authType, @Param("userId") Long userId);
List<UserAuthRel> selectByUserId(@Param("userId") Long userId);
int logicDeleteByUserId(@Param("userId") Long userId);
}
\ No newline at end of file
package cn.com.glsx.app.modules.mapper;
import cn.com.glsx.app.modules.entity.UserLocalAccount;
import cn.com.glsx.app.modules.model.AppUser;
import com.glsx.plat.mybatis.mapper.CommonBaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface UserLocalAccountMapper extends CommonBaseMapper<UserLocalAccount> {
UserLocalAccount selectByAuthId(@Param("authId") Long authId);
AppUser selectAppUserByUserId(@Param("userId") Long userId);
}
\ No newline at end of file
......@@ -8,10 +8,8 @@ import org.apache.ibatis.annotations.Param;
@Mapper
public interface UserLocalAuthMapper extends CommonBaseMapper<UserLocalAuth> {
UserLocalAuth selectById(@Param("id") Long id);
UserLocalAuth selectByAuthId(@Param("authId") Long authId);
int updatePasswordByUserId(@Param("userId") Long userId, @Param("password") String password);
int logicDeleteById(@Param("id") Long id);
UserLocalAuth selectByUserId(@Param("userId") Long userId);
}
\ No newline at end of file
package cn.com.glsx.app.modules.mapper;
import cn.com.glsx.app.modules.entity.User;
import com.glsx.plat.mybatis.mapper.CommonBaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface UserMapper extends CommonBaseMapper<User> {
User selectById(@Param("id") Long id);
User selectByAccount(@Param("account") String account);
int saveOrUpdate(User user);
}
\ No newline at end of file
......@@ -10,8 +10,6 @@ public interface UserThirdAuthMapper extends CommonBaseMapper<UserThirdAuth> {
UserThirdAuth selectByOpenid(@Param("openid") String openid);
UserThirdAuth selectByLoginTypeAndUserId(@Param("loginType") String loginType, @Param("userId") Long userId);
int logicDeleteById(@Param("id") Long id);
UserThirdAuth selectByLoginTypeAndAuthId(@Param("loginType") String loginType, @Param("authId") Long authId);
}
\ No newline at end of file
package cn.com.glsx.app.modules.model;
import lombok.Data;
import javax.validation.constraints.NotBlank;
@Data
public class QQLoginBO {
@NotBlank
private String openId;
}
package cn.com.glsx.app.modules.model;
import lombok.Data;
import javax.validation.constraints.NotBlank;
@Data
public class WechatLoginBO {
@NotBlank
private String openId;
@NotBlank
private String unionId;
}
package cn.com.glsx.app.modules.service;
import cn.com.glsx.app.common.exception.AppServerException;
import cn.com.glsx.app.modules.converter.UserConverter;
import cn.com.glsx.app.common.enums.AuthType;
import cn.com.glsx.app.common.enums.LoginType;
import cn.com.glsx.app.modules.entity.UserAuthRel;
import cn.com.glsx.app.modules.entity.UserThirdAuth;
import cn.com.glsx.app.modules.mapper.UserAuthRelMapper;
import cn.com.glsx.app.modules.mapper.UserLocalAccountMapper;
import cn.com.glsx.app.modules.mapper.UserThirdAuthMapper;
import cn.com.glsx.app.modules.model.AppUser;
import cn.com.glsx.auth.api.AuthFeignClient;
import cn.com.glsx.auth.model.SimpleUser;
import cn.hutool.core.lang.UUID;
import com.glsx.plat.common.utils.ObjectUtils;
import com.glsx.plat.core.web.R;
import com.glsx.plat.jwt.base.ComJwtUser;
import com.glsx.plat.jwt.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.Map;
@Service
......@@ -22,8 +25,14 @@ public class UserService {
@Autowired
private JwtUtils<ComJwtUser> jwtUtils;
@Autowired
private AuthFeignClient authFeignClient;
@Resource
private UserAuthRelMapper userAuthRelMapper;
@Resource
private UserThirdAuthMapper userThirdAuthMapper;
@Resource
private UserLocalAccountMapper userLocalAccountMapper;
/**
* 生成带用户信息的token
......@@ -45,12 +54,17 @@ public class UserService {
}
public AppUser getAppUserByUserId(Long userId) {
R<SimpleUser> userR = authFeignClient.getSimpleUserById(userId);
SimpleUser simpleUser = userR.getData(SimpleUser.class);
if (!userR.isSuccess() || simpleUser == null) {
throw new AppServerException(userR.getMessage());
}
return UserConverter.INSTANCE.su2au(simpleUser);
//获取用户本地信息
return userLocalAccountMapper.selectAppUserByUserId(userId);
}
public AppUser getAppUserByAuthId(LoginType loginType, Long authId) {
//获取具体第三方授权
UserThirdAuth thirdAuth = userThirdAuthMapper.selectByLoginTypeAndAuthId(loginType.getType(), authId);
//获取第三方授权关联
UserAuthRel userAuthRel = userAuthRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId());
//获取用户本地信息
return this.getAppUserByUserId(userAuthRel.getUserId());
}
}
......@@ -28,7 +28,7 @@ public class VehicleMonitorService {
}
public List<VehicleLocationDTO> getVehicleLocations(String sns) {
R<List<DeviceLocationDTO>> listR = gpsBsFeignService.listDeviceMonitorInfo(sns, null);
R<List<DeviceLocationDTO>> listR = gpsBsFeignService.listDeviceMonitorInfo(sns);
List<DeviceLocationDTO> dataList = listR.getDataList(new TypeToken<List<DeviceLocationDTO>>() {
}.getType());
......
......@@ -30,6 +30,7 @@
where auth_type = #{authType}
and auth_id = #{authId}
and del_flag = 0
limit 1
</select>
<select id="selectByAuthTypeAndUserId" resultMap="BaseResultMap">
......@@ -40,36 +41,4 @@
and user_id = #{userId}
and del_flag = 0
</select>
<select id="selectLocalAuthRelByUserId" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_user_auth_rel
where auth_type = 'local'
and user_id = #{userId}
and del_flag = 0
limit 1
</select>
<select id="selectById" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_user_auth_rel
where id = #{id}
and del_flag = 0
</select>
<select id="selectByUserId" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_user_auth_rel
where user_id = #{userId}
and del_flag = 0
</select>
<update id="logicDeleteByUserId">
update t_user_auth_rel
set del_flag = -1
where user_id = #{userId}
</update>
</mapper>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.com.glsx.app.modules.mapper.UserLocalAccountMapper">
<resultMap id="BaseResultMap" type="cn.com.glsx.app.modules.entity.UserLocalAccount">
<!--
WARNING - @mbg.generated
-->
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="user_id" jdbcType="INTEGER" property="userId"/>
<result column="auth_id" jdbcType="INTEGER" property="authId"/>
<result column="union_id" jdbcType="VARCHAR" property="unionId"/>
<result column="username" jdbcType="VARCHAR" property="username"/>
<result column="avatar" jdbcType="VARCHAR" property="avatar"/>
<result column="del_flag" jdbcType="TINYINT" property="delFlag"/>
<result column="created_date" jdbcType="TIMESTAMP" property="createdDate"/>
<result column="created_by" jdbcType="INTEGER" property="createdBy"/>
<result column="updated_date" jdbcType="TIMESTAMP" property="updatedDate"/>
<result column="updated_by" jdbcType="INTEGER" property="updatedBy"/>
</resultMap>
<sql id="Base_Column_List">
<!--
WARNING - @mbg.generated
-->
id, user_id, auth_id, union_id, username, avatar, del_flag, created_date, created_by,
updated_date, updated_by
</sql>
<select id="selectByAuthId" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_cust_user_local_account
where auth_id = #{authId}
and del_flag = 0
</select>
<select id="selectAppUserByUserId" resultType="cn.com.glsx.app.modules.model.AppUser">
select tcula.user_id as id,
tcula.union_id unionId,
tcu.account,
tcula.username,
tcu.blacklist
from t_cust_user_local_account tcula
inner join t_cust_user_auth_rel tcuar on tcula.id = tcuar.auth_id
left join t_cust_user tcu on tcu.id = tcula.user_id
where tcula.del_flag = 0
and tcuar.auth_type = 'local'
and tcula.user_id = #{userId} limit 1
</select>
</mapper>
\ No newline at end of file
......@@ -6,7 +6,8 @@
WARNING - @mbg.generated
-->
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="user_id" jdbcType="INTEGER" property="userId"/>
<result column="account" jdbcType="VARCHAR" property="account"/>
<result column="password" jdbcType="VARCHAR" property="password"/>
<result column="del_flag" jdbcType="TINYINT" property="delFlag"/>
<result column="created_date" jdbcType="TIMESTAMP" property="createdDate"/>
<result column="created_by" jdbcType="INTEGER" property="createdBy"/>
......@@ -17,29 +18,26 @@
<!--
WARNING - @mbg.generated
-->
id, user_id, del_flag, created_date, created_by,
id, account, `password`, del_flag, created_date, created_by,
updated_date, updated_by
</sql>
<select id="selectById" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_user_local_auth
where id = #{id}
and del_flag = 0
<select id="selectByAuthId" resultMap="BaseResultMap">
select tcula.*
from t_cust_user_local_auth tcula
inner join t_cust_user_auth_rel tcuar on tcula.id = tcuar.auth_id
where tcuar.del_flag = 0
and tcuar.auth_type = 'local'
and tcuar.auth_id = #{authId} limit 1
</select>
<update id="updatePasswordByUserId">
update t_user_local_auth tcula ,t_user_auth_rel tcuar
set tcula.password = #{password}
where tcula.id = tcuar.auth_id
and auth_type = 'local'
and tcuar.user_id = #{userId}
</update>
<update id="logicDeleteById">
update t_user_local_auth
set del_flag = -1
where id = #{id}
</update>
<select id="selectByUserId" resultMap="BaseResultMap">
select tcula.*
from t_cust_user_local_auth tcula
inner join t_cust_user_auth_rel tcuar on tcula.id = tcuar.auth_id
inner join t_cust_user_local_account tcula2 on tcula2.auth_id = tcuar.auth_id
where tcuar.del_flag = 0
and tcuar.auth_type = 'local'
and tcula2.user_id = #{userId} limit 1
</select>
</mapper>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.com.glsx.app.modules.mapper.UserMapper">
<resultMap id="BaseResultMap" type="cn.com.glsx.app.modules.entity.User">
<!--
WARNING - @mbg.generated
-->
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="token" jdbcType="VARCHAR" property="token"/>
<result column="expire_in" jdbcType="INTEGER" property="expireIn"/>
<result column="try_times" jdbcType="INTEGER" property="tryTimes"/>
<result column="account" jdbcType="VARCHAR" property="account"/>
<result column="blacklist" jdbcType="TINYINT" property="blacklist"/>
<result column="del_flag" jdbcType="TINYINT" property="delFlag"/>
<result column="created_date" jdbcType="TIMESTAMP" property="createdDate"/>
<result column="created_by" jdbcType="INTEGER" property="createdBy"/>
<result column="updated_date" jdbcType="TIMESTAMP" property="updatedDate"/>
<result column="updated_by" jdbcType="INTEGER" property="updatedBy"/>
</resultMap>
<sql id="Base_Column_List">
<!--
WARNING - @mbg.generated
-->
id, token, expire_in, try_times, account, blacklist, del_flag, created_date, created_by, updated_date,
updated_by
</sql>
<select id="selectById" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_cust_user
where id = #{id}
and del_flag = 0
</select>
<select id="selectByAccount" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_cust_user
where account = #{account}
and del_flag = 0
</select>
<insert id="saveOrUpdate" useGeneratedKeys="true" keyProperty="id" parameterType="cn.com.glsx.app.modules.entity.User">
INSERT INTO t_cust_user (id, account, blacklist, del_flag, created_date)
VALUES (#{id}, #{account}, #{blacklist}, 0, sysdate()) ON DUPLICATE KEY
UPDATE updated_date = sysdate()
</insert>
</mapper>
\ No newline at end of file
......@@ -31,21 +31,13 @@
and del_flag = 0
</select>
<select id="selectByLoginTypeAndUserId" resultMap="BaseResultMap">
<select id="selectByLoginTypeAndAuthId" resultMap="BaseResultMap">
select tcuta.*
from t_user_third_auth tcuta,
t_user_auth_rel tcuar
where tcuta.id = tcuar.auth_id
from t_cust_user_third_auth tcuta
inner join t_cust_user_auth_rel tcuar on tcuta.id = tcuar.auth_id
where tcuta.del_flag = 0
and tcuar.auth_type = 'third'
and tcuta.login_type = #{loginType}
and tcuar.user_id = #{userId}
and tcuta.del_flag = 0
and tcuar.del_flag = 0
and tcuar.auth_id = #{userId};
</select>
<update id="logicDeleteById">
update t_user_third_auth
set del_flag = -1
where id = #{id}
</update>
</mapper>
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!