package com.zg.project.wisdom.controller;

import com.zg.common.constant.Constants;
import com.zg.framework.security.LoginBody;
import com.zg.framework.security.LoginUser;
import com.zg.framework.security.service.SysLoginService;
import com.zg.framework.security.service.TokenService;
import com.zg.framework.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private TokenService tokenService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        String username = loginBody.getUsername();
        String password = loginBody.getPassword();


        LoginUser loginUser = loginService.loginWithoutCaptcha(username, password);

        String token = tokenService.createToken(loginUser);

        Map<String, Object> data = new HashMap<>();
        data.put("token", token);
        data.put("user", loginUser.getUser());

        return AjaxResult.success(data);
    }
}
