
package com.tmerclub.cloud.auth.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.tmerclub.cloud.api.auth.bo.UidInfoBO;
import com.tmerclub.cloud.auth.manager.TokenStore;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author tmerclub
 * @date 2020/6/30
 */
@RestController
@Tag(name = "token")
public class TokenController {

    @Autowired
    private TokenStore tokenStore;


    @PostMapping("/ua/token/refresh")
    public ServerResponseEntity<Integer> refreshToken() {
        UidInfoBO uidInfoBO = AuthUserContext.get();
        if (uidInfoBO == null) {
            // 登录过期，请重新登录
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        // 获取请求的用户信息
        UidInfoBO uidInfoInToken = tokenStore.getUidInfoInToken();
        // 获取续签时间
        int expiresIn = tokenStore.getExpiresIn(uidInfoBO.getSysType());
        // 续签时间
        StpUtil.renewTimeout(expiresIn);
        // 返回续签时间
        return ServerResponseEntity.success(expiresIn);
    }

}
