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.
78 lines
3.2 KiB
78 lines
3.2 KiB
package com.community.pocket.api;
|
|
|
|
import com.community.pocket.Result;
|
|
import com.community.pocket.domain.Manager;
|
|
import com.community.pocket.domain.Res;
|
|
import com.community.pocket.domain.Token;
|
|
import com.community.pocket.domain.form.ManagerLogin;
|
|
import com.community.pocket.domain.form.ManagerRegister;
|
|
import com.community.pocket.repository.ManagerDao;
|
|
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;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/manager")
|
|
@CrossOrigin("http://localhost:4200")
|
|
public class ManagerController {
|
|
|
|
@Autowired
|
|
private ManagerDao managerDao;
|
|
|
|
// 登陆
|
|
@PostMapping("login")
|
|
@ResponseBody
|
|
public Res<Token> login(@RequestBody ManagerLogin managerLogin){
|
|
if(StringUtils.isEmpty(managerLogin.getManagerName())){
|
|
return new Res<>(Result.FAIL,"管理员不能为空!",null);
|
|
}else if(StringUtils.isEmpty(managerLogin.getPassword())){
|
|
return new Res<>(Result.FAIL,"密码不能为空!",null);
|
|
}else if(!managerDao.login(managerLogin)){
|
|
return new Res<>(Result.FAIL,"账号或密码错误!",null);
|
|
}
|
|
else{
|
|
Token token=new Token();
|
|
Calendar c=Calendar.getInstance();
|
|
c.setTime(new Date());
|
|
token.setCreateTime(c.getTimeInMillis());
|
|
c.add(Calendar.HOUR,1);
|
|
token.setUseTime(c.getTimeInMillis());
|
|
token.setToken(DigestUtils.md5DigestAsHex((token.getCreateTime()+""+token.getUseTime()+"").getBytes()));
|
|
return new Res<>(Result.OK,"登陆成功",token);
|
|
}
|
|
}
|
|
|
|
// 注册
|
|
@PostMapping("register")
|
|
@ResponseBody
|
|
public Res<String> register(@RequestBody ManagerRegister manager){
|
|
if(StringUtils.isEmpty(manager.getManagerName())){
|
|
return new Res<>(Result.FAIL,"管理员不能为空",null);
|
|
}else if(StringUtils.isEmpty(manager.getPassword())){
|
|
return new Res<>(Result.FAIL,"密码不能为空",null);
|
|
}else if(StringUtils.isEmpty(manager.getConfirmPassword())){
|
|
return new Res<>(Result.FAIL,"确认密码不能为空",null);
|
|
}else if(!manager.getPassword().equals(manager.getConfirmPassword())){
|
|
return new Res<>(Result.FAIL,"两次密码输入不一致",null);
|
|
}else if(StringUtils.isEmpty(manager.getMobie())){
|
|
return new Res<>(Result.FAIL,"手机号不能为空",null);
|
|
}else if(StringUtils.isEmpty(manager.getEmail())){
|
|
return new Res<>(Result.FAIL,"邮箱不能为空",null);
|
|
}else if(managerDao.hasUser(manager.getManagerName())){
|
|
return new Res<>(Result.FAIL,"用户已存在",null);
|
|
}else{
|
|
Manager m=new Manager();
|
|
m.setManagerName(manager.getManagerName());
|
|
m.setPassword(DigestUtils.md5DigestAsHex(manager.getPassword().getBytes()));
|
|
m.setMobie(manager.getMobie());
|
|
m.setEmail(manager.getEmail());
|
|
managerDao.save(m);
|
|
return new Res<>(Result.OK,"注册成功",null);
|
|
}
|
|
}
|
|
|
|
}
|
|
|