actions.go 1.3 KB

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