首页>代码>基于SpringBoot+Vue实现的物流快递仓库管理系统>/服务端代码/api/src/main/java/com/example/api/controller/AdminController.java
package com.example.api.controller;

import com.example.api.exception.AccountAndPasswordError;
import com.example.api.model.dto.LoginDto;
import com.example.api.model.entity.Admin;
import com.example.api.model.entity.LoginLog;
import com.example.api.model.enums.Role;
import com.example.api.model.support.ResponseResult;
import com.example.api.repository.AdminRepository;
import com.example.api.service.AdminService;
import com.example.api.service.LoginLogService;
import com.example.api.utils.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/api/admin")
@Slf4j
public class AdminController {
    //获取日志对象
    Logger logger = LoggerFactory.getLogger(AdminController.class);

    @Resource
    private AdminService adminService;

    @Resource
    private AdminRepository adminRepository;

    @Resource
    private LoginLogService loginLogService;

    @GetMapping("hasInit")
    public boolean hasInit() {
        return adminRepository.existsAdminByRoles(Role.ROLE_SUPER_ADMIN.getValue());
    }

    @PostMapping("/init")
    public Admin init(@RequestBody Admin admin) throws Exception {
        admin.setRoles(Role.ROLE_SUPER_ADMIN.getValue());
        return adminService.save(admin);
    }

    @GetMapping("")
    @PreAuthorize("hasAnyRole('ROLE_SUPER_ADMIN' ,'ROLE_ADMIN')")
    public List<Admin> findAll() {
        return adminService.findAll();
    }

    @DeleteMapping("")
    @PreAuthorize("hasAnyRole('ROLE_SUPER_ADMIN' ,'ROLE_ADMIN')")
    public void delete(String id) {
        adminService.delete(id);
    }

    @PostMapping("")
    @PreAuthorize("hasAnyRole('ROLE_SUPER_ADMIN' ,'ROLE_ADMIN')")
    public Admin save(@RequestBody Admin admin) throws Exception {
        return adminService.save(admin);
    }

    @PostMapping("/login")
    public Map<String, Object> loginByEmail(String type, @RequestBody LoginDto dto, HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<>();
        Admin admin = null;
        String token = null;
        try {
            admin = type.equals("email") ? adminService.loginByEmail(dto) : adminService.loginByPassword(dto);
            token = adminService.createToken(admin,
                    dto.isRemember() ? JwtTokenUtil.REMEMBER_EXPIRATION_TIME : JwtTokenUtil.EXPIRATION_TIME);
        }catch (Exception e){
            throw new Exception("邮箱或密码错误");
        }finally {
            loginLogService.recordLog(dto,admin,request);
        }
        map.put("admin", admin);
        map.put("token", token);
        return map;
    }

    @GetMapping("/sendEmail")
    public ResponseResult sendEmail(String email) throws Exception {
        Boolean flag = adminService.sendEmail(email);
        ResponseResult res = new ResponseResult();
        if (flag){
            res.setMsg("发送成功,请登录邮箱查看");
        }else {
            res.setMsg("发送验证码失败,请检查邮箱服务");
        }
        res.setStatus(flag);
        return res;
    }

}
最近下载更多
5418888  LV3 5月13日
luhong  LV3 5月6日
xp95323  LV15 3月6日
hubugai1  LV11 2月20日
微信网友_6961718086799360  LV2 2024年12月22日
sshiqi  LV2 2024年12月11日
3993zby  LV2 2024年11月11日
初心不负丶方得始终  LV11 2024年8月22日
mlml123  LV6 2024年8月20日
最近浏览更多
zhaopb  LV8 5月17日
5418888  LV3 5月13日
ljy050925 5月13日
暂无贡献等级
luhong  LV3 5月6日
黄志琴  LV1 4月13日
youwuzuichen  LV11 4月3日
aaappp  LV1 3月25日
billy-zuidaima  LV1 3月24日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友