Some checks failed
continuous-integration/drone/push Build is failing
This reverts commit e35189b203.
85 lines
2.3 KiB
Java
85 lines
2.3 KiB
Java
package com.example.building.controller;
|
|
|
|
import com.example.building.common.Result;
|
|
import com.example.building.service.AuthService;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* 认证控制器
|
|
* 支持:手机号验证码登录、微信扫码登录、支付宝扫码登录
|
|
*/
|
|
@RestController
|
|
@RequestMapping("/api/v1/auth")
|
|
public class AuthController {
|
|
|
|
@Autowired
|
|
private AuthService authService;
|
|
|
|
/**
|
|
* 发送验证码
|
|
*/
|
|
@PostMapping("/send-code")
|
|
public Result<Void> sendCode(@RequestParam String phone) {
|
|
authService.sendCode(phone);
|
|
return Result.success();
|
|
}
|
|
|
|
/**
|
|
* 手机号验证码登录
|
|
*/
|
|
@PostMapping("/phone-login")
|
|
public Result<Map<String, Object>> phoneLogin(@RequestParam String phone, @RequestParam String code) {
|
|
Map<String, Object> result = authService.phoneLogin(phone, code);
|
|
return Result.success(result);
|
|
}
|
|
|
|
/**
|
|
* 微信登录
|
|
*/
|
|
@PostMapping("/wechat")
|
|
public Result<Map<String, Object>> wechatLogin(@RequestParam String code) {
|
|
Map<String, Object> result = authService.wechatLogin(code);
|
|
return Result.success(result);
|
|
}
|
|
|
|
/**
|
|
* 支付宝登录
|
|
*/
|
|
@PostMapping("/alipay")
|
|
public Result<Map<String, Object>> alipayLogin(@RequestParam String code) {
|
|
Map<String, Object> result = authService.alipayLogin(code);
|
|
return Result.success(result);
|
|
}
|
|
|
|
/**
|
|
* 刷新Token
|
|
*/
|
|
@PostMapping("/refresh")
|
|
public Result<Map<String, Object>> refresh(@RequestParam String refreshToken) {
|
|
Map<String, Object> result = authService.refreshToken(refreshToken);
|
|
return Result.success(result);
|
|
}
|
|
|
|
/**
|
|
* 获取当前用户
|
|
*/
|
|
@GetMapping("/me")
|
|
public Result<Map<String, Object>> me(@RequestHeader("X-User-Id") String userId) {
|
|
Map<String, Object> result = authService.getCurrentUser(userId);
|
|
return Result.success(result);
|
|
}
|
|
|
|
/**
|
|
* 退出登录
|
|
*/
|
|
@PostMapping("/logout")
|
|
public Result<Void> logout(@RequestHeader("Authorization") String token) {
|
|
String jwtToken = token.replace("Bearer ", "");
|
|
authService.logout(jwtToken);
|
|
return Result.success();
|
|
}
|
|
}
|