out_link.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/utils"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "strings"
  8. "time"
  9. )
  10. type OutLinkController struct {
  11. BaseAuthController
  12. }
  13. // Save
  14. // @Title 保存外部链接
  15. // @Description 保存外部链接
  16. // @Param request body models.OutLinkSaveReq true "type json string"
  17. // @Success 200 Ret=200 操作成功
  18. // @router /save [post]
  19. func (ol *OutLinkController) Save() {
  20. br := new(models.BaseResponse).Init()
  21. defer func() {
  22. if br.ErrMsg == "" {
  23. br.IsSendEmail = false
  24. }
  25. ol.Data["json"] = br
  26. ol.ServeJSON()
  27. }()
  28. var req models.OutLinkSaveReq
  29. if e := json.Unmarshal(ol.Ctx.Input.RequestBody, &req); e != nil {
  30. br.Msg = "参数解析异常!"
  31. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  32. return
  33. }
  34. req.Title = strings.TrimSpace(req.Title)
  35. if req.Title == "" {
  36. br.Msg = "请输入菜单名称"
  37. return
  38. }
  39. req.Url = strings.TrimSpace(req.Url)
  40. if req.Url == "" {
  41. br.Msg = "请输入页面链接地址"
  42. return
  43. }
  44. outLink := new(models.OutLink)
  45. if req.Id > 0 {
  46. // 编辑
  47. err := outLink.GetItemById(req.Id)
  48. if err != nil {
  49. br.Msg = "该记录不存在"
  50. br.ErrMsg = "该记录不存在,Err:" + err.Error()
  51. return
  52. }
  53. if outLink.Title == req.Title && outLink.Url == req.Url {
  54. br.Ret = 200
  55. br.Success = true
  56. br.Msg = "操作成功"
  57. return
  58. }
  59. outLink.Title = req.Title
  60. outLink.Url = req.Url
  61. outLink.ModifyTime = time.Now()
  62. err = outLink.Update([]string{"Title", "Url", "ModifyTime"})
  63. if err != nil {
  64. br.Msg = "更新失败"
  65. br.ErrMsg = "更新失败,Err:" + err.Error()
  66. return
  67. }
  68. } else {
  69. outLink.Title = req.Title
  70. outLink.Url = req.Url
  71. outLink.CreateTime = time.Now()
  72. outLink.SysAdminId = ol.SysUser.AdminId
  73. outLink.SysAdminName = ol.SysUser.AdminName
  74. outLink.ModifyTime = time.Now()
  75. err := outLink.Create()
  76. if err != nil {
  77. br.Msg = "新增失败"
  78. br.ErrMsg = "新增失败,Err:" + err.Error()
  79. return
  80. }
  81. }
  82. br.Ret = 200
  83. br.Success = true
  84. br.Msg = "操作成功"
  85. return
  86. }
  87. // Del
  88. // @Title 删除外部链接
  89. // @Description 删除外部链接
  90. // @Param request body models.OutLinkReq true "type json string"
  91. // @Success 200 Ret=200 操作成功
  92. // @router /del [post]
  93. func (ol *OutLinkController) Del() {
  94. br := new(models.BaseResponse).Init()
  95. defer func() {
  96. if br.ErrMsg == "" {
  97. br.IsSendEmail = false
  98. }
  99. ol.Data["json"] = br
  100. ol.ServeJSON()
  101. }()
  102. var req models.OutLinkReq
  103. if e := json.Unmarshal(ol.Ctx.Input.RequestBody, &req); e != nil {
  104. br.Msg = "参数解析异常!"
  105. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  106. return
  107. }
  108. if req.Id == 0 {
  109. br.Msg = "请选择要删除的外部链接"
  110. return
  111. }
  112. outLink := new(models.OutLink)
  113. // 查询
  114. err := outLink.GetItemById(req.Id)
  115. if err != nil {
  116. if utils.IsErrNoRow(err) {
  117. br.Msg = "该记录已被删除, 请刷新页面"
  118. return
  119. }
  120. br.Msg = "操作失败"
  121. br.ErrMsg = "操作失败,Err:" + err.Error()
  122. return
  123. }
  124. err = outLink.Del()
  125. if err != nil {
  126. br.Msg = "操作失败"
  127. br.ErrMsg = "操作失败,Err:" + err.Error()
  128. return
  129. }
  130. br.Ret = 200
  131. br.Success = true
  132. br.Msg = "操作成功"
  133. return
  134. }
  135. // List
  136. // @Title 外部链接列表
  137. // @Description 外部链接列表
  138. // @Param PageSize query int true "每页记录数"
  139. // @Param CurrentIndex query int true "请求页码"
  140. // @Success 200 Ret=200 操作成功
  141. // @router /list [get]
  142. func (ol *OutLinkController) List() {
  143. br := new(models.BaseResponse).Init()
  144. defer func() {
  145. if br.ErrMsg == "" {
  146. br.IsSendEmail = false
  147. }
  148. ol.Data["json"] = br
  149. ol.ServeJSON()
  150. }()
  151. var startSize int
  152. pageSize, _ := ol.GetInt("PageSize")
  153. currentIndex, _ := ol.GetInt("CurrentIndex")
  154. sortParam := ol.GetString("SortParam")
  155. sortType := ol.GetString("SortType")
  156. if pageSize <= 0 {
  157. pageSize = utils.PageSize20
  158. }
  159. if currentIndex <= 0 {
  160. currentIndex = 1
  161. }
  162. var orderRule string
  163. if sortParam == "create_time" {
  164. orderRule = " create_time "
  165. }
  166. if orderRule != "" {
  167. if strings.ToLower(sortType) == "asc" || strings.ToLower(sortType) == "desc" {
  168. orderRule += sortType
  169. }
  170. }
  171. startSize = paging.StartIndex(currentIndex, pageSize)
  172. item := new(models.OutLink)
  173. cond := ``
  174. pars := make([]interface{}, 0)
  175. total, list, e := item.GetPageItemsByCondition(startSize, pageSize, cond, pars, []string{}, orderRule)
  176. if e != nil {
  177. br.Msg = "获取失败"
  178. br.ErrMsg = "获取外部链接列表失败, Err: " + e.Error()
  179. return
  180. }
  181. respList := make([]*models.OutLinkItem, 0)
  182. for i := range list {
  183. v := new(models.OutLinkItem)
  184. v.Id = list[i].Id
  185. v.Title = list[i].Title
  186. v.Url = list[i].Url
  187. v.CreateTime = list[i].CreateTime.Format(utils.FormatDateTime)
  188. v.ModifyTime = list[i].ModifyTime.Format(utils.FormatDateTime)
  189. respList = append(respList, v)
  190. }
  191. page := paging.GetPaging(currentIndex, pageSize, total)
  192. resp := &models.OutLinkListResp{
  193. Paging: page,
  194. List: respList,
  195. }
  196. br.Data = resp
  197. br.Ret = 200
  198. br.Success = true
  199. br.Msg = "获取成功"
  200. return
  201. }
  202. // AllList
  203. // @Title 外部链接展示列表
  204. // @Description 外部链接展示列表
  205. // @Success 200 Ret=200 操作成功
  206. // @router /all [get]
  207. func (ol *OutLinkController) AllList() {
  208. br := new(models.BaseResponse).Init()
  209. defer func() {
  210. if br.ErrMsg == "" {
  211. br.IsSendEmail = false
  212. }
  213. ol.Data["json"] = br
  214. ol.ServeJSON()
  215. }()
  216. item := new(models.OutLink)
  217. cond := ``
  218. pars := make([]interface{}, 0)
  219. list, e := item.GetItemsByCondition(cond, pars, "")
  220. if e != nil {
  221. br.Msg = "获取失败"
  222. br.ErrMsg = "获取外部链接列表失败, Err: " + e.Error()
  223. return
  224. }
  225. respList := make([]*models.OutLinkItem, 0)
  226. for i := range list {
  227. v := new(models.OutLinkItem)
  228. v.Id = list[i].Id
  229. v.Title = list[i].Title
  230. v.Url = list[i].Url
  231. v.CreateTime = list[i].CreateTime.Format(utils.FormatDateTime)
  232. v.ModifyTime = list[i].ModifyTime.Format(utils.FormatDateTime)
  233. respList = append(respList, v)
  234. }
  235. resp := &models.OutLinkListResp{
  236. List: respList,
  237. }
  238. if ol.SysUser.AdminName != "" {
  239. desEncrypt := utils.DesBase64Encrypt([]byte(ol.SysUser.AdminName), utils.DesKeySalt)
  240. resp.UserCode = string(desEncrypt)
  241. }
  242. br.Data = resp
  243. br.Ret = 200
  244. br.Success = true
  245. br.Msg = "获取成功"
  246. return
  247. }