offlineactivity.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package http
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/csv"
  6. "encoding/json"
  7. "fmt"
  8. "go-common/app/admin/main/growup/dao/shell"
  9. "go-common/app/admin/main/growup/model"
  10. "go-common/app/admin/main/growup/model/offlineactivity"
  11. "go-common/library/ecode"
  12. "go-common/library/log"
  13. "go-common/library/net/http/blademaster"
  14. "go-common/library/net/http/blademaster/render"
  15. "io/ioutil"
  16. "net/http"
  17. "reflect"
  18. )
  19. func bmHTTPErrorWithMsg(c *blademaster.Context, err error, msg string) {
  20. if c.IsAborted() {
  21. return
  22. }
  23. c.Error = err
  24. bcode := ecode.Cause(err)
  25. if msg == "" {
  26. msg = err.Error()
  27. }
  28. c.Render(http.StatusOK, render.JSON{
  29. Code: bcode.Code(),
  30. Message: msg,
  31. Data: nil,
  32. })
  33. }
  34. func checkCookieFun(c *blademaster.Context) (err error) {
  35. _, _, err = checkCookie(c)
  36. return
  37. }
  38. // service的函数原型
  39. type serviceFunc func(context context.Context, arg interface{}) (res interface{}, err error)
  40. // response writer
  41. type responseWriter func(c *blademaster.Context, arg interface{}, res interface{}, err error)
  42. type argParser func(c *blademaster.Context, arg interface{}) (err error)
  43. func argGetParser(c *blademaster.Context, arg interface{}) (err error) {
  44. err = c.Bind(arg)
  45. return
  46. }
  47. func argPosJSONParser(c *blademaster.Context, arg interface{}) (err error) {
  48. respBody, _ := ioutil.ReadAll(c.Request.Body)
  49. if err = json.Unmarshal(respBody, arg); err != nil {
  50. log.Error("json unmarshal fail, err=%v", err)
  51. }
  52. return
  53. }
  54. func jsonWriter(c *blademaster.Context, arg interface{}, res interface{}, err error) {
  55. c.JSON(res, err)
  56. }
  57. func csvWriter(c *blademaster.Context, arg interface{}, res interface{}, err error) {
  58. formater, ok := res.(model.CsvFormatter)
  59. if !ok {
  60. log.Error("res cannot convert CsvFommater, res type=%s", reflect.TypeOf(res).Name())
  61. c.String(ecode.ServerErr.Code(), "res cannot convert CsvFommater")
  62. return
  63. }
  64. c.Writer.Header().Set("Content-Type", "application/csv")
  65. c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment;filename=\"%s\"", formater.GetFileName()))
  66. var buf = &bytes.Buffer{}
  67. var csvWriter = csv.NewWriter(buf)
  68. formater.ToCsv(csvWriter)
  69. csvWriter.Flush()
  70. c.Writer.Write(buf.Bytes())
  71. }
  72. func decideWriter(c *blademaster.Context, arg interface{}, res interface{}, err error) {
  73. var writer responseWriter
  74. var decider, ok = arg.(model.ExportArgInterface)
  75. if ok {
  76. switch decider.ExportFormat() {
  77. case "json":
  78. writer = jsonWriter
  79. case "csv":
  80. writer = csvWriter
  81. }
  82. }
  83. if writer != nil {
  84. writer(c, arg, res, err)
  85. } else {
  86. jsonWriter(c, arg, res, err)
  87. }
  88. }
  89. func httpGetFuncWithWriter(arg interface{}, sfunc serviceFunc, description string, writer responseWriter) func(*blademaster.Context) {
  90. return httpGenerateFunc(arg, sfunc, description, argGetParser, writer)
  91. }
  92. func httpGenerateFunc(arg interface{}, sfunc serviceFunc, description string, parser argParser, writer responseWriter, preFuncs ...func(*blademaster.Context) error) func(*blademaster.Context) {
  93. return func(c *blademaster.Context) {
  94. var res interface{}
  95. var err error
  96. var errMsg string
  97. exitswitch:
  98. switch {
  99. default:
  100. for _, f := range preFuncs {
  101. err = f(c)
  102. if err != nil {
  103. log.Error("request err=%s, arg=%+v", err, arg)
  104. break exitswitch
  105. }
  106. }
  107. if err = parser(c, arg); err != nil {
  108. log.Error("%s, request argument bind fail, err=%v", description, err)
  109. errMsg = fmt.Sprintf("wrong argument, %s", err.Error())
  110. err = ecode.RequestErr
  111. break
  112. }
  113. var scoreRes, e = sfunc(c, arg)
  114. err = e
  115. if e != nil {
  116. errMsg = err.Error()
  117. log.Error("%s query fail, req=%+v, err=%+v", description, arg, err)
  118. break
  119. }
  120. log.Info("%s query ok, req=%+v, result=%+v", description, arg, scoreRes)
  121. res = scoreRes
  122. }
  123. if err != nil {
  124. bmHTTPErrorWithMsg(c, err, errMsg)
  125. } else {
  126. writer(c, arg, res, err)
  127. }
  128. }
  129. }
  130. func httpPostFunCheckCookie(arg interface{}, sfunc serviceFunc, description string, preFuncs ...func(*blademaster.Context) error) func(*blademaster.Context) {
  131. preFuncs = append(preFuncs, checkCookieFun)
  132. return httpGenerateFunc(arg, sfunc, description, argPosJSONParser, jsonWriter, preFuncs...)
  133. }
  134. // 贝壳回调
  135. func offlineactivityShellCallback(c *blademaster.Context) {
  136. var err error
  137. var v = new(shell.OrderCallbackParam)
  138. if e := c.Bind(v); e != nil {
  139. err = e
  140. log.Error("parse arg error, err=%s", err)
  141. return
  142. }
  143. var result = "SUCCESS"
  144. if err = svr.ShellCallback(c, v); err != nil {
  145. log.Error("shell call back err, err=%s, arg=%v", err, v)
  146. result = "FAIL"
  147. }
  148. c.String(0, result)
  149. }
  150. func offlineactivityAdd(c *blademaster.Context) {
  151. httpPostFunCheckCookie(
  152. new(offlineactivity.AddActivityArg),
  153. func(context context.Context, arg interface{}) (res interface{}, err error) {
  154. return svr.AddOfflineActivity(context, arg.(*offlineactivity.AddActivityArg))
  155. },
  156. "offlineactivityAdd")(c)
  157. }
  158. func offlineactivityPreAdd(c *blademaster.Context) {
  159. httpPostFunCheckCookie(
  160. new(offlineactivity.AddActivityArg),
  161. func(context context.Context, arg interface{}) (res interface{}, err error) {
  162. return svr.PreAddOfflineActivity(context, arg.(*offlineactivity.AddActivityArg))
  163. },
  164. "offlienactivityPreAdd")(c)
  165. }
  166. func offlineactivityQueryActivity(c *blademaster.Context) {
  167. httpGetFuncWithWriter(
  168. new(offlineactivity.QueryActivityByIDArg),
  169. func(context context.Context, arg interface{}) (res interface{}, err error) {
  170. return svr.OfflineActivityQueryActivity(context, arg.(*offlineactivity.QueryActivityByIDArg))
  171. },
  172. "offlineactivityQueryActivity",
  173. decideWriter)(c)
  174. }
  175. func offlineactivityQueryUpBonusSummary(c *blademaster.Context) {
  176. httpGetFuncWithWriter(
  177. new(offlineactivity.QueryUpBonusByMidArg),
  178. func(context context.Context, arg interface{}) (res interface{}, err error) {
  179. return svr.OfflineActivityQueryUpBonusSummary(context, arg.(*offlineactivity.QueryUpBonusByMidArg))
  180. },
  181. "offlineactivityQueryUpBonus",
  182. decideWriter)(c)
  183. }
  184. func offlineactivityQueryUpBonusActivity(c *blademaster.Context) {
  185. httpGetFuncWithWriter(
  186. new(offlineactivity.QueryUpBonusByMidArg),
  187. func(context context.Context, arg interface{}) (res interface{}, err error) {
  188. return svr.OfflineActivityQueryUpBonusByActivity(context, arg.(*offlineactivity.QueryUpBonusByMidArg))
  189. },
  190. "offlineactivityQueryUpBonusActivity",
  191. decideWriter)(c)
  192. }
  193. func offlineactivityQueryMonth(c *blademaster.Context) {
  194. httpGetFuncWithWriter(
  195. new(offlineactivity.QueryActvityMonthArg),
  196. func(context context.Context, arg interface{}) (res interface{}, err error) {
  197. return svr.OfflineActivityQueryActivityByMonth(context, arg.(*offlineactivity.QueryActvityMonthArg))
  198. },
  199. "offlineactivityQueryMonth",
  200. decideWriter)(c)
  201. }