/
authcontroller.go
54 lines (49 loc) · 1.32 KB
/
authcontroller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package controllers
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
"github.com/xiliangMa/restapi/models"
"github.com/xiliangMa/restapi/utils"
)
type AuthController struct {
beego.Controller
}
// @Title Signin
// @Description get auth
// @Param name query string true "user name"
// @Param pwd query string true "user pwd"
// @Success 200 {object} models.Result
// @router /signin [post]
func (this *AuthController) Signin() {
name := this.GetString("name")
pwd := this.GetString("pwd")
var ResultData models.Result
result, code := utils.GenToken(name, pwd)
ResultData.Code = code
if code != utils.Success {
ResultData.Message = result
} else {
ResultData.Data = result
}
this.Data["json"] = ResultData
this.ServeJSON(false)
}
// @Title authorize
// @Description authorize
// @Param token header string true "Auth token"
// @Success 200 {object} models.Result
// @router /authorize [post]
func (this *AuthController) Authorize() {
token := this.GetString("token")
var ResultData models.Result
result, code := utils.CheckToken(token)
ResultData.Code = code
if code != utils.Success {
ResultData.Message = result
logs.Error("Authorize failed, code: %d, err: %s", ResultData.Code, ResultData.Message)
} else {
ResultData.Data = result
}
this.Data["json"] = ResultData
this.ServeJSON(false)
}