package com.jiayue.insu.incloud.permisson;

import cn.dev33.satoken.stp.StpUtil;
import com.jiayue.insu.common.core.util.R;
import com.jiayue.insu.incloud.entity.User;
import com.jiayue.insu.incloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
public class Login {

    @Autowired
    private UserService userService;

    @PostMapping("/doLogin")
    public R doLogin(@RequestBody User user ){
        User userTar = userService.findByNameAndPwd(user.getUsername(), user.getPassword());
        if(userTar!=null){
            StpUtil.login(userTar.getUsername());
            return R.ok(StpUtil.getTokenInfo());
        }else{
            return R.ok(null);
        }
    }

    @PostMapping("/logout")
    public R logout() {
        try {
            StpUtil.logout();
        }catch (Exception e){
            e.printStackTrace();
        }

        return R.ok();
    }
    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @GetMapping("/isLogin")
    public R isLogin() {
        return R.ok( StpUtil.isLogin());

    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @GetMapping("/getLoginId")
    public R getLoginId() {
        return R.ok( StpUtil.getLoginId());

    }



}
