package com.dl.web.controller.system; import cn.dev33.satoken.annotation.SaIgnore; import com.dl.common.constant.Constants; import com.dl.common.core.domain.R; import com.dl.common.core.domain.entity.SysMenu; import com.dl.common.core.domain.entity.SysUser; import com.dl.common.core.domain.model.EmailLoginBody; import com.dl.common.core.domain.model.LoginBody; import com.dl.common.core.domain.model.LoginUser; import com.dl.common.core.domain.model.SmsLoginBody; import com.dl.common.helper.LoginHelper; import com.dl.system.domain.vo.RouterVo; import com.dl.system.service.ISysMenuService; import com.dl.system.service.ISysUserService; import com.dl.system.service.SysLoginService; import lombok.RequiredArgsConstructor; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import javax.validation.constraints.NotBlank; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 登录验证 * * @author Lion Li */ @Validated @RequiredArgsConstructor @RestController public class SysLoginController { private final SysLoginService loginService; private final ISysMenuService menuService; private final ISysUserService userService; /** * 登录方法 * * @param loginBody 登录信息 * @return 结果 */ @SaIgnore @PostMapping("/login") public R> login(@Validated @RequestBody LoginBody loginBody) { Map ajax = new HashMap<>(); // 生成令牌 String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), loginBody.getUuid()); ajax.put(Constants.TOKEN, token); return R.ok(ajax); } /** * 短信登录 * * @param smsLoginBody 登录信息 * @return 结果 */ @SaIgnore @PostMapping("/smsLogin") public R> smsLogin(@Validated @RequestBody SmsLoginBody smsLoginBody) { Map ajax = new HashMap<>(); // 生成令牌 String token = loginService.smsLogin(smsLoginBody.getPhonenumber(), smsLoginBody.getSmsCode()); ajax.put(Constants.TOKEN, token); return R.ok(ajax); } /** * 邮件登录 * * @param body 登录信息 * @return 结果 */ @PostMapping("/emailLogin") public R> emailLogin(@Validated @RequestBody EmailLoginBody body) { Map ajax = new HashMap<>(); // 生成令牌 String token = loginService.emailLogin(body.getEmail(), body.getEmailCode()); ajax.put(Constants.TOKEN, token); return R.ok(ajax); } /** * 小程序登录(示例) * * @param xcxCode 小程序code * @return 结果 */ @SaIgnore @PostMapping("/xcxLogin") public R> xcxLogin(@NotBlank(message = "{xcx.code.not.blank}") String xcxCode) { Map ajax = new HashMap<>(); // 生成令牌 String token = loginService.xcxLogin(xcxCode); ajax.put(Constants.TOKEN, token); return R.ok(ajax); } /** * 退出登录 */ @SaIgnore @PostMapping("/logout") public R logout() { loginService.logout(); return R.ok("退出成功"); } /** * 获取用户信息 * * @return 用户信息 */ @GetMapping("getInfo") public R> getInfo() { LoginUser loginUser = LoginHelper.getLoginUser(); SysUser user = userService.selectUserById(loginUser.getUserId()); Map ajax = new HashMap<>(); ajax.put("user", user); ajax.put("roles", loginUser.getRolePermission()); ajax.put("permissions", loginUser.getMenuPermission()); return R.ok(ajax); } /** * 获取路由信息 * * @return 路由信息 */ @GetMapping("getRouters") public R> getRouters() { String userId = LoginHelper.getUserId(); List menus = menuService.selectMenuTreeByUserId(userId); return R.ok(menuService.buildMenus(menus)); } }