package com.hzs.member.login.controller;

import com.hzs.common.auth.LoginBody;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.security.auth.AuthMemberUtil;
import com.hzs.common.security.service.MemberTokenService;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.base.dto.LoginMember;
import com.hzs.member.login.service.IMemberLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 会员端 token 控制
 *
 */
@RequestMapping("/auth/api")
@RestController
public class MemberTokenController {

    @Autowired
    private MemberTokenService memberTokenService;

    @Autowired
    private IMemberLoginService iMemberLoginService;

//    /**
//     * 会员登录
//     *
//     * @param form
//     * @return
//     */
//    @PostMapping("/login")
//    public AjaxResult login(@RequestBody LoginBody form) {
//        // 用户登录
//        LoginMember loginMember = iMemberLoginService.login(form.getUsername(), form.getPassword());
//
//        // 获取登录token
//        return AjaxResult.success(memberTokenService.createToken(loginMember));
//    }

    /**
     * 会员登出
     *
     * @param request
     * @return
     */
    @DeleteMapping("/logout")
    public AjaxResult logout(HttpServletRequest request) {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token)) {
            // 删除用户缓存记录
            AuthMemberUtil.logoutByToken(token);
        }
        return AjaxResult.success();
    }

}
