You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
4.3 KiB
108 lines
4.3 KiB
package com.community.pocket.api.web;
|
|
|
|
import com.community.pocket.entity.po.Manager;
|
|
import com.community.pocket.entity.po.Token;
|
|
import com.community.pocket.entity.vo.*;
|
|
import com.community.pocket.repository.ManagerDao;
|
|
import com.community.pocket.util.EmailService;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.util.DigestUtils;
|
|
import org.springframework.util.StringUtils;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.Calendar;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/manager")
|
|
//客户端跨域测试
|
|
@CrossOrigin("http://localhost:4200")
|
|
//管理员接口
|
|
public class ManagerController {
|
|
|
|
@Autowired
|
|
private ManagerDao managerDao;
|
|
|
|
//邮箱配置
|
|
@Autowired
|
|
private EmailTypeConfig emailTypeConfig;
|
|
|
|
//邮箱服务
|
|
@Autowired
|
|
private EmailService emailService;
|
|
|
|
// 登陆
|
|
@PostMapping("login")
|
|
public JSONResponse<Token> login(@RequestBody ManagerLogin managerLogin){
|
|
if(StringUtils.isEmpty(managerLogin.getManagerName())){
|
|
return new JSONResponse<>(Result.FAIL,"管理员不能为空!",null);
|
|
}else if(StringUtils.isEmpty(managerLogin.getPassword())){
|
|
return new JSONResponse<>(Result.FAIL,"密码不能为空!",null);
|
|
//校验账号密码
|
|
}else if(!managerDao.login(managerLogin)){
|
|
return new JSONResponse<>(Result.FAIL,"账号或密码错误!",null);
|
|
}
|
|
else{
|
|
// 验证通过生成令牌响应给服务端
|
|
Token token=new Token();
|
|
Calendar c=Calendar.getInstance();
|
|
c.setTime(new Date());
|
|
token.setCreateTime(c.getTimeInMillis());
|
|
// 令牌有效时间1小时
|
|
c.add(Calendar.HOUR,1);
|
|
token.setUseTime(c.getTimeInMillis());
|
|
// 生成token
|
|
token.setToken(DigestUtils.md5DigestAsHex((token.getCreateTime()+""+token.getUseTime()+"").getBytes()));
|
|
token.setManagerName(managerLogin.getManagerName());
|
|
return new JSONResponse<>(Result.OK,"登陆成功",token);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取邮箱类型
|
|
*/
|
|
@GetMapping("emailType")
|
|
public JSONResponse<List<EmailType>> getEmailType(){
|
|
return new JSONResponse<>(Result.OK,"获取邮箱类型成功", emailTypeConfig.getSupportType());
|
|
}
|
|
|
|
@PostMapping("sendcode")
|
|
public JSONResponse<String> sendEmail(String sender){
|
|
if(emailService.sendManagerCode(sender)){
|
|
return new JSONResponse<>(Result.OK,"发送邮件成功",null);
|
|
}else{
|
|
return new JSONResponse<>(Result.OK,"发送邮件失败",null);
|
|
}
|
|
}
|
|
|
|
// 注册
|
|
@PostMapping("register")
|
|
public JSONResponse<String> register(@RequestBody ManagerRegister manager){
|
|
if(StringUtils.isEmpty(manager.getManagerName())){
|
|
return new JSONResponse<>(Result.FAIL,"管理员不能为空",null);
|
|
}else if(StringUtils.isEmpty(manager.getPassword())){
|
|
return new JSONResponse<>(Result.FAIL,"密码不能为空",null);
|
|
}else if(StringUtils.isEmpty(manager.getConfirmPassword())){
|
|
return new JSONResponse<>(Result.FAIL,"确认密码不能为空",null);
|
|
}else if (!manager.getPassword().equals(manager.getConfirmPassword())) {
|
|
return new JSONResponse<>(Result.FAIL, "两次密码输入不一致", null);
|
|
} else if (StringUtils.isEmpty(manager.getMobile())) {
|
|
return new JSONResponse<>(Result.FAIL, "手机号不能为空", null);
|
|
} else if (StringUtils.isEmpty(manager.getEmail())) {
|
|
return new JSONResponse<>(Result.FAIL, "邮箱不能为空", null);
|
|
} else if (managerDao.hasUser(manager.getManagerName())) {
|
|
return new JSONResponse<>(Result.FAIL, "用户已存在", null);
|
|
} else {
|
|
// 表单数据转换到管理员实体,调用dao层持久化到数据库
|
|
Manager m = new Manager();
|
|
m.setManagerName(manager.getManagerName());
|
|
m.setPassword(DigestUtils.md5DigestAsHex(manager.getPassword().getBytes()));
|
|
m.setMobile(manager.getMobile());
|
|
m.setEmail(manager.getEmail());
|
|
managerDao.save(m);
|
|
return new JSONResponse<>(Result.OK,"注册成功",null);
|
|
}
|
|
}
|
|
|
|
}
|
|
|