answer.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package http
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "go-common/app/interface/main/answer/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/xstr"
  11. )
  12. func localized(c *bm.Context) string {
  13. langs := detectLocalizedWeb(c)
  14. if len(langs) == 0 {
  15. return model.LangZhCN
  16. }
  17. switch langs[0] {
  18. case model.LangZhTW:
  19. return model.LangZhTW
  20. case model.LangZhHK:
  21. return model.LangZhTW
  22. default:
  23. return model.LangZhCN
  24. }
  25. }
  26. func checkBirthDay(c *bm.Context) {
  27. var mid, _ = c.Get("mid")
  28. if ok := svc.CheckBirthday(c, mid.(int64)); !ok {
  29. c.JSON(nil, ecode.MemberBirthdayInfoIsNull)
  30. return
  31. }
  32. c.JSON(nil, nil)
  33. }
  34. // checkPro check second step answers
  35. func checkPro(c *bm.Context) {
  36. var (
  37. err error
  38. ids []int64
  39. params = c.Request.Form
  40. qIds = params.Get("qs_ids")
  41. mid, _ = c.Get("mid")
  42. ansHash = make(map[int64]string)
  43. )
  44. qidArr := strings.Split(qIds, ",")
  45. for _, qid := range qidArr {
  46. id, _ := strconv.ParseInt(qid, 10, 64)
  47. ansHash[id] = params.Get("ans_hash_" + qid)
  48. ids = append(ids, id)
  49. }
  50. hid, err := svc.ProCheck(c, mid.(int64), ids, ansHash, localized(c))
  51. if err != nil {
  52. log.Error("svc.ProCheck(%d,%s,%+v) error(%+v)", mid.(int64), xstr.JoinInts(ids), ansHash, err)
  53. c.JSON(nil, err)
  54. return
  55. }
  56. c.JSON(fmt.Sprintf(model.ProPassed, hid), nil)
  57. }
  58. // checkBase check first step answers
  59. func checkBase(c *bm.Context) {
  60. var (
  61. err error
  62. ids []int64
  63. params = c.Request.Form
  64. qIds = params.Get("qs_ids")
  65. mid, _ = c.Get("mid")
  66. ansHash = make(map[int64]string)
  67. )
  68. qidsArr := strings.Split(qIds, ",")
  69. for _, qid := range qidsArr {
  70. id, _ := strconv.ParseInt(qid, 10, 64)
  71. ansHash[id] = params.Get("ans_hash_" + qid)
  72. ids = append(ids, id)
  73. }
  74. req, err := svc.CheckBase(c, mid.(int64), ids, ansHash, localized(c))
  75. if err != nil {
  76. log.Error("svc.BaseCheck(%d,%s,%+v) error(%+v)", mid.(int64), xstr.JoinInts(ids), ansHash, err)
  77. c.JSON(nil, err)
  78. return
  79. }
  80. res := make(map[string]interface{})
  81. if req != nil && len(req.QidList) > 0 {
  82. res["next"] = false
  83. res["ids"] = req.QidList
  84. c.JSON(res, nil)
  85. return
  86. }
  87. res["next"] = true
  88. c.JSON(res, nil)
  89. }
  90. // checkExtra extra question check.
  91. func checkExtra(c *bm.Context) {
  92. var (
  93. ids []int64
  94. params = c.Request.Form
  95. qIds = params.Get("qs_ids")
  96. mid, ok = c.Get("mid")
  97. ansHash = make(map[int64]string)
  98. ua = c.Request.Header.Get("User-Agent")
  99. refer = c.Request.Header.Get("Referer")
  100. )
  101. if !ok {
  102. c.JSON(nil, ecode.RequestErr)
  103. return
  104. }
  105. qidsArr := strings.Split(qIds, ",")
  106. for _, qid := range qidsArr {
  107. id, _ := strconv.ParseInt(qid, 10, 64)
  108. h := params.Get("ans_hash_" + qid)
  109. if h != "" {
  110. ansHash[id] = params.Get("ans_hash_" + qid)
  111. ids = append(ids, id)
  112. }
  113. }
  114. buvid := c.Request.Header.Get("Buvid")
  115. if buvid == "" {
  116. cookie, _ := c.Request.Cookie("buvid3")
  117. if cookie != nil {
  118. buvid = cookie.Value
  119. }
  120. }
  121. c.JSON(nil, svc.ExtraCheck(c, mid.(int64), ids, ansHash, ua, localized(c), refer, buvid))
  122. }
  123. // getBaseQ get first step questions
  124. func baseQus(c *bm.Context) {
  125. var (
  126. mid, ok = c.Get("mid")
  127. mobile = strings.Contains(c.Request.UserAgent(), model.MobileUserAgentFlag)
  128. )
  129. if !ok {
  130. c.JSON(nil, ecode.NoLogin)
  131. return
  132. }
  133. c.JSON(svc.BaseQ(c, mid.(int64), localized(c), mobile))
  134. }
  135. // getProType get second step question types
  136. func proType(c *bm.Context) {
  137. mid, ok := c.Get("mid")
  138. if !ok {
  139. c.JSON(nil, ecode.NoLogin)
  140. return
  141. }
  142. c.JSON(svc.ProType(c, mid.(int64), localized(c)))
  143. }
  144. // getQstByType get second step questions
  145. func proQus(c *bm.Context) {
  146. var (
  147. params = c.Request.Form
  148. mid, ok = c.Get("mid")
  149. tIdsStr = params.Get("type_ids")
  150. mobile = strings.Contains(c.Request.UserAgent(), model.MobileUserAgentFlag)
  151. )
  152. if !ok {
  153. c.JSON(nil, ecode.NoLogin)
  154. return
  155. }
  156. c.JSON(svc.ConvertProQues(c, mid.(int64), tIdsStr, localized(c), mobile))
  157. }
  158. // extraQus extra question.
  159. func extraQus(c *bm.Context) {
  160. var (
  161. mid, ok = c.Get("mid")
  162. mobile = strings.Contains(c.Request.UserAgent(), model.MobileUserAgentFlag)
  163. )
  164. if !ok {
  165. c.JSON(nil, ecode.NoLogin)
  166. return
  167. }
  168. c.JSON(svc.ConvertExtraQs(c, mid.(int64), localized(c), mobile))
  169. }
  170. func cool(c *bm.Context) {
  171. var (
  172. err error
  173. mid int64
  174. hid int64
  175. params = c.Request.Form
  176. hidStr = params.Get("id")
  177. )
  178. if midI, ok := c.Get("mid"); ok {
  179. mid = midI.(int64)
  180. }
  181. if hid, err = strconv.ParseInt(hidStr, 10, 64); err != nil {
  182. c.JSON(nil, ecode.RequestErr)
  183. return
  184. }
  185. c.JSON(svc.Cool(c, hid, mid))
  186. }
  187. func extraScore(c *bm.Context) {
  188. mid, ok := c.Get("mid")
  189. if !ok {
  190. c.JSON(nil, ecode.NoLogin)
  191. return
  192. }
  193. c.JSON(svc.ExtraScore(c, mid.(int64)))
  194. }