|
@@ -3,62 +3,51 @@ package actions
|
|
|
import (
|
|
|
"fmt"
|
|
|
"strings"
|
|
|
+ "reflect"
|
|
|
|
|
|
"golang-fave/engine/wrapper"
|
|
|
)
|
|
|
|
|
|
-type hRun func(e *Action)
|
|
|
+type hRun func(this *Action)
|
|
|
|
|
|
type Action struct {
|
|
|
- w *wrapper.Wrapper
|
|
|
- list map[string]hRun
|
|
|
+ wrapper *wrapper.Wrapper
|
|
|
}
|
|
|
|
|
|
-func (e *Action) register(name string, handle hRun) {
|
|
|
- e.list[name] = handle
|
|
|
+func (this *Action) write(data string) {
|
|
|
+ (*this.wrapper.W).Write([]byte(data))
|
|
|
}
|
|
|
|
|
|
-func (e *Action) write(data string) {
|
|
|
- (*e.w.W).Write([]byte(data))
|
|
|
-}
|
|
|
-
|
|
|
-func (e *Action) msg_show(title string, msg string) {
|
|
|
- e.write(fmt.Sprintf(
|
|
|
+func (this *Action) msg_show(title string, msg string) {
|
|
|
+ this.write(fmt.Sprintf(
|
|
|
`ModalShowMsg('%s', '%s');`,
|
|
|
strings.Replace(strings.Replace(title, `'`, `’`, -1), `"`, `”`, -1),
|
|
|
strings.Replace(strings.Replace(msg, `'`, `’`, -1), `"`, `”`, -1)))
|
|
|
}
|
|
|
|
|
|
-func (e *Action) msg_success(msg string) {
|
|
|
- e.msg_show("Success", msg)
|
|
|
+func (this *Action) msg_success(msg string) {
|
|
|
+ this.msg_show("Success", msg)
|
|
|
}
|
|
|
|
|
|
-func (e *Action) msg_error(msg string) {
|
|
|
- e.msg_show("Error", msg)
|
|
|
+func (this *Action) msg_error(msg string) {
|
|
|
+ this.msg_show("Error", msg)
|
|
|
}
|
|
|
|
|
|
-func New(w *wrapper.Wrapper) *Action {
|
|
|
- act := Action{w, make(map[string]hRun)}
|
|
|
-
|
|
|
- // Register all action here
|
|
|
- act.register("mysql", action_mysql)
|
|
|
- act.register("signin", action_signin)
|
|
|
-
|
|
|
- return &act
|
|
|
+func New(wrapper *wrapper.Wrapper) *Action {
|
|
|
+ return &Action{wrapper}
|
|
|
}
|
|
|
|
|
|
-func (e *Action) Call() bool {
|
|
|
- if e.w.R.Method != "POST" {
|
|
|
+func (this *Action) Call() bool {
|
|
|
+ if this.wrapper.R.Method != "POST" {
|
|
|
return false
|
|
|
}
|
|
|
- if err := e.w.R.ParseForm(); err == nil {
|
|
|
- action := e.w.R.FormValue("action")
|
|
|
+ if err := this.wrapper.R.ParseForm(); err == nil {
|
|
|
+ action := this.wrapper.R.FormValue("action")
|
|
|
if action != "" {
|
|
|
- function, ok := e.list[action]
|
|
|
- if ok {
|
|
|
- (*e.w.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
|
- (*e.w.W).Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
- function(e)
|
|
|
+ if _, ok := reflect.TypeOf(this).MethodByName("Action_" + action); ok {
|
|
|
+ (*this.wrapper.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
|
+ (*this.wrapper.W).Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
+ reflect.ValueOf(this).MethodByName("Action_" + action).Call([]reflect.Value{})
|
|
|
return true
|
|
|
}
|
|
|
}
|