app.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package http
  2. import (
  3. "encoding/json"
  4. "go-common/app/admin/main/creative/model/app"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/http/blademaster/render"
  9. "net/http"
  10. "sort"
  11. "time"
  12. )
  13. func viewPortal(c *bm.Context) {
  14. var (
  15. err error
  16. info = &app.Portal{}
  17. )
  18. v := new(struct {
  19. ID int64 `form:"id"`
  20. })
  21. if err = c.Bind(v); err != nil {
  22. return
  23. }
  24. if v.ID == 0 {
  25. c.JSON(nil, ecode.RequestErr)
  26. return
  27. }
  28. if err = svc.DB.Debug().Model(&app.Portal{}).First(info, v.ID).Error; err != nil {
  29. c.JSON(nil, err)
  30. return
  31. }
  32. if info.ID == 0 {
  33. c.JSON(nil, ecode.NothingFound)
  34. } else {
  35. layout := "2006-01-02 15:04:05"
  36. mtime, _ := time.Parse(time.RFC3339, info.MTime)
  37. info.MTime = mtime.Format(layout)
  38. ptime, _ := time.Parse(time.RFC3339, info.PTime)
  39. info.PTime = ptime.Format(layout)
  40. ctime, _ := time.Parse(time.RFC3339, info.CTime)
  41. info.CTime = ctime.Format(layout)
  42. c.JSON(info, nil)
  43. }
  44. }
  45. func addPortal(c *bm.Context) {
  46. var (
  47. err error
  48. )
  49. v := new(struct {
  50. Build int64 `form:"build"`
  51. BuildExp string `form:"buildexp"`
  52. Platform int8 `form:"platform"`
  53. Compare int8 `form:"compare"`
  54. Pos int16 `form:"pos"`
  55. Mark int8 `form:"mark"`
  56. More int8 `form:"more"`
  57. Type int8 `form:"type"`
  58. Title string `form:"title" validate:"required"`
  59. Icon string `form:"icon" validate:"required"`
  60. URL string `form:"url" validate:"required"`
  61. Whiteexp string `form:"whiteexp"`
  62. SubTitle string `form:"subtitle"`
  63. })
  64. if err = c.Bind(v); err != nil {
  65. return
  66. }
  67. if !checkBuild(v.BuildExp) {
  68. httpCode(c, "buildexp is wrong", ecode.RequestErr)
  69. return
  70. }
  71. if !checkWhite(v.Whiteexp) {
  72. httpCode(c, "whiteexp is wrong", ecode.RequestErr)
  73. return
  74. }
  75. m := &app.Portal{
  76. Build: v.Build,
  77. BuildExp: v.BuildExp,
  78. Platform: v.Platform,
  79. Compare: v.Compare,
  80. State: 1,
  81. Pos: v.Pos,
  82. Mark: v.Mark,
  83. More: v.More,
  84. Type: v.Type,
  85. Title: v.Title,
  86. Icon: v.Icon,
  87. URL: v.URL,
  88. CTime: time.Now().Format("2006-01-02 15:04:05"),
  89. MTime: time.Now().Format("2006-01-02 15:04:05"),
  90. PTime: time.Now().Format("2006-01-02 15:04:05"),
  91. SubTitle: v.SubTitle,
  92. WhiteExp: v.Whiteexp,
  93. }
  94. db := svc.DB.Create(m)
  95. if err = db.Error; err != nil {
  96. log.Error("creativeSvc.addPortal error(%v)", err)
  97. c.JSON(nil, err)
  98. return
  99. }
  100. c.JSON(map[string]interface{}{
  101. "id": db.Value.(*app.Portal).ID,
  102. }, nil)
  103. }
  104. func upPortal(c *bm.Context) {
  105. var (
  106. err error
  107. ap = &app.Portal{}
  108. )
  109. v := new(struct {
  110. ID int64 `form:"id"`
  111. Build int64 `form:"build"`
  112. BuildExp string `form:"buildexp"`
  113. Platform int8 `form:"platform"`
  114. Compare int8 `form:"compare"`
  115. Pos int16 `form:"pos"`
  116. Mark int8 `form:"mark"`
  117. More int8 `form:"more"`
  118. Type int8 `form:"type"`
  119. Title string `form:"title" validate:"required"`
  120. Icon string `form:"icon" validate:"required"`
  121. URL string `form:"url" validate:"required"`
  122. Whiteexp string `form:"whiteexp"`
  123. SubTitle string `form:"subtitle"`
  124. })
  125. if err = c.Bind(v); err != nil {
  126. return
  127. }
  128. if !checkBuild(v.BuildExp) {
  129. httpCode(c, "buildexp is wrong", ecode.RequestErr)
  130. return
  131. }
  132. if !checkWhite(v.Whiteexp) {
  133. httpCode(c, "whiteexp is wrong", ecode.RequestErr)
  134. return
  135. }
  136. if err = svc.DB.Model(ap).Find(ap, v.ID).Error; err != nil {
  137. c.JSON(nil, err)
  138. return
  139. }
  140. if err = svc.DB.Model(&app.Portal{}).Update(map[string]interface{}{
  141. "id": v.ID,
  142. "build": v.Build,
  143. "buildexp": v.BuildExp,
  144. "platform": v.Platform,
  145. "compare": v.Compare,
  146. "state": 1,
  147. "pos": v.Pos,
  148. "mark": v.Mark,
  149. "more": v.More,
  150. "type": v.Type,
  151. "title": v.Title,
  152. "icon": v.Icon,
  153. "url": v.URL,
  154. "mtime": time.Now().Format("2006-01-02 15:04:05"),
  155. "subtitle": v.SubTitle,
  156. "whiteexp": v.Whiteexp,
  157. }).Error; err != nil {
  158. log.Error("creativeSvc.addPortal error(%v)", err)
  159. }
  160. c.JSON(nil, err)
  161. }
  162. func downPortal(c *bm.Context) {
  163. var (
  164. err error
  165. ap = &app.Portal{}
  166. )
  167. v := new(struct {
  168. ID int64 `form:"id"`
  169. })
  170. if err = c.Bind(v); err != nil {
  171. return
  172. }
  173. if v.ID == 0 {
  174. c.JSON(nil, ecode.RequestErr)
  175. return
  176. }
  177. if err = svc.DB.Model(ap).Find(ap, v.ID).Error; err != nil {
  178. c.JSON(nil, err)
  179. return
  180. }
  181. if err = svc.DB.Model(&app.Portal{ID: v.ID}).Update(map[string]interface{}{
  182. "id": v.ID,
  183. "state": 0,
  184. "mtime": time.Now().Format("2006-01-02 15:04:05"),
  185. }).Error; err != nil {
  186. log.Error("creativeSvc.addPortal error(%v)", err)
  187. }
  188. c.JSON(nil, err)
  189. }
  190. // 入口列表查询增加subtitle和whiteexp字段
  191. func portalList(c *bm.Context) {
  192. var (
  193. pts []*app.Portal
  194. items []*app.Item
  195. total int64
  196. err error
  197. )
  198. v := new(struct {
  199. Pn int `form:"pn" validate:"required,min=1"`
  200. Ps int `form:"ps" validate:"required,min=1"`
  201. State int8 `form:"state"`
  202. Type int8 `form:"type"`
  203. Platform string `form:"platform"`
  204. })
  205. if err = c.Bind(v); err != nil {
  206. return
  207. }
  208. if v.Pn < 1 {
  209. v.Pn = 1
  210. }
  211. if v.Ps < 20 {
  212. v.Ps = 20
  213. }
  214. if v.Type < 0 {
  215. v.Type = 0
  216. }
  217. db := svc.DB.Model(&app.Portal{}).Where("type=?", v.Type)
  218. if v.Platform != "" {
  219. db = db.Where("platform=?", atoi(v.Platform))
  220. }
  221. if v.State != 2 {
  222. db = db.Where("state=?", v.State)
  223. }
  224. db.Count(&total)
  225. if err = db.Order("ctime DESC").Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&pts).Error; err != nil {
  226. log.Error("portalList error(%v)", err)
  227. c.JSON(nil, err)
  228. return
  229. }
  230. if len(pts) > 0 {
  231. items = make([]*app.Item, 0, len(pts))
  232. for _, v := range pts {
  233. i := &app.Item{}
  234. if v == nil {
  235. continue
  236. }
  237. i.ID = v.ID
  238. i.Build = v.Build
  239. i.BuildExp = v.BuildExp
  240. i.Platform = v.Platform
  241. i.Compare = v.Compare
  242. i.State = v.State
  243. i.Pos = v.Pos
  244. i.Mark = v.Mark
  245. i.More = v.More
  246. i.Type = v.Type
  247. i.Title = v.Title
  248. i.Icon = v.Icon
  249. i.URL = v.URL
  250. i.SubTitle = v.SubTitle
  251. if len(v.WhiteExp) > 0 {
  252. if err = json.Unmarshal([]byte(v.WhiteExp), &i.WhiteExps); err != nil {
  253. log.Error("json.Unmarshal buildComps failed error(%v)", err)
  254. }
  255. sort.Slice(i.WhiteExps, func(m, n int) bool {
  256. return i.WhiteExps[m].TP <= i.WhiteExps[n].TP
  257. })
  258. }
  259. ct, err := time.Parse(time.RFC3339, v.CTime)
  260. if err != nil {
  261. log.Error("ctime time.Parse error(%v)", err)
  262. }
  263. mt, err := time.Parse(time.RFC3339, v.MTime)
  264. if err != nil {
  265. log.Error("mtime time.Parse error(%v)", err)
  266. }
  267. pt, err := time.Parse(time.RFC3339, v.PTime)
  268. if err != nil {
  269. log.Error("ptime time.Parse error(%v)", err)
  270. }
  271. i.CTime = ct.Unix()
  272. i.MTime = mt.Unix()
  273. i.PTime = pt.Unix()
  274. items = append(items, i)
  275. }
  276. } else {
  277. items = []*app.Item{}
  278. }
  279. data := &app.PortalPager{
  280. Pn: v.Pn,
  281. Ps: v.Ps,
  282. Items: items,
  283. Total: total,
  284. }
  285. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  286. "code": 0,
  287. "message": "0",
  288. "data": data,
  289. }))
  290. }