/
base.go
86 lines (76 loc) · 1.6 KB
/
base.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package controllers
import (
"fmt"
m "git.oschina.net/scitc/bbs/models"
"github.com/astaxie/beego"
"path"
"reflect"
"strconv"
"time"
)
const (
DEFAULT = iota
SUCCESS
FAILED
)
const (
INDEX = "admin/index.html"
INFO = "info"
CURRENT_PAGE = 1
PAGE_SIZE = 12
PAGE_COUNT = 0
UPLOAD_FILE = "/file"
)
type SimpleController struct {
beego.Controller
}
func (this *SimpleController) paginator(per int, nums int64) *m.Paginator {
p := m.NewPaginator(this.Ctx.Request, per, nums)
this.Data["paginator"] = p
return p
}
type Info struct {
name string
}
func (this *SimpleController) Prepare() {
u := this.GetSession("user")
if u != nil {
user, ok := u.(m.User)
if ok {
this.Data["user"] = user
this.Data["info"] = SUCCESS
if user.UserType == "系统管理员" {
this.Data["type"] = 1
}
} else {
this.Data["info"] = FAILED
}
} else {
this.Data["info"] = FAILED
}
}
func (this *SimpleController) uploadFile(hd interface{}, formName string) string {
if hd != nil {
timestap := time.Now().UnixNano()
filepath := path.Join("static/files", strconv.FormatInt(timestap, 10)+".jpg")
err := this.SaveToFile(formName, filepath)
if err != nil {
beego.Error("upload file error:", err.Error())
}
return filepath
}
return ""
}
//////util/////////////////
func ToInt64(value interface{}) (d int64, err error) {
val := reflect.ValueOf(value)
switch value.(type) {
case int, int8, int16, int32, int64:
d = val.Int()
case uint, uint8, uint16, uint32, uint64:
d = int64(val.Uint())
default:
err = fmt.Errorf("ToInt64 need numeric not `%T`", value)
}
return
}