Browse Source

登录模块

v1
Mac 3 years ago
parent
commit
831f462b53
  1. 2
      conf/app.conf
  2. 54
      controllers/BaseController.go
  3. 1
      controllers/LoginController.go
  4. 89
      models/AdminModel.go
  5. 4
      models/UserModel.go
  6. 2
      models/init.go
  7. 0
      static/layui/css/layui.css
  8. 0
      static/layui/css/layui.mobile.css
  9. 0
      static/layui/css/modules/code.css
  10. 0
      static/layui/css/modules/laydate/default/laydate.css
  11. 0
      static/layui/css/modules/layer/default/icon-ext.png
  12. 0
      static/layui/css/modules/layer/default/icon.png
  13. 0
      static/layui/css/modules/layer/default/layer.css
  14. 0
      static/layui/css/modules/layer/default/loading-0.gif
  15. 0
      static/layui/css/modules/layer/default/loading-1.gif
  16. 0
      static/layui/css/modules/layer/default/loading-2.gif
  17. 0
      static/layui/font/iconfont.eot
  18. 0
      static/layui/font/iconfont.svg
  19. 0
      static/layui/font/iconfont.ttf
  20. 0
      static/layui/font/iconfont.woff
  21. 0
      static/layui/images/.DS_Store
  22. 0
      static/layui/lay/modules/carousel.js
  23. 0
      static/layui/lay/modules/code.js
  24. 0
      static/layui/lay/modules/colorpicker.js
  25. 0
      static/layui/lay/modules/element.js
  26. 0
      static/layui/lay/modules/flow.js
  27. 0
      static/layui/lay/modules/form.js
  28. 0
      static/layui/lay/modules/jquery.js
  29. 0
      static/layui/lay/modules/laydate.js
  30. 0
      static/layui/lay/modules/layedit.js
  31. 0
      static/layui/lay/modules/layer.js
  32. 0
      static/layui/lay/modules/laypage.js
  33. 0
      static/layui/lay/modules/laytpl.js
  34. 0
      static/layui/lay/modules/mobile.js
  35. 0
      static/layui/lay/modules/rate.js
  36. 0
      static/layui/lay/modules/slider.js
  37. 0
      static/layui/lay/modules/table.js
  38. 0
      static/layui/lay/modules/tree.js
  39. 0
      static/layui/lay/modules/upload.js
  40. 0
      static/layui/lay/modules/util.js
  41. 0
      static/layui/layui.all.js
  42. 0
      static/layui/layui.js
  43. 11
      sysinit/sysinit.go
  44. 5
      utils/Cache.go
  45. 61
      utils/String.go
  46. 4
      views/common/layout.html
  47. 4
      views/common/layout_edit.html

2
conf/app.conf

@ -10,5 +10,5 @@ db_name = "xcms"
db_user = "root"
db_pwd = "135246"
db_host = "localhost"
db_port = 3306
db_port = 3307
db_charset = "utf8"

54
controllers/BaseController.go

