depend.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package dao
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "net/url"
  7. "strconv"
  8. "time"
  9. "go-common/app/job/main/credit/model"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. "go-common/library/xstr"
  13. )
  14. // SendPendant send pendant.
  15. func (d *Dao) SendPendant(c context.Context, mid int64, pids []int64, day int64) (err error) {
  16. params := url.Values{}
  17. params.Set("pids", xstr.JoinInts(pids))
  18. params.Set("mid", strconv.FormatInt(mid, 10))
  19. var days []int64
  20. for range pids {
  21. days = append(days, day)
  22. }
  23. params.Set("expires", xstr.JoinInts(days))
  24. var res struct {
  25. Code int `json:"code"`
  26. }
  27. if err = d.client.Post(c, d.sendPendantURL, "", params, &res); err != nil {
  28. log.Error("d.sendPendantURL url(%s) res(%d) error(%v)", d.sendPendantURL+"?"+params.Encode(), res.Code, err)
  29. return
  30. }
  31. if res.Code != 0 {
  32. log.Error("d.sendPendantURL url(%s) code(%d)", d.sendPendantURL+"?"+params.Encode(), res.Code)
  33. err = errors.New("sendPendant failed")
  34. }
  35. return
  36. }
  37. // SendMedal send Medal.
  38. func (d *Dao) SendMedal(c context.Context, mid int64, nid int64) (err error) {
  39. params := url.Values{}
  40. params.Set("nid", strconv.FormatInt(nid, 10))
  41. params.Set("mid", strconv.FormatInt(mid, 10))
  42. var res struct {
  43. Code int `json:"code"`
  44. }
  45. if err = d.client.Post(c, d.sendMedalURL, "", params, &res); err != nil {
  46. log.Error("d.sendMedalURL url(%s) res(%d) error(%v)", d.sendMedalURL+"?"+params.Encode(), res.Code, err)
  47. return
  48. }
  49. if res.Code != 0 {
  50. log.Error("d.sendMedalURL url(%s) code(%d)", d.sendMedalURL+"?"+params.Encode(), res.Code)
  51. err = errors.New("sendMedalURL failed")
  52. }
  53. return
  54. }
  55. // DelTag del tag.
  56. func (d *Dao) DelTag(c context.Context, tid, aid string) (err error) {
  57. params := url.Values{}
  58. params.Set("aid", aid)
  59. params.Set("tag_id", tid)
  60. params.Set("mid", "0")
  61. var res struct {
  62. Code int `json:"code"`
  63. }
  64. if err = d.client.Post(c, d.delTagURL, "", params, &res); err != nil {
  65. log.Info("d.delTagURL url(%s) res(%v) error(%v)", d.delTagURL+"?"+params.Encode(), res, err)
  66. return
  67. }
  68. if res.Code != 0 {
  69. log.Error("d.delTagURL url(%s) code(%d)", d.delTagURL+"?"+params.Encode(), res.Code)
  70. }
  71. log.Info("d.delTagURL url(%s) res(%d)", d.delTagURL+"?"+params.Encode(), res.Code)
  72. return
  73. }
  74. // ReportDM report dm is delete.
  75. func (d *Dao) ReportDM(c context.Context, cid string, dmid string, result int64) (err error) {
  76. params := url.Values{}
  77. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  78. params.Set("cid", cid)
  79. params.Set("dmid", dmid)
  80. params.Set("result", strconv.FormatInt(result, 10))
  81. var res struct {
  82. Code int `json:"code"`
  83. }
  84. if err = d.client.Post(c, d.delDMURL, "", params, &res); err != nil {
  85. log.Error("d.delDMURL url(%s) error(%v)", d.delDMURL+"?"+params.Encode(), err)
  86. return
  87. }
  88. if res.Code != 0 {
  89. log.Error("d.delDMURL url(%s) code(%d)", d.delDMURL+"?"+params.Encode(), res.Code)
  90. err = ecode.Int(res.Code)
  91. }
  92. return
  93. }
  94. // BlockAccount block account.
  95. func (d *Dao) BlockAccount(c context.Context, r *model.BlockedInfo) (err error) {
  96. params := url.Values{}
  97. params.Set("mid", strconv.FormatInt(r.UID, 10))
  98. params.Set("source", "1")
  99. if int8(r.PunishType) == model.PunishTypeForever && r.BlockedForever == model.InBlockedForever {
  100. params.Set("action", "2")
  101. } else {
  102. params.Set("action", "1")
  103. }
  104. if r.CaseID == 0 {
  105. params.Set("notify", "1")
  106. } else {
  107. params.Set("notify", "0")
  108. }
  109. params.Set("duration", strconv.FormatInt(r.BlockedDays*86400, 10))
  110. params.Set("start_time", fmt.Sprintf("%d", time.Now().Unix()))
  111. params.Set("op_id", strconv.FormatInt(r.OPID, 10))
  112. params.Set("operator", r.OperatorName)
  113. params.Set("reason", model.ReasonTypeDesc(int8(r.ReasonType)))
  114. params.Set("comment", r.BlockedRemark)
  115. var res struct {
  116. Code int `json:"code"`
  117. }
  118. if err = d.client.Post(c, d.blockAccountURL, "", params, &res); err != nil {
  119. log.Error("d.blockAccountURL res(%d) url(%s)", res.Code, d.blockAccountURL+"?"+params.Encode())
  120. }
  121. log.Info("d.blockAccountURL res(%d) url(%s)", res.Code, d.blockAccountURL+"?"+params.Encode())
  122. return
  123. }
  124. // UnBlockAccount unblock account.
  125. func (d *Dao) UnBlockAccount(c context.Context, r *model.BlockedInfo) (err error) {
  126. params := url.Values{}
  127. params.Set("mid", strconv.FormatInt(r.UID, 10))
  128. params.Set("source", "1")
  129. params.Set("op_id", strconv.FormatInt(r.OPID, 10))
  130. params.Set("operator", r.OperatorName)
  131. var res struct {
  132. Code int `json:"code"`
  133. }
  134. if err = d.client.Post(c, d.unBlockAccountURL, "", params, &res); err != nil {
  135. log.Error("d.unBlockAccountURL res(%d) url(%s)", res.Code, d.unBlockAccountURL+"?"+params.Encode())
  136. }
  137. log.Info("d.unBlockAccountURL res(%d) url(%s)", res.Code, d.unBlockAccountURL+"?"+params.Encode())
  138. return
  139. }
  140. // SendMsg send message.
  141. func (d *Dao) SendMsg(c context.Context, mid int64, title string, context string) (err error) {
  142. params := url.Values{}
  143. params.Set("mc", "2_1_13")
  144. params.Set("title", title)
  145. params.Set("data_type", "4")
  146. params.Set("context", context)
  147. params.Set("mid_list", fmt.Sprintf("%d", mid))
  148. var res struct {
  149. Code int `json:"code"`
  150. Data *struct {
  151. Status int8 `json:"status"`
  152. Remark string `json:"remark"`
  153. } `json:"data"`
  154. }
  155. if err = d.client.Post(c, d.sendMsgURL, "", params, &res); err != nil {
  156. log.Error("sendMsgURL(%s) error(%v)", d.sendMsgURL+"?"+params.Encode(), err)
  157. return
  158. }
  159. if res.Code != 0 {
  160. log.Error("sendMsgURL(%s) res(%d)", d.sendMsgURL+"?"+params.Encode(), res.Code)
  161. }
  162. log.Info("d.sendMsgURL url(%s) res(%d)", d.sendMsgURL+"?"+params.Encode(), res.Code)
  163. return
  164. }
  165. // Sms send monitor sms.
  166. func (d *Dao) Sms(c context.Context, phone, token, msg string) (err error) {
  167. var (
  168. urlStr = "http://ops-mng.bilibili.co/api/sendsms"
  169. res struct {
  170. Result bool `json:"result"`
  171. }
  172. )
  173. params := url.Values{}
  174. params.Set("phone", phone)
  175. params.Set("message", msg)
  176. params.Set("token", token)
  177. if err = d.client.Get(c, urlStr, "", params, &res); err != nil {
  178. log.Error("d.Sms url(%s) res(%v) err(%v)", urlStr+"?"+params.Encode(), res, err)
  179. return
  180. }
  181. if !res.Result {
  182. log.Error("ops-mng sendsms url(%s) result(%v)", urlStr+"?"+params.Encode(), res.Result)
  183. }
  184. log.Info("d.Sms url(%s) res(%v)", urlStr+"?"+params.Encode(), res)
  185. return
  186. }
  187. // AddMoral add or reduce moral to user
  188. func (d *Dao) AddMoral(c context.Context, mid int64, moral float64, reasonType int8, oper, reason, remark, remoteIP string) (err error) {
  189. params := url.Values{}
  190. params.Set("mid", strconv.FormatInt(mid, 10))
  191. params.Set("addMoral", strconv.FormatFloat(moral, 'f', -1, 64))
  192. if moral > 0 {
  193. params.Set("origin", "1")
  194. } else {
  195. params.Set("origin", "2")
  196. }
  197. if oper == "" {
  198. params.Set("operater", "系统")
  199. } else {
  200. params.Set("operater", oper)
  201. }
  202. params.Set("reason", reason)
  203. params.Set("reason_type", strconv.FormatInt(int64(reasonType), 10))
  204. params.Set("remark", remark)
  205. params.Set("is_notify", "1")
  206. var res struct {
  207. Code int `json:"code"`
  208. }
  209. err = d.client.Get(c, d.addMoralURL, remoteIP, params, &res)
  210. if err != nil {
  211. log.Error("d.addMoralURL url(%s) error(%v)", d.addMoralURL+"?"+params.Encode(), err)
  212. return
  213. }
  214. if res.Code != 0 {
  215. log.Error("d.addMoralURL url(%s) error(%v)", d.addMoralURL+"?"+params.Encode(), res.Code)
  216. err = ecode.Int(res.Code)
  217. }
  218. log.Info("d.addMoralURL url(%s) res(%v)", d.addMoralURL+"?"+params.Encode(), res)
  219. return
  220. }
  221. // AddMoney Modify user Coins(old).
  222. func (d *Dao) AddMoney(c context.Context, mid int64, money float64, reason string) (err error) {
  223. params := url.Values{}
  224. params.Set("mid", strconv.FormatInt(mid, 10))
  225. params.Set("count", strconv.FormatFloat(money, 'f', -1, 64))
  226. params.Set("reason", reason)
  227. var res struct {
  228. Code int `json:"code"`
  229. }
  230. err = d.client.Post(c, d.modifyCoinsURL, "", params, &res)
  231. log.Info("d.modifyCoinsURL url(%s) res(%v)", d.modifyCoinsURL+"?"+params.Encode(), res)
  232. if err != nil {
  233. log.Error("d.modifyCoinsURL url(%s) error(%v)", d.modifyCoinsURL+"?"+params.Encode(), err)
  234. return
  235. }
  236. if res.Code != 0 {
  237. log.Error("d.modifyCoinsURL url(%s) error(%v)", d.modifyCoinsURL+"?"+params.Encode(), res.Code)
  238. err = ecode.Int(res.Code)
  239. }
  240. return
  241. }
  242. // CheckFilter check content filter.
  243. func (d *Dao) CheckFilter(c context.Context, area, msg, ip string) (need bool, err error) {
  244. params := url.Values{}
  245. params.Set("area", area)
  246. params.Set("msg", msg)
  247. var res struct {
  248. Code int `json:"code"`
  249. Data *struct {
  250. Level int `json:"level"`
  251. Limit int `json:"limit"`
  252. Msg string `json:"msg"`
  253. } `json:"data"`
  254. }
  255. err = d.client.Get(c, d.filterURL, ip, params, &res)
  256. if err != nil {
  257. log.Error("d.filterURL url(%s) error(%v)", d.filterURL+"?"+params.Encode(), err)
  258. return
  259. }
  260. if res.Code != 0 {
  261. log.Error("d.filterURL url(%s) error(%v)", d.filterURL+"?"+params.Encode(), res.Code)
  262. err = ecode.Int(res.Code)
  263. return
  264. }
  265. if res.Data == nil {
  266. log.Warn("d.filterURL url(%s) res(%+v)", d.filterURL+"?"+params.Encode(), res.Data)
  267. return
  268. }
  269. need = res.Data.Level >= 20
  270. return
  271. }
  272. // UpAppealState .
  273. func (d *Dao) UpAppealState(c context.Context, bid, oid, eid int64) (err error) {
  274. params := url.Values{}
  275. params.Set("business", strconv.FormatInt(bid, 10))
  276. params.Set("oid", strconv.FormatInt(oid, 10))
  277. params.Set("eid", strconv.FormatInt(eid, 10))
  278. var res struct {
  279. Code int `json:"code"`
  280. }
  281. err = d.client.Post(c, d.upAppealStateURL, "", params, &res)
  282. log.Info("d.upAppealStateURL url(%s) res(%v)", d.upAppealStateURL+"?"+params.Encode(), res)
  283. if err != nil {
  284. log.Error("d.upAppealStateURL url(%s) error(%v)", d.upAppealStateURL+"?"+params.Encode(), err)
  285. return
  286. }
  287. if res.Code != 0 {
  288. log.Error("d.upAppealStateURL url(%s) error(%v)", d.upAppealStateURL+"?"+params.Encode(), res.Code)
  289. err = ecode.Int(res.Code)
  290. }
  291. return
  292. }