mtfosbot/router/api/api.go

152 lines
3.0 KiB
Go
Raw Normal View History

2018-08-28 03:55:38 +00:00
package api
import (
2018-09-21 13:37:33 +00:00
"strconv"
2018-08-28 03:55:38 +00:00
"git.trj.tw/golang/mtfosbot/model"
"git.trj.tw/golang/mtfosbot/module/apis/twitch"
2018-08-28 03:55:38 +00:00
"git.trj.tw/golang/mtfosbot/module/context"
2018-09-21 13:37:33 +00:00
"git.trj.tw/golang/mtfosbot/module/utils"
2018-09-02 01:15:27 +00:00
"github.com/gin-gonic/contrib/sessions"
2018-08-28 03:55:38 +00:00
"golang.org/x/crypto/bcrypt"
)
// CheckSession -
func CheckSession(c *context.Context) {
session := sessions.Default(c.Context)
userData := session.Get("user")
loginType := session.Get("loginType")
if userData == nil || loginType == nil {
c.LoginFirst(nil)
return
}
var name string
var ltype string
var ok bool
switch userData.(type) {
case model.Account:
name = userData.(model.Account).Account
case twitch.UserInfo:
2018-09-20 17:14:08 +00:00
name = userData.(twitch.UserInfo).Login
default:
c.LoginFirst(nil)
return
}
if ltype, ok = loginType.(string); !ok {
c.LoginFirst(nil)
return
}
loginUser := map[string]string{
"name": name,
"type": ltype,
}
session.Set("loginUser", loginUser)
session.Save()
c.Next()
}
2018-08-28 03:55:38 +00:00
// UserLogin - system user login
func UserLogin(c *context.Context) {
bodyArg := struct {
Account string `form:"account" json:"account" binding:"required"`
Password string `form:"password" json:"password" binding:"required"`
}{}
err := c.BindData(&bodyArg)
if err != nil {
c.DataFormat(nil)
return
}
acc, err := model.GetAccount(bodyArg.Account)
if err != nil {
2018-09-02 01:15:27 +00:00
c.ServerError(`account or password error`)
2018-08-28 03:55:38 +00:00
return
}
err = bcrypt.CompareHashAndPassword([]byte(acc.Password), []byte(bodyArg.Password))
if err != nil {
c.DataFormat(`account or password error`)
return
}
2018-09-02 01:15:27 +00:00
session := sessions.Default(c.Context)
acc.Password = ""
session.Set("user", acc)
2018-09-02 01:15:27 +00:00
session.Set("loginType", "system")
session.Save()
c.Success(nil)
2018-08-28 03:55:38 +00:00
}
2018-09-07 08:33:37 +00:00
// UserLogout -
func UserLogout(c *context.Context) {
session := sessions.Default(c.Context)
session.Clear()
session.Save()
c.Success(nil)
}
2018-09-20 17:14:08 +00:00
// GetSessionData -
func GetSessionData(c *context.Context) {
session := sessions.Default(c.Context)
loginUser := session.Get("loginUser")
if loginUser == nil {
c.LoginFirst(nil)
return
}
user := map[string]interface{}{
"user": loginUser,
}
c.Success(user)
}
2018-09-21 13:37:33 +00:00
// GetLineMessageLog -
func GetLineMessageLog(c *context.Context) {
numP := 1
if p, ok := c.GetQuery("p"); ok {
if i, err := strconv.Atoi(p); err == nil {
numP = i
}
}
numMax := 20
if max, ok := c.GetQuery("max"); ok {
if m, err := strconv.Atoi(max); err == nil {
numMax = m
}
}
g := c.DefaultQuery("group", "")
u := c.DefaultQuery("user", "")
count, err := model.GetLineMessageLogCount()
if err != nil {
c.ServerError(nil)
return
}
page := utils.CalcPage(count, numP, numMax)
logs, err := model.GetLineMessageLogList(g, u, page.Offset, page.Limit)
if err != nil {
c.ServerError(nil)
return
}
resMap := make([]map[string]interface{}, 0)
for _, v := range logs {
m := utils.ToMap(v.LineMessageLog)
m["group_name"] = v.GroupName
m["user_name"] = v.UserName
resMap = append(resMap, m)
}
c.Success(map[string]interface{}{
"list": resMap,
})
}