@ -2,18 +2,26 @@ package controllers
import (
"fmt"
"strconv"
"strings"
"github.com/astaxie/beego"
cache "github.com/patrickmn/go-cache"
"github.com/ziyoubiancheng/xcms/consts"
"github.com/ziyoubiancheng/xcms/models"
"github.com/ziyoubiancheng/xcms/utils"
)
type BaseController struct {
beego.Controller
controllerName string
actionName string
curUser models.UserModel
user *models.AdminModel
userId int
userName string
loginName string
pageSize int
allowUrl string
}
func (c *BaseController) Prepare() {
@ -21,6 +29,7 @@ func (c *BaseController) Prepare() {
c.controllerName, c.actionName = c.GetControllerAndAction()
beego.Informational(c.controllerName, c.actionName)
// TODO 保存用户数据
//c.auth()
fmt.Println("beego:perpare:" + c.controllerName + "," + c.actionName)
var m models.MenuModel
@ -66,6 +75,49 @@ func (c *BaseController) listJsonResult(code consts.JsonResultCode, msg string,
c.StopRun()
}
func (c *BaseController) auth() {
arr := strings.Split(c.Ctx.GetCookie("auth"), "|")
c.userId = 0
if len(arr) == 2 {
idstr, password := arr[0], arr[1]
userId, _ := strconv.Atoi(idstr)
if userId > 0 {
var err error
cheUser, found := utils.Cache.Get("uid" + strconv.Itoa(userId))
user := &models.AdminModel{}
if found && cheUser != nil { //从缓存取用户
user = cheUser.(*models.AdminModel)
} else {
user, err = user.GetById(userId)
utils.Cache.Set("uid"+strconv.Itoa(userId), user, cache.DefaultExpiration)
}
if err == nil && password == utils.Md5([]byte(user.Password+user.Salt)) {
c.userId = user.Id
c.loginName = user.LoginName
c.userName = user.RealName
c.user = user
//c.AdminAuth()
}
isHasAuth := strings.Contains(c.allowUrl, c.controllerName+"/"+c.actionName)
//不需要权限检查
noAuth := "ajaxsave/ajaxdel/table/loginin/loginout/getnodes/start/show/ajaxapisave/index/group/public/env/code/apidetail"
isNoAuth := strings.Contains(noAuth, c.actionName)
if isHasAuth == false && isNoAuth == false {
c.Ctx.WriteString("没有权限")
c.jsonResult(consts.JRCodeFailed, "", 0)
return
}
}
}
if c.userId == 0 && (c.controllerName != "login" && c.actionName != "loginin") {
c.redirect(beego.URLFor("LoginController.LoginIn"))
}
}
// 重定向
func (c *BaseController) redirect(url string) {
c.Redirect(url, 302)

1
controllers/LoginController.go

@ -0,0 +1 @@
package controllers

89
models/AdminModel.go

@ -0,0 +1,89 @@
package models
import (
"github.com/astaxie/beego/orm"
)
type AdminModel struct {
Id int
LoginName string
RealName string
Password string
RoleIds string
Phone string
Email string
Salt string
LastLogin int64
LastIp string
Status int
CreateId int
UpdateId int
CreateTime int64
UpdateTime int64
}
func (m *AdminModel) TableName() string {
return "xcms_admin"
}
func AdminModelAdd(a *AdminModel) (int64, error) {
return orm.NewOrm().Insert(a)
}
func (m *AdminModel) GetByName(loginName string) (*AdminModel, error) {
a := new(AdminModel)
err := orm.NewOrm().QueryTable(m.TableName()).Filter("login_name", loginName).One(a)
if err != nil {
return nil, err
}
return a, nil
}
func (m *AdminModel) GetList(page, pageSize int, filters ...interface{}) ([]*AdminModel, int64) {
offset := (page - 1) * pageSize
list := make([]*AdminModel, 0)
query := orm.NewOrm().QueryTable(m.TableName())
if len(filters) > 0 {
l := len(filters)
for k := 0; k < l; k += 2 {
query = query.Filter(filters[k].(string), filters[k+1])
}
}
total, _ := query.Count()
query.OrderBy("-id").Limit(pageSize, offset).All(&list)
return list, total
}
func (m *AdminModel) GetById(id int) (*AdminModel, error) {
r := new(AdminModel)
err := orm.NewOrm().QueryTable(m.TableName()).Filter("id", id).One(r)
if err != nil {
return nil, err
}
return r, nil
}
func (m *AdminModel) Update(fields ...string) error {
if _, err := orm.NewOrm().Update(m, fields...); err != nil {
return err
}
return nil
}
// func RoleAuthDelete(id int) (int64, error) {
// query := orm.NewOrm().QueryTable(TableName("role_auth"))
// return query.Filter("role_id", id).Delete()
// }
// func RoleAuthMultiAdd(ras []*RoleAuth) (n int, err error) {
// query := orm.NewOrm().QueryTable(TableName("role_auth"))
// i, _ := query.PrepareInsert()
// for _, ra := range ras {
// _, err := i.Insert(ra)
// if err == nil {
// n = n + 1
// }
// }
// i.Close() // 别忘记关闭 statement
// return n, err
// }

4
models/UserModel.go

@ -1,6 +1,6 @@
package models
type UserModel struct {
userId string
role int8
UserId int64
Role int8
}

2
models/init.go

@ -7,5 +7,5 @@ import (
// init 初始化
func init() {
orm.RegisterModel(new(MenuModel))
orm.RegisterModel(new(MenuModel), new(AdminModel))
}

0
static/css/layui.css → static/layui/css/layui.css

0
static/css/layui.mobile.css → static/layui/css/layui.mobile.css

0
static/css/modules/code.css → static/layui/css/modules/code.css

0
static/css/modules/laydate/default/laydate.css → static/layui/css/modules/laydate/default/laydate.css

0
static/css/modules/layer/default/icon-ext.png → static/layui/css/modules/layer/default/icon-ext.png

Before

Width:  |  Height:  |  Size: 5.8 KiB

After

Width:  |  Height:  |  Size: 5.8 KiB

0
static/css/modules/layer/default/icon.png → static/layui/css/modules/layer/default/icon.png

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

0
static/css/modules/layer/default/layer.css → static/layui/css/modules/layer/default/layer.css

0
static/css/modules/layer/default/loading-0.gif → static/layui/css/modules/layer/default/loading-0.gif

Before

Width:  |  Height:  |  Size: 5.7 KiB

After

Width:  |  Height:  |  Size: 5.7 KiB

0
static/css/modules/layer/default/loading-1.gif → static/layui/css/modules/layer/default/loading-1.gif

Before

Width:  |  Height:  |  Size: 701 B

After

Width:  |  Height:  |  Size: 701 B

0
static/css/modules/layer/default/loading-2.gif → static/layui/css/modules/layer/default/loading-2.gif

Before

Width:  |  Height:  |  Size: 1.7 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

0
static/font/iconfont.eot → static/layui/font/iconfont.eot

0
static/font/iconfont.svg → static/layui/font/iconfont.svg

Before

Width:  |  Height:  |  Size: 274 KiB

After

Width:  |  Height:  |  Size: 274 KiB

0
static/font/iconfont.ttf → static/layui/font/iconfont.ttf

0
static/font/iconfont.woff → static/layui/font/iconfont.woff

0
static/images/.DS_Store → static/layui/images/.DS_Store vendored

0
static/lay/modules/carousel.js → static/layui/lay/modules/carousel.js vendored

0
static/lay/modules/code.js → static/layui/lay/modules/code.js vendored

0
static/lay/modules/colorpicker.js → static/layui/lay/modules/colorpicker.js vendored

0
static/lay/modules/element.js → static/layui/lay/modules/element.js vendored

0
static/lay/modules/flow.js → static/layui/lay/modules/flow.js vendored

0
static/lay/modules/form.js → static/layui/lay/modules/form.js vendored

0
static/lay/modules/jquery.js → static/layui/lay/modules/jquery.js vendored

0
static/lay/modules/laydate.js → static/layui/lay/modules/laydate.js vendored

0
static/lay/modules/layedit.js → static/layui/lay/modules/layedit.js vendored

0
static/lay/modules/layer.js → static/layui/lay/modules/layer.js vendored

0
static/lay/modules/laypage.js → static/layui/lay/modules/laypage.js vendored

0
static/lay/modules/laytpl.js → static/layui/lay/modules/laytpl.js vendored

0
static/lay/modules/mobile.js → static/layui/lay/modules/mobile.js vendored

0
static/lay/modules/rate.js → static/layui/lay/modules/rate.js vendored

0
static/lay/modules/slider.js → static/layui/lay/modules/slider.js vendored

0
static/lay/modules/table.js → static/layui/lay/modules/table.js vendored

0
static/lay/modules/tree.js → static/layui/lay/modules/tree.js vendored

0
static/lay/modules/upload.js → static/layui/lay/modules/upload.js vendored

0
static/lay/modules/util.js → static/layui/lay/modules/util.js vendored

0
static/layui.all.js → static/layui/layui.all.js

0
static/layui.js → static/layui/layui.js vendored

11
sysinit/sysinit.go

@ -1,5 +1,16 @@
package sysinit
import (
"time"
cache "github.com/patrickmn/go-cache"
"github.com/ziyoubiancheng/xcms/utils"
)
func init() {
//init cache
utils.Cache = cache.New(60*time.Minute, 120*time.Minute)
//init db
initDB()
}

5
utils/Cache.go

@ -0,0 +1,5 @@
package utils
import cache "github.com/patrickmn/go-cache"
var Cache *cache.Cache

61
utils/String.go

@ -0,0 +1,61 @@
package utils
import (
"crypto/md5"
"fmt"
"math/rand"
"regexp"
"time"
)
var emailPattern = regexp.MustCompile("[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[a-zA-Z0-9](?:[\\w-]*[\\w])?")
func Md5(buf []byte) string {
hash := md5.New()
hash.Write(buf)
return fmt.Sprintf("%x", hash.Sum(nil))
}
func SizeFormat(size float64) string {
units := []string{"Byte", "KB", "MB", "GB", "TB"}
n := 0
for size > 1024 {
size /= 1024
n += 1
}
return fmt.Sprintf("%.2f %s", size, units[n])
}
func IsEmail(b []byte) bool {
return emailPattern.Match(b)
}
func Password(len int, pwdO string) (pwd string, salt string) {
salt = GetRandomString(4)
defaultPwd := "george518"
if pwdO != "" {
defaultPwd = pwdO
}
pwd = Md5([]byte(defaultPwd + salt))
return pwd, salt
}
// 生成32位MD5
// func MD5(text string) string{
// ctx := md5.New()
// ctx.Write([]byte(text))
// return hex.EncodeToString(ctx.Sum(nil))
// }
//生成随机字符串
func GetRandomString(lens int) string {
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
bytes := []byte(str)
result := []byte{}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < lens; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
return string(result)
}

4
views/common/layout.html

@ -4,8 +4,8 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>XCMS</title>
<link rel="stylesheet" href="/static/css/layui.css">
<script src="/static/layui.js"></script>
<link rel="stylesheet" href="/static/layui/css/layui.css">
<script src="/static/layui/layui.js"></script>
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">

4
views/common/layout_edit.html

@ -4,8 +4,8 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>XCMS</title>
<link rel="stylesheet" href="/static/css/layui.css">
<script src="/static/layui.js"></script>
<link rel="stylesheet" href="/static/layui/css/layui.css">
<script src="/static/layui/layui.js"></script>
</head>
<body class="layui-layout-body">

Loading…
Cancel
Save