Commit 58c0cdbb authored by xuxin's avatar xuxin

vps相关功能,以及注册赠送

parent 9824d925
......@@ -16,6 +16,7 @@ import com.edgec.browserbackend.browser.ErrorCode.BrowserErrorCode;
import com.edgec.browserbackend.browser.domain.IpSummary;
import com.edgec.browserbackend.browser.domain.ShopSummary;
import com.edgec.browserbackend.browser.dto.PageInfo;
import com.edgec.browserbackend.browser.repository.GlobalFieldRepository;
import com.edgec.browserbackend.browser.service.IpResourceService;
import com.edgec.browserbackend.browser.service.ShopService;
import com.edgec.browserbackend.common.commons.error.ClientRequestException;
......@@ -101,6 +102,9 @@ public class AccountServiceImpl implements AccountService {
@Autowired
private PaymentService paymentService;
@Autowired
private GlobalFieldRepository globalFieldRepository;
@Override
public List<UserBillList> getUserBills0(String name) {
......@@ -473,7 +477,7 @@ public class AccountServiceImpl implements AccountService {
list.add("184");
list.add("170");
if (!StringUtils.isEmpty(account.getPhoneNumber()) && !list.contains(account.getPhoneNumber().substring(0, 3)) && inviter != null) {
paymentService.bankTransferInsertion(account.getName(), 16);
paymentService.bankTransferInsertion(account.getName(), globalFieldRepository.findAll().get(0).getRegisterGift());
}
log.info("new account has been created: " + account.getName());
......
package com.edgec.browserbackend.browser.ErrorCode;
import com.edgec.browserbackend.common.commons.error.ErrorCode;
import com.fasterxml.jackson.annotation.JsonValue;
/**
* @author xuxin
* @date 2020/8/13 17:19
* @description
*/
public enum VpsErrorCode implements ErrorCode {
/**
* 未知错误
*/
UNKNOWN(ErrorCode.COMMON_UNKNOWN, "unknown"),
/**
* vps不存在
*/
VPS_NOT_EXIST(VPS_BASE + 201, "The VPS do not exist"),
/**
* 不具备分配权限
*/
VPS_NOT_ACCESS(VPS_BASE + 202, "The user do not has access to assign vps");
private final int code;
private final String reason;
VpsErrorCode(int value, String reasonPhrase) {
this.code = value;
this.reason = reasonPhrase;
}
@Override
public int value() {
return this.code;
}
@JsonValue
@Override
public int getCode() {
return code;
}
@Override
public String toString() {
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.Vps;
import com.edgec.browserbackend.browser.dto.AssignVpsDto;
import com.edgec.browserbackend.browser.service.VpsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.security.Principal;
/**
* @author xuxin
* @date 2020/8/13 11:49
* @description
*/
@RestController
@RequestMapping("/vps")
public class VpsController {
@Autowired
private VpsService vpsService;
@PostMapping("/add")
public ResultDto addVps(Principal principal, @Validated @RequestBody Vps vps) {
ResultDto resultDto = new ResultDto();
vps.setOwner1(principal.getName());
String s = vpsService.addVps(vps);
resultDto.setData(s);
return resultDto;
}
@DeleteMapping("/{id}")
public ResultDto deleteVps(Principal principal, @PathVariable String id) {
ResultDto resultDto = new ResultDto();
vpsService.deleteVps(principal.getName(), id);
resultDto.setStatus(0);
return resultDto;
}
@PutMapping("/update")
public ResultDto updateVps(@RequestBody Vps vps) {
ResultDto resultDto = new ResultDto();
vpsService.updateVps(vps);
resultDto.setStatus(0);
return resultDto;
}
/**
* 登录
*/
@GetMapping("/login_vps/{id}")
public ResultDto getVps(@PathVariable String id) {
ResultDto resultDto = new ResultDto();
String rdp = vpsService.loginVps(id);
resultDto.setStatus(0);
resultDto.setData(rdp);
return resultDto;
}
/**
* 列表
*/
@GetMapping("/list")
public ResultDto getVpsList(Principal principal, @RequestParam(value = "page") int page, @RequestParam(value = "size") int size) {
ResultDto resultDto = new ResultDto();
Pageable pageable = PageRequest.of(page, size);
Page<Vps> pageInfo = vpsService.queryPage(principal.getName(), pageable);
resultDto.setStatus(0);
resultDto.setData(pageInfo);
return resultDto;
}
/**
* 分配 vps 给子账户
*/
@PostMapping("/assign")
public ResultDto assignVpsList(Principal principal, @RequestBody AssignVpsDto assignVpsDto) {
ResultDto resultDto = new ResultDto();
vpsService.assignVpsList(principal.getName(), assignVpsDto);
resultDto.setStatus(0);
return resultDto;
}
}
package com.edgec.browserbackend.browser.domain;
import com.edgec.browserbackend.common.utils.Aes;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import javax.validation.constraints.NotEmpty;
/**
* @author xuxin
* @date 2020/8/13 10:49
* @description
*/
@Data
@Document("vps")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Vps {
/**
* vps id
*/
@Id
private String id;
/**
* vps ip
*/
@NotEmpty
private String vpsIp;
/**
* vps 登录名
*/
@NotEmpty
private String vpsUser;
/**
* vps 登录密码
*/
@NotEmpty
private String vpsPassword;
/**
* vps 所绑定的店铺名
*/
@NotEmpty
private String vpsShopName;
/**
* vps 创建者,即当前登录用户
*/
private String owner1;
/**
* vps 创建者的父账号id
*/
private String owner2;
/**
* ip的购买时间点
*/
private long createTime;
public String getVpsPassword() {
return Aes.aesDecrypt(vpsPassword);
}
public void setVpsPassword(String vpsPassword) {
this.vpsPassword = Aes.aesEncrypt(vpsPassword);
}
}
package com.edgec.browserbackend.browser.dto;
import lombok.Data;
import java.util.List;
/**
* @author xuxin
* @date 2020/8/15 18:06
* @description
*/
@Data
public class AssignVpsDto {
/**
* 需要分配的 vpsId 列表
*/
private List<String> vpsIds;
/**
* 接收vps分配的用户id
*/
private List<String> userIds;
}
package com.edgec.browserbackend.browser.dto;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.List;
/**
* @author xuxin
* @date 2020/8/14 14:57
* @description 记录的是 vps 的分配
*/
@Data
@AllArgsConstructor
@Document("uservps")
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserVpsDto {
private String userId;
private List<String> vpsIdList;
}
package com.edgec.browserbackend.browser.repository;
/**
* @author xuxin
* @date 2020/8/3 17:00
* @description
*/
public interface GlobalFieldRepositoryCustom {
}
package com.edgec.browserbackend.browser.repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
/**
* @author xuxin
* @date 2020/8/3 17:02
* @description
*/
public class GlobalFieldRepositoryCustomImpl implements GlobalFieldRepositoryCustom {
@Autowired
private MongoTemplate mongoTemplate;
}
package com.edgec.browserbackend.browser.repository;
import com.edgec.browserbackend.browser.dto.UserVpsDto;
import org.springframework.data.mongodb.repository.MongoRepository;
/**
* @author xuxin
* @date 2020/8/15 17:35
* @description
*/
public interface UserVpsRepository extends MongoRepository<UserVpsDto, String> {
}
package com.edgec.browserbackend.browser.repository;
import com.edgec.browserbackend.browser.domain.Vps;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
/**
* @author xuxin
* @date 2020/8/13 11:44
* @description
*/
public interface VpsRepository extends MongoRepository<Vps, String> {
Page<Vps> findByIdInOrderByCreateTimeDesc(List<String> idList, Pageable pageable);
List<Vps> findByOwner1OrOwner2(String userId1, String userId2);
}
package com.edgec.browserbackend.browser.service.Impl;
import com.edgec.browserbackend.account.domain.Account;
import com.edgec.browserbackend.account.repository.AccountRepository;
import com.edgec.browserbackend.browser.ErrorCode.VpsErrorCode;
import com.edgec.browserbackend.browser.domain.Vps;
import com.edgec.browserbackend.browser.dto.AssignVpsDto;
import com.edgec.browserbackend.browser.dto.UserVpsDto;
import com.edgec.browserbackend.browser.repository.UserVpsRepository;
import com.edgec.browserbackend.browser.repository.VpsRepository;
import com.edgec.browserbackend.browser.service.VpsService;
import com.edgec.browserbackend.common.commons.error.ClientRequestException;
import com.edgec.browserbackend.common.utils.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* @author xuxin
* @date 2020/8/13 11:45
* @description
*/
@Slf4j
@Service
public class VpsServiceImpl implements VpsService {
@Autowired
private VpsRepository vpsRepository;
@Autowired
private AccountRepository accountRepository;
@Autowired
private UserVpsRepository userVpsRepository;
private static String rdpString = "screen mode id:i:2\n" +
"use multimon:i:0\n" +
"desktopwidth:i:1920\n" +
"desktopheight:i:1080\n" +
"session bpp:i:32\n" +
"winposstr:s:0,1,96,127,1920,1001\n" +
"compression:i:1\n" +
"keyboardhook:i:2\n" +
"audiocapturemode:i:0\n" +
"videoplaybackmode:i:1\n" +
"connection type:i:7\n" +
"networkautodetect:i:1\n" +
"bandwidthautodetect:i:1\n" +
"displayconnectionbar:i:1\n" +
"enableworkspacereconnect:i:0\n" +
"disable wallpaper:i:0\n" +
"allow font smoothing:i:0\n" +
"allow desktop composition:i:0\n" +
"disable full window drag:i:1\n" +
"disable menu anims:i:1\n" +
"disable themes:i:0\n" +
"disable cursor setting:i:0\n" +
"bitmapcachepersistenable:i:1\n" +
"full address:s:#VPS_IP#\n" +
"audiomode:i:0\n" +
"redirectprinters:i:1\n" +
"redirectcomports:i:0\n" +
"redirectsmartcards:i:1\n" +
"redirectclipboard:i:1\n" +
"redirectposdevices:i:0\n" +
"autoreconnection enabled:i:1\n" +
"authentication level:i:2\n" +
"prompt for credentials:i:0\n" +
"negotiate security layer:i:1\n" +
"remoteapplicationmode:i:0\n" +
"alternate shell:s:\n" +
"shell working directory:s:\n" +
"gatewayhostname:s:\n" +
"gatewayusagemethod:i:4\n" +
"gatewaycredentialssource:i:4\n" +
"gatewayprofileusagemethod:i:0\n" +
"promptcredentialonce:i:0\n" +
"gatewaybrokeringtype:i:0\n" +
"use redirection server name:i:0\n" +
"rdgiskdcproxy:i:0\n" +
"kdcproxyname:s:\n" +
"drivestoredirect:s:C:\\:s:D:\\\n" +
"username:s:#VPS_USER#\n" +
"password 51:b:#VPS_PASSWORD#\n";
@Override
public String addVps(Vps vps) {
Optional<Account> byId = accountRepository.findById(vps.getOwner1());
String parent = null;
if (byId.isPresent()) {
parent = byId.get().getParent();
}
if (!StringUtils.isEmpty(parent)) {
vps.setOwner2(parent);
}
vps.setCreateTime(Instant.now().toEpochMilli());
Vps save = vpsRepository.save(vps);
return save.getId();
}
@Override
public void deleteVps(String userId, String id) {
Vps vps = vpsRepository.findById(id).orElseThrow(() -> new ClientRequestException(VpsErrorCode.VPS_NOT_EXIST));
if (userId.equals(vps.getOwner1()) || userId.equals(vps.getOwner2())) {
vpsRepository.deleteById(id);
}
}
@Override
public void updateVps(Vps vps) {
Vps destination = vpsRepository.findById(vps.getId()).orElseThrow(() -> new ClientRequestException(VpsErrorCode.VPS_NOT_EXIST));
vps.setId(null);
BeanUtils.mergeObject(vps, destination);
vpsRepository.save(destination);
}
@Override
public String loginVps(String id) {
Vps vps = vpsRepository.findById(id).orElseThrow(() -> new ClientRequestException(VpsErrorCode.VPS_NOT_EXIST));
return rdpString.replace("#VPS_IP#", vps.getVpsIp()).replace("#VPS_USER#", vps.getVpsUser()).replace("#VPS_PASSWORD#", vps.getVpsPassword()) + "shopname:" + vps.getVpsShopName();
}
@Override
public Page<Vps> queryPage(String userId, Pageable pageable) {
// 1. 获取属于当前创建的 vps
List<String> ids = new ArrayList<>(vpsRepository.findByOwner1OrOwner2(userId, userId).stream().map(Vps::getId).collect(Collectors.toList()));
// 2. 获取分配给当前登录用户的vps
UserVpsDto userVpsDto = userVpsRepository.findById(userId).orElse(null);
if (userVpsDto != null) {
ids.addAll(userVpsDto.getVpsIdList());
}
Page<Vps> page = vpsRepository.findByIdInOrderByCreateTimeDesc(ids, pageable);
// 由于在删除vps的时候,没有在分配的账户中删除对应的 vps。所以需要在这里进行惰性删除
List<String> list = page.getContent().stream().map(Vps::getId).collect(Collectors.toList());
if (userVpsDto != null) {
userVpsDto.getVpsIdList().removeAll(list);
userVpsRepository.save(userVpsDto);
}
return page;
}
@Override
public void assignVpsList(String userId, AssignVpsDto assignVpsDto) {
Optional<Account> byId = accountRepository.findById(userId);
String parent = null;
if (byId.isPresent()) {
parent = byId.get().getParent();
}
if (!StringUtils.isEmpty(parent)) {
throw new ClientRequestException(VpsErrorCode.VPS_NOT_ACCESS);
}
List<UserVpsDto> list = new ArrayList<>();
for (String id : assignVpsDto.getUserIds()) {
UserVpsDto uv = new UserVpsDto(id, assignVpsDto.getVpsIds());
list.add(uv);
}
userVpsRepository.saveAll(list);
}
}
package com.edgec.browserbackend.browser.service;
import com.edgec.browserbackend.browser.domain.Vps;
import com.edgec.browserbackend.browser.dto.AssignVpsDto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
/**
* @author xuxin
* @date 2020/8/13 11:45
* @description
*/
public interface VpsService {
String addVps(Vps vps);
void deleteVps(String userId, String id);
void updateVps(Vps vps);
String loginVps(String id);
Page<Vps> queryPage(String user, Pageable pageable);
void assignVpsList(String userId, AssignVpsDto assignVpsDto);
}
......@@ -17,6 +17,7 @@ public interface ErrorCode {
public static final int AUTH_BASE = 500000;
public static final int BROWSER_BASE = 600000;
public static final int VPS_BASE = 700000;
int value();
......
package com.edgec.browserbackend.common.utils;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Field;
/**
* @author xuxin
* @date 2020/8/17 11:27
* @description
*/
@Slf4j
public class BeanUtils {
/**
* 赋值给同类对象
* 将origin属性注入到destination中
*
* @param origin 原始对象
* @param destination 目标对象
* @param <T> 泛型
*/
public static <T> void mergeObject(T origin, T destination) {
if (origin == null || destination == null) {
return;
}
if (!origin.getClass().equals(destination.getClass())) {
return;
}
Field[] fields = origin.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Object value = null;
try {
value = field.get(origin);
if (value != null && !"".equals(value)) {
field.set(destination, value);
}
} catch (IllegalAccessException e) {
log.error("同类对象赋值失败", e);
}
field.setAccessible(false);
}
}
}
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