package com.project.zcustom.controller.system;

import cn.dev33.satoken.stp.StpUtil;
import com.project.zcustom.controller.core.AjaxResult;
import com.project.zcustom.domain.system.LoginBody;
import com.project.zcustom.service.system.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @Description
 * @Author bqyang
 * @Date 2025/2/25 18:27
 * @Version 1.0
 */
@RestController
@RequestMapping
public class LoginController {

    @Autowired
    private ISysUserService sysUserService;

    @PostMapping("/doLogin")
    public AjaxResult login(@Validated @RequestBody LoginBody loginBody) {
        String token = sysUserService.login(loginBody.getUsername(), loginBody.getPassword());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("token", token);
        return ajax;
    }

    @PostMapping("/logout")
    public AjaxResult logout(){
        StpUtil.logout();
        return AjaxResult.success("操作成功");
    }

    @GetMapping("/login")
    public AjaxResult logout2(@RequestParam(value = "logout", required = false) String logout){
        StpUtil.logout();
        return AjaxResult.success("操作成功");
    }

    @GetMapping("/getInfo")
    public AjaxResult getInfo(){
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", StpUtil.getLoginId());
        ajax.put("roles", "");
        ajax.put("permissions", "");
        return ajax;
    }

    @GetMapping("/getRouters")
    public AjaxResult getRouters(){
       return AjaxResult.success(new ArrayList<>());
    }
}
