web_article.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. package http
  2. import (
  3. "encoding/base64"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "go-common/app/interface/main/creative/model/article"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/net/metadata"
  14. )
  15. func webArticlePre(c *bm.Context) {
  16. ip := metadata.String(c, metadata.RemoteIP)
  17. // check user
  18. midI, ok := c.Get("mid")
  19. if !ok {
  20. c.JSON(nil, ecode.NoLogin)
  21. return
  22. }
  23. mid, ok := midI.(int64)
  24. if !ok || mid <= 0 {
  25. c.JSON(nil, ecode.NoLogin)
  26. return
  27. }
  28. mf, err := accSvc.MyInfo(c, mid, ip, time.Now())
  29. if err != nil {
  30. c.JSON(nil, err)
  31. return
  32. }
  33. categories, err := artSvc.Categories(c)
  34. if err != nil {
  35. c.JSON(nil, err)
  36. return
  37. }
  38. rc, _ := artSvc.RemainCount(c, mid, ip)
  39. c.JSON(map[string]interface{}{
  40. "categories": categories,
  41. "myinfo": mf,
  42. "toplimit": rc,
  43. }, nil)
  44. }
  45. func webSubArticle(c *bm.Context) {
  46. params := c.Request.Form
  47. aidStr := params.Get("aid")
  48. title := params.Get("title")
  49. content := params.Get("content")
  50. summary := params.Get("summary")
  51. bannerURL := params.Get("banner_url")
  52. tidStr := params.Get("tid")
  53. categoryStr := params.Get("category")
  54. reprintStr := params.Get("reprint")
  55. tags := params.Get("tags")
  56. imageURLs := params.Get("image_urls")
  57. wordsStr := params.Get("words")
  58. actIDStr := params.Get("act_id")
  59. dynamicIntrosStr := params.Get("dynamic_intro")
  60. originImageURLs := params.Get("origin_image_urls")
  61. ip := metadata.String(c, metadata.RemoteIP)
  62. ck := c.Request.Header.Get("cookie")
  63. // check params
  64. midI, ok := c.Get("mid")
  65. if !ok {
  66. c.JSON(nil, ecode.NoLogin)
  67. return
  68. }
  69. mid, ok := midI.(int64)
  70. if !ok || mid <= 0 {
  71. c.JSON(nil, ecode.NoLogin)
  72. return
  73. }
  74. var (
  75. aid int64
  76. actID int64
  77. )
  78. if aidStr != "" {
  79. id, err := strconv.ParseInt(aidStr, 10, 64)
  80. if err != nil || id <= 0 {
  81. c.JSON(nil, ecode.RequestErr)
  82. return
  83. }
  84. aid = id
  85. }
  86. if actIDStr != "" {
  87. actid, err := strconv.ParseInt(actIDStr, 10, 64)
  88. if err != nil || actid <= 0 {
  89. c.JSON(nil, ecode.RequestErr)
  90. return
  91. }
  92. actID = actid
  93. }
  94. words, _ := strconv.ParseInt(wordsStr, 10, 64)
  95. artParam, err := artSvc.ParseParam(c, categoryStr, reprintStr, tidStr, imageURLs, originImageURLs)
  96. if err != nil {
  97. c.JSON(nil, err)
  98. return
  99. }
  100. // params
  101. art := &article.ArtParam{
  102. AID: aid,
  103. MID: mid,
  104. Title: title,
  105. Content: content,
  106. Summary: summary,
  107. BannerURL: bannerURL,
  108. Tags: tags,
  109. ImageURLs: artParam.ImageURLs,
  110. OriginImageURLs: artParam.OriginImageURLs,
  111. RealIP: ip,
  112. Category: artParam.Category,
  113. TemplateID: artParam.TemplateID,
  114. Reprint: artParam.Reprint,
  115. Words: words,
  116. DynamicIntro: dynamicIntrosStr,
  117. ActivityID: actID,
  118. }
  119. // submit
  120. id, err := artSvc.SubArticle(c, mid, art, "", ck, metadata.String(c, metadata.RemoteIP))
  121. if err != nil {
  122. c.JSON(nil, err)
  123. return
  124. }
  125. c.JSON(map[string]int64{
  126. "aid": id,
  127. }, nil)
  128. }
  129. func webUpdateArticle(c *bm.Context) {
  130. params := c.Request.Form
  131. aidStr := params.Get("aid")
  132. title := params.Get("title")
  133. content := params.Get("content")
  134. summary := params.Get("summary")
  135. bannerURL := params.Get("banner_url")
  136. tidStr := params.Get("tid")
  137. categoryStr := params.Get("category")
  138. reprintStr := params.Get("reprint")
  139. tags := params.Get("tags")
  140. imageURLs := params.Get("image_urls")
  141. wordsStr := params.Get("words")
  142. dynamicIntrosStr := params.Get("dynamic_intro")
  143. originImageURLs := params.Get("origin_image_urls")
  144. ip := metadata.String(c, metadata.RemoteIP)
  145. ck := c.Request.Header.Get("cookie")
  146. // check params
  147. midI, ok := c.Get("mid")
  148. if !ok {
  149. c.JSON(nil, ecode.NoLogin)
  150. return
  151. }
  152. mid, ok := midI.(int64)
  153. if !ok || mid <= 0 {
  154. c.JSON(nil, ecode.NoLogin)
  155. return
  156. }
  157. aid, err := strconv.ParseInt(aidStr, 10, 64)
  158. if err != nil || aid <= 0 {
  159. c.JSON(nil, ecode.RequestErr)
  160. return
  161. }
  162. words, _ := strconv.ParseInt(wordsStr, 10, 64)
  163. artParam, err := artSvc.ParseParam(c, categoryStr, reprintStr, tidStr, imageURLs, originImageURLs)
  164. if err != nil {
  165. c.JSON(nil, err)
  166. return
  167. }
  168. // params
  169. art := &article.ArtParam{
  170. AID: aid,
  171. MID: mid,
  172. Title: title,
  173. Content: content,
  174. Summary: summary,
  175. BannerURL: bannerURL,
  176. Tags: tags,
  177. ImageURLs: artParam.ImageURLs,
  178. OriginImageURLs: artParam.OriginImageURLs,
  179. RealIP: ip,
  180. Category: artParam.Category,
  181. TemplateID: artParam.TemplateID,
  182. Reprint: artParam.Reprint,
  183. Words: words,
  184. DynamicIntro: dynamicIntrosStr,
  185. }
  186. c.JSON(nil, artSvc.UpdateArticle(c, mid, art, "", ck, ip))
  187. }
  188. func webDelArticle(c *bm.Context) {
  189. params := c.Request.Form
  190. aidStr := params.Get("aid")
  191. ip := metadata.String(c, metadata.RemoteIP)
  192. // check params
  193. midI, ok := c.Get("mid")
  194. if !ok {
  195. c.JSON(nil, ecode.NoLogin)
  196. return
  197. }
  198. mid, ok := midI.(int64)
  199. if !ok || mid <= 0 {
  200. c.JSON(nil, ecode.NoLogin)
  201. return
  202. }
  203. aid, err := strconv.ParseInt(aidStr, 10, 64)
  204. if err != nil || aid <= 0 {
  205. c.JSON(nil, ecode.RequestErr)
  206. return
  207. }
  208. c.JSON(artSvc.DelArticle(c, aid, mid, ip), nil)
  209. }
  210. func webArticle(c *bm.Context) {
  211. params := c.Request.Form
  212. aidStr := params.Get("aid")
  213. ip := metadata.String(c, metadata.RemoteIP)
  214. // check params
  215. midI, ok := c.Get("mid")
  216. if !ok {
  217. c.JSON(nil, ecode.NoLogin)
  218. return
  219. }
  220. mid, ok := midI.(int64)
  221. if !ok || mid <= 0 {
  222. c.JSON(nil, ecode.NoLogin)
  223. return
  224. }
  225. aid, err := strconv.ParseInt(aidStr, 10, 64)
  226. if err != nil || aid <= 0 {
  227. c.JSON(nil, ecode.RequestErr)
  228. return
  229. }
  230. art, err := artSvc.View(c, aid, mid, ip)
  231. if err != nil {
  232. c.JSON(nil, err)
  233. return
  234. }
  235. c.JSON(art, nil)
  236. }
  237. func webArticleList(c *bm.Context) {
  238. params := c.Request.Form
  239. pnStr := params.Get("pn")
  240. psStr := params.Get("ps")
  241. sortStr := params.Get("sort")
  242. groupStr := params.Get("group")
  243. categoryStr := params.Get("category")
  244. ip := metadata.String(c, metadata.RemoteIP)
  245. // check
  246. midI, ok := c.Get("mid")
  247. if !ok {
  248. c.JSON(nil, ecode.NoLogin)
  249. return
  250. }
  251. mid, ok := midI.(int64)
  252. if !ok || mid <= 0 {
  253. c.JSON(nil, ecode.NoLogin)
  254. return
  255. }
  256. pn, err := strconv.Atoi(pnStr)
  257. if err != nil || pn < 1 {
  258. pn = 1
  259. }
  260. ps, err := strconv.Atoi(psStr)
  261. if err != nil || ps <= 10 {
  262. ps = 20
  263. }
  264. sort, err := strconv.Atoi(sortStr)
  265. if err != nil || sort < 0 {
  266. sort = 0
  267. }
  268. group, err := strconv.Atoi(groupStr)
  269. if err != nil || group < 0 {
  270. group = 0
  271. }
  272. category, err := strconv.Atoi(categoryStr)
  273. if err != nil || category < 0 {
  274. category = 0
  275. }
  276. arts, err := artSvc.Articles(c, mid, int(pn), int(ps), sort, group, category, ip)
  277. if err != nil {
  278. c.JSON(nil, err)
  279. return
  280. }
  281. c.JSONMap(map[string]interface{}{
  282. "artlist": arts,
  283. }, nil)
  284. }
  285. func webWithDrawArticle(c *bm.Context) {
  286. params := c.Request.Form
  287. aidStr := params.Get("aid")
  288. ip := metadata.String(c, metadata.RemoteIP)
  289. // check params
  290. midI, ok := c.Get("mid")
  291. if !ok {
  292. c.JSON(nil, ecode.NoLogin)
  293. return
  294. }
  295. mid, ok := midI.(int64)
  296. if !ok || mid <= 0 {
  297. c.JSON(nil, ecode.NoLogin)
  298. return
  299. }
  300. aid, err := strconv.ParseInt(aidStr, 10, 64)
  301. if err != nil || aid <= 0 {
  302. c.JSON(nil, ecode.RequestErr)
  303. return
  304. }
  305. c.JSON(nil, artSvc.WithDrawArticle(c, aid, mid, ip))
  306. }
  307. func webArticleUpCover(c *bm.Context) {
  308. midI, ok := c.Get("mid")
  309. if !ok {
  310. c.JSON(nil, ecode.NoLogin)
  311. return
  312. }
  313. mid, ok := midI.(int64)
  314. if !ok || mid <= 0 {
  315. c.JSON(nil, ecode.NoLogin)
  316. return
  317. }
  318. log.Info("webArticleUpCover mid(%d)", mid)
  319. cover := c.Request.Form.Get("cover")
  320. c.Request.Form.Del("cover") // NOTE: make sure write log concise
  321. ss := strings.Split(cover, ",")
  322. if len(ss) != 2 || len(ss[1]) == 0 {
  323. log.Error("webArticleUpCover format error mid(%d)|cover(%s)|coverSlice(%s)", mid, cover, ss)
  324. c.JSON(nil, ecode.RequestErr)
  325. return
  326. }
  327. bs, err := base64.StdEncoding.DecodeString(ss[1])
  328. if err != nil {
  329. log.Error("webArticleUpCover base64.StdEncoding.DecodeString(%s)|mid(%d)|error(%v)", ss[1], mid, err)
  330. c.JSON(nil, ecode.RequestErr)
  331. return
  332. }
  333. ftype := http.DetectContentType(bs)
  334. if ftype != "image/jpeg" && ftype != "image/jpg" && ftype != "image/png" && ftype != "image/gif" {
  335. log.Error("webArticleUpCover file type not allow file type(%s)|mid(%d)", ftype, mid)
  336. c.JSON(nil, ecode.CreativeArticleImageTypeErr)
  337. return
  338. }
  339. url, err := artSvc.ArticleUpCover(c, ftype, bs)
  340. if err != nil {
  341. c.JSON(nil, err)
  342. return
  343. }
  344. c.JSON(map[string]interface{}{
  345. "url": url,
  346. "size": len(bs),
  347. }, nil)
  348. }
  349. func webSubmitDraft(c *bm.Context) {
  350. params := c.Request.Form
  351. aidStr := params.Get("aid")
  352. title := params.Get("title")
  353. content := params.Get("content")
  354. summary := params.Get("summary")
  355. bannerURL := params.Get("banner_url")
  356. tidStr := params.Get("tid")
  357. categoryStr := params.Get("category")
  358. reprintStr := params.Get("reprint")
  359. tags := params.Get("tags")
  360. imageURLs := params.Get("image_urls")
  361. wordsStr := params.Get("words")
  362. dynamicIntrosStr := params.Get("dynamic_intro")
  363. originImageURLs := params.Get("origin_image_urls")
  364. ip := metadata.String(c, metadata.RemoteIP)
  365. // check params
  366. midI, ok := c.Get("mid")
  367. if !ok {
  368. c.JSON(nil, ecode.NoLogin)
  369. return
  370. }
  371. mid, ok := midI.(int64)
  372. if !ok || mid <= 0 {
  373. c.JSON(nil, ecode.NoLogin)
  374. return
  375. }
  376. var (
  377. did int64
  378. err error
  379. )
  380. if aidStr != "" {
  381. did, err = strconv.ParseInt(aidStr, 10, 64)
  382. if err != nil || did <= 0 {
  383. c.JSON(nil, ecode.RequestErr)
  384. return
  385. }
  386. }
  387. words, _ := strconv.ParseInt(wordsStr, 10, 64)
  388. artParam, err := artSvc.ParseDraftParam(c, categoryStr, reprintStr, tidStr, imageURLs, originImageURLs)
  389. if err != nil {
  390. c.JSON(nil, err)
  391. return
  392. }
  393. // params
  394. art := &article.ArtParam{
  395. AID: did,
  396. MID: mid,
  397. Title: title,
  398. Content: content,
  399. Summary: summary,
  400. BannerURL: bannerURL,
  401. Tags: tags,
  402. ImageURLs: artParam.ImageURLs,
  403. OriginImageURLs: artParam.OriginImageURLs,
  404. RealIP: ip,
  405. Category: artParam.Category,
  406. TemplateID: artParam.TemplateID,
  407. Reprint: artParam.Reprint,
  408. Words: words,
  409. DynamicIntro: dynamicIntrosStr,
  410. }
  411. // add draft
  412. id, err := artSvc.AddDraft(c, mid, art)
  413. if err != nil {
  414. c.JSON(nil, err)
  415. return
  416. }
  417. c.JSON(map[string]int64{
  418. "aid": id,
  419. }, nil)
  420. }
  421. func webDeleteDraft(c *bm.Context) {
  422. params := c.Request.Form
  423. aidStr := params.Get("aid")
  424. ip := metadata.String(c, metadata.RemoteIP)
  425. // check params
  426. midI, ok := c.Get("mid")
  427. if !ok {
  428. c.JSON(nil, ecode.NoLogin)
  429. return
  430. }
  431. mid, ok := midI.(int64)
  432. if !ok || mid <= 0 {
  433. c.JSON(nil, ecode.NoLogin)
  434. return
  435. }
  436. aid, err := strconv.ParseInt(aidStr, 10, 64)
  437. if err != nil || aid <= 0 {
  438. c.JSON(nil, ecode.RequestErr)
  439. return
  440. }
  441. c.JSON(nil, artSvc.DelDraft(c, aid, mid, ip))
  442. }
  443. func webDraft(c *bm.Context) {
  444. params := c.Request.Form
  445. aidStr := params.Get("aid")
  446. ip := metadata.String(c, metadata.RemoteIP)
  447. // check params
  448. midI, ok := c.Get("mid")
  449. if !ok {
  450. c.JSON(nil, ecode.NoLogin)
  451. return
  452. }
  453. mid, ok := midI.(int64)
  454. if !ok || mid <= 0 {
  455. c.JSON(nil, ecode.NoLogin)
  456. return
  457. }
  458. aid, err := strconv.ParseInt(aidStr, 10, 64)
  459. if err != nil || aid <= 0 {
  460. c.JSON(nil, ecode.RequestErr)
  461. return
  462. }
  463. art, err := artSvc.Draft(c, aid, mid, ip)
  464. if err != nil {
  465. c.JSON(nil, err)
  466. return
  467. }
  468. c.JSON(art, nil)
  469. }
  470. func webDraftList(c *bm.Context) {
  471. params := c.Request.Form
  472. pnStr := params.Get("pn")
  473. psStr := params.Get("ps")
  474. ip := metadata.String(c, metadata.RemoteIP)
  475. // check
  476. midI, ok := c.Get("mid")
  477. if !ok {
  478. c.JSON(nil, ecode.NoLogin)
  479. return
  480. }
  481. mid, ok := midI.(int64)
  482. if !ok || mid <= 0 {
  483. c.JSON(nil, ecode.NoLogin)
  484. return
  485. }
  486. pn, err := strconv.Atoi(pnStr)
  487. if err != nil || pn < 1 {
  488. pn = 1
  489. }
  490. ps, err := strconv.Atoi(psStr)
  491. if err != nil || ps <= 10 {
  492. ps = 20
  493. }
  494. arts, err := artSvc.Drafts(c, mid, pn, ps, ip)
  495. if err != nil {
  496. c.JSON(nil, err)
  497. return
  498. }
  499. c.JSONMap(map[string]interface{}{
  500. "artlist": arts,
  501. }, nil)
  502. }
  503. func webAuthor(c *bm.Context) {
  504. ip := metadata.String(c, metadata.RemoteIP)
  505. // check params
  506. midI, ok := c.Get("mid")
  507. if !ok {
  508. c.JSON(nil, ecode.NoLogin)
  509. return
  510. }
  511. mid, ok := midI.(int64)
  512. if !ok || mid <= 0 {
  513. c.JSON(nil, ecode.NoLogin)
  514. return
  515. }
  516. isAuthor, _ := artSvc.IsAuthor(c, mid, ip)
  517. c.JSON(map[string]interface{}{
  518. "mid": mid,
  519. "is_author": isAuthor,
  520. }, nil)
  521. }
  522. func webArticleCapture(c *bm.Context) {
  523. params := c.Request.Form
  524. originURL := params.Get("url")
  525. // check params
  526. midI, ok := c.Get("mid")
  527. if !ok {
  528. c.JSON(nil, ecode.NoLogin)
  529. return
  530. }
  531. mid, ok := midI.(int64)
  532. if !ok || mid <= 0 {
  533. c.JSON(nil, ecode.NoLogin)
  534. return
  535. }
  536. if originURL == "" {
  537. c.JSON(nil, ecode.RequestErr)
  538. return
  539. }
  540. log.Info("capture mid(%d) origin imageURL (%s)", mid, originURL)
  541. _, err := url.ParseRequestURI(originURL)
  542. if err != nil {
  543. log.Error("capture check url(%s) format error(%v)", originURL, err)
  544. c.JSON(nil, ecode.RequestErr)
  545. return
  546. }
  547. imgURL, size, err := artSvc.ArticleCapture(c, originURL)
  548. if err != nil {
  549. c.JSON(nil, err)
  550. return
  551. }
  552. c.JSON(map[string]interface{}{
  553. "url": imgURL,
  554. "size": size,
  555. }, nil)
  556. }