forked from wbteve/easygo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.go
More file actions
209 lines (180 loc) · 4.01 KB
/
controller.go
File metadata and controls
209 lines (180 loc) · 4.01 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package easygo
import (
"fmt"
"html/template"
"io/ioutil"
// "strconv"
"log"
"net/http"
//"time"
"github.com/matyhtf/easygo/php"
"encoding/json"
_ "github.com/go-sql-driver/mysql"
"github.com/lunny/xorm"
// "strings"
)
const ERR_PARAM = 1001
//基础controller
type Layout struct {
Fn string
Title string
HeaderScript []string
FooterScript []string
}
type Controller struct {
Resp http.ResponseWriter
Req *http.Request
Name string
Action string
Uid int
layoutFn string
Layout Layout
jsonEncoder *json.Encoder
Session *SessionType
DB *xorm.Engine
tpl *php.Task
}
//启动时初始化
func NewController() *Controller {
c := new(Controller)
//前端
c.Layout.Fn = "template/layout/main.html.mustache"
c.Layout.HeaderScript = []string{}
c.jsonEncoder = json.NewEncoder(c.Resp)
return c
}
//request初始化
func (c *Controller) OnRequest(resp http.ResponseWriter, req *http.Request, action string) {
c.Req = req
c.Resp = resp
c.DB = Server.DB
c.Action = action
//启动session
c.Session = NewSession(c.Req, c.Resp)
c.tpl = php.NewTask(Server.PHP)
c.Header("Cache-Control", "must-revalidate, private, max-age=0")
err := c.Req.ParseForm()
if err != nil {
log.Println("parse form fail")
}
}
//request结束
func (c *Controller) OnFinish() {
c.Session.Save()
}
func (c *Controller) Init() {
}
func (c *Controller) Destroy() {
}
func (c *Controller) IsLogin() bool {
var flag = true
i := c.Session.Get("userId")
if i == "" {
flag = false
}
return flag
}
func (c *Controller) Redirect(url string) {
http.Redirect(c.Resp, c.Req, url, 302)
}
func (c *Controller) Display(tpl string, data interface{}) error {
bytes, err := ioutil.ReadFile(tpl)
if err != nil {
return err
}
t, _ := template.New("").Parse(string(bytes))
return t.Execute(c.Resp, data)
}
func (c *Controller) Render(tpl string) {
c.Header("Content-Type", "text/html;charset="+Server.Charset)
str, err := c.tpl.Render(tpl)
if err != nil {
c.Echo(err.Error())
} else {
c.Echo(str)
}
}
func (c *Controller) CookieSet(cookie *http.Cookie) {
http.SetCookie(c.Resp, cookie)
}
func (c *Controller) CookieDel(key string) {
var cookie http.Cookie
cookie.Name = key
cookie.Value = ""
http.SetCookie(c.Resp, &cookie)
}
func (c *Controller) Assign(name string, data interface{}) bool {
err := c.tpl.Assign(name, data)
if err == nil {
return true
} else {
log.Println(err.Error())
return false
}
}
func (c *Controller) GetIP() string {
return c.Req.RemoteAddr
}
func (c *Controller) Echo(str string) {
fmt.Fprint(c.Resp, str)
}
func (c *Controller) Message(code int, msg string) {
c.Echo(fmt.Sprintf("{\"code\": %d, \"msg\": \"%s\"}", code, msg))
}
func (c *Controller) Header(key string, value string) {
c.Resp.Header().Add(key, value)
}
func (c *Controller) EchoJson(data interface{}) {
b, err := json.Marshal(data)
if err != nil {
log.Println(err.Error())
} else {
c.Echo(string(b))
}
}
//DoAction
func (c *Controller) OnAction(action string) {
}
func (c *Controller) EchoApiError(m string) {
var data = map[string]string{
"Status": "Error",
"Msg": m,
}
b, err := json.Marshal(data)
if err != nil {
fmt.Println(err.Error())
} else {
c.Echo(string(b))
}
}
func (c *Controller) EchoApiData(data interface{}) {
var dataString string
b, err := json.Marshal(data)
if err != nil {
c.Echo("panic : failed encode data")
} else {
dataString = string(b)
}
var result = map[string]string{
"Status": "Success",
"Msg": "",
"data": dataString,
}
r, err := json.Marshal(result)
if err != nil {
c.EchoApiError("panic : " + err.Error())
} else {
c.Echo(string(r))
}
}
func (c *Controller) RenderJson(context ...interface{}) {
c.jsonEncoder.Encode(context)
}
func (c *Controller) Form(key string) string {
return c.Req.FormValue(key)
}
func NotFound(resp http.ResponseWriter, msg string) {
resp.WriteHeader(http.StatusNotFound)
resp.Header().Add("Content-Type", "text/html;charset="+Server.Charset)
resp.Write([]byte(msg))
}