actions.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package actions
  2. import (
  3. "fmt"
  4. "strings"
  5. "reflect"
  6. "golang-fave/engine/wrapper"
  7. )
  8. type hRun func(this *Action)
  9. type Action struct {
  10. wrapper *wrapper.Wrapper
  11. }
  12. func (this *Action) write(data string) {
  13. (*this.wrapper.W).Write([]byte(data))
  14. }
  15. func (this *Action) msg_show(title string, msg string) {
  16. this.write(fmt.Sprintf(
  17. `ModalShowMsg('%s', '%s');`,
  18. strings.Replace(strings.Replace(title, `'`, `’`, -1), `"`, `”`, -1),
  19. strings.Replace(strings.Replace(msg, `'`, `’`, -1), `"`, `”`, -1)))
  20. }
  21. func (this *Action) msg_success(msg string) {
  22. this.msg_show("Success", msg)
  23. }
  24. func (this *Action) msg_error(msg string) {
  25. this.msg_show("Error", msg)
  26. }
  27. func New(wrapper *wrapper.Wrapper) *Action {
  28. return &Action{wrapper}
  29. }
  30. func (this *Action) Call() bool {
  31. if this.wrapper.R.Method != "POST" {
  32. return false
  33. }
  34. if err := this.wrapper.R.ParseForm(); err == nil {
  35. action := this.wrapper.R.FormValue("action")
  36. if action != "" {
  37. if _, ok := reflect.TypeOf(this).MethodByName("Action_" + action); ok {
  38. (*this.wrapper.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  39. (*this.wrapper.W).Header().Set("Content-Type", "text/html; charset=utf-8")
  40. reflect.ValueOf(this).MethodByName("Action_" + action).Call([]reflect.Value{})
  41. return true
  42. }
  43. }
  44. }
  45. return false
  46. }