You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.1 KiB
75 lines
2.1 KiB
import Router from "utils/router.js"
|
|
import { logger } from "@/logger.js"
|
|
import BaseController from "@/base/BaseController.js"
|
|
import AuthService from "../services"
|
|
|
|
export default class AuthController extends BaseController {
|
|
/**
|
|
* 创建基础页面相关路由
|
|
* @returns {Router} 路由实例
|
|
*/
|
|
static createRoutes() {
|
|
const controller = new this()
|
|
const router = new Router({ auth: false })
|
|
|
|
router.get("/login", controller.handleRequest(controller.loginGet))
|
|
router.post("/login", controller.handleRequest(controller.loginPost))
|
|
|
|
router.post("/login/validate/username", controller.handleRequest(controller.validateUsername))
|
|
router.post("/login/validate/password", controller.handleRequest(controller.validatePassword))
|
|
|
|
router.post("/logout", controller.handleRequest(controller.logout), { auth: true })
|
|
|
|
return router
|
|
}
|
|
|
|
constructor() {
|
|
super()
|
|
}
|
|
|
|
// 首页
|
|
async loginGet(ctx) {
|
|
return this.render(ctx, "page/login/index", {})
|
|
}
|
|
|
|
async loginPost(ctx) {
|
|
const res = await AuthService.login(ctx.request.body)
|
|
ctx.session.user = res.user
|
|
ctx.set("HX-Redirect", "/")
|
|
}
|
|
|
|
async validateUsername(ctx) {
|
|
const { username } = ctx.request.body
|
|
const uiPath = "page/login/_ui/username"
|
|
if (username === "") {
|
|
return this.render(ctx, uiPath, {
|
|
value: username,
|
|
error: "用户名不能为空",
|
|
})
|
|
}
|
|
return this.render(ctx, uiPath, {
|
|
value: username,
|
|
error: undefined,
|
|
})
|
|
}
|
|
|
|
async validatePassword(ctx) {
|
|
const { password } = ctx.request.body
|
|
const uiPath = "page/login/_ui/password"
|
|
if (password === "") {
|
|
return this.render(ctx, uiPath, {
|
|
value: password,
|
|
error: "密码不能为空",
|
|
})
|
|
}
|
|
return this.render(ctx, uiPath, {
|
|
value: password,
|
|
error: undefined,
|
|
})
|
|
}
|
|
|
|
async logout(ctx) {
|
|
ctx.session.user = null
|
|
ctx.set("HX-Redirect", "/")
|
|
}
|
|
}
|
|
|