plugin_web_request.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package core
  2. import (
  3. "io/ioutil"
  4. "time"
  5. "github.com/cdle/sillyplus/utils"
  6. "github.com/gin-gonic/gin"
  7. "github.com/goccy/go-json"
  8. )
  9. var web_sessions = MakeBucket("web_sessions")
  10. type Web struct {
  11. uuid string
  12. method string
  13. path string
  14. handles []func(*Request, *Response)
  15. }
  16. var webs []Web
  17. func CancelPluginWebs(uuid string) {
  18. for i := range webs {
  19. if webs[i].uuid == uuid {
  20. webs[i].handles = nil
  21. }
  22. }
  23. }
  24. type Request struct {
  25. c *gin.Context
  26. ress [][]string
  27. parsedForm bool
  28. handled bool
  29. uuid string
  30. bodyData []byte
  31. _event string
  32. Mark interface{}
  33. }
  34. func (r *Request) Body(tp string) interface{} {
  35. if len(r.bodyData) == 0 {
  36. r.bodyData, _ = ioutil.ReadAll(r.c.Request.Body)
  37. }
  38. if tp == "bytes" {
  39. return r.bodyData
  40. }
  41. return string(r.bodyData)
  42. }
  43. func (r *Request) Json() interface{} {
  44. var i interface{}
  45. if len(r.bodyData) == 0 {
  46. r.bodyData, _ = ioutil.ReadAll(r.c.Request.Body)
  47. }
  48. if json.Unmarshal(r.bodyData, &i) != nil {
  49. return nil
  50. }
  51. return i
  52. }
  53. func (r *Request) Ip() string {
  54. return r.c.ClientIP()
  55. }
  56. func (r *Request) Event() string {
  57. return r._event
  58. }
  59. func (r *Request) OriginalUrl() string {
  60. return r.c.Request.URL.String()
  61. }
  62. func (r *Request) Query(param string) string {
  63. return r.c.Query(param)
  64. }
  65. func (r *Request) Param(i int) string {
  66. return r.ress[i-1][1]
  67. }
  68. func (r *Request) Querys() map[string][]string {
  69. return r.c.Request.URL.Query()
  70. }
  71. func (r *Request) PostForm(s string) string {
  72. if !r.parsedForm {
  73. r.c.Request.ParseForm()
  74. }
  75. return r.c.PostForm(s)
  76. }
  77. func (r *Request) PostForms() map[string][]string {
  78. if !r.parsedForm {
  79. r.c.Request.ParseForm()
  80. }
  81. return r.c.Request.PostForm
  82. }
  83. func (r *Request) Path() string {
  84. return r.c.Request.URL.Path
  85. }
  86. func (r *Request) Header(s string) string {
  87. return r.c.GetHeader(s)
  88. }
  89. func (r *Request) Get(s string) string {
  90. return r.c.GetHeader(s)
  91. }
  92. func (r *Request) Is(s string) bool { //判断是否传入的MIME类型
  93. return true
  94. }
  95. func (r *Request) Headers() map[string][]string {
  96. return r.c.Request.Header
  97. }
  98. func (r *Request) Method() string {
  99. return r.c.Request.Method
  100. }
  101. func (r *Request) Logined() bool {
  102. auth, _ := CheckAuth(r.Cookie("token"))
  103. return auth != nil
  104. }
  105. func (r *Request) Cookie(s string) string {
  106. var cookie, _ = r.c.Cookie(s)
  107. return cookie
  108. }
  109. func (r *Request) Cookies() map[string]string {
  110. var cookies = map[string]string{}
  111. for _, v := range r.c.Request.Cookies() {
  112. cookies[v.Name] = v.Value
  113. }
  114. return cookies
  115. }
  116. func (r *Request) Continue() {
  117. r.handled = false
  118. }
  119. func (r *Request) SetSession(k, v string) string {
  120. j := map[string]interface{}{}
  121. json.Unmarshal(web_sessions.GetBytes(r.uuid), &j)
  122. j[k] = v
  123. j["time"] = time.Now().Unix()
  124. _, _, err := web_sessions.Set(r.uuid, utils.JsonMarshal(j))
  125. if err != nil {
  126. return err.Error()
  127. }
  128. return ""
  129. }
  130. func (r *Request) GetSession(k string) string {
  131. j := map[string]interface{}{}
  132. json.Unmarshal(web_sessions.GetBytes(r.uuid), &j)
  133. v, ok := j[k].(string)
  134. if !ok {
  135. return ""
  136. }
  137. return v
  138. }
  139. func (r *Request) GetSessionID() string {
  140. return r.uuid
  141. }
  142. func (r *Request) DestroySession() string {
  143. return ""
  144. }