package com.free.controller; import java.util.Map; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.PathVariable; 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; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.free.dto.LoginDTO; import com.free.entity.system.Admin; import com.free.frame.CustomizationException; import com.free.frame.ExceptionEnum; import com.free.frame.ResponseFormat; import com.free.service.system.AdminService; import com.free.utils.BcryptUtil; import com.free.utils.JwtUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @RestController @RequestMapping("/login") @Api(tags = "登录服务") public class LoginController { /** 管理员登录类型 */ private String adminLoginType = "Admin"; /** 客服人员登录类型 */ private String csLoginType = "User"; @Autowired private AdminService adminService; @ApiOperation("客服服务登录") @PostMapping("/{type}") public Object login(@PathVariable String type, @RequestBody @Valid LoginDTO data) { Map userInfo = null; if (this.adminLoginType.equals(type)) { // 管理员service QueryWrapper qw = new QueryWrapper<>(); qw.select("id", "nick_name", "account", "password", "role", "is_use", "is_super"); qw.eq("account", data.getAccount()); userInfo = this.adminService.getMap(qw); } else if (this.csLoginType.equals(type)) { // 客服service } // 未根据账号信息找到用户数据,抛出异常 if (null == userInfo) { throw new CustomizationException(ExceptionEnum.ACCOUNT_NOT_FOUND); } // 密码验证 String dbPwd = (String) userInfo.get("password"); boolean isSame = BcryptUtil.matchesPassword(data.getPassword(), dbPwd); // 密码不一致,抛出异常 if (!isSame) { throw new CustomizationException(ExceptionEnum.PASSWORD_ERROR); } userInfo.remove("password"); // 将用户类型也放进去 userInfo.put("type", type); // jwt加密,返回 String code = JwtUtil.sign(userInfo); return ResponseFormat.success(code); } }