打通短视频系统账户和商城账户关联
This commit is contained in:
parent
f207488645
commit
dea2a88574
@ -2,14 +2,39 @@ package com.imooc.config;
|
|||||||
|
|
||||||
import com.imooc.intercepter.PassportInterceptor;
|
import com.imooc.intercepter.PassportInterceptor;
|
||||||
import com.imooc.intercepter.UserTokenInterceptor;
|
import com.imooc.intercepter.UserTokenInterceptor;
|
||||||
|
import java.security.KeyManagementException;
|
||||||
|
import java.security.KeyStoreException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import javax.net.ssl.SSLContext;
|
||||||
|
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
||||||
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.impl.client.HttpClients;
|
||||||
|
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||||
|
import org.apache.http.ssl.SSLContexts;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
import org.springframework.web.cors.CorsConfiguration;
|
||||||
|
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||||
|
import org.springframework.web.filter.CorsFilter;
|
||||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class InterceptorConfig implements WebMvcConfigurer {
|
public class InterceptorConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
|
private String[] CORS_HTTP_METHODS = { HttpMethod.HEAD.name(), HttpMethod.GET.name(), HttpMethod.POST.name(),
|
||||||
|
HttpMethod.PUT.name(), HttpMethod.DELETE.name(), HttpMethod.OPTIONS.name(), HttpMethod.PATCH.name() };
|
||||||
|
|
||||||
|
private static final long CORS_MAX_AGE = 30 * 24 * 60 * 60;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public PassportInterceptor passportInterceptor() {
|
public PassportInterceptor passportInterceptor() {
|
||||||
return new PassportInterceptor();
|
return new PassportInterceptor();
|
||||||
@ -22,11 +47,70 @@ public class InterceptorConfig implements WebMvcConfigurer {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addInterceptors(InterceptorRegistry registry) {
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
registry.addInterceptor(passportInterceptor())
|
registry.addInterceptor(passportInterceptor()).addPathPatterns("/passport/getSMSCode");
|
||||||
.addPathPatterns("/passport/getSMSCode");
|
|
||||||
|
|
||||||
registry.addInterceptor(userTokenInterceptor())
|
registry.addInterceptor(userTokenInterceptor()).addPathPatterns("/userInfo/modifyUserInfo")
|
||||||
.addPathPatterns("/userInfo/modifyUserInfo")
|
|
||||||
.addPathPatterns("/userInfo/modifyImage");
|
.addPathPatterns("/userInfo/modifyImage");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||||
|
@Bean
|
||||||
|
public CorsFilter corsFilter() {
|
||||||
|
// 添加跨域配置信息
|
||||||
|
CorsConfiguration config = new CorsConfiguration();
|
||||||
|
// 设置访问源地址
|
||||||
|
config.addAllowedOriginPattern("*");
|
||||||
|
// 设置访问源请求头
|
||||||
|
config.addAllowedHeader("*");
|
||||||
|
// 设置是否发送Cookie信息
|
||||||
|
config.setAllowCredentials(true);
|
||||||
|
// 设置访问源请求方法
|
||||||
|
Arrays.stream(CORS_HTTP_METHODS).forEach(method -> config.addAllowedMethod(method));
|
||||||
|
// 设置请求最大有效时长
|
||||||
|
config.setMaxAge(CORS_MAX_AGE);
|
||||||
|
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||||
|
// 配置跨域
|
||||||
|
source.registerCorsConfiguration("/**", config);
|
||||||
|
return new CorsFilter(source);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Primary
|
||||||
|
@Bean
|
||||||
|
public RestTemplate restTemplate() {
|
||||||
|
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
|
||||||
|
// 连接池最大连接数
|
||||||
|
connManager.setMaxTotal(1000);
|
||||||
|
// 每个主机的并发
|
||||||
|
connManager.setDefaultMaxPerRoute(500);
|
||||||
|
|
||||||
|
CloseableHttpClient httpClient = HttpClients.custom()//
|
||||||
|
.setConnectionManager(connManager)// 设置HTTP连接管理器
|
||||||
|
.build();
|
||||||
|
return buildRestTemplate(httpClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RestTemplate restTemplateIgnoreSSL()
|
||||||
|
throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
|
||||||
|
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, (chain, authType) -> true).build();
|
||||||
|
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext,
|
||||||
|
NoopHostnameVerifier.INSTANCE);
|
||||||
|
|
||||||
|
CloseableHttpClient httpClient = HttpClients.custom()//
|
||||||
|
.setSSLSocketFactory(sslSocketFactory)//
|
||||||
|
.build();
|
||||||
|
return buildRestTemplate(httpClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
private RestTemplate buildRestTemplate(CloseableHttpClient httpClient) {
|
||||||
|
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(
|
||||||
|
httpClient);
|
||||||
|
// 获取链接超时时间
|
||||||
|
httpRequestFactory.setConnectionRequestTimeout(3000);
|
||||||
|
// 指客户端和服务器建立连接的timeout
|
||||||
|
httpRequestFactory.setConnectTimeout(3000);
|
||||||
|
// 读取数据的超时时间
|
||||||
|
httpRequestFactory.setReadTimeout(120000);
|
||||||
|
return new RestTemplate(httpRequestFactory);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.imooc.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* spring security配置类.
|
||||||
|
*
|
||||||
|
* @author lzc
|
||||||
|
* @version 1.0
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
|
http.authorizeRequests().antMatchers("/**").permitAll() // 所有请求都可以访问
|
||||||
|
.and().csrf().disable() // 跨域请求关闭
|
||||||
|
.headers().frameOptions().disable(); // 资源下载权限关闭
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
PasswordEncoder passwordEncoder() {
|
||||||
|
return new BCryptPasswordEncoder();
|
||||||
|
}
|
||||||
|
}
|
@ -5,20 +5,26 @@ import com.imooc.bo.RegistLoginBO;
|
|||||||
import com.imooc.bo.LoginWithPasswordBO;
|
import com.imooc.bo.LoginWithPasswordBO;
|
||||||
import com.imooc.grace.result.GraceJSONResult;
|
import com.imooc.grace.result.GraceJSONResult;
|
||||||
import com.imooc.grace.result.ResponseStatusEnum;
|
import com.imooc.grace.result.ResponseStatusEnum;
|
||||||
|
import com.imooc.mo.Token;
|
||||||
import com.imooc.pojo.Users;
|
import com.imooc.pojo.Users;
|
||||||
import com.imooc.service.UserService;
|
import com.imooc.service.UserService;
|
||||||
import com.imooc.utils.IPUtil;
|
import com.imooc.utils.IPUtil;
|
||||||
import com.imooc.utils.SMSUtils;
|
import com.imooc.utils.SMSUtils;
|
||||||
import com.imooc.vo.UsersVO;
|
import com.imooc.vo.UsersVO;
|
||||||
|
import com.imooc.utils.CommonUtil;
|
||||||
|
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -32,30 +38,35 @@ import java.util.UUID;
|
|||||||
@Api(tags = "通行证,验证码登录注册")
|
@Api(tags = "通行证,验证码登录注册")
|
||||||
public class PassportController extends BaseInfoProperties {
|
public class PassportController extends BaseInfoProperties {
|
||||||
@Autowired
|
@Autowired
|
||||||
SMSUtils smsUtils;
|
private SMSUtils smsUtils;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
UserService userService;
|
private UserService userService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
@PostMapping("getSMSCode")
|
@PostMapping("getSMSCode")
|
||||||
public Object getSMSCode(@RequestParam String mobile, HttpServletRequest request) throws Exception {
|
public Object getSMSCode(@RequestParam String mobile, HttpServletRequest request) throws Exception {
|
||||||
if (StringUtils.isBlank(mobile)){
|
if (StringUtils.isBlank(mobile)) {
|
||||||
return GraceJSONResult.ok();
|
return GraceJSONResult.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO 获得用户ip 限制时间60s只能1次
|
// TODO 获得用户ip 限制时间60s只能1次
|
||||||
String userIp=IPUtil.getRequestIp(request);
|
String userIp = IPUtil.getRequestIp(request);
|
||||||
|
|
||||||
redis.setnx60s(MOBILE_SMSCODE+":"+userIp, userIp);
|
redis.setnx60s(MOBILE_SMSCODE + ":" + userIp, userIp);
|
||||||
String code=(int)((Math.random()*9+1)*100000)+"";
|
String code = (int) ((Math.random() * 9 + 1) * 100000) + "";
|
||||||
|
|
||||||
smsUtils.sendSMS(mobile,code);
|
smsUtils.sendSMS(mobile, code);
|
||||||
|
|
||||||
log.info(code);
|
log.info(code);
|
||||||
redis.set(MOBILE_SMSCODE+":"+mobile, code,30*60);
|
redis.set(MOBILE_SMSCODE + ":" + mobile, code, 30 * 60);
|
||||||
//TODO 验证码放入redis
|
// TODO 验证码放入redis
|
||||||
return GraceJSONResult.ok();
|
return GraceJSONResult.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("login")
|
/*@PostMapping("login")
|
||||||
public Object login(@Valid @RequestBody RegistLoginBO registLoginBO){
|
public Object login(@Valid @RequestBody RegistLoginBO registLoginBO){
|
||||||
|
|
||||||
String rediscode = redis.get(MOBILE_SMSCODE + ":" + registLoginBO.getMobile());
|
String rediscode = redis.get(MOBILE_SMSCODE + ":" + registLoginBO.getMobile());
|
||||||
@ -83,37 +94,85 @@ public class PassportController extends BaseInfoProperties {
|
|||||||
usersVO.setUserToken(uToken);
|
usersVO.setUserToken(uToken);
|
||||||
|
|
||||||
return GraceJSONResult.ok(usersVO);
|
return GraceJSONResult.ok(usersVO);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
@PostMapping("login")
|
||||||
|
public Object login(@Valid @RequestBody RegistLoginBO registLoginBO) {
|
||||||
|
|
||||||
|
/*String rediscode = redis.get(MOBILE_SMSCODE + ":" + registLoginBO.getMobile());
|
||||||
|
|
||||||
|
if (StringUtils.isBlank(rediscode) || !rediscode.equalsIgnoreCase(registLoginBO.getSmsCode())) {
|
||||||
|
System.out.println("rediscode" + rediscode);
|
||||||
|
System.out.println("registLoginBO.getMobile()" + registLoginBO.getSmsCode());
|
||||||
|
return GraceJSONResult.errorCustom(ResponseStatusEnum.SMS_CODE_ERROR);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
// 手机号
|
||||||
|
String mobile = registLoginBO.getMobile();
|
||||||
|
Users user = userService.queryMobileIsExist(mobile);
|
||||||
|
Token shopToken = null;
|
||||||
|
if (user == null) {
|
||||||
|
Map<String, Object> result = userService.createUserNew(mobile);
|
||||||
|
if (CommonUtil.isEmpty(result)) {
|
||||||
|
return GraceJSONResult.errorCustom(ResponseStatusEnum.FAILED);
|
||||||
|
}
|
||||||
|
user = (Users) result.get("user");
|
||||||
|
shopToken = (Token) result.get("token");
|
||||||
|
} else {
|
||||||
|
shopToken = userService.getShopToken(user.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
String uToken = UUID.randomUUID().toString();
|
||||||
|
log.info(uToken);
|
||||||
|
redis.set(REDIS_USER_TOKEN + ":" + user.getId(), uToken);
|
||||||
|
|
||||||
|
// 清除验证码
|
||||||
|
redis.del(MOBILE_SMSCODE + ":" + user.getMobile());
|
||||||
|
|
||||||
|
// 返回给前端
|
||||||
|
UsersVO usersVO = new UsersVO();
|
||||||
|
BeanUtils.copyProperties(user, usersVO);
|
||||||
|
usersVO.setUserToken(uToken);
|
||||||
|
usersVO.setShopToken(shopToken);
|
||||||
|
|
||||||
|
return GraceJSONResult.ok(usersVO);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("loginWithPassword")
|
@PostMapping("loginWithPassword")
|
||||||
public Object loginWithPassword(@Valid @RequestBody LoginWithPasswordBO loginWithPasswordBO){
|
public Object loginWithPassword(@Valid @RequestBody LoginWithPasswordBO loginWithPasswordBO) {
|
||||||
String phone = loginWithPasswordBO.getMobile();
|
String phone = loginWithPasswordBO.getMobile();
|
||||||
String password = loginWithPasswordBO.getPassword();
|
String password = loginWithPasswordBO.getPassword();
|
||||||
log.info(phone);
|
log.info(phone);
|
||||||
log.info(password);
|
log.info(password);
|
||||||
Users user = userService.queryMobileIsExist(loginWithPasswordBO.getMobile());
|
Users user = userService.queryMobileIsExist(phone);
|
||||||
if (user==null){
|
if (user == null) {
|
||||||
return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_NOT_EXIST_ERROR);
|
return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_NOT_EXIST_ERROR);
|
||||||
}
|
}
|
||||||
|
if (!passwordEncoder.matches(password, user.getPassword())) {
|
||||||
|
return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_PASSWORD_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
String uToken = UUID.randomUUID().toString();
|
String uToken = UUID.randomUUID().toString();
|
||||||
log.info(uToken);
|
log.info(uToken);
|
||||||
redis.set(REDIS_USER_TOKEN+":"+user.getId(),uToken);
|
redis.set(REDIS_USER_TOKEN + ":" + user.getId(), uToken);
|
||||||
|
//商城token
|
||||||
|
Token shopToken = userService.getShopToken(user.getId());
|
||||||
|
|
||||||
//返回给前端
|
// 返回给前端
|
||||||
UsersVO usersVO = new UsersVO();
|
UsersVO usersVO = new UsersVO();
|
||||||
BeanUtils.copyProperties(user, usersVO);
|
BeanUtils.copyProperties(user, usersVO);
|
||||||
usersVO.setUserToken(uToken);
|
usersVO.setUserToken(uToken);
|
||||||
|
usersVO.setShopToken(shopToken);
|
||||||
|
|
||||||
return GraceJSONResult.ok(usersVO);
|
return GraceJSONResult.ok(usersVO);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@PostMapping("logout")
|
@PostMapping("logout")
|
||||||
public Object logout(@RequestParam String userId){
|
public Object logout(@RequestParam String userId) {
|
||||||
|
|
||||||
redis.del(REDIS_USER_TOKEN+":"+userId);
|
redis.del(REDIS_USER_TOKEN + ":" + userId);
|
||||||
|
|
||||||
return GraceJSONResult.ok();
|
return GraceJSONResult.ok();
|
||||||
}
|
}
|
||||||
|
@ -5,9 +5,9 @@ spring:
|
|||||||
datasource: # 数据源的相关配置
|
datasource: # 数据源的相关配置
|
||||||
type: com.zaxxer.hikari.HikariDataSource # 数据源的类型,可以更改为其他的数据源配置,比如druid
|
type: com.zaxxer.hikari.HikariDataSource # 数据源的类型,可以更改为其他的数据源配置,比如druid
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver # mysql/MariaDB 的数据库驱动类名称
|
driver-class-name: com.mysql.cj.jdbc.Driver # mysql/MariaDB 的数据库驱动类名称
|
||||||
url: jdbc:mysql://182.92.182.217:3306/tiktok?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
|
url: jdbc:mysql://82.156.121.2:23306/shop_dev?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
|
||||||
username: tiktok
|
username: wzj
|
||||||
password: Bc3T5AA2pBdt2FBk
|
password: A085F27A43B0
|
||||||
hikari:
|
hikari:
|
||||||
connection-timeout: 30000 # 等待连接池分配连接的最大时间(毫秒),超过这个时长还没有可用的连接,则会抛出SQLException
|
connection-timeout: 30000 # 等待连接池分配连接的最大时间(毫秒),超过这个时长还没有可用的连接,则会抛出SQLException
|
||||||
minimum-idle: 5 # 最小连接数
|
minimum-idle: 5 # 最小连接数
|
||||||
@ -18,9 +18,9 @@ spring:
|
|||||||
max-lifetime: 18000000 # 连接池的最大生命时长(毫秒),超时则会被释放(retired)
|
max-lifetime: 18000000 # 连接池的最大生命时长(毫秒),超时则会被释放(retired)
|
||||||
connection-test-query: SELECT 1
|
connection-test-query: SELECT 1
|
||||||
redis:
|
redis:
|
||||||
host: 182.92.182.217
|
host: 82.156.121.2
|
||||||
port: 6379
|
port: 26379
|
||||||
password: '!aw5)lJf'
|
password: e4ea0caebfd2
|
||||||
database: 1 # 使用的数据库编号
|
database: 1 # 使用的数据库编号
|
||||||
jedis:
|
jedis:
|
||||||
pool:
|
pool:
|
||||||
|
@ -35,6 +35,10 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-aop</artifactId>
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-security</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- 引入 redis 依赖 -->
|
<!-- 引入 redis 依赖 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -109,7 +113,21 @@
|
|||||||
<!-- <version>2.0.23</version>-->
|
<!-- <version>2.0.23</version>-->
|
||||||
<!--</dependency>-->
|
<!--</dependency>-->
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.auth0</groupId>
|
||||||
|
<artifactId>java-jwt</artifactId>
|
||||||
|
<version>4.4.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpmime</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.sf.json-lib</groupId>
|
||||||
|
<artifactId>json-lib</artifactId>
|
||||||
|
<version>2.4</version>
|
||||||
|
<classifier>jdk15</classifier>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
@ -32,6 +32,7 @@ public enum ResponseStatusEnum {
|
|||||||
FILE_NOT_EXIST_ERROR(514,false,"你所查看的文件不存在!"),
|
FILE_NOT_EXIST_ERROR(514,false,"你所查看的文件不存在!"),
|
||||||
USER_STATUS_ERROR(515,false,"用户状态参数出错!"),
|
USER_STATUS_ERROR(515,false,"用户状态参数出错!"),
|
||||||
USER_NOT_EXIST_ERROR(516,false,"用户不存在!"),
|
USER_NOT_EXIST_ERROR(516,false,"用户不存在!"),
|
||||||
|
USER_PASSWORD_ERROR(517,false,"密码错误!"),
|
||||||
|
|
||||||
// 自定义系统级别异常 54x
|
// 自定义系统级别异常 54x
|
||||||
SYSTEM_INDEX_OUT_OF_BOUNDS(541, false, "系统错误,数组越界!"),
|
SYSTEM_INDEX_OUT_OF_BOUNDS(541, false, "系统错误,数组越界!"),
|
||||||
|
442
book-common/src/main/java/com/imooc/utils/CommonUtil.java
Normal file
442
book-common/src/main/java/com/imooc/utils/CommonUtil.java
Normal file
@ -0,0 +1,442 @@
|
|||||||
|
package com.imooc.utils;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import com.google.common.base.Joiner;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通用工具类
|
||||||
|
*
|
||||||
|
* @version 1.0.0
|
||||||
|
* @author Leon
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public final class CommonUtil {
|
||||||
|
|
||||||
|
private CommonUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断数组是否为空
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static <T> boolean isEmpty(T[] array) {
|
||||||
|
return array == null || (array != null && array.length == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断集合是否为空
|
||||||
|
*
|
||||||
|
* @param collection
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static <T> boolean isEmpty(Collection<T> collection) {
|
||||||
|
return collection == null || collection.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断映射是否为空
|
||||||
|
*
|
||||||
|
* @param map
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static <K, V> boolean isEmpty(Map<K, V> map) {
|
||||||
|
return map == null || map.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断文件是否为空
|
||||||
|
*
|
||||||
|
* @param file
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean isEmpty(MultipartFile file) {
|
||||||
|
return file == null || file.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断引用类型是否为空
|
||||||
|
*
|
||||||
|
* @param <T>
|
||||||
|
* @param value
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static <T> boolean isEmpty(T val) {
|
||||||
|
return Optional.ofNullable(val).map(t -> false).orElseGet(() -> true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得线程安全的List
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static <T> List<T> syncList(List<T> list) {
|
||||||
|
if (isEmpty(list)) {
|
||||||
|
return Collections.synchronizedList(Lists.newArrayList());
|
||||||
|
}
|
||||||
|
return Collections.synchronizedList(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得线程安全的Set
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static <T> Set<T> syncSet(Set<T> set) {
|
||||||
|
if (isEmpty(set)) {
|
||||||
|
return Collections.synchronizedSet(Sets.newHashSet());
|
||||||
|
}
|
||||||
|
return Collections.synchronizedSet(set);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得线程安全的Map
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static <K, V> Map<K, V> syncMap(Map<K, V> map) {
|
||||||
|
if (isEmpty(map)) {
|
||||||
|
return new ConcurrentHashMap<>();
|
||||||
|
}
|
||||||
|
return Collections.synchronizedMap(map);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断与目标值是否相等
|
||||||
|
*
|
||||||
|
* @param value
|
||||||
|
* @param target
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean isEquals(Short val, short target) {
|
||||||
|
return Optional.ofNullable(val).map(t -> t.shortValue() == target).orElseGet(() -> false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断与目标值是否相等
|
||||||
|
*
|
||||||
|
* @param value
|
||||||
|
* @param target
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean isEquals(Integer val, int target) {
|
||||||
|
return Optional.ofNullable(val).map(t -> t.intValue() == target).orElseGet(() -> false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断与目标值是否相等
|
||||||
|
*
|
||||||
|
* @param value
|
||||||
|
* @param target
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean isEquals(Long val, long target) {
|
||||||
|
return Optional.ofNullable(val).map(t -> t.longValue() == target).orElseGet(() -> false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断与目标值是否相等
|
||||||
|
*
|
||||||
|
* @param value
|
||||||
|
* @param target
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean isEquals(Float val, float target) {
|
||||||
|
return Optional.ofNullable(val).map(t -> t.floatValue() == target).orElseGet(() -> false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断与目标值是否相等
|
||||||
|
*
|
||||||
|
* @param value
|
||||||
|
* @param target
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean isEquals(Double val, double target) {
|
||||||
|
return Optional.ofNullable(val).map(t -> t.doubleValue() == target).orElseGet(() -> false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断与目标值是否相等
|
||||||
|
*
|
||||||
|
* @param value
|
||||||
|
* @param target
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean isEquals(Boolean val, boolean target) {
|
||||||
|
return Optional.ofNullable(val).map(t -> t.booleanValue() == target).orElseGet(() -> false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取map中的值,null值转换为空字符串
|
||||||
|
*
|
||||||
|
* @param map
|
||||||
|
* @param key
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String getValue(Map<String, Object> map, String key) {
|
||||||
|
if (isEmpty(map)) {
|
||||||
|
return StringUtils.EMPTY;
|
||||||
|
}
|
||||||
|
return Optional.ofNullable(map.get(key)).map(t -> StringUtils.trim(t.toString()))
|
||||||
|
.orElseGet(() -> StringUtils.EMPTY);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* String转int类型
|
||||||
|
*
|
||||||
|
* @param val
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int getIntValue(String val) {
|
||||||
|
if (StringUtils.isNotBlank(val)) {
|
||||||
|
return Integer.parseInt(StringUtils.trim(val));
|
||||||
|
}
|
||||||
|
throw new NumberFormatException("String转int异常");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Integer类型转int类型
|
||||||
|
*
|
||||||
|
* @param val
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int getIntValue(Integer val) {
|
||||||
|
return Optional.ofNullable(val).map(t -> t.intValue())
|
||||||
|
.orElseThrow(() -> new NumberFormatException("Integer类型转int异常"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* String转double类型
|
||||||
|
*
|
||||||
|
* @param val
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static double getDoubleValue(String val) {
|
||||||
|
if (StringUtils.isNotBlank(val)) {
|
||||||
|
return Double.parseDouble(StringUtils.trim(val));
|
||||||
|
}
|
||||||
|
throw new NumberFormatException("String转double异常");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Double类型转double类型
|
||||||
|
*
|
||||||
|
* @param val
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static double getDoubleValue(Double val) {
|
||||||
|
return Optional.ofNullable(val).map(t -> t.doubleValue())
|
||||||
|
.orElseThrow(() -> new NumberFormatException("Double类型转double异常"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* String转float类型
|
||||||
|
*
|
||||||
|
* @param val
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static float getFloatValue(String val) {
|
||||||
|
if (StringUtils.isNotBlank(val)) {
|
||||||
|
return Float.parseFloat(StringUtils.trim(val));
|
||||||
|
}
|
||||||
|
throw new NumberFormatException("String转float异常");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Float类型转float类型
|
||||||
|
*
|
||||||
|
* @param val
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static float getFloatValue(Float val) {
|
||||||
|
return Optional.ofNullable(val).map(t -> t.floatValue())
|
||||||
|
.orElseThrow(() -> new NumberFormatException("Float类型转float异常"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* String转long类型
|
||||||
|
*
|
||||||
|
* @param val
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static long getLongValue(String val) {
|
||||||
|
if (StringUtils.isNotBlank(val)) {
|
||||||
|
return Long.parseLong(StringUtils.trim(val));
|
||||||
|
}
|
||||||
|
throw new NumberFormatException("String转long异常");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Long类型转long类型
|
||||||
|
*
|
||||||
|
* @param val
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static long getLongValue(Long val) {
|
||||||
|
return Optional.ofNullable(val).map(t -> t.longValue())
|
||||||
|
.orElseThrow(() -> new NumberFormatException("Long类型转long异常"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* String转byte数组
|
||||||
|
*
|
||||||
|
* @param val 字符串
|
||||||
|
* @param cs
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static byte[] stringToBytes(String val, Charset charset) {
|
||||||
|
if (StringUtils.isBlank(val)) {
|
||||||
|
return new byte[0];
|
||||||
|
}
|
||||||
|
return Optional.ofNullable(charset).map(t -> val.getBytes(t)).orElseGet(() -> val.getBytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* byte数组转String
|
||||||
|
*
|
||||||
|
* @param bytes
|
||||||
|
* @param cs
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String bytesToString(byte[] bytes, Charset charset) {
|
||||||
|
if (bytes == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return Optional.ofNullable(charset).map(t -> new String(bytes, t)).orElseGet(() -> new String(bytes));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List转数组
|
||||||
|
*
|
||||||
|
* @param list
|
||||||
|
* @param t
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static <T> T[] listToArray(List<T> list, T[] t) {
|
||||||
|
if (isEmpty(list)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return list.toArray(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set转数组
|
||||||
|
*
|
||||||
|
* @param set
|
||||||
|
* @param t
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static <T> T[] setToArray(Set<T> set, T[] t) {
|
||||||
|
if (isEmpty(set)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return set.toArray(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set转List
|
||||||
|
*
|
||||||
|
* @param set
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static <T> List<T> setToList(Set<T> set) {
|
||||||
|
if (isEmpty(set)) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
return set.stream().collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数组转List
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static <T> List<T> arrayToList(T[] array) {
|
||||||
|
if (isEmpty(array)) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
return Arrays.stream(array).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List转Set
|
||||||
|
*
|
||||||
|
* @param list
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static <T> Set<T> listToSet(List<T> list) {
|
||||||
|
if (isEmpty(list)) {
|
||||||
|
return Collections.emptySet();
|
||||||
|
}
|
||||||
|
return list.stream().collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数组转Set
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static <T> Set<T> arrayToSet(T[] array) {
|
||||||
|
if (isEmpty(array)) {
|
||||||
|
return Collections.emptySet();
|
||||||
|
}
|
||||||
|
return Arrays.stream(array).collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 拼接字符串
|
||||||
|
*
|
||||||
|
* @param list
|
||||||
|
* @param regex
|
||||||
|
* @param parallel
|
||||||
|
* @param function
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static <T> String concatString(Collection<T> list, String regex, boolean parallel, Function<T, String> fn) {
|
||||||
|
if (isEmpty(list)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (StringUtils.isEmpty(regex)) {
|
||||||
|
regex = ",";
|
||||||
|
}
|
||||||
|
Stream<T> stream = parallel ? list.parallelStream() : list.stream();
|
||||||
|
if (fn != null) {
|
||||||
|
return stream.map(t -> fn.apply(t)).collect(Collectors.joining(regex));
|
||||||
|
}
|
||||||
|
return stream.map(String::valueOf).collect(Collectors.joining(regex));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 拼接url参数字符串
|
||||||
|
*
|
||||||
|
* @param paramMap
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String getUrlParamStr(Map<String, String> paramMap) {
|
||||||
|
return isEmpty(paramMap) ? "" : Joiner.on("&").withKeyValueSeparator("=").join(paramMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getSimpleUUID() {
|
||||||
|
return UUID.randomUUID().toString().replaceAll("-", "");
|
||||||
|
}
|
||||||
|
}
|
24
book-common/src/main/java/com/imooc/utils/GsonUtil.java
Normal file
24
book-common/src/main/java/com/imooc/utils/GsonUtil.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package com.imooc.utils;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
|
||||||
|
public class GsonUtil {
|
||||||
|
|
||||||
|
private static Gson gson = null;
|
||||||
|
|
||||||
|
private GsonUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
|
static {
|
||||||
|
gson = new GsonBuilder().create();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String beanToJson(Object value) {
|
||||||
|
return gson.toJson(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T jsonToBean(String content, Class<T> valueType) {
|
||||||
|
return gson.fromJson(content, valueType);
|
||||||
|
}
|
||||||
|
}
|
30
book-common/src/main/java/com/imooc/utils/JWTUtils.java
Normal file
30
book-common/src/main/java/com/imooc/utils/JWTUtils.java
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package com.imooc.utils;
|
||||||
|
|
||||||
|
import com.auth0.jwt.JWT;
|
||||||
|
import com.auth0.jwt.algorithms.Algorithm;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 基于JWT创建Token.
|
||||||
|
*
|
||||||
|
* @author lzc
|
||||||
|
* @version 1.0
|
||||||
|
*/
|
||||||
|
public class JWTUtils {
|
||||||
|
|
||||||
|
public static String createJWT(String userId, String password) {
|
||||||
|
Map<String, Object> headers = new HashMap<>();
|
||||||
|
Date issureDate = new Date();
|
||||||
|
Date expireDate = new Date(issureDate.getTime() + 3600 * 24 * 1000);
|
||||||
|
headers.put("typ", "JWT");
|
||||||
|
headers.put("alg", "HS256");
|
||||||
|
return JWT.create().withHeader(headers) // 头部信息
|
||||||
|
.withIssuer("Lzc") // 签发人
|
||||||
|
.withClaim("userId", userId) // 中间部分
|
||||||
|
.withIssuedAt(issureDate) // 签发时间
|
||||||
|
.withExpiresAt(expireDate) // 过期时间
|
||||||
|
.sign(Algorithm.HMAC256(password)); // 尾部
|
||||||
|
}
|
||||||
|
}
|
212
book-common/src/main/java/com/imooc/utils/RestTemplateUtil.java
Normal file
212
book-common/src/main/java/com/imooc/utils/RestTemplateUtil.java
Normal file
@ -0,0 +1,212 @@
|
|||||||
|
package com.imooc.utils;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.MultiValueMap;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
import net.sf.json.JSONObject;
|
||||||
|
import static com.imooc.utils.CommonUtil.isEmpty;
|
||||||
|
import static com.imooc.utils.CommonUtil.getUrlParamStr;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RestTemplate工具类
|
||||||
|
*
|
||||||
|
* @version 1.0.0
|
||||||
|
* @author Leon
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public final class RestTemplateUtil {
|
||||||
|
|
||||||
|
private static RestTemplate TEMPLATE;
|
||||||
|
|
||||||
|
private static RestTemplate TEMPLATE_IGNORE_SSL;
|
||||||
|
|
||||||
|
@Resource(name = "restTemplate")
|
||||||
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Resource(name = "restTemplateIgnoreSSL")
|
||||||
|
private RestTemplate restTemplateIgnoreSSL;
|
||||||
|
|
||||||
|
private RestTemplateUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
private void init() {
|
||||||
|
TEMPLATE = this.restTemplate;
|
||||||
|
TEMPLATE_IGNORE_SSL = this.restTemplateIgnoreSSL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GET请求
|
||||||
|
*
|
||||||
|
* @param <T>
|
||||||
|
* @param mediaType
|
||||||
|
* @param headerHandler
|
||||||
|
* @param headerData
|
||||||
|
* @param url
|
||||||
|
* @param param
|
||||||
|
* @param valueType
|
||||||
|
* @param responseHandler
|
||||||
|
*/
|
||||||
|
public static <T> void get(MediaType mediaType, Consumer<HttpHeaders> headerHandler, Map<String, String> headerData,
|
||||||
|
String url, Map<String, String> param, Class<T> valueType, Consumer<ResponseEntity<T>> responseHandler) {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
Optional.ofNullable(mediaType).ifPresent(t -> headers.setContentType(t));
|
||||||
|
// 请求头信息
|
||||||
|
buildHttpHeaders(headers, headerHandler, headerData);
|
||||||
|
// 构建请求
|
||||||
|
HttpEntity<T> entity = new HttpEntity<>(headers);
|
||||||
|
ResponseEntity<T> responseEntity = buildRestTemplate(url).exchange(buildUrl(url, param), HttpMethod.GET, entity,
|
||||||
|
valueType);
|
||||||
|
// 处理响应
|
||||||
|
responseHandler(responseEntity, responseHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* POST请求
|
||||||
|
*
|
||||||
|
* @param <T>
|
||||||
|
* @param mediaType
|
||||||
|
* @param headerHandler
|
||||||
|
* @param headerData
|
||||||
|
* @param url
|
||||||
|
* @param param
|
||||||
|
* @param valueType
|
||||||
|
* @param responseHandler
|
||||||
|
*/
|
||||||
|
public static <T> void post(MediaType mediaType, Consumer<HttpHeaders> headerHandler,
|
||||||
|
Map<String, String> headerData, String url, JSONObject param, Class<T> valueType,
|
||||||
|
Consumer<ResponseEntity<T>> responseHandler) {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
if (mediaType == null) {
|
||||||
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||||
|
} else {
|
||||||
|
headers.setContentType(mediaType);
|
||||||
|
}
|
||||||
|
// 请求头信息
|
||||||
|
buildHttpHeaders(headers, headerHandler, headerData);
|
||||||
|
// 构建请求
|
||||||
|
HttpEntity<JSONObject> entity = new HttpEntity<>(param, headers);
|
||||||
|
ResponseEntity<T> responseEntity = buildRestTemplate(url).exchange(url, HttpMethod.POST, entity, valueType);
|
||||||
|
// 处理响应
|
||||||
|
responseHandler(responseEntity, responseHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* POST请求
|
||||||
|
*
|
||||||
|
* @param <T>
|
||||||
|
* @param mediaType
|
||||||
|
* @param headerHandler
|
||||||
|
* @param headerData
|
||||||
|
* @param url
|
||||||
|
* @param param
|
||||||
|
* @param valueType
|
||||||
|
* @param responseHandler
|
||||||
|
*/
|
||||||
|
public static <T> void post(MediaType mediaType, Consumer<HttpHeaders> headerHandler,
|
||||||
|
Map<String, String> headerData, String url, MultiValueMap<String, Object> param, Class<T> valueType,
|
||||||
|
Consumer<ResponseEntity<T>> responseHandler) {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
if (mediaType == null) {
|
||||||
|
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||||
|
} else {
|
||||||
|
headers.setContentType(mediaType);
|
||||||
|
}
|
||||||
|
// 请求头信息
|
||||||
|
buildHttpHeaders(headers, headerHandler, headerData);
|
||||||
|
// 构建请求
|
||||||
|
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(param, headers);
|
||||||
|
ResponseEntity<T> responseEntity = buildRestTemplate(url).postForEntity(url, entity, valueType);
|
||||||
|
// 处理响应
|
||||||
|
responseHandler(responseEntity, responseHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PUT请求
|
||||||
|
*
|
||||||
|
* @param <T>
|
||||||
|
* @param headerHandler
|
||||||
|
* @param headerData
|
||||||
|
* @param url
|
||||||
|
* @param jsonBody
|
||||||
|
* @param valueType
|
||||||
|
* @param responseHandler
|
||||||
|
*/
|
||||||
|
public static <T> void put(Consumer<HttpHeaders> headerHandler, Map<String, String> headerData, String url,
|
||||||
|
String jsonBody, Class<T> valueType, Consumer<ResponseEntity<T>> responseHandler) {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||||
|
// 请求头信息
|
||||||
|
buildHttpHeaders(headers, headerHandler, headerData);
|
||||||
|
// 构建请求
|
||||||
|
HttpEntity<String> entity = new HttpEntity<>(jsonBody, headers);
|
||||||
|
ResponseEntity<T> responseEntity = buildRestTemplate(url).exchange(url, HttpMethod.PUT, entity, valueType);
|
||||||
|
// 处理响应
|
||||||
|
responseHandler(responseEntity, responseHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建请求模板
|
||||||
|
*
|
||||||
|
* @param ssl
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static RestTemplate buildRestTemplate(String url) {
|
||||||
|
boolean ssl = StringUtils.startsWithIgnoreCase(url, "https");
|
||||||
|
return ssl ? TEMPLATE_IGNORE_SSL : TEMPLATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建请求头信息
|
||||||
|
*
|
||||||
|
* @param headers
|
||||||
|
* @param headerHandler
|
||||||
|
* @param headerData
|
||||||
|
*/
|
||||||
|
private static void buildHttpHeaders(HttpHeaders headers, Consumer<HttpHeaders> headerHandler,
|
||||||
|
Map<String, String> headerData) {
|
||||||
|
Optional.ofNullable(headerHandler).ifPresent(t -> t.accept(headers));
|
||||||
|
if (!isEmpty(headerData)) {
|
||||||
|
headerData.forEach((k, v) -> headers.add(k, v));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建请求url
|
||||||
|
*
|
||||||
|
* @param url
|
||||||
|
* @param param
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static String buildUrl(String url, Map<String, String> param) {
|
||||||
|
if (isEmpty(param)) {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
String paramStr = getUrlParamStr(param);
|
||||||
|
return url + "?" + paramStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 响应处理
|
||||||
|
*
|
||||||
|
* @param <T>
|
||||||
|
* @param responseEntity
|
||||||
|
* @param responseHandler
|
||||||
|
*/
|
||||||
|
private static <T> void responseHandler(ResponseEntity<T> responseEntity,
|
||||||
|
Consumer<ResponseEntity<T>> responseHandler) {
|
||||||
|
Optional.ofNullable(responseHandler).ifPresent(t -> t.accept(responseEntity));
|
||||||
|
}
|
||||||
|
}
|
@ -7,7 +7,6 @@ import lombok.ToString;
|
|||||||
import org.springframework.data.annotation.Id;
|
import org.springframework.data.annotation.Id;
|
||||||
import org.springframework.data.mongodb.core.mapping.Document;
|
import org.springframework.data.mongodb.core.mapping.Document;
|
||||||
import org.springframework.data.mongodb.core.mapping.Field;
|
import org.springframework.data.mongodb.core.mapping.Field;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
16
book-model/src/main/java/com/imooc/mo/Token.java
Normal file
16
book-model/src/main/java/com/imooc/mo/Token.java
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package com.imooc.mo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class Token {
|
||||||
|
/**
|
||||||
|
* 访问token
|
||||||
|
*/
|
||||||
|
private String accessToken;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 刷新token
|
||||||
|
*/
|
||||||
|
private String refreshToken;
|
||||||
|
}
|
@ -2,8 +2,10 @@ package com.imooc.pojo;
|
|||||||
|
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Table;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Table(name="t_comment")
|
||||||
public class Comment {
|
public class Comment {
|
||||||
@Id
|
@Id
|
||||||
private String id;
|
private String id;
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
package com.imooc.pojo;
|
package com.imooc.pojo;
|
||||||
|
|
||||||
import org.springframework.data.annotation.Id;
|
import org.springframework.data.annotation.Id;
|
||||||
|
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
|
@Table(name = "t_fans")
|
||||||
public class Fans {
|
public class Fans {
|
||||||
@Id
|
@Id
|
||||||
private String id;
|
private String id;
|
||||||
@ -79,7 +80,8 @@ public class Fans {
|
|||||||
/**
|
/**
|
||||||
* 获取粉丝是否是vloger的朋友,如果成为朋友,则本表的双方此字段都需要设置为1,如果有一人取关,则两边都需要设置为0
|
* 获取粉丝是否是vloger的朋友,如果成为朋友,则本表的双方此字段都需要设置为1,如果有一人取关,则两边都需要设置为0
|
||||||
*
|
*
|
||||||
* @return is_fan_friend_of_mine - 粉丝是否是vloger的朋友,如果成为朋友,则本表的双方此字段都需要设置为1,如果有一人取关,则两边都需要设置为0
|
* @return is_fan_friend_of_mine -
|
||||||
|
* 粉丝是否是vloger的朋友,如果成为朋友,则本表的双方此字段都需要设置为1,如果有一人取关,则两边都需要设置为0
|
||||||
*/
|
*/
|
||||||
public Integer getIsFanFriendOfMine() {
|
public Integer getIsFanFriendOfMine() {
|
||||||
return isFanFriendOfMine;
|
return isFanFriendOfMine;
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
package com.imooc.pojo;
|
package com.imooc.pojo;
|
||||||
|
|
||||||
import org.springframework.data.annotation.Id;
|
import org.springframework.data.annotation.Id;
|
||||||
|
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
@Table(name = "my_liked_vlog")
|
@Table(name = "t_my_liked_vlog")
|
||||||
public class MyLikedVlog {
|
public class MyLikedVlog {
|
||||||
@Id
|
@Id
|
||||||
private String id;
|
private String id;
|
||||||
|
@ -2,8 +2,10 @@ package com.imooc.pojo;
|
|||||||
|
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Table;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Table(name="t_users")
|
||||||
public class Users {
|
public class Users {
|
||||||
@Id
|
@Id
|
||||||
private String id;
|
private String id;
|
||||||
|
@ -2,8 +2,10 @@ package com.imooc.pojo;
|
|||||||
|
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Table;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Table(name="t_vlog")
|
||||||
public class Vlog {
|
public class Vlog {
|
||||||
@Id
|
@Id
|
||||||
private String id;
|
private String id;
|
||||||
|
@ -5,11 +5,10 @@ import lombok.Data;
|
|||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
import javax.persistence.Column;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
import javax.validation.constraints.NotBlank;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
import com.imooc.mo.Token;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@ -34,9 +33,10 @@ public class UsersVO {
|
|||||||
|
|
||||||
private String userToken; // 用户token,传递给前端
|
private String userToken; // 用户token,传递给前端
|
||||||
|
|
||||||
private Integer myFollowsCounts; //我关注的
|
private Integer myFollowsCounts; // 我关注的
|
||||||
private Integer myFansCounts; //我的粉丝数量
|
private Integer myFansCounts; // 我的粉丝数量
|
||||||
//private Integer myLikedVlogCounts;
|
// private Integer myLikedVlogCounts;
|
||||||
private Integer totalLikeMeCounts;
|
private Integer totalLikeMeCounts;
|
||||||
|
private Token shopToken;
|
||||||
|
|
||||||
}
|
}
|
@ -1,6 +1,8 @@
|
|||||||
package com.imooc.service;
|
package com.imooc.service;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
import com.imooc.bo.UpdatedUserBO;
|
import com.imooc.bo.UpdatedUserBO;
|
||||||
|
import com.imooc.mo.Token;
|
||||||
import com.imooc.pojo.Users;
|
import com.imooc.pojo.Users;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -14,7 +16,6 @@ public interface UserService {
|
|||||||
*/
|
*/
|
||||||
public Users queryMobileIsExist(String mobile);
|
public Users queryMobileIsExist(String mobile);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建用户信息,并且返回用户对象
|
* 创建用户信息,并且返回用户对象
|
||||||
*/
|
*/
|
||||||
@ -35,4 +36,19 @@ public interface UserService {
|
|||||||
*/
|
*/
|
||||||
public Users updateUserInfo(UpdatedUserBO updatedUserBO, Integer type);
|
public Users updateUserInfo(UpdatedUserBO updatedUserBO, Integer type);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建用户打通短视频和商城系统
|
||||||
|
*
|
||||||
|
* @param mobile
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Map<String, Object> createUserNew(String mobile);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取商城token
|
||||||
|
*
|
||||||
|
* @param userId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Token getShopToken(String userId);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.imooc.service.impl;
|
package com.imooc.service.impl;
|
||||||
|
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
import com.imooc.bo.UpdatedUserBO;
|
import com.imooc.bo.UpdatedUserBO;
|
||||||
import com.imooc.enums.Sex;
|
import com.imooc.enums.Sex;
|
||||||
import com.imooc.enums.UserInfoModifyType;
|
import com.imooc.enums.UserInfoModifyType;
|
||||||
@ -7,21 +8,27 @@ import com.imooc.enums.YesOrNo;
|
|||||||
import com.imooc.exceptions.GraceException;
|
import com.imooc.exceptions.GraceException;
|
||||||
import com.imooc.grace.result.ResponseStatusEnum;
|
import com.imooc.grace.result.ResponseStatusEnum;
|
||||||
import com.imooc.mapper.UsersMapper;
|
import com.imooc.mapper.UsersMapper;
|
||||||
|
import com.imooc.mo.Token;
|
||||||
import com.imooc.pojo.Users;
|
import com.imooc.pojo.Users;
|
||||||
import com.imooc.service.UserService;
|
import com.imooc.service.UserService;
|
||||||
import com.imooc.utils.DateUtil;
|
import com.imooc.utils.DateUtil;
|
||||||
import com.imooc.utils.DesensitizationUtil;
|
import com.imooc.utils.DesensitizationUtil;
|
||||||
|
import com.imooc.utils.GsonUtil;
|
||||||
|
import com.imooc.utils.RestTemplateUtil;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.n3r.idworker.Sid;
|
import org.n3r.idworker.Sid;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
|
import org.springframework.util.MultiValueMap;
|
||||||
import tk.mybatis.mapper.entity.Example;
|
import tk.mybatis.mapper.entity.Example;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author vercen
|
* @author vercen
|
||||||
@ -33,6 +40,8 @@ public class UserServiceImpl implements UserService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private UsersMapper usersMapper;
|
private UsersMapper usersMapper;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private Sid sid;
|
private Sid sid;
|
||||||
@ -125,4 +134,82 @@ public class UserServiceImpl implements UserService {
|
|||||||
return updateUserInfo(updatedUserBO);
|
return updateUserInfo(updatedUserBO);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional(rollbackFor = RuntimeException.class)
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> createUserNew(String mobile) {
|
||||||
|
// 获得全局唯一主键
|
||||||
|
String userId = sid.nextShort();
|
||||||
|
// 创建短视频系统账号
|
||||||
|
Users user = new Users();
|
||||||
|
user.setId(userId);
|
||||||
|
user.setPassword(passwordEncoder.encode(mobile));
|
||||||
|
user.setMobile(mobile);
|
||||||
|
user.setNickname("用户:" + DesensitizationUtil.commonDisplay(mobile));
|
||||||
|
user.setImoocNum("用户:" + DesensitizationUtil.commonDisplay(mobile));
|
||||||
|
user.setFace(USER_FACE1);
|
||||||
|
user.setBirthday(DateUtil.stringToDate("1999-01-01"));
|
||||||
|
user.setSex(Sex.secret.type);
|
||||||
|
user.setBgImg("http://82.156.121.2:29000/bucket/IMG20241212102441.jpg");
|
||||||
|
user.setCountry("中国");
|
||||||
|
user.setProvince("");
|
||||||
|
user.setCity("");
|
||||||
|
user.setDistrict("");
|
||||||
|
user.setDescription("这家伙很懒,什么都没留下~");
|
||||||
|
user.setBgImg("");
|
||||||
|
user.setCanImoocNumBeUpdated(YesOrNo.YES.type);
|
||||||
|
user.setCreatedTime(new Date());
|
||||||
|
user.setUpdatedTime(new Date());
|
||||||
|
|
||||||
|
int row = usersMapper.insert(user);
|
||||||
|
|
||||||
|
if (row > 0) {
|
||||||
|
Map<String, Object> result = Maps.newHashMap();
|
||||||
|
result.put("user", user);
|
||||||
|
// 创建商城系统账号,获取商城系统token
|
||||||
|
String url = "http://localhost:8888/buyer/passport/member/registerFromTik";
|
||||||
|
MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
|
||||||
|
param.add("userName", mobile);
|
||||||
|
param.add("password", mobile);
|
||||||
|
param.add("mobilePhone", mobile);
|
||||||
|
param.add("userId", user.getId());
|
||||||
|
|
||||||
|
RestTemplateUtil.post(null, null, null, url, param, String.class, t -> {
|
||||||
|
if (t.getStatusCode() == HttpStatus.OK) {
|
||||||
|
String content = t.getBody();
|
||||||
|
if (StringUtils.isNotBlank(content)) {
|
||||||
|
Token token = GsonUtil.jsonToBean(content, Token.class);
|
||||||
|
result.put("token", token);
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("账号注册异常");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("账号注册异常");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Token getShopToken(String userId) {
|
||||||
|
Token[] tokens = { null };
|
||||||
|
String url = "http://localhost:8888/buyer/passport/member/getTokenFromTik";
|
||||||
|
MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
|
||||||
|
param.add("userId", userId);
|
||||||
|
RestTemplateUtil.post(null, null, null, url, param, String.class, t -> {
|
||||||
|
if (t.getStatusCode() == HttpStatus.OK) {
|
||||||
|
String content = t.getBody();
|
||||||
|
if (StringUtils.isNotBlank(content)) {
|
||||||
|
tokens[0] = GsonUtil.jsonToBean(content, Token.class);
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("账号注册异常");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("账号注册异常");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return tokens[0];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user