Commit 89c85255 by levphon

微信小程序C端账号体系

1 parent 2c3a9260
......@@ -2,9 +2,9 @@
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>glsx-ne-shield</artifactId>
<artifactId>ne-shield-parent</artifactId>
<groupId>com.glsx</groupId>
<version>1.0-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
......
package cn.com.glsx.app.common.enums;
import lombok.Getter;
@Getter
public enum MiniUserType {
NORMAL(1, "normal"), SHARED(2, "shared");
private Integer flag;
private String type;
MiniUserType(Integer flag, String type) {
this.flag = flag;
this.type = type;
}
}
package cn.com.glsx.app.config;
import cn.com.glsx.auth.interceptor.WebRequestAuthorizeInterceptor;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
/**
* @author payu
*/
......@@ -35,9 +32,7 @@ public class WebConfig implements WebMvcConfigurer {
.excludePathPatterns("/")
.excludePathPatterns("/login")
.excludePathPatterns("/wechatAuth")
.excludePathPatterns("/wechatLogin")
.excludePathPatterns("/qqAuth")
.excludePathPatterns("/qqLogin")
.excludePathPatterns("/xactuator/**")
.excludePathPatterns("/api/**")
.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
......
......@@ -5,6 +5,8 @@ import cn.com.glsx.app.modules.model.LoginByAccountBO;
import cn.com.glsx.app.modules.service.LoginService;
import cn.com.glsx.app.modules.service.UserService;
import com.glsx.plat.core.web.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
......@@ -22,6 +24,7 @@ import java.util.Map;
* @author liuyf
*/
@Slf4j
@Api(value = "登录(本地)", tags = {"登录(本地)"})
@RestController
public class LocalLoginController {
......@@ -39,6 +42,7 @@ public class LocalLoginController {
/**
* 本地登录
*/
@ApiOperation(value = "登录系统")
@PostMapping(value = "/login")
public R login(@RequestBody @Validated LoginByAccountBO loginBO) {
......@@ -52,6 +56,7 @@ public class LocalLoginController {
return R.ok().data(rtnMap);
}
@ApiOperation(value = "登出系统")
@GetMapping(value = "/logout")
public R logout() {
//todo 拉黑token
......
......@@ -3,6 +3,7 @@ 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.common.enums.MiniUserType;
import cn.com.glsx.app.modules.entity.UserAuthRel;
import cn.com.glsx.app.modules.model.AppUser;
import cn.com.glsx.app.modules.model.QQOAuth2AccessToken;
......@@ -11,6 +12,8 @@ import cn.com.glsx.app.modules.service.UserService;
import cn.hutool.json.JSONUtil;
import com.glsx.plat.common.annotation.NoLogin;
import com.glsx.plat.core.web.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -29,6 +32,7 @@ import java.util.Optional;
* @author liuyf
*/
@Slf4j
@Api(value = "登录(第三方)", tags = {"登录(第三方)"})
@RestController
public class ThirdLoginController {
......@@ -42,6 +46,7 @@ public class ThirdLoginController {
private WxMaService wxMaService;
@NoLogin
@ApiOperation(value = "QQ授权登录")
@GetMapping(value = "/qqAuth")
public R qqAuth(@RequestParam String code) {
//获取openId、unionId等
......@@ -50,38 +55,71 @@ public class ThirdLoginController {
log.info("QQ code[{}]获取的Session信息为{}", code, JSONUtil.toJsonStr(accessToken));
UserAuthRel authRel = loginService.qqAuth(accessToken);
Map<String, Object> rtnMap = thirdLogin(LoginType.QQ, authRel);
Map<String, Object> rtnMap = thirdLogin(LoginType.QQ, 0, authRel);
return R.ok().data(rtnMap);
}
@NoLogin
@ApiOperation(value = "微信授权登录")
@GetMapping(value = "/wechatAuth")
public R wechatAuth(@RequestParam String code) throws WxErrorException {
public R wechatAuth(@RequestParam String code, @RequestParam Integer accessMode) 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(LoginType.WECHAT, authRel);
Map<String, Object> rtnMap = thirdLogin(LoginType.WECHAT, accessMode, authRel);
return R.ok().data(rtnMap);
}
private Map<String, Object> thirdLogin(LoginType loginType, UserAuthRel authRel) {
/**
* 第三方登录
*
* @param loginType
* @param accessMode 针对loginType而言更具体的访问模式,比如微信小程序登录,通过常规入口方式,还是分享链接方式
* @param authRel
* @return
*/
private Map<String, Object> thirdLogin(LoginType loginType, Integer accessMode, UserAuthRel authRel) {
Map<String, Object> rtnMap = new HashMap<>();
Optional<Long> optUserId = Optional.ofNullable(authRel.getUserId());
if (optUserId.isPresent()) {
//已绑定本地账号
AppUser user = userService.getAppUserByUserId(authRel.getUserId());
//分两种情况:
if (MiniUserType.NORMAL.getFlag().equals(accessMode)) {
//1、常规免登录(通过小程序正常入口)
Optional<Long> optUserId = Optional.ofNullable(authRel.getUserId());
if (optUserId.isPresent()) {
//已绑定本地账号
AppUser user = userService.getAppUserByUserId(authRel.getUserId());
user.setUserType(MiniUserType.NORMAL.getType());
String token = userService.createToken(user);
rtnMap.put("user", user);
rtnMap.put("token", token);
} else {
//未绑定本地账号,跳转登录界面登录,回传此id
rtnMap.put("authRelId", authRel.getId());
}
} else if (MiniUserType.SHARED.getFlag().equals(accessMode)) {
//2、通过分享车辆地址登录,也分两种情况
//a、存在账号的openid发起免登录
//b、不存在账号的openid发起免登录
AppUser user = userService.getAppUserByAuthId(loginType, authRel.getAuthId());
if (user != null) {
} else {
user = new AppUser();
// TODO: 2022/6/29 设置用户标识
}
user.setUserType(MiniUserType.SHARED.getType());
String token = userService.createToken(user);
rtnMap.put("user", user);
rtnMap.put("token", token);
} else {
//未绑定本地账号,跳转登录界面登录,回传此id
rtnMap.put("authRelId", authRel.getId());
// TODO: 2022/6/29 抛出异常
}
return rtnMap;
}
......
package cn.com.glsx.app.modules.controller;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping(value = "/dashboard")
@Api(value = "看板", tags = {"看板"})
public class DashboardController {
}
......@@ -35,6 +35,7 @@ public class VehicleMonitorController {
return R.ok().data(list);
}
@ApiOperation(value = "设备定位")
@GetMapping("/location")
public R location(@RequestParam("sns") String sns) {
List<VehicleLocationDTO> vehicleLocations = vehicleMonitorService.getVehicleLocations(sns);
......
......@@ -28,7 +28,13 @@ public class User extends BaseEntity {
private Integer tryTimes;
/**
* 用户账号/手机号
* 关联人(账户)id
*/
@Column(name = "linker_id")
private Long linkerId;
/**
* 用户账户/手机号
*/
private String account;
......
......@@ -5,11 +5,23 @@ import lombok.Data;
@Data
public class AppUser {
private Long id;
/**
* 用户id(对应后台t_user表id)
*/
private Long userId;
/**
* C端用户id(对应后台t_cust_user表id)
*/
private Long cuserId;
private String account;
private String username;
private String avatar;
private Integer gender;
/**
* 用户类型:normal正常用户,shared被分享用户,见枚举MiniUserType
*/
private String userType;
private Integer blacklist;
}
}
\ No newline at end of file
......@@ -20,12 +20,6 @@ public class LoginByAccountBO {
@NotBlank(message = "验证码/密码不能为空")
private String code;
/**
* 第三方授权关联id
*/
private Long authRelId;
// @NotNull(message = "APP客户端ID不能为空")
// private String clientId;
private String codeForWx;
}
......@@ -67,6 +67,7 @@ public class LoginService {
}
User user = Optional.ofNullable(userMapper.selectByAccount(simpleUser.getAccount())).orElse(new User());
user.setLinkerId(simpleUser.getId());
user.setAccount(simpleUser.getAccount());
user.setBlacklist(0);
userMapper.saveOrUpdate(user);
......@@ -159,6 +160,7 @@ public class LoginService {
UserAuthRel authRel = userAuthRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId());
if (authRel == null) {
authRel = new UserAuthRel();
//authRel.setUserId(null);
authRel.setAuthId(thirdAuth.getId());
authRel.setAuthType(AuthType.THIRD.getType());
userAuthRelMapper.insertUseGeneratedKeys(authRel);
......
......@@ -43,12 +43,13 @@ public class UserService {
public String createToken(AppUser user) {
Assert.notNull(user, "获取当前用户信息失败");
String uuid = UUID.randomUUID().toString(); //JWT 随机ID,做为验证的key
String jwtId = jwtUtils.getApplication() + ":" + uuid + "_" + jwtUtils.JWT_SESSION_PREFIX + user.getId();
String jwtId = jwtUtils.getApplication() + ":" + uuid + "_" + jwtUtils.JWT_SESSION_PREFIX + user.getUserId();
ComJwtUser jwtUser = new ComJwtUser();
jwtUser.setApplication(jwtUtils.getApplication());
jwtUser.setJwtId(jwtId);
jwtUser.setUserId(String.valueOf(user.getId()));
jwtUser.setUserId(String.valueOf(user.getUserId()));
jwtUser.setAccount(user.getAccount());
jwtUser.setBelong(user.getUserType());
Map<String, Object> userMap = (Map<String, Object>) ObjectUtils.objectToMap(jwtUser);
return jwtUtils.createToken(userMap);
}
......
......@@ -34,8 +34,9 @@
</select>
<select id="selectAppUserByUserId" resultType="cn.com.glsx.app.modules.model.AppUser">
select tcula.user_id as id,
tcula.union_id unionId,
select tcu.linker_id as userId,
tcu.id as cuserId,
tcula.union_id as unionId,
tcu.account,
tcula.username,
tcu.blacklist
......
......@@ -9,6 +9,7 @@
<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="linker_id" jdbcType="INTEGER" property="linkerId"/>
<result column="account" jdbcType="VARCHAR" property="account"/>
<result column="blacklist" jdbcType="TINYINT" property="blacklist"/>
<result column="del_flag" jdbcType="TINYINT" property="delFlag"/>
......@@ -21,7 +22,7 @@
<!--
WARNING - @mbg.generated
-->
id, token, expire_in, try_times, account, blacklist, del_flag, created_date, created_by, updated_date,
id, token, expire_in, try_times, linker_id, account, blacklist, del_flag, created_date, created_by, updated_date,
updated_by
</sql>
......@@ -42,8 +43,8 @@
</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
INSERT INTO t_cust_user (id, linker_id, account, blacklist, del_flag, created_date)
VALUES (#{id}, #{linkerId}, #{account}, #{blacklist}, 0, sysdate()) ON DUPLICATE KEY
UPDATE updated_date = sysdate()
</insert>
</mapper>
\ No newline at end of file
......@@ -8,7 +8,7 @@
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="auth_id" jdbcType="INTEGER" property="authId"/>
<result column="union_id" jdbcType="VARCHAR" property="unionId"/>
<result column="qq_avatar" jdbcType="VARCHAR" property="qqAvatar"/>
<result column="qq_avatar" jdbcType="VARCHAR" property="avatar"/>
<result column="qq_name" jdbcType="VARCHAR" property="qqName"/>
<result column="del_flag" jdbcType="TINYINT" property="delFlag"/>
<result column="created_date" jdbcType="TIMESTAMP" property="createdDate"/>
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!