coin.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package http
  2. import (
  3. "strconv"
  4. "time"
  5. pb "go-common/app/service/main/coin/api"
  6. "go-common/library/ecode"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/metadata"
  9. )
  10. // addCoin
  11. func addCoin(c *bm.Context) {
  12. var (
  13. tp int64
  14. upid int64
  15. )
  16. params := c.Request.Form
  17. aidStr := params.Get("aid")
  18. tpStr := params.Get("avtype")
  19. multiplyStr := params.Get("multiply")
  20. tpidStr := params.Get("typeid")
  21. maxStr := params.Get("max")
  22. upidStr := params.Get("upid")
  23. mid, _ := strconv.ParseInt(params.Get("mid"), 10, 64)
  24. if mid <= 0 {
  25. c.JSON(nil, ecode.RequestErr)
  26. return
  27. }
  28. aid, err := strconv.ParseInt(aidStr, 10, 64)
  29. if err != nil || aid <= 0 {
  30. c.JSON(nil, ecode.RequestErr)
  31. return
  32. }
  33. multiply, err := strconv.ParseInt(multiplyStr, 10, 64)
  34. if err != nil || multiply <= 0 {
  35. c.JSON(nil, ecode.RequestErr)
  36. return
  37. }
  38. if business, ok := c.Get("business"); ok {
  39. tp = business.(int64)
  40. } else {
  41. if tpStr != "" {
  42. if tp, err = strconv.ParseInt(tpStr, 10, 64); err != nil || tp < 1 || tp > 3 {
  43. c.JSON(nil, ecode.RequestErr)
  44. return
  45. }
  46. } else {
  47. tp = 1
  48. }
  49. }
  50. if upidStr != "" {
  51. if upid, err = strconv.ParseInt(upidStr, 10, 64); err != nil || upid <= 0 {
  52. c.JSON(nil, ecode.RequestErr)
  53. return
  54. }
  55. }
  56. typeid, _ := strconv.ParseInt(tpidStr, 10, 64)
  57. max, _ := strconv.ParseInt(maxStr, 10, 8)
  58. c.JSON(nil, coinSvc.WebAddCoin(c, mid, upid, max, aid, tp, multiply, int16(typeid)))
  59. }
  60. func list(c *bm.Context) {
  61. params := c.Request.Form
  62. midStr := params.Get("mid")
  63. tpStr := params.Get("tp")
  64. mid, err := strconv.ParseInt(midStr, 10, 64)
  65. if err != nil {
  66. c.JSON(nil, ecode.RequestErr)
  67. return
  68. }
  69. var tp int64
  70. if business, ok := c.Get("business"); ok {
  71. tp = business.(int64)
  72. } else {
  73. if tp, err = strconv.ParseInt(tpStr, 10, 64); err != nil {
  74. tp = 1
  75. }
  76. }
  77. b, err := coinSvc.GetBusinessName(tp)
  78. if err != nil {
  79. c.JSON(nil, err)
  80. }
  81. arg := &pb.ListReq{Mid: mid, Business: b, Ts: time.Now().Unix()}
  82. c.JSON(coinSvc.List(c, arg))
  83. }
  84. func todayexp(c *bm.Context) {
  85. v := new(pb.TodayExpReq)
  86. if err := c.Bind(v); err != nil {
  87. return
  88. }
  89. res, err := coinSvc.TodayExp(c, v)
  90. c.JSONMap(map[string]interface{}{
  91. "number": res.Exp,
  92. }, err)
  93. }
  94. func updateSettle(c *bm.Context) {
  95. form := c.Request.Form
  96. aidStr := form.Get("aid")
  97. tpStr := form.Get("avtype")
  98. aid, err := strconv.ParseInt(aidStr, 10, 64)
  99. if err != nil {
  100. c.JSON(nil, ecode.RequestErr)
  101. return
  102. }
  103. expSubStr := form.Get("exp_sub")
  104. expSub, err := strconv.ParseInt(expSubStr, 10, 64)
  105. if err != nil {
  106. c.JSON(nil, ecode.RequestErr)
  107. return
  108. }
  109. var tp int64
  110. if business, ok := c.Get("business"); ok {
  111. tp = business.(int64)
  112. } else {
  113. var err error
  114. tp, err = strconv.ParseInt(tpStr, 10, 64)
  115. if err != nil || tp <= 0 {
  116. tp = 1
  117. }
  118. }
  119. c.JSON(nil, coinSvc.UpdateSettle(c, aid, tp, expSub, form.Get("describe")))
  120. }
  121. func coins(c *bm.Context) {
  122. form := c.Request.Form
  123. midStr := form.Get("mid")
  124. upMidStr := form.Get("up_mid")
  125. mid, err := strconv.ParseInt(midStr, 10, 64)
  126. if err != nil {
  127. c.JSON(nil, ecode.RequestErr)
  128. return
  129. }
  130. upMid, err := strconv.ParseInt(upMidStr, 10, 64)
  131. if err != nil {
  132. c.JSON(nil, ecode.RequestErr)
  133. return
  134. }
  135. c.JSON(coinSvc.AddedCoins(c, int64(mid), int64(upMid)))
  136. }
  137. func amend(c *bm.Context) {
  138. form := c.Request.Form
  139. aidStr := form.Get("aid")
  140. tpStr := form.Get("avtype")
  141. coinsStr := form.Get("coins")
  142. aid, err := strconv.ParseInt(aidStr, 10, 64)
  143. if err != nil {
  144. c.JSON(nil, ecode.RequestErr)
  145. return
  146. }
  147. var tp int64
  148. if business, ok := c.Get("business"); ok {
  149. tp = business.(int64)
  150. } else {
  151. var err1 error
  152. tp, err1 = strconv.ParseInt(tpStr, 10, 64)
  153. if err1 != nil || tp <= 0 {
  154. tp = 1
  155. }
  156. }
  157. coins, err := strconv.ParseInt(coinsStr, 10, 64)
  158. if err != nil {
  159. c.JSON(nil, ecode.RequestErr)
  160. return
  161. }
  162. c.JSON(nil, coinSvc.UpdateItemCoins(c, aid, tp, int64(coins)))
  163. }
  164. func ccounts(c *bm.Context) {
  165. params := new(struct {
  166. Aid int64 `json:"aid" form:"aid" validate:"required,min=1"`
  167. Avtype int64 `json:"avtype" form:"avtype"`
  168. IP string `json:"ip" `
  169. })
  170. if err := c.Bind(params); err != nil {
  171. return
  172. }
  173. if business, ok := c.Get("business"); ok {
  174. params.Avtype = business.(int64)
  175. }
  176. if params.Avtype == 0 {
  177. c.JSON(nil, ecode.RequestErr)
  178. return
  179. }
  180. count, err := coinSvc.ItemCoin(c, params.Aid, params.Avtype)
  181. c.JSON(map[string]interface{}{
  182. "count": count,
  183. }, err)
  184. }
  185. // @params AddCoinReq
  186. // @router get /x/internal/v1/coin/add
  187. // @response AddCoinReply
  188. func internalAddCoin(c *bm.Context) {
  189. v := new(pb.AddCoinReq)
  190. if err := c.Bind(v); err != nil {
  191. return
  192. }
  193. v.IP = metadata.String(c, metadata.RemoteIP)
  194. c.JSON(coinSvc.AddCoin(c, v))
  195. }
  196. // @params ItemUserCoinsReq
  197. // @router get /x/internal/v1/coin/item/coins
  198. // @response ItemUserCoinsReply
  199. func itemCoins(c *bm.Context) {
  200. v := new(pb.ItemUserCoinsReq)
  201. if err := c.Bind(v); err != nil {
  202. return
  203. }
  204. c.JSON(coinSvc.ItemUserCoins(c, v))
  205. }