代理商 注册
This commit is contained in:
parent
c9242db805
commit
297c028985
|
|
@ -1,12 +1,19 @@
|
|||
package com.sqx.modules.sys.controller;
|
||||
|
||||
import com.sqx.common.utils.Result;
|
||||
import com.sqx.common.validator.ValidatorUtils;
|
||||
import com.sqx.common.validator.group.AddGroup;
|
||||
import com.sqx.modules.app.dao.MsgDao;
|
||||
import com.sqx.modules.app.entity.Msg;
|
||||
import com.sqx.modules.sys.entity.SysUserEntity;
|
||||
import com.sqx.modules.sys.form.SysLoginForm;
|
||||
import com.sqx.modules.sys.service.SysCaptchaService;
|
||||
import com.sqx.modules.sys.service.SysUserService;
|
||||
import com.sqx.modules.sys.service.SysUserTokenService;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiImplicitParams;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.shiro.crypto.hash.Sha256Hash;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
|
@ -19,80 +26,111 @@ import javax.servlet.ServletOutputStream;
|
|||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 登录相关
|
||||
*
|
||||
*/
|
||||
@RestController
|
||||
public class SysLoginController extends AbstractController {
|
||||
@Autowired
|
||||
private SysUserService sysUserService;
|
||||
@Autowired
|
||||
private SysUserTokenService sysUserTokenService;
|
||||
@Autowired
|
||||
private SysCaptchaService sysCaptchaService;
|
||||
@Autowired
|
||||
private SysUserService sysUserService;
|
||||
@Autowired
|
||||
private SysUserTokenService sysUserTokenService;
|
||||
@Autowired
|
||||
private SysCaptchaService sysCaptchaService;
|
||||
@Autowired
|
||||
private MsgDao msgDao;
|
||||
|
||||
/**
|
||||
* 验证码
|
||||
*/
|
||||
@GetMapping("captcha.jpg")
|
||||
public void captcha(HttpServletResponse response, String uuid)throws IOException {
|
||||
response.setHeader("Cache-Control", "no-store, no-cache");
|
||||
response.setContentType("image/jpeg");
|
||||
/**
|
||||
* 验证码
|
||||
*/
|
||||
@GetMapping("captcha.jpg")
|
||||
public void captcha(HttpServletResponse response, String uuid) throws IOException {
|
||||
response.setHeader("Cache-Control", "no-store, no-cache");
|
||||
response.setContentType("image/jpeg");
|
||||
|
||||
//获取图片验证码
|
||||
BufferedImage image = sysCaptchaService.getCaptcha(uuid);
|
||||
//获取图片验证码
|
||||
BufferedImage image = sysCaptchaService.getCaptcha(uuid);
|
||||
|
||||
ServletOutputStream out = response.getOutputStream();
|
||||
ImageIO.write(image, "jpg", out);
|
||||
IOUtils.closeQuietly(out);
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录
|
||||
*/
|
||||
@PostMapping("/sys/login")
|
||||
public Map<String, Object> login(@RequestBody SysLoginForm form)throws IOException {
|
||||
boolean captcha = sysCaptchaService.validate(form.getUuid(), form.getCaptcha());
|
||||
if(!captcha){
|
||||
return Result.error("验证码不正确");
|
||||
}
|
||||
|
||||
//用户信息
|
||||
SysUserEntity user = sysUserService.queryByUserName(form.getUsername());
|
||||
|
||||
//账号不存在、密码错误
|
||||
if(user == null || !user.getPassword().equals(new Sha256Hash(form.getPassword(), user.getSalt()).toHex())) {
|
||||
return Result.error("账号或密码不正确");
|
||||
}
|
||||
|
||||
//账号锁定
|
||||
if(user.getStatus() == 0){
|
||||
return Result.error("账号已被锁定,请联系管理员");
|
||||
}
|
||||
|
||||
//判断角色类型
|
||||
if(form.getAdminType()==1 && user.getIsChannel()!=null && user.getIsChannel()==1){
|
||||
return Result.error("代理账号请登录代理端!");
|
||||
}else if(form.getAdminType()==2 && user.getIsChannel()==null){
|
||||
return Result.error("管理员请登录管理端!");
|
||||
}
|
||||
|
||||
//生成token,并保存到数据库
|
||||
Result r = sysUserTokenService.createToken(user.getUserId());
|
||||
return r;
|
||||
}
|
||||
ServletOutputStream out = response.getOutputStream();
|
||||
ImageIO.write(image, "jpg", out);
|
||||
IOUtils.closeQuietly(out);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 退出
|
||||
*/
|
||||
@PostMapping("/sys/logout")
|
||||
public Result logout() {
|
||||
sysUserTokenService.logout(getUserId());
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@PostMapping("/registered")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "msg", value = "验证码", dataTypeClass = String.class, paramType = "param"),
|
||||
})
|
||||
public Result registered(@RequestBody SysUserEntity user, String msg) {
|
||||
if(StringUtils.isBlank(user.getMobile())){
|
||||
return Result.error("注册失败,请输入手机号");
|
||||
}
|
||||
if(StringUtils.isBlank(msg)){
|
||||
return Result.error("注册失败,请输入验证码");
|
||||
}
|
||||
Msg msg1 = msgDao.findByPhoneAndCode(user.getMobile(), msg);
|
||||
if (msg1 == null) {
|
||||
return Result.error("验证码不正确!");
|
||||
}
|
||||
user.setIsChannel(1);
|
||||
user.setQdRate(new BigDecimal("0.01"));
|
||||
user.setStatus(1);
|
||||
user.setRoleIdList(Collections.singletonList(4L));
|
||||
ValidatorUtils.validateEntity(user, AddGroup.class);
|
||||
sysUserService.saveUser(user);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 登录
|
||||
*/
|
||||
@PostMapping("/sys/login")
|
||||
public Map<String, Object> login(@RequestBody SysLoginForm form) throws IOException {
|
||||
boolean captcha = sysCaptchaService.validate(form.getUuid(), form.getCaptcha());
|
||||
if (!captcha) {
|
||||
return Result.error("验证码不正确");
|
||||
}
|
||||
|
||||
//用户信息
|
||||
SysUserEntity user = sysUserService.queryByUserName(form.getUsername());
|
||||
|
||||
//账号不存在、密码错误
|
||||
if (user == null || !user.getPassword().equals(new Sha256Hash(form.getPassword(), user.getSalt()).toHex())) {
|
||||
return Result.error("账号或密码不正确");
|
||||
}
|
||||
|
||||
//账号锁定
|
||||
if (user.getStatus() == 0) {
|
||||
return Result.error("账号已被锁定,请联系管理员");
|
||||
}
|
||||
|
||||
//判断角色类型
|
||||
if (form.getAdminType() == 1 && user.getIsChannel() != null && user.getIsChannel() == 1) {
|
||||
return Result.error("代理账号请登录代理端!");
|
||||
} else if (form.getAdminType() == 2 && user.getIsChannel() == null) {
|
||||
return Result.error("管理员请登录管理端!");
|
||||
}
|
||||
|
||||
//生成token,并保存到数据库
|
||||
Result r = sysUserTokenService.createToken(user.getUserId());
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 退出
|
||||
*/
|
||||
@PostMapping("/sys/logout")
|
||||
public Result logout() {
|
||||
sysUserTokenService.logout(getUserId());
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import com.sqx.modules.sys.entity.SysUserEntity;
|
|||
import com.sqx.modules.sys.service.SysRoleService;
|
||||
import com.sqx.modules.sys.service.SysUserRoleService;
|
||||
import com.sqx.modules.sys.service.SysUserService;
|
||||
import com.sqx.modules.utils.InvitationCodeUtil;
|
||||
import org.apache.commons.lang.RandomStringUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.shiro.crypto.hash.Sha256Hash;
|
||||
|
|
@ -25,103 +26,105 @@ import java.util.Map;
|
|||
|
||||
/**
|
||||
* 系统用户
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("ALL")
|
||||
@Service("sysUserService")
|
||||
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {
|
||||
@Autowired
|
||||
private SysUserRoleService sysUserRoleService;
|
||||
@Autowired
|
||||
private SysRoleService sysRoleService;
|
||||
@Autowired
|
||||
private SysUserRoleService sysUserRoleService;
|
||||
@Autowired
|
||||
private SysRoleService sysRoleService;
|
||||
|
||||
@Override
|
||||
public PageUtils queryPage(Map<String, Object> params) {
|
||||
String username = (String)params.get("username");
|
||||
Long createUserId = (Long)params.get("createUserId");
|
||||
Object isChannel = params.get("isChannel");
|
||||
Object sysUserId = params.get("sysUserId");
|
||||
IPage<SysUserEntity> page = this.page(
|
||||
new Query<SysUserEntity>().getPage(params),
|
||||
new QueryWrapper<SysUserEntity>()
|
||||
.like(StringUtils.isNotBlank(username),"username", username)
|
||||
.eq(createUserId != null,"create_user_id", createUserId)
|
||||
.eq(isChannel!=null,"is_channel",isChannel)
|
||||
.eq(sysUserId!=null,"sys_user_id",sysUserId)
|
||||
.isNull(sysUserId==null,"sys_user_id")
|
||||
.isNull(isChannel==null,"is_channel")
|
||||
);
|
||||
@Override
|
||||
public PageUtils queryPage(Map<String, Object> params) {
|
||||
String username = (String) params.get("username");
|
||||
Long createUserId = (Long) params.get("createUserId");
|
||||
Object isChannel = params.get("isChannel");
|
||||
Object sysUserId = params.get("sysUserId");
|
||||
IPage<SysUserEntity> page = this.page(
|
||||
new Query<SysUserEntity>().getPage(params),
|
||||
new QueryWrapper<SysUserEntity>()
|
||||
.like(StringUtils.isNotBlank(username), "username", username)
|
||||
.eq(createUserId != null, "create_user_id", createUserId)
|
||||
.eq(isChannel != null, "is_channel", isChannel)
|
||||
.eq(sysUserId != null, "sys_user_id", sysUserId)
|
||||
.isNull(sysUserId == null, "sys_user_id")
|
||||
.isNull(isChannel == null, "is_channel")
|
||||
);
|
||||
|
||||
return new PageUtils(page);
|
||||
}
|
||||
return new PageUtils(page);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> queryAllPerms(Long userId) {
|
||||
return baseMapper.queryAllPerms(userId);
|
||||
}
|
||||
@Override
|
||||
public List<String> queryAllPerms(Long userId) {
|
||||
return baseMapper.queryAllPerms(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Long> queryAllMenuId(Long userId) {
|
||||
return baseMapper.queryAllMenuId(userId);
|
||||
}
|
||||
@Override
|
||||
public List<Long> queryAllMenuId(Long userId) {
|
||||
return baseMapper.queryAllMenuId(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SysUserEntity queryByUserName(String username) {
|
||||
return baseMapper.queryByUserName(username);
|
||||
}
|
||||
@Override
|
||||
public SysUserEntity queryByUserName(String username) {
|
||||
return baseMapper.queryByUserName(username);
|
||||
}
|
||||
|
||||
@SuppressWarnings("AlibabaTransactionMustHaveRollback")
|
||||
@Override
|
||||
@Transactional
|
||||
public void saveUser(SysUserEntity user) {
|
||||
user.setCreateTime(new Date());
|
||||
//sha256加密
|
||||
String salt = RandomStringUtils.randomAlphanumeric(20);
|
||||
user.setPassword(new Sha256Hash(user.getPassword(), salt).toHex());
|
||||
user.setSalt(salt);
|
||||
this.save(user);
|
||||
|
||||
//检查角色是否越权
|
||||
checkRole(user);
|
||||
|
||||
//保存用户与角色关系
|
||||
sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
|
||||
}
|
||||
@SuppressWarnings("AlibabaTransactionMustHaveRollback")
|
||||
@Override
|
||||
@Transactional
|
||||
public void saveUser(SysUserEntity user) {
|
||||
user.setCreateTime(new Date());
|
||||
//sha256加密
|
||||
String salt = RandomStringUtils.randomAlphanumeric(20);
|
||||
user.setPassword(new Sha256Hash(user.getPassword(), salt).toHex());
|
||||
user.setSalt(salt);
|
||||
this.save(user);
|
||||
if (user.getIsChannel() != null && user.getIsChannel().equals(1) && StringUtils.isBlank(user.getQdCode())) {
|
||||
user.setQdCode(InvitationCodeUtil.toRegisteredCode(user.getUserId()));
|
||||
this.save(user);
|
||||
}
|
||||
//检查角色是否越权
|
||||
checkRole(user);
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void update(SysUserEntity user) {
|
||||
if(StringUtils.isBlank(user.getPassword())){
|
||||
user.setPassword(null);
|
||||
}else{
|
||||
user.setPassword(new Sha256Hash(user.getPassword(), user.getSalt()).toHex());
|
||||
}
|
||||
this.updateById(user);
|
||||
|
||||
//检查角色是否越权
|
||||
checkRole(user);
|
||||
|
||||
//保存用户与角色关系
|
||||
sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
|
||||
}
|
||||
//保存用户与角色关系
|
||||
sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteBatch(Long[] userId) {
|
||||
this.removeByIds(Arrays.asList(userId));
|
||||
}
|
||||
@Override
|
||||
@Transactional
|
||||
public void update(SysUserEntity user) {
|
||||
if (StringUtils.isBlank(user.getPassword())) {
|
||||
user.setPassword(null);
|
||||
} else {
|
||||
user.setPassword(new Sha256Hash(user.getPassword(), user.getSalt()).toHex());
|
||||
}
|
||||
this.updateById(user);
|
||||
|
||||
@Override
|
||||
public boolean updatePassword(Long userId, String password, String newPassword) {
|
||||
SysUserEntity userEntity = new SysUserEntity();
|
||||
userEntity.setPassword(newPassword);
|
||||
return this.update(userEntity,
|
||||
new QueryWrapper<SysUserEntity>().eq("user_id", userId).eq("password", password));
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查角色是否越权
|
||||
*/
|
||||
private void checkRole(SysUserEntity user){
|
||||
//检查角色是否越权
|
||||
checkRole(user);
|
||||
|
||||
//保存用户与角色关系
|
||||
sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteBatch(Long[] userId) {
|
||||
this.removeByIds(Arrays.asList(userId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updatePassword(Long userId, String password, String newPassword) {
|
||||
SysUserEntity userEntity = new SysUserEntity();
|
||||
userEntity.setPassword(newPassword);
|
||||
return this.update(userEntity,
|
||||
new QueryWrapper<SysUserEntity>().eq("user_id", userId).eq("password", password));
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查角色是否越权
|
||||
*/
|
||||
private void checkRole(SysUserEntity user) {
|
||||
/*if(user.getRoleIdList() == null || user.getRoleIdList().size() == 0){
|
||||
return;
|
||||
}
|
||||
|
|
@ -137,11 +140,11 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity> i
|
|||
if(!roleIdList.containsAll(user.getRoleIdList())){
|
||||
throw new SqxException("新增用户所选角色,不是本人创建");
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SysUserEntity selectSysUserByQdCode(String qdCode){
|
||||
return baseMapper.selectOne(new QueryWrapper<SysUserEntity>().isNull("sys_user_id").eq("qd_code", qdCode));
|
||||
}
|
||||
@Override
|
||||
public SysUserEntity selectSysUserByQdCode(String qdCode) {
|
||||
return baseMapper.selectOne(new QueryWrapper<SysUserEntity>().isNull("sys_user_id").eq("qd_code", qdCode));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -2,85 +2,125 @@ package com.sqx.modules.utils;
|
|||
|
||||
/**
|
||||
* 邀请码生成解密工具类
|
||||
*
|
||||
* @author fang
|
||||
* @date 2020/7/8
|
||||
*/
|
||||
public class InvitationCodeUtil {
|
||||
|
||||
|
||||
/** 自定义进制(选择你想要的进制数,不能重复且最好不要0、1这些容易混淆的字符) */
|
||||
private static final char[] r=new char[]{ 'M', 'J', 'U', 'D', 'Z', 'X', '9', 'C', '7', 'P','E', '8', '6', 'B', 'G', 'H', 'S', '2', '5', 'F', 'R', '4','Q', 'W', 'K', '3', 'V', 'Y', 'T', 'N'};
|
||||
/**
|
||||
* 自定义进制(选择你想要的进制数,不能重复且最好不要0、1这些容易混淆的字符)
|
||||
*/
|
||||
private static final char[] r = new char[]{'M', 'J', 'U', 'D', 'Z', 'X', '9', 'C', '7', 'P', 'E', '8', '6', 'B', 'G', 'H', 'S', '2', '5', 'F', 'R', '4', 'Q', 'W', 'K', '3', 'V', 'Y', 'T', 'N'};
|
||||
|
||||
/** 定义一个字符用来补全邀请码长度(该字符前面是计算出来的邀请码,后面是用来补全用的) */
|
||||
private static final char b='A';
|
||||
/**
|
||||
* 定义一个字符用来补全邀请码长度(该字符前面是计算出来的邀请码,后面是用来补全用的)
|
||||
*/
|
||||
private static final char b = 'A';
|
||||
|
||||
/** 进制长度 */
|
||||
private static final int binLen=r.length;
|
||||
/**
|
||||
* 进制长度
|
||||
*/
|
||||
private static final int binLen = r.length;
|
||||
|
||||
/** 邀请码长度 */
|
||||
private static final int s=6;
|
||||
/**
|
||||
* 邀请码长度
|
||||
*/
|
||||
private static final int s = 6;
|
||||
|
||||
|
||||
/** 补位字符串 */
|
||||
private static final String e="KSLFXFR";
|
||||
/**
|
||||
* 补位字符串
|
||||
*/
|
||||
private static final String e = "KSLFXFR";
|
||||
|
||||
/**
|
||||
* 代理注册 补位字符串
|
||||
*/
|
||||
private static final String re = "REGISTER";
|
||||
|
||||
/**
|
||||
* 根据ID生成六位随机码
|
||||
*
|
||||
* @param id ID
|
||||
* @return 随机码
|
||||
*/
|
||||
public static String toRegisteredCode(long id) {
|
||||
char[] buf = new char[32];
|
||||
int charPos = 32;
|
||||
|
||||
while ((id / binLen) > 0) {
|
||||
int ind = (int) (id % binLen);
|
||||
buf[--charPos] = r[ind];
|
||||
id /= binLen;
|
||||
}
|
||||
buf[--charPos] = r[(int) (id % binLen)];
|
||||
String str = new String(buf, charPos, (32 - charPos));
|
||||
// 不够长度的自动补全
|
||||
if (str.length() < s) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(re.subSequence(0, s - str.length()));
|
||||
str += sb.toString();
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID生成六位随机码
|
||||
*
|
||||
* @param id ID
|
||||
* @return 随机码
|
||||
*/
|
||||
public static String toSerialCode(long id) {
|
||||
char[] buf=new char[32];
|
||||
int charPos=32;
|
||||
char[] buf = new char[32];
|
||||
int charPos = 32;
|
||||
|
||||
while((id / binLen) > 0) {
|
||||
int ind=(int)(id % binLen);
|
||||
buf[--charPos]=r[ind];
|
||||
while ((id / binLen) > 0) {
|
||||
int ind = (int) (id % binLen);
|
||||
buf[--charPos] = r[ind];
|
||||
id /= binLen;
|
||||
}
|
||||
buf[--charPos]=r[(int)(id % binLen)];
|
||||
String str=new String(buf, charPos, (32 - charPos));
|
||||
buf[--charPos] = r[(int) (id % binLen)];
|
||||
String str = new String(buf, charPos, (32 - charPos));
|
||||
// 不够长度的自动补全
|
||||
if(str.length() < s) {
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sb.append(e.subSequence(0, s-str.length()));
|
||||
str+=sb.toString();
|
||||
if (str.length() < s) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(e.subSequence(0, s - str.length()));
|
||||
str += sb.toString();
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据随机码生成ID
|
||||
*
|
||||
* @param code 随机码
|
||||
* @return ID
|
||||
*/
|
||||
public static long codeToId(String code) {
|
||||
char[] chs;
|
||||
chs = code.toCharArray();
|
||||
long res=0L;
|
||||
for(int i=0; i < chs.length; i++) {
|
||||
int ind=0;
|
||||
for(int j=0; j < binLen; j++) {
|
||||
if(chs[i] == r[j]) {
|
||||
ind=j;
|
||||
long res = 0L;
|
||||
for (int i = 0; i < chs.length; i++) {
|
||||
int ind = 0;
|
||||
for (int j = 0; j < binLen; j++) {
|
||||
if (chs[i] == r[j]) {
|
||||
ind = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(chs[i] == b) {
|
||||
if (chs[i] == b) {
|
||||
break;
|
||||
}
|
||||
if(i > 0) {
|
||||
res=res * binLen + ind;
|
||||
if (i > 0) {
|
||||
res = res * binLen + ind;
|
||||
} else {
|
||||
res=ind;
|
||||
res = ind;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue