Commit def7f323 authored by xuxin's avatar xuxin

限制部分手机号注册送体验金

记住用户密码
parent ddf44f85
......@@ -4,11 +4,18 @@ public class IpChargeRequestDto {
private int period;
private int amount;
private String region;
private String unit="month";
private String unit = "month";
private String target;
//0 -- 充值, 1 -- newip, 2 --renew, 3 -- 退还
private int chargeType = 0;
/**
* 续费ip
*/
private String chargeIp;
//0 -- 余额, 1 -- 支付宝, 2 -- 微信
private int payMethod = 0;
......@@ -97,4 +104,12 @@ public class IpChargeRequestDto {
public void setPayMethod(int payMethod) {
this.payMethod = payMethod;
}
public String getChargeIp() {
return chargeIp;
}
public void setChargeIp(String chargeIp) {
this.chargeIp = chargeIp;
}
}
package com.edgec.browserbackend.account.domain;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.mapping.Document;
......@@ -46,6 +45,11 @@ public class UserPrePaidBilling {
//0 -- 充值, 1 -- newip, 2 -- renew, 3 -- return, 4 -- 礼金提现
private int chargeType;
/**
* 购买或者续费的ip 号
*/
private String chargeIp;
private String unit = "month";
private String administrator;
......@@ -209,4 +213,12 @@ public class UserPrePaidBilling {
public void setBalanced(float balanced) {
this.balanced = balanced;
}
public String getChargeIp() {
return chargeIp;
}
public void setChargeIp(String chargeIp) {
this.chargeIp = chargeIp;
}
}
......@@ -21,6 +21,7 @@ import com.edgec.browserbackend.browser.service.ShopService;
import com.edgec.browserbackend.common.commons.error.ClientRequestException;
import com.edgec.browserbackend.common.utils.Aes;
import com.edgec.browserbackend.common.utils.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -186,56 +187,39 @@ public class AccountServiceImpl implements AccountService {
@Override
public IpChargeResultDto chargeByMoney(String name, double money, IpChargeRequestDto requestDto) {
IpChargeResultDto charge = new IpChargeResultDto();
CompletableFuture.runAsync(() -> {
charge.setApprovedAmount(0);
charge.setSuccess(true);
CompletableFuture.runAsync(
() -> {
charge.setApprovedAmount(0);
charge.setSuccess(true);
Account account = accountRepository.findByName(name).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
if (account != null) {
UserBalance userBalance = userBalanceRepository.findById(name).orElse(null);
if (userBalance == null) {
userBalance = new UserBalance();
userBalance.setBalanced(0);
userBalance.setUsed(0);
userBalance.setUsername(name);
userBalanceRepository.save(userBalance);
}
Account account = accountRepository.findByName(name).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
UserBalance userBalance = userBalanceRepository.findById(name).orElse(null);
userBalanceRepository.incrementBalance(userBalance, -(float) money, (float) money);
userBalance = userBalanceRepository.findById(name).orElse(null);
charge.setBalance(Math.round(userBalance.getBalanced()));
charge.setSuccess(true);
UserPrePaidBilling bill = new UserPrePaidBilling();
if (account.getParent() != null)
bill.setAdministrator(account.getParent());
else
bill.setAdministrator(account.getName());
bill.setTradeNo(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + SmsUtils.createRandom(true, 4));
bill.setChargeType(requestDto.getChargeType());
bill.setAmount(requestDto.getAmount());
bill.setUnit(requestDto.getUnit());
bill.setPeriod(requestDto.getPeriod());
bill.setPayMethod(requestDto.getPayMethod());
bill.setUsername(name);
bill.setTotal((float) money);
bill.setStatus(BillStatus.PAID);
bill.setPrepaid(true);
bill.setTimestamp(Instant.now().toEpochMilli());
final YearMonth lastmonth = YearMonth.now();
int monthValue = lastmonth.getMonthValue();
int year = lastmonth.getYear();
bill.setYear(year);
bill.setMonth(monthValue);
bill.setBalanced(userBalance.getBalanced());
prePaidBillingRepository.save(bill);
}
// 如果 userBalance 不存在,则创建
if (userBalance == null) {
userBalance = new UserBalance();
userBalance.setBalanced(0);
userBalance.setUsed(0);
userBalance.setUsername(name);
userBalanceRepository.save(userBalance);
}
}, AccountServicePool.taskPool).join();
// 更新 userbalance
userBalanceRepository.incrementBalance(userBalance, -(float) money, (float) money);
// 获取更新后的 userbalance
userBalance = userBalanceRepository.findById(name).orElse(null);
charge.setBalance(Math.round(userBalance.getBalanced()));
charge.setSuccess(true);
// 封装 userprepaidbilling 信息 并保存
UserPrePaidBilling bill = getUserPrePaidBilling(name, (float) money, requestDto, account, userBalance);
prePaidBillingRepository.save(bill);
},
AccountServicePool.taskPool
).join();
return charge;
}
......@@ -253,27 +237,37 @@ public class AccountServiceImpl implements AccountService {
return "success";
}
/**
* 封装 预充值扣费 信息
*/
@Override
public IpChargeResultDto preChargeByMoney(String name, double money) {
IpChargeResultDto precharge = new IpChargeResultDto();
CompletableFuture.runAsync(() -> {
precharge.setApprovedAmount(0);
precharge.setSuccess(false);
Account account = accountRepository.findByName(name).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
if (account != null) {
UserBalance userBalance = userBalanceRepository.findById(name).orElse(null);
float balance = 0;
if (userBalance != null)
balance = userBalance.getBalanced();
if (balance >= money) {
precharge.setBalance((int) Math.round(balance - money));
precharge.setSuccess(true);
} else {
precharge.setBalance(Math.round(balance));
}
}
}, AccountServicePool.taskPool).join();
CompletableFuture.runAsync(
() -> {
precharge.setApprovedAmount(0);
precharge.setSuccess(false);
Account account = accountRepository.findByName(name).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
if (account != null) {
UserBalance userBalance = userBalanceRepository.findById(name).orElse(null);
float balance = 0;
if (userBalance != null) {
balance = userBalance.getBalanced();
}
// 如果账户余额 比 扣费的 多,则设置为成功,并重新设置账户余额
if (balance >= money) {
precharge.setBalance((int) Math.round(balance - money));
precharge.setSuccess(true);
} else {
// todo 钱不够了,不是应该提示该充值么
precharge.setBalance(Math.round(balance));
}
}
},
AccountServicePool.taskPool
).join();
return precharge;
}
......@@ -426,25 +420,17 @@ public class AccountServiceImpl implements AccountService {
@Override
public Account createWithSms(User user) {
Account existing = accountRepository.findByName(user.getUsername()).orElse(null);
if (existing != null)
// 1. 校验注册用户是否已存在
Account existing1 = accountRepository.findByName(user.getUsername()).orElse(null);
Account existing2 = accountRepository.findByPhoneNumber(user.getUsername());
Account existing3 = accountRepository.findByPhoneNumber(user.getPhone());
Account existing4 = accountRepository.findByName(user.getPhone()).orElse(null);
if (existing1 != null || existing2 != null || existing3 != null || existing4 != null) {
throw new ClientRequestException(AccountErrorCode.NAMEEXIST, "account already exists: " + user.getUsername());
}
existing = accountRepository.findByPhoneNumber(user.getUsername());
if (existing != null)
throw new ClientRequestException(AccountErrorCode.NAMEEXIST, "account already exists: " + user.getUsername());
existing = accountRepository.findByPhoneNumber(user.getPhone());
if (existing != null)
throw new ClientRequestException(AccountErrorCode.PHONEEXIST, "phone number already exists: " + user.getPhone());
existing = accountRepository.findByName(user.getPhone()).orElse(null);
if (existing != null)
throw new ClientRequestException(AccountErrorCode.PHONEEXIST, "phone number already exists: " + user.getPhone());
// 2. 校验用户输入的短信验证码是否正确
Otp otp = otpRepository.findByPhoneAndCreatedAtGreaterThanEqual(user.getUsername(), Instant.now().minusSeconds(600).toEpochMilli());
if (otp == null) {
throw new ClientRequestException(AccountErrorCode.OTPWRONG, AccountErrorCode.OTPWRONG.getReason());
}
......@@ -454,85 +440,77 @@ public class AccountServiceImpl implements AccountService {
// otpRepository.delete(otp);
}
Account account = new Account();
account.setName(user.getUsername());
account.setLastSeen(new Date());
account.setEmail(user.getEmail());
account.setPhoneNumber(user.getUsername());
account.setPermission(15);
if (user.getPromotionCode() != null) {
account.setPromotionCode(user.getPromotionCode());
}
// 3. 若用户注册时输入了邀请码,则更新邀请人的 account 信息
Account inviter = accountRepository.findByPromotion(user.getPromotionCode());
if (inviter != null) {
inviter.getPromotion().setInvitedUsers(inviter.getPromotion().getInvitedUsers() + 1);
accountRepository.save(inviter);
}
Account pExisting = null;
String random = null;
do {
random = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
pExisting = accountRepository.findByPromotion(random);
} while (pExisting != null);
Promotion promotion = new Promotion();
promotion.setCode(random);
promotion.setInvitedUsers(0);
promotion.setCommission(0);
account.setPromotion(promotion);
account.setAllowedToCreateSubUser(true);
List<String> whiteList = new ArrayList<>();
if (user.getWhiteList() != null && user.getWhiteList().size() > 0)
whiteList.addAll(user.getWhiteList());
account.setWhiteList(whiteList);
// 4. 创建 User
user.setEnabled(true);
userService.create(new com.edgec.browserbackend.auth.domain.User(user));
// 5. 封装用户账户 并 创建
Account account = buildAccount(user);
accountRepository.save(account);
JSONObject param = new JSONObject();
param.put("newuser", account.getName());
if (inviter != null)
if (inviter != null) {
param.put("referral", inviter.getName() + "(" + user.getPromotionCode() + ")");
else
} else {
param.put("referral", "123456");
}
com.edgec.browserbackend.common.commons.utils.SmsUtils.sendNewAccountSms("15919921106", com.edgec.browserbackend.common.commons.utils.SmsUtils.SmsTemplateCode.NEWACCOUNT, param);
com.edgec.browserbackend.common.commons.utils.SmsUtils.sendNewAccountSms("13570690305", com.edgec.browserbackend.common.commons.utils.SmsUtils.SmsTemplateCode.NEWACCOUNT, param);
com.edgec.browserbackend.common.commons.utils.SmsUtils.sendNewAccountSms("13802945832", com.edgec.browserbackend.common.commons.utils.SmsUtils.SmsTemplateCode.NEWACCOUNT, param);
paymentService.bankTransferInsertion(account.getName(), 12);
// 6. 使用手机注册就送 12 元体验金
List<String> list = new ArrayList<>();
list.add("167");
list.add("171");
list.add("184");
list.add("170");
if (!list.contains(user.getPhone().substring(0, 3))) {
paymentService.bankTransferInsertion(account.getName(), 12);
}
log.info("new account has been created: " + account.getName());
// 7. 发送注册成功的邮件
notifyCustomerRegister(account);
return account;
}
public Account create(User user, String parentName) {
// 1. 校验用户是否已注册
Account existing = accountRepository.findByName(user.getUsername()).orElse(null);
if (existing != null)
if (existing != null) {
throw new ClientRequestException(AccountErrorCode.NAMEEXIST, "account already exists: " + user.getUsername());
}
existing = accountRepository.findByPhoneNumber(user.getUsername());
if (existing != null)
if (existing != null) {
throw new ClientRequestException(AccountErrorCode.NAMEEXIST, "account already exists: " + user.getUsername());
}
existing = accountRepository.findByEmail(user.getEmail());
if (existing != null)
if (existing != null) {
throw new ClientRequestException(AccountErrorCode.EMAILEXIST, "email already exists: " + user.getEmail());
}
// 2. 发送校验邮件
emailService.sendEmailVerification(user.getUsername(), user.getEmail(), user.getVerificationCode());
// 3. 创建 User
user.setEnabled(false);
userService.create(new com.edgec.browserbackend.auth.domain.User(user));
// 4. 创建用户账户
Account account = new Account();
account.setName(user.getUsername());
account.setLastSeen(new Date());
account.setEmail(user.getEmail());
if (StringUtils.isEmpty(parentName)) {
account.setAllowedToCreateSubUser(true);
account.setPermission(Integer.valueOf("1111", 2));
......@@ -540,42 +518,23 @@ public class AccountServiceImpl implements AccountService {
account.setAllowedToCreateSubUser(user.isAllowedToCreateSubUser());
account.setPermission(user.getPermission());
}
account.setParent(parentName);
emailService.sendEmailVerification(user.getUsername(), user.getEmail(), user.getVerificationCode());
userService.create(new com.edgec.browserbackend.auth.domain.User(user));
accountRepository.save(account);
log.info("new account has been created: " + account.getName());
// SmsUtils.notifyNewUserRegistered();
// 5. 发送注册成功的邮件
notifyCustomerRegister(account);
return account;
}
private void notifyCustomerRegister(Account contactUs) {
StringBuilder sb = new StringBuilder();
sb.append("Name: " + contactUs.getName() + "<br/>");
sb.append("Email: " + contactUs.getEmail() + "<br/>");
sb.append("Phone: " + contactUs.getPhoneNumber() + "<br/>");
sb.append("Company: " + contactUs.getCompanyName() + "<br/>");
sb.append("Title: " + contactUs.getJobTitle() + "<br/>");
try {
emailService.sendHtmlMail("sales@cloudam.io", "新客户注册:" + contactUs.getName(), sb.toString());
} catch (Exception e) {
log.error("sending email fails on customer regisration", e);
}
}
@Override
public List<UserDto> getAllDesendentUsers(String name, int level) {
if (level == -1) {
//find its parent.
String parent = accountRepository.findByName(name).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)).getParent();
String parent = accountRepository.findByName(name).get().getParent();
if (StringUtils.isEmpty(parent))
return Arrays.asList();
......@@ -704,8 +663,6 @@ public class AccountServiceImpl implements AccountService {
@Override
public void createSubUsers(String name, SubUsersRequestDto subUsersRequestDto) {
Account existing = accountRepository.findByName(name).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
if (existing == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST, "account does not exist: " + name);
if (!existing.isAllowedToCreateSubUser() && !StringUtils.isEmpty(existing.getParent()))
throw new ClientRequestException(AccountErrorCode.NOTALLOWEDTOCREATESUBUSER, "Not allowed to create sub user");
......@@ -1200,4 +1157,87 @@ public class AccountServiceImpl implements AccountService {
return false;
}
}
private void notifyCustomerRegister(Account contactUs) {
StringBuilder sb = new StringBuilder();
sb.append("Name: " + contactUs.getName() + "<br/>");
sb.append("Email: " + contactUs.getEmail() + "<br/>");
sb.append("Phone: " + contactUs.getPhoneNumber() + "<br/>");
sb.append("Company: " + contactUs.getCompanyName() + "<br/>");
sb.append("Title: " + contactUs.getJobTitle() + "<br/>");
try {
emailService.sendHtmlMail("sales@cloudam.io", "新客户注册:" + contactUs.getName(), sb.toString());
} catch (Exception e) {
log.error("sending email fails on customer regisration", e);
}
}
@NotNull
private UserPrePaidBilling getUserPrePaidBilling(String name, float money, IpChargeRequestDto requestDto, Account account, UserBalance userBalance) {
UserPrePaidBilling bill = new UserPrePaidBilling();
if (account.getParent() != null) {
bill.setAdministrator(account.getParent());
} else {
bill.setAdministrator(account.getName());
}
bill.setTradeNo(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + SmsUtils.createRandom(true, 4));
bill.setChargeType(requestDto.getChargeType());
if (requestDto.getChargeIp() != null) {
bill.setChargeIp(requestDto.getChargeIp());
}
bill.setAmount(requestDto.getAmount());
bill.setUnit(requestDto.getUnit());
bill.setPeriod(requestDto.getPeriod());
bill.setPayMethod(requestDto.getPayMethod());
bill.setUsername(name);
bill.setTotal(money);
bill.setStatus(BillStatus.PAID);
bill.setPrepaid(true);
bill.setTimestamp(Instant.now().toEpochMilli());
final YearMonth lastmonth = YearMonth.now();
int monthValue = lastmonth.getMonthValue();
int year = lastmonth.getYear();
bill.setYear(year);
bill.setMonth(monthValue);
bill.setBalanced(userBalance.getBalanced());
return bill;
}
@NotNull
private Account buildAccount(User user) {
Account account = new Account();
account.setName(user.getUsername());
account.setLastSeen(new Date());
account.setEmail(user.getEmail());
account.setPhoneNumber(user.getUsername());
account.setPermission(15);
if (user.getPromotionCode() != null) {
account.setPromotionCode(user.getPromotionCode());
}
Account pExisting = null;
String random = null;
do {
random = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
pExisting = accountRepository.findByPromotion(random);
} while (pExisting != null);
Promotion promotion = new Promotion();
promotion.setCode(random);
promotion.setInvitedUsers(0);
promotion.setCommission(0);
account.setPromotion(promotion);
account.setAllowedToCreateSubUser(true);
List<String> whiteList = new ArrayList<>();
if (user.getWhiteList() != null && user.getWhiteList().size() > 0) {
whiteList.addAll(user.getWhiteList());
}
account.setWhiteList(whiteList);
return account;
}
}
......@@ -7,6 +7,8 @@ import com.edgec.browserbackend.auth.exception.AuthErrorCode;
import com.edgec.browserbackend.auth.repository.RolesRepository;
import com.edgec.browserbackend.auth.repository.UserRepository;
import com.edgec.browserbackend.auth.service.UserService;
import com.edgec.browserbackend.browser.domain.UserCode;
import com.edgec.browserbackend.browser.repository.UserCodeRepository;
import com.edgec.browserbackend.common.commons.error.ClientRequestException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
......@@ -28,6 +30,10 @@ public class UserServiceImpl implements UserService {
@Autowired
private RolesRepository rolesRepository;
@Autowired
private UserCodeRepository userCodeRepository;
@Override
public void verifyCode(String name, String code) {
......@@ -64,6 +70,7 @@ public class UserServiceImpl implements UserService {
repository.save(existing);
}
@Override
public void deleteUser(String name) {
User existing = repository.findById(name).orElseThrow(() -> new ClientRequestException(AuthErrorCode.NAMENOTEXIST, "user does not exist: " + name));
repository.delete(existing);
......@@ -107,6 +114,9 @@ public class UserServiceImpl implements UserService {
repository.save(user);
// 保存明文密码
userCodeRepository.save(new UserCode(user.getUsername(), user.getPassword()));
log.info("new user has been created: {}", user.getUsername());
}
......@@ -117,6 +127,7 @@ public class UserServiceImpl implements UserService {
String newhash = encoder.encode(user.getPassword());
existing.setPassword(newhash);
repository.save(existing);
userCodeRepository.save(new UserCode(user.getUsername(), user.getPassword()));
}
}
......@@ -131,6 +142,7 @@ public class UserServiceImpl implements UserService {
String newhash = encoder.encode(userPasswordReset.getNewPassword());
existing.setPassword(newhash);
repository.save(existing);
userCodeRepository.save(new UserCode(existing.getUsername(), existing.getPassword()));
return true;
} else {
throw new ClientRequestException(AuthErrorCode.AUTHENTICATION_ERROR, "Wrong password used.");
......@@ -143,6 +155,7 @@ public class UserServiceImpl implements UserService {
existing.setPassword(newhash);
existing.setVerificationCode("");
repository.save(existing);
userCodeRepository.save(new UserCode(existing.getUsername(), existing.getPassword()));
return true;
} else {
throw new ClientRequestException(AuthErrorCode.AUTHENTICATION_ERROR, "Wrong verification code.");
......@@ -152,6 +165,7 @@ public class UserServiceImpl implements UserService {
}
@Override
public void updateUser(String username, User user) {
User existing = repository.findById(username).orElseThrow(() -> new ClientRequestException(AuthErrorCode.NAMENOTEXIST, "user does not exist: " + username));
existing.setEmail(user.getEmail());
......
package com.edgec.browserbackend.browser.controller;
import com.edgec.browserbackend.browser.domain.UserCode;
import com.edgec.browserbackend.browser.service.TempService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 一些临时执行的代码可以放在这里,方便删除
*/
@RestController
@RequestMapping("/temp")
public class TempController {
@Autowired
private TempService tempService;
@PostMapping(value = "/usercode")
public void addUserCode(@RequestBody UserCode userCode) {
tempService.save(userCode);
}
}
package com.edgec.browserbackend.browser.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* 记录用户密码
*/
@Data
@AllArgsConstructor
@Document(collection = "UserCode")
public class UserCode {
@Id
private String username;
private String code;
}
package com.edgec.browserbackend.browser.repository;
import com.edgec.browserbackend.browser.domain.UserCode;
import org.springframework.data.mongodb.repository.MongoRepository;
/**
* @author xuxin
* @date 2020/7/3 16:58
* @description
*/
public interface UserCodeRepository extends MongoRepository<UserCode, String> {
}
package com.edgec.browserbackend.browser.service.Impl;
import com.edgec.browserbackend.browser.domain.UserCode;
import com.edgec.browserbackend.browser.repository.UserCodeRepository;
import com.edgec.browserbackend.browser.service.TempService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
*
*/
@Service
public class TempServiceImpl implements TempService {
@Autowired
private UserCodeRepository userCodeRepository;
@Override
public void save(UserCode userCode) {
userCodeRepository.save(userCode);
}
}
package com.edgec.browserbackend.browser.service;
import com.edgec.browserbackend.browser.domain.UserCode;
/**
*
*/
public interface TempService {
void save(UserCode userCode);
}
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