Commit e90964d0 authored by xuxin's avatar xuxin

浏览器bug修复

parent 7d5c94f5
package com.edgec.browserbackend;
import com.mongodb.MongoClient;
import net.javacrumbs.shedlock.core.LockProvider;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.Jsr310Converters;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.provider.token.AuthenticationKeyGenerator;
import org.springframework.security.oauth2.provider.token.DefaultAuthenticationKeyGenerator;
import org.springframework.web.cors.CorsConfiguration;
......@@ -35,10 +24,8 @@ import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@SpringBootApplication
@EnableGlobalMethodSecurity(prePostEnabled = true)
......@@ -134,7 +121,7 @@ public class BrowserBackendApplication {
return new MongoCustomConversions(converters);
}
enum LocalDateToStringConverter implements Converter<LocalDate, String> {
enum LocalDateToStringConverter implements Converter<LocalDate, String> {
INSTANCE;
......@@ -144,7 +131,7 @@ public class BrowserBackendApplication {
}
}
enum StringToLocalDateConverter implements Converter<String, LocalDate> {
enum StringToLocalDateConverter implements Converter<String, LocalDate> {
INSTANCE;
......@@ -174,7 +161,7 @@ public class BrowserBackendApplication {
}
}
}
}
}
......@@ -33,7 +33,7 @@ import java.time.format.DateTimeFormatter;
import java.util.*;
@RestController
@RequestMapping("user")
@RequestMapping("/user")
public class AccountController {
public static final String WECHAT_PAY_CALLBACK_URL = "https://cloudam.cn/accounts/0xwxpaycallback/";
......
......@@ -7,37 +7,37 @@ public enum AccountErrorCode implements ErrorCode {
/* client errors */
UNKNOWN(ErrorCode.COMMON_UNKNOWN,"unknown"),
NAMEEXIST(ACCOUNT_BASE+100, "Username exists"),
NAMENOTEXIST(ACCOUNT_BASE+102, "Username does not exist"),
EMAILEXIST(ACCOUNT_BASE+101, "Email exists"),
EMAILNOTEXIST(ACCOUNT_BASE+103, "Email does not exist"),
NAMEOREMAILNOTEXIST(ACCOUNT_BASE+104, "Username or Email does not exist"),
NOTALLOWEDTOCREATESUBUSER(ACCOUNT_BASE+105, "The account isn't allowed to create sub user"),
PHONEEXIST(ACCOUNT_BASE+106, "Phone number exists"),
OTPWRONG(ACCOUNT_BASE+107, "otp invalid or expires"),
OTPSENDFAILS(ACCOUNT_BASE+108, "otp sends failure"),
PHONENOTEXIST(ACCOUNT_BASE+109, "Can not find account with phone number"),
UNKNOWN(ErrorCode.COMMON_UNKNOWN, "unknown"),
NAMEEXIST(ACCOUNT_BASE + 100, "Username exists"),
NAMENOTEXIST(ACCOUNT_BASE + 102, "Username does not exist"),
EMAILEXIST(ACCOUNT_BASE + 101, "Email exists"),
EMAILNOTEXIST(ACCOUNT_BASE + 103, "Email does not exist"),
NAMEOREMAILNOTEXIST(ACCOUNT_BASE + 104, "Username or Email does not exist"),
NOTALLOWEDTOCREATESUBUSER(ACCOUNT_BASE + 105, "The account isn't allowed to create sub user"),
PHONEEXIST(ACCOUNT_BASE + 106, "Phone number exists"),
OTPWRONG(ACCOUNT_BASE + 107, "otp invalid or expires"),
OTPSENDFAILS(ACCOUNT_BASE + 108, "otp sends failure"),
PHONENOTEXIST(ACCOUNT_BASE + 109, "Can not find account with phone number"),
INVALIDTOKEN(ErrorCode.COMMON_INVALID_TOKEN, "Invalid token"),
UNAUTHORIZED(ErrorCode.COMMON_UNAUTHRORIZED, "Unauthorized operation"),
NAMEEMPTY(ACCOUNT_BASE+110, "Username connot be empty"),
EMAILEMPTY(ACCOUNT_BASE+111, "Email connot be empty"),
PHONEEMPTY(ACCOUNT_BASE+112, "PhoneNumber connot be empty"),
AGENCYNOEXIST(ACCOUNT_BASE+113, "uuid dose not exists"),
NAMEEMPTY(ACCOUNT_BASE + 110, "Username connot be empty"),
EMAILEMPTY(ACCOUNT_BASE + 111, "Email connot be empty"),
PHONEEMPTY(ACCOUNT_BASE + 112, "PhoneNumber connot be empty"),
AGENCYNOEXIST(ACCOUNT_BASE + 113, "uuid dose not exists"),
CHILDSCALINGSTATEUNABATED(ACCOUNT_BASE+114, "Can't delete child whose still have scaling group "),
CHILDSCALINGSTATEUNABATED(ACCOUNT_BASE + 114, "Can't delete child whose still have scaling group "),
BINDORDERERROR_NOTEXIST(ACCOUNT_BASE+115,"This order is not exist"),
BINDORDERERROR_NOTEXIST(ACCOUNT_BASE + 115, "This order is not exist"),
BINDORDERERROR_BINDBYOTHERS(ACCOUNT_BASE+116,"This order has been bind by others"),
BINDORDERERROR_BINDBYOTHERS(ACCOUNT_BASE + 116, "This order has been bind by others"),
BINDORDERERROR_NOTQUALIFIED(ACCOUNT_BASE+117,"You don't have free qualified"),
BINDORDERERROR_NOTQUALIFIED(ACCOUNT_BASE + 117, "You don't have free qualified"),
BINDORDERERROR_HAVEORDER(ACCOUNT_BASE+118,"You have an unpaid order"),
BINDORDERERROR_HAVEORDER(ACCOUNT_BASE + 118, "You have an unpaid order"),
BINDORDERERROR_NOTKNOW(ACCOUNT_BASE+119,"Order generate error,please try again"),
BINDORDERERROR_NOTKNOW(ACCOUNT_BASE + 119, "Order generate error,please try again"),
/* http request error */
OTHERS(ErrorCode.COMMON_OTHERS, "http request error"),
......@@ -45,19 +45,22 @@ public enum AccountErrorCode implements ErrorCode {
/* server errors */
EMAILSENTERROR(ErrorCode.COMMON_SERVER_ERROR, "email sending error"),
/** wechat errors */
WECHATERROR(ACCOUNT_BASE+120, "Wechat order generation error"),
/**
* wechat errors
*/
WECHATERROR(ACCOUNT_BASE + 120, "Wechat order generation error"),
/** alipay errors */
ALIPAYERROR(ACCOUNT_BASE+130, "Alipay order generation error"),
/**
* alipay errors
*/
ALIPAYERROR(ACCOUNT_BASE + 130, "Alipay order generation error"),
NOPERMISSION(ACCOUNT_BASE+140, "You have no right to do this operarion"),
GROUPMAX(ACCOUNT_BASE+141, "You can not have more groups"),
SHOPMAX(ACCOUNT_BASE+142, "You can not have more shops"),
CHILDMAX(ACCOUNT_BASE+143, "You can not have more subUsers"),
NOTENOUGHBALANCE(ErrorCode.ACCOUNT_BASE+150, "Your balance is not enough");
NOPERMISSION(ACCOUNT_BASE + 140, "You have no right to do this operarion"),
GROUPMAX(ACCOUNT_BASE + 141, "You can not have more groups"),
SHOPMAX(ACCOUNT_BASE + 142, "You can not have more shops"),
CHILDMAX(ACCOUNT_BASE + 143, "You can not have more subUsers"),
NOTENOUGHBALANCE(ErrorCode.ACCOUNT_BASE + 150, "Your balance is not enough");
private final int code;
......@@ -68,10 +71,12 @@ public enum AccountErrorCode implements ErrorCode {
this.reason = reasonPhrase;
}
@Override
public int value() {
return this.code;
}
@Override
@JsonValue
public int getCode() {
return code;
......@@ -82,6 +87,7 @@ public enum AccountErrorCode implements ErrorCode {
return Integer.toString(this.code);
}
@Override
public String getReason() {
return reason;
}
......
package com.edgec.browserbackend.account.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.edgec.browserbackend.account.dto.*;
import com.edgec.browserbackend.account.service.AccountService;
import com.edgec.browserbackend.account.service.PaymentService;
import com.edgec.browserbackend.account.domain.*;
import com.edgec.browserbackend.account.dto.*;
import com.edgec.browserbackend.account.exception.AccountErrorCode;
import com.edgec.browserbackend.account.repository.*;
import com.edgec.browserbackend.account.service.AccountService;
import com.edgec.browserbackend.account.service.EmailService;
import com.edgec.browserbackend.account.service.PaymentService;
import com.edgec.browserbackend.account.service.SmsUtils;
import com.edgec.browserbackend.account.utils.AccountServicePool;
import com.edgec.browserbackend.auth.exception.AuthErrorCode;
......@@ -33,7 +33,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.thymeleaf.util.StringUtils;
import java.io.*;
import java.io.File;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.YearMonth;
......@@ -322,7 +322,8 @@ public class AccountServiceImpl implements AccountService {
@Override
public ResultDto getAccountByName(String name) {
Assert.hasLength(name);
Assert.hasLength(name, "账户名不能为空");
ResultDto resultDto = new ResultDto();
try {
Account account = this.findByName(name);
......@@ -398,6 +399,7 @@ public class AccountServiceImpl implements AccountService {
return resultDto;
}
@Override
public void deleteByName(String name) {
userService.deleteUser(name);
repository.deleteById(name);
......@@ -411,6 +413,7 @@ public class AccountServiceImpl implements AccountService {
return create(user, null);
}
@Override
public void deleteSub(String parent, String child) {
Assert.hasText(parent);
Account childAccount = repository.findByName(child);
......@@ -1177,8 +1180,7 @@ public class AccountServiceImpl implements AccountService {
CompanyAuthorize companyAuthorize = companyAuthorizeRepository.findByUsername(username);
account.setCompanyName(companyAuthorize.getCompanyName());
repository.save(account);
}
else {
} else {
account.setAuthorized(3);
account.setCompanyName(null);
repository.save(account);
......
......@@ -30,16 +30,17 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.csrf().disable();
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers( "/user/authCode", "/user/signUp",
"/user/forgot**", "/0xadministrator/getconfig**");
.antMatchers("/user/authCode", "/user/signUp",
"/user/forgot**", "/0xadministrator/getconfig**");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService)
.passwordEncoder(new BCryptPasswordEncoder());
.passwordEncoder(new BCryptPasswordEncoder());
}
@Override
......
......@@ -8,16 +8,16 @@ public enum AuthErrorCode implements ErrorCode {
/* http request error */
OTHERS(ErrorCode.COMMON_OTHERS, "http request error"),
AUTHENTICATION_ERROR(ErrorCode.AUTH_BASE+100, "authentication failure"),
AUTHORIZATION_ERROR(ErrorCode.AUTH_BASE+200, "authorization failure"),
AUTHENTICATION_ERROR(ErrorCode.AUTH_BASE + 100, "authentication failure"),
AUTHORIZATION_ERROR(ErrorCode.AUTH_BASE + 200, "authorization failure"),
NAMEEXIST(AUTH_BASE+106, "Username exists"),
NAMENOTEXIST(AUTH_BASE+102, "Username does not exist"),
EMAILEXIST(AUTH_BASE+101, "Email exists"),
EMAILNOTEXIST(AUTH_BASE+103, "Email does not exist"),
NAMEOREMAILNOTEXIST(AUTH_BASE+104, "Username or Email does not exist"),
WRONGEMAILCODE(AUTH_BASE+105, "Wrong verification code"),
LOGINTIMESEXCEEDED(AUTH_BASE+107, "Login times exceeded"),
NAMEEXIST(AUTH_BASE + 106, "Username exists"),
NAMENOTEXIST(AUTH_BASE + 102, "Username does not exist"),
EMAILEXIST(AUTH_BASE + 101, "Email exists"),
EMAILNOTEXIST(AUTH_BASE + 103, "Email does not exist"),
NAMEOREMAILNOTEXIST(AUTH_BASE + 104, "Username or Email does not exist"),
WRONGEMAILCODE(AUTH_BASE + 105, "Wrong verification code"),
LOGINTIMESEXCEEDED(AUTH_BASE + 107, "Login times exceeded"),
/* server errors */
INTERNALSERVERERROR(ErrorCode.COMMON_SERVER_ERROR, "internal server error");
......@@ -31,11 +31,13 @@ public enum AuthErrorCode implements ErrorCode {
this.reason = reasonPhrase;
}
@Override
public int value() {
return this.code;
}
@JsonValue
@Override
public int getCode() {
return code;
}
......@@ -45,6 +47,7 @@ public enum AuthErrorCode implements ErrorCode {
return Integer.toString(this.code);
}
@Override
public String getReason() {
return reason;
}
......
......@@ -12,8 +12,6 @@ import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class AuthenticationSuccessEventListener implements ApplicationListener<AuthenticationSuccessEvent> {
......@@ -30,9 +28,9 @@ public class AuthenticationSuccessEventListener implements ApplicationListener<A
User user = userRepository.findById(username).orElse(null);
if (user != null) {
UserAttemptsLogin userAttempts = userAttemptsLoginRepository.findById(username).orElse(null);
if(userAttempts != null) {
if (userAttempts != null) {
if (userAttempts.getAttempts() >= 20) {
if (userAttempts.getDate().getTime() >= (new Date().getTime() - 600000))
if (userAttempts.getDate().getTime() >= (System.currentTimeMillis() - 600000))
throw new ClientRequestException(AuthErrorCode.LOGINTIMESEXCEEDED, "Login times exceeded");
} else if (userAttempts.getAttempts() != 0) {
saveUserAttemptsLogin(username, 0);
......
......@@ -7,20 +7,20 @@ public enum BrowserErrorCode implements ErrorCode {
/* client errors */
UNKNOWN(ErrorCode.COMMON_UNKNOWN,"unknown"),
INFORMATIONNOTCOMPELETE(BROWSER_BASE+100, "The information about shop does not complete"),
SHOPNOTEXIST(BROWSER_BASE+101, "The shop does not exist"),
UNKNOWN(ErrorCode.COMMON_UNKNOWN, "unknown"),
INFORMATIONNOTCOMPELETE(BROWSER_BASE + 100, "The information about shop does not complete"),
SHOPNOTEXIST(BROWSER_BASE + 101, "The shop does not exist"),
SHOP_BINDED(BROWSER_BASE + 102, "The shop has bind with ip"),
IPNOTEXIST(BROWSER_BASE+201, "The ip do not exist"),
IPNOTBINDTOSHOP(BROWSER_BASE+202, "The ip does not bind this shop."),
IPNOTEXIST(BROWSER_BASE + 201, "The ip do not exist"),
IPNOTBINDTOSHOP(BROWSER_BASE + 202, "The ip does not bind this shop."),
GROUPNOTEXIST(BROWSER_BASE+301, "The group does not exist"),
GROUPNOTEXIST(BROWSER_BASE + 301, "The group does not exist"),
IPTRANSACTIONNOTEXIST(BROWSER_BASE+401, "The ipTransaction does not exist"),
COMPANYAUTHORIZEEXIST(BROWSER_BASE+501, "The authority exists"),
COMPANYAUTHORIZENOTEXIST(BROWSER_BASE+502, "The authority dose not exist");
IPTRANSACTIONNOTEXIST(BROWSER_BASE + 401, "The ipTransaction does not exist"),
COMPANYAUTHORIZEEXIST(BROWSER_BASE + 501, "The authority exists"),
COMPANYAUTHORIZENOTEXIST(BROWSER_BASE + 502, "The authority dose not exist");
private final int code;
......@@ -31,11 +31,13 @@ public enum BrowserErrorCode implements ErrorCode {
this.reason = reasonPhrase;
}
@Override
public int value() {
return this.code;
}
@JsonValue
@Override
public int getCode() {
return code;
}
......@@ -45,6 +47,7 @@ public enum BrowserErrorCode implements ErrorCode {
return Integer.toString(this.code);
}
@Override
public String getReason() {
return reason;
}
......
package com.edgec.browserbackend.browser.controller;
import com.edgec.browserbackend.account.dto.ResultDto;
import com.edgec.browserbackend.browser.domain.OperationHistory;
import com.edgec.browserbackend.browser.dto.HistoryListRequestDto;
import com.edgec.browserbackend.browser.dto.IpResourceRequestDto;
import com.edgec.browserbackend.browser.dto.LoginHistoryDto;
import com.edgec.browserbackend.browser.dto.OperationHistoryDto;
import com.edgec.browserbackend.browser.service.HistoryService;
......@@ -32,11 +30,7 @@ public class HistoryController {
historyService.addLoginHistory(principal.getName(), loginHistoryDto);
resultDto.setStatus(0);
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
dealClientRequestException(resultDto, e);
}
return resultDto;
}
......@@ -48,11 +42,7 @@ public class HistoryController {
historyService.addOperationHistory(principal.getName(), operationHistoryDto);
resultDto.setStatus(0);
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
dealClientRequestException(resultDto, e);
}
return resultDto;
}
......@@ -64,11 +54,7 @@ public class HistoryController {
resultDto.setData(historyService.getLoginHistories(principal.getName(), historyListRequestDto));
resultDto.setStatus(0);
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
dealClientRequestException(resultDto, e);
}
return resultDto;
}
......@@ -80,12 +66,16 @@ public class HistoryController {
resultDto.setData(historyService.getOperationHistories(principal.getName(), historyListRequestDto));
resultDto.setStatus(0);
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
dealClientRequestException(resultDto, e);
}
return resultDto;
}
private void dealClientRequestException(ResultDto resultDto, ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
}
}
package com.edgec.browserbackend.browser.controller;
import com.edgec.browserbackend.account.dto.ResultDto;
import com.edgec.browserbackend.browser.dto.*;
import com.edgec.browserbackend.browser.dto.IpListRequestDto;
import com.edgec.browserbackend.browser.dto.IpPageResultDto;
import com.edgec.browserbackend.browser.dto.IpResourceRequestDto;
import com.edgec.browserbackend.browser.dto.IpResourceUpdateDto;
import com.edgec.browserbackend.browser.service.IpResourceService;
import com.edgec.browserbackend.common.auth.Securitys;
import com.edgec.browserbackend.common.commons.error.ClientRequestException;
......@@ -25,7 +28,7 @@ public class IpControlloer {
private IpResourceService ipResourceService;
@RequestMapping(value = "/buy", method = RequestMethod.POST)
public ResultDto buyIp(Principal principal, @RequestBody IpResourceRequestDto ipResourceRequestDto){
public ResultDto buyIp(Principal principal, @RequestBody IpResourceRequestDto ipResourceRequestDto) {
ResultDto resultDto = new ResultDto();
try {
List<String> ipResourceDto = ipResourceService.buyIp(principal.getName(), ipResourceRequestDto);
......@@ -93,7 +96,7 @@ public class IpControlloer {
ipListRequestDto.getAmount(), ipListRequestDto.getFilter());
resultDto.setData(ipResourceDto);
resultDto.setStatus(0);
}catch (ClientRequestException e) {
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
......@@ -110,6 +113,12 @@ public class IpControlloer {
try {
ipResourceService.updateIp(principal.getName(), ipResourceUpdateDto);
resultDto.setStatus(0);
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode().getCode());
statusInfo.put("message", e.getErrorCode().getReason());
resultDto.setStatusInfo(statusInfo);
} catch (Exception e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
......
......@@ -43,11 +43,7 @@ public class ShopController {
resultDto.setData(shopStringResultDto);
resultDto.setStatus(0);
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
dealClientRequestException(resultDto, e);
}
return resultDto;
}
......@@ -73,10 +69,7 @@ public class ShopController {
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
}
return resultDto;
}
@RequestMapping(value = "/update", method = RequestMethod.POST)
......@@ -88,11 +81,7 @@ public class ShopController {
resultDto.setData(shopStringResultDto);
resultDto.setStatus(0);
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
dealClientRequestException(resultDto, e);
}
return resultDto;
}
......@@ -104,11 +93,7 @@ public class ShopController {
shopService.deleteShop(principal.getName(), shopRequestDto.getShopId());
resultDto.setStatus(0);
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
dealClientRequestException(resultDto, e);
}
return resultDto;
}
......@@ -121,11 +106,7 @@ public class ShopController {
resultDto.setStatus(0);
} catch (ClientRequestException e) {
logger.error("fail to bind shop", e);
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
dealClientRequestException(resultDto, e);
}
return resultDto;
}
......@@ -137,11 +118,7 @@ public class ShopController {
ipAndShopService.unBindShop(principal.getName(), shopRequestDto);
resultDto.setStatus(0);
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
dealClientRequestException(resultDto, e);
}
return resultDto;
}
......@@ -153,11 +130,7 @@ public class ShopController {
shopService.transferShop(principal.getName(), shopRequestDto.getShopId(), shopRequestDto.getToGroup());
resultDto.setStatus(0);
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
dealClientRequestException(resultDto, e);
}
return resultDto;
}
......@@ -169,11 +142,7 @@ public class ShopController {
shopService.assignShops(principal.getName(), shopRequestDto.getShopIds(), shopRequestDto.getToUsers());
resultDto.setStatus(0);
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
dealClientRequestException(resultDto, e);
}
return resultDto;
}
......@@ -182,16 +151,11 @@ public class ShopController {
public ResultDto getShopList(Principal principal, @RequestBody ShopRequestDto shopRequestDto) {
ResultDto resultDto = new ResultDto();
try {
ShopPageResultDto shopDtos = shopService.getShopList(principal.getName(), shopRequestDto.getGroup(), shopRequestDto.getPage(),
shopRequestDto.getAmount(), shopRequestDto.getFilter());
ShopPageResultDto shopDtos = shopService.getShopList(principal.getName(), shopRequestDto.getGroup(), shopRequestDto.getPage(), shopRequestDto.getAmount(), shopRequestDto.getFilter());
resultDto.setData(shopDtos);
resultDto.setStatus(0);
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
dealClientRequestException(resultDto, e);
}
logger.error(JSONObject.toJSONString(resultDto));
return resultDto;
......@@ -205,11 +169,7 @@ public class ShopController {
resultDto.setData(subUsers);
resultDto.setStatus(0);
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
dealClientRequestException(resultDto, e);
}
return resultDto;
}
......@@ -222,11 +182,7 @@ public class ShopController {
resultDto.setData(subUsers);
resultDto.setStatus(0);
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
dealClientRequestException(resultDto, e);
}
return resultDto;
}
......@@ -239,12 +195,16 @@ public class ShopController {
resultDto.setData(shopDto);
resultDto.setStatus(0);
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
dealClientRequestException(resultDto, e);
}
return resultDto;
}
private void dealClientRequestException(ResultDto resultDto, ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
}
}
......@@ -36,39 +36,44 @@ public class HistoryServiceImpl implements HistoryService {
@Override
public void addLoginHistory(String username, LoginHistoryDto loginHistoryDto) {
Account account = accountRepository.findById(username).orElse(null);
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
Account account = accountRepository.findById(username)
.orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
int num = loginHistoryRepository.countByUsername(username);
if (num > 10000)
if (num > 10000) {
loginHistoryRepository.delete(loginHistoryRepository.findFirstByUsername(username));
}
LoginHistory loginHistory = new LoginHistory(loginHistoryDto);
loginHistory.setLoginTime(Instant.now().toEpochMilli());
loginHistory.setUsername(username);
if (account.getParent() == null)
if (account.getParent() == null) {
loginHistory.setAdministrator(username);
else
} else {
loginHistory.setAdministrator(account.getParent());
}
loginHistory.setNickname(account.getNickname());
loginHistoryRepository.save(loginHistory);
}
@Override
public void addOperationHistory(String username, OperationHistoryDto operationHistoryDto) {
Account account = accountRepository.findById(username).orElse(null);
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
Account account = accountRepository.findById(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
int num = operationHistoryRepository.countByUsername(username);
if (num > 10000)
if (num > 10000) {
operationHistoryRepository.delete(operationHistoryRepository.findFirstByUsername(username));
}
OperationHistory operationHistory = new OperationHistory(operationHistoryDto);
operationHistory.setOperationTime(Instant.now().toEpochMilli());
operationHistory.setUsername(username);
operationHistory.setNickname(account.getNickname());
if (account.getParent() == null)
if (account.getParent() == null) {
operationHistory.setAdministrator(username);
else
} else {
operationHistory.setAdministrator(account.getParent());
}
operationHistoryRepository.save(operationHistory);
}
......@@ -99,7 +104,7 @@ public class HistoryServiceImpl implements HistoryService {
PageInfo pageInfo = new PageInfo();
pageInfo.setCurrentPage(loginHistoryPage.getNumber());
pageInfo.setTotalPages(loginHistoryPage.getTotalPages());
pageInfo.setTotalItems((int)loginHistoryPage.getTotalElements());
pageInfo.setTotalItems((int) loginHistoryPage.getTotalElements());
historyPageResultDto.setHistoryPage(pageInfo);
return historyPageResultDto;
}
......@@ -131,7 +136,7 @@ public class HistoryServiceImpl implements HistoryService {
PageInfo pageInfo = new PageInfo();
pageInfo.setCurrentPage(operationHistories.getNumber());
pageInfo.setTotalPages(operationHistories.getTotalPages());
pageInfo.setTotalItems((int)operationHistories.getTotalElements());
pageInfo.setTotalItems((int) operationHistories.getTotalElements());
historyPageResultDto.setHistoryPage(pageInfo);
return historyPageResultDto;
}
......
......@@ -43,18 +43,22 @@ public class IpAndShopServiceImpl implements IpAndShopService {
@Override
public void bindShop(String username, ShopRequestDto shopRequestDto) {
String shopId = shopRequestDto.getShopId();
// 1. 根据id 来获取账户
Account account = accountRepository.findByName(username);
if (account == null) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
}
String shopId = shopRequestDto.getShopId();
// 2.获取当前账户下指定的商铺
UserShop userShop = userShopRepository.findByUsernameAndShopId(username, shopId);
if (account.getPermission() < 4 || userShop == null) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
}
Shop shop = shopRepository.findById(shopId).orElse(null);
if (shop == null)
throw new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST);
// 3.根据shopId获取商铺的详情
Shop shop = shopRepository.findById(shopId).orElseThrow(() -> new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST));
// 4.根据传入的id或者addr查询是否有指定的 ip 资源
IpResource ipResource = null;
if (StringUtils.isNotBlank(shopRequestDto.getIpAddr())) {
ipResource = ipResourceRepository.findByAddrAndIsDeleted(shopRequestDto.getIpAddr(), false);
......@@ -62,14 +66,18 @@ public class IpAndShopServiceImpl implements IpAndShopService {
ipResource = ipResourceRepository.findByIdAndIsDeleted(shopRequestDto.getIpId(), false);
}
if (ipResource == null)
if (ipResource == null) {
throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST);
}
// 5.根据shopId来查询指定 isDeleted 为 false 的 ip资源
IpResource bind = ipResourceRepository.findFirstByShopIdsIsAndIsDeleted(shopId, false);
if (bind != null) {
return;
throw new ClientRequestException(BrowserErrorCode.SHOP_BINDED);
}
try {
// 6.绑定ip,将shopId添加到ip资源中,且将 bind 状态设置为 true
ipResourceRepository.addShopId(ipResource.getId(), shopId);
} catch (Exception e) {
logger.error("fail to bind shop and ip", e);
......
......@@ -47,33 +47,11 @@ public class IpResourceServiceImpl implements IpResourceService {
private static String USERNAME = "fangguanlianbrowser";
public static List<String> region = Arrays.asList(
"asiapa",
"hongkong",
"japan",
"s-korea",
"us",
"malaysia",
"yajiada",
"singapore",
"australia",
"germany",
"uk",
"brazil",
"moscow",
"canada",
"france",
"sweden",
"s-korea",
"india",
"meast",
"brazil",
"virginia",
"ohio",
"california",
"oregon",
"ireland",
"london",
"ireland");
"asiapa", "hongkong", "japan", "s-korea", "us", "malaysia",
"yajiada", "singapore", "australia", "germany", "uk", "brazil",
"moscow", "canada", "france", "sweden", "s-korea", "india", "meast",
"brazil", "virginia", "ohio", "california", "oregon", "ireland", "london", "ireland"
);
private static List<String> port = Arrays.asList("20000", "20001");
......@@ -204,8 +182,8 @@ public class IpResourceServiceImpl implements IpResourceService {
if (!ipResourceRequestDto.getVendor().equals("own")) {
List<String> vendorPrices = priceList.get(ipResourceRequestDto.getRegionCn());
String price = vendorPrices.stream()
.filter(x -> Vendor.valueOf(ipResourceRequestDto.getVendor()).getValue().equals(x.substring(0, x.indexOf("-"))))
.map(x -> x.substring(x.lastIndexOf("-") + 1)).collect(Collectors.joining());
.filter(x -> Vendor.valueOf(ipResourceRequestDto.getVendor()).getValue().equals(x.substring(0, x.indexOf("-"))))
.map(x -> x.substring(x.lastIndexOf("-") + 1)).collect(Collectors.joining());
newprice = ipResourceRequestDto.getUnit().equals("week") ? (Integer.valueOf(price) / 3) : Integer.valueOf(price);
}
......@@ -333,21 +311,21 @@ public class IpResourceServiceImpl implements IpResourceService {
AtomicReference<Double> totalprice = new AtomicReference<>((double) 0);
ipResourceRequestDto.getAddr().stream().map(x -> ipResourceRepository.findByAddrAndIsDeleted(x, false))
.forEach(ipResource -> {
List<String> prices;
if (!ipResourceRequestDto.getVendor().equals("own"))
prices = priceList.get(ipResource.getRegionCn());
else {
if (ipResource.isSpecialLine())
prices = priceList.get("自有专线");
else
prices = priceList.get("自有");
}
for (String vendorprice : prices) {
if (ipResource.getVendor().getValue().equals(vendorprice.substring(0, vendorprice.indexOf("-"))))
totalprice.updateAndGet(v -> new Double(v + Double.valueOf(vendorprice.substring(vendorprice.lastIndexOf("-") + 1))));
}
});
.forEach(ipResource -> {
List<String> prices;
if (!ipResourceRequestDto.getVendor().equals("own"))
prices = priceList.get(ipResource.getRegionCn());
else {
if (ipResource.isSpecialLine())
prices = priceList.get("自有专线");
else
prices = priceList.get("自有");
}
for (String vendorprice : prices) {
if (ipResource.getVendor().getValue().equals(vendorprice.substring(0, vendorprice.indexOf("-"))))
totalprice.updateAndGet(v -> new Double(v + Double.valueOf(vendorprice.substring(vendorprice.lastIndexOf("-") + 1))));
}
});
double newprice = ipResourceRequestDto.getUnit().equals("week") ? (int) (totalprice.get() / 3) : totalprice.get().intValue();
IpChargeResultDto ipChargeResultDto = accountService.preChargeByMoney(username, newprice * ipResourceRequestDto.getPeriod() * ipResourceRequestDto.getAmount());
......@@ -384,8 +362,8 @@ public class IpResourceServiceImpl implements IpResourceService {
IpResource ipResource = ipResourceRepository.findByAddrAndIsDeleted(x.getIp(), false);
List<String> vendorPrices = priceList.get(ipResource.getRegionCn());
String price = vendorPrices.stream()
.filter(vendorprice -> ipResource.getVendor().getValue().equals(vendorprice.substring(0, vendorprice.indexOf("-"))))
.map(vendorprice -> vendorprice.substring(vendorprice.lastIndexOf("-") + 1)).collect(Collectors.joining());
.filter(vendorprice -> ipResource.getVendor().getValue().equals(vendorprice.substring(0, vendorprice.indexOf("-"))))
.map(vendorprice -> vendorprice.substring(vendorprice.lastIndexOf("-") + 1)).collect(Collectors.joining());
double newprice1 = ipResourceRequestDto.getUnit().equals("week") ? (Integer.valueOf(price) / 3) : Integer.valueOf(price);
IpChargeRequestDto ipChargeRequestDto = buildIpChargeRequestDto(ipResourceRequestDto, 2, ipResourceRequestDto.getPayMethod());
......@@ -415,8 +393,8 @@ public class IpResourceServiceImpl implements IpResourceService {
}
String price = vendorPrices.stream()
.filter(vendorprice -> ipResource.getVendor().getValue().equals(vendorprice.substring(0, vendorprice.indexOf("-"))))
.map(vendorprice -> vendorprice.substring(vendorprice.lastIndexOf("-") + 1)).collect(Collectors.joining());
.filter(vendorprice -> ipResource.getVendor().getValue().equals(vendorprice.substring(0, vendorprice.indexOf("-"))))
.map(vendorprice -> vendorprice.substring(vendorprice.lastIndexOf("-") + 1)).collect(Collectors.joining());
double newprice1 = ipResourceRequestDto.getUnit().equals("week") ? (Integer.valueOf(price) / 3) : Integer.valueOf(price);
IpChargeRequestDto ipChargeRequestDto = buildIpChargeRequestDto(ipResourceRequestDto, 2, ipResourceRequestDto.getPayMethod());
......@@ -424,10 +402,10 @@ public class IpResourceServiceImpl implements IpResourceService {
if (ipResourceRequestDto.getUnit().equals("week")) {
ipResource.setValidTime(Instant.ofEpochMilli(ipResource.getValidTime()).atZone(ZoneOffset.UTC)
.plusWeeks(finalPeriod).toInstant().toEpochMilli());
.plusWeeks(finalPeriod).toInstant().toEpochMilli());
} else {
ipResource.setValidTime(Instant.ofEpochMilli(ipResource.getValidTime()).atZone(ZoneOffset.UTC)
.plusMonths(finalPeriod).toInstant().toEpochMilli());
.plusMonths(finalPeriod).toInstant().toEpochMilli());
}
ipResourceRepository.save(ipResource);
ipOperationResultDto.getSuccessList().add(x);
......@@ -734,24 +712,41 @@ public class IpResourceServiceImpl implements IpResourceService {
@Override
public void updateIp(String username, IpResourceUpdateDto ipResourceUpdateDto) {
// 1. 根据id 来获取账户
Account account = accountRepository.findByName(username);
if (account == null)
if (account == null) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
if (account.getPermission() < 8)
}
/// 这个地方本来是8,现在由于更新ip失败,改为和绑定店铺时一样的 4
if (account.getPermission() < 4) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
}
// 2.根据传入的id查询是否有指定的 isDelete 为 false的 ip 资源
IpResource ipResource = ipResourceRepository.findByIdAndIsDeleted(ipResourceUpdateDto.getIpId(), false);
if (ipResource == null)
if (ipResource == null) {
throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST);
}
// 封装 ipResource 部分信息
dealIpResource(ipResourceUpdateDto, ipResource);
// 3.更新ip资源
ipResourceRepository.save(ipResource);
}
private void dealIpResource(IpResourceUpdateDto ipResourceUpdateDto, IpResource ipResource) {
if (StringUtils.isNotBlank(ipResourceUpdateDto.getAddr())) {
ipResource.setAddr(ipResourceUpdateDto.getAddr());
}
if (StringUtils.isNotBlank(ipResource.getRegion()) && region.contains(ipResource.getRegion())) {
ipResource.setProxyUsername(ipResource.getAddr());
ipResource.setProxyPassword(genRandom(3, 12));
ipResource.setSpecialLine(true);
}
ipResource.setStatus(ipResourceUpdateDto.getStatus());
ipResourceRepository.save(ipResource);
}
@Override
......
......@@ -26,7 +26,7 @@ public class ClientRequestException extends IllegalArgumentException {
this.errorCode = errorCode;
}
public ClientRequestException(int errorCode, String message){
public ClientRequestException(int errorCode, String message) {
super(message);
}
......
......@@ -12,7 +12,6 @@ public interface ErrorCode {
public static final int COMMON_UNAUTHRORIZED = 100004;
public static final int ACCOUNT_BASE = 400000;
public static final int AUTH_BASE = 500000;
......@@ -20,9 +19,11 @@ public interface ErrorCode {
public static final int BROWSER_BASE = 600000;
int value() ;
int value();
@JsonValue
int getCode() ;
int getCode();
String getReason();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment