web_appeal.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package http
  2. import (
  3. "go-common/app/interface/main/creative/model/appeal"
  4. "go-common/library/ecode"
  5. "go-common/library/log"
  6. bm "go-common/library/net/http/blademaster"
  7. "go-common/library/net/metadata"
  8. "strconv"
  9. "strings"
  10. )
  11. func webAppealContact(c *bm.Context) {
  12. ip := metadata.String(c, metadata.RemoteIP)
  13. cookie := c.Request.Header.Get("cookie")
  14. // check user
  15. _, ok := c.Get("mid")
  16. if !ok {
  17. c.JSON(nil, ecode.NoLogin)
  18. return
  19. }
  20. ct, err := apSvc.PhoneEmail(c, cookie, ip)
  21. if err != nil {
  22. c.JSON(nil, err)
  23. return
  24. }
  25. c.JSON(map[string]string{
  26. "phone": ct.TelPhone,
  27. "email": ct.Email,
  28. }, nil)
  29. }
  30. func webAppealList(c *bm.Context) {
  31. params := c.Request.Form
  32. state := params.Get("state")
  33. pnStr := params.Get("pn")
  34. psStr := params.Get("ps")
  35. // check
  36. midI, ok := c.Get("mid")
  37. if !ok {
  38. c.JSON(nil, ecode.NoLogin)
  39. return
  40. }
  41. mid, _ := midI.(int64)
  42. pn, err := strconv.Atoi(pnStr)
  43. if err != nil || pn < 1 {
  44. pn = 1
  45. }
  46. ps, err := strconv.Atoi(psStr)
  47. if err != nil || ps <= 10 {
  48. ps = 10
  49. }
  50. all, open, closed, aps, err := apSvc.List(c, mid, pn, ps, state, metadata.String(c, metadata.RemoteIP))
  51. if err != nil {
  52. c.JSON(nil, err)
  53. return
  54. }
  55. c.JSONMap(map[string]interface{}{
  56. "pager": map[string]int{
  57. "current": pn,
  58. "size": ps,
  59. "total": all,
  60. "open_count": open,
  61. "closed_count": closed,
  62. },
  63. "appeals": aps,
  64. }, nil)
  65. }
  66. func webAppealDetail(c *bm.Context) {
  67. params := c.Request.Form
  68. apidStr := params.Get("apid")
  69. // check params
  70. apid, err := strconv.ParseInt(apidStr, 10, 64)
  71. if err != nil {
  72. c.JSON(nil, ecode.RequestErr)
  73. return
  74. }
  75. midI, ok := c.Get("mid")
  76. if !ok {
  77. c.JSON(nil, ecode.NoLogin)
  78. return
  79. }
  80. mid, _ := midI.(int64)
  81. ap, err := apSvc.Detail(c, mid, apid, metadata.String(c, metadata.RemoteIP))
  82. if err != nil {
  83. log.Error("apSvc.Detail error(%v)", err)
  84. c.JSON(nil, err)
  85. return
  86. }
  87. if ap == nil {
  88. c.JSON(nil, ecode.NothingFound)
  89. return
  90. }
  91. c.JSON(ap, nil)
  92. }
  93. func webAppealDown(c *bm.Context) {
  94. params := c.Request.Form
  95. apidStr := params.Get("apid")
  96. ip := metadata.String(c, metadata.RemoteIP)
  97. // check params
  98. apid, err := strconv.ParseInt(apidStr, 10, 64)
  99. if err != nil {
  100. c.JSON(nil, ecode.RequestErr)
  101. return
  102. }
  103. midI, ok := c.Get("mid")
  104. if !ok {
  105. c.JSON(nil, ecode.NoLogin)
  106. return
  107. }
  108. mid, _ := midI.(int64)
  109. var is bool
  110. if is, err = checkStateAndMID(c, mid, apid, ip); err != nil {
  111. log.Error("checkStateAndMID error(%v)", err)
  112. c.JSON(nil, err)
  113. return
  114. }
  115. if !is {
  116. log.Error("checkStateAndMID not your appeal (%v)", is)
  117. c.JSON(nil, ecode.NothingFound)
  118. return
  119. }
  120. c.JSON(nil, apSvc.State(c, mid, apid, appeal.StateUserClosed, ip))
  121. }
  122. func webAppealAdd(c *bm.Context) {
  123. params := c.Request.Form
  124. aidStr := params.Get("aid")
  125. content := params.Get("content")
  126. qq := params.Get("qq")
  127. pics := params.Get("pics")
  128. phone := params.Get("phone")
  129. email := params.Get("email")
  130. typeidStr := params.Get("typeid")
  131. title := params.Get("title")
  132. desc := params.Get("desc")
  133. ip := metadata.String(c, metadata.RemoteIP)
  134. midI, ok := c.Get("mid")
  135. if !ok {
  136. c.JSON(nil, ecode.NoLogin)
  137. return
  138. }
  139. mid, _ := midI.(int64)
  140. aid, err := strconv.ParseInt(aidStr, 10, 64)
  141. if err != nil || aid < 1 {
  142. log.Error("strconv.ParseInt(%s) error(%v)", aidStr, err)
  143. c.JSON(nil, ecode.RequestErr)
  144. return
  145. }
  146. tid, err := strconv.ParseInt(typeidStr, 10, 64)
  147. if err != nil || tid < 1 {
  148. log.Error("strconv.ParseInt(%s) error(%v)", typeidStr, err)
  149. c.JSON(nil, ecode.RequestErr)
  150. return
  151. }
  152. ap := &appeal.BusinessAppeal{
  153. BusinessTypeID: tid,
  154. BusinessMID: mid,
  155. BusinessTitle: title,
  156. BusinessContent: desc,
  157. }
  158. apid, err := apSvc.Add(c, mid, aid, qq, phone, email, content, strings.Replace(pics, ";", ",", -1), ip, ap)
  159. if err != nil {
  160. c.JSON(nil, err)
  161. return
  162. }
  163. if apid > 0 {
  164. var is bool
  165. if is, err = checkStateAndMID(c, mid, apid, ip); err != nil {
  166. log.Error("checkStateAndMID error(%v)", err)
  167. c.JSON(nil, err)
  168. return
  169. }
  170. if !is {
  171. log.Error("checkStateAndMID not your appeal (%v)", is)
  172. c.JSON(nil, ecode.NothingFound)
  173. return
  174. }
  175. c.JSON(nil, apSvc.Reply(c, mid, apid, appeal.ReplySystemEvent, appeal.ReplyMsg, "", ip))
  176. }
  177. }
  178. func webAppealReply(c *bm.Context) {
  179. params := c.Request.Form
  180. apidStr := params.Get("apid")
  181. content := params.Get("content")
  182. pics := params.Get("pics")
  183. ip := metadata.String(c, metadata.RemoteIP)
  184. // check user
  185. midI, ok := c.Get("mid")
  186. if !ok {
  187. c.JSON(nil, ecode.NoLogin)
  188. return
  189. }
  190. mid, _ := midI.(int64)
  191. apid, err := strconv.ParseInt(apidStr, 10, 64)
  192. if err != nil {
  193. log.Error("strconv.ParseInt(%s) error(%v)", apidStr, err)
  194. c.JSON(nil, ecode.RequestErr)
  195. return
  196. }
  197. var is bool
  198. if is, err = checkStateAndMID(c, mid, apid, ip); err != nil {
  199. log.Error("checkStateAndMID error(%v)", err)
  200. c.JSON(nil, err)
  201. return
  202. }
  203. if !is {
  204. log.Error("checkStateAndMID not your appeal (%v)", is)
  205. c.JSON(nil, ecode.NothingFound)
  206. return
  207. }
  208. c.JSON(nil, apSvc.Reply(c, mid, apid, appeal.ReplyUserEvent, content, pics, ip))
  209. }
  210. func webAppealStar(c *bm.Context) {
  211. params := c.Request.Form
  212. apidStr := params.Get("apid")
  213. starStr := params.Get("star")
  214. ip := metadata.String(c, metadata.RemoteIP)
  215. // check params
  216. midI, ok := c.Get("mid")
  217. if !ok {
  218. c.JSON(nil, ecode.NoLogin)
  219. return
  220. }
  221. mid, _ := midI.(int64)
  222. apid, err := strconv.ParseInt(apidStr, 10, 64)
  223. if err != nil {
  224. log.Error("strconv.ParseInt(%s) error(%v)", apidStr, err)
  225. c.JSON(nil, ecode.RequestErr)
  226. return
  227. }
  228. star, err := strconv.ParseInt(starStr, 10, 64)
  229. if err != nil {
  230. log.Error("strconv.ParseInt(%s) error(%v)", starStr, err)
  231. c.JSON(nil, ecode.RequestErr)
  232. return
  233. }
  234. if star < 0 || star > 3 {
  235. c.JSON(nil, ecode.RequestErr)
  236. return
  237. }
  238. var is bool
  239. if is, err = checkMID(c, mid, apid, ip); err != nil {
  240. log.Error("star checkMID error(%v)", err)
  241. c.JSON(nil, err)
  242. return
  243. }
  244. if !is {
  245. log.Error("star checkMID not your appeal (%v)", is)
  246. c.JSON(nil, ecode.NothingFound)
  247. return
  248. }
  249. c.JSON(nil, apSvc.Star(c, mid, apid, star, ip))
  250. }
  251. func checkStateAndMID(c *bm.Context, mid, apid int64, ip string) (is bool, err error) {
  252. ap, err := apSvc.Detail(c, mid, apid, ip)
  253. if err != nil || ap == nil {
  254. return
  255. }
  256. if appeal.IsClosed(ap.State) {
  257. err = ecode.NothingFound
  258. return
  259. }
  260. if ap.Mid == mid {
  261. is = true
  262. }
  263. return
  264. }
  265. func checkMID(c *bm.Context, mid, apid int64, ip string) (is bool, err error) {
  266. ap, err := apSvc.Detail(c, mid, apid, ip)
  267. if err != nil || ap == nil {
  268. return
  269. }
  270. if ap.Mid == mid {
  271. is = true
  272. }
  273. return
  274. }