package com.das.modules.auth.controller;

import com.das.common.result.R;
import com.das.modules.auth.domain.LoginUserDetails;
import com.das.modules.auth.domain.vo.LoginVO;
import com.das.modules.auth.service.ILoginService;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;

/**
 * @author chenhaojie
 * @Description 登录控制层
 */
@Slf4j
@RequestMapping("/api/auth")
@RestController
public class LoginController {
    @Resource
    private ILoginService loginService;
     @Value("${aesKey}")
     String key;


    /**
     * 登录接口
     * @return 退出结果提示信息
     */
    @PostMapping("/login")
    public R<LoginUserDetails> login(@RequestParam String param, HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException {
        LoginUserDetails loginInfo = loginService.login(param,request, response);
        return R.success(loginInfo);
    }

    /**
     * 退出登录接口
     * @return 退出结果提示信息
     */
    @PostMapping("/logout")
    public R<?> logout(HttpServletRequest request) {
        if (loginService.logout(request)) {
            return R.success("退出成功");
        } else {
            return R.fail("退出失败");
        }

    }
    @PostMapping("/refreshToken")
    public R<LoginVO> refreshToken(@NotEmpty(message = "刷新token不允许为空") @RequestParam("refreshToken")String refreshToken){
        LoginVO loginVO  = loginService.refreshToken(refreshToken);
        return R.data(loginVO);
    }


}
