source.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /**********************************************
  2. ** @Des: 资源设置
  3. ** @Author: haodaquan
  4. ** @Date: 2017-09-08 17:48:30
  5. ** @Last Modified by: haodaquan
  6. ** @Last Modified time: 2017-09-09 18:50:41
  7. ***********************************************/
  8. package backgroundc
  9. import (
  10. "strings"
  11. "time"
  12. "github.com/astaxie/beego"
  13. "wuyebaoxiuapi/models/backgroundm"
  14. )
  15. type ApiSourceController struct {
  16. BaseController
  17. }
  18. func (self *ApiSourceController) List() {
  19. self.Data["pageTitle"] = "API资源"
  20. self.display()
  21. }
  22. func (self *ApiSourceController) Table() {
  23. //列表
  24. page, err := self.GetInt("page")
  25. if err != nil {
  26. page = 1
  27. }
  28. limit, err := self.GetInt("limit")
  29. if err != nil {
  30. limit = 30
  31. }
  32. sourceName := strings.TrimSpace(self.GetString("sourceName"))
  33. //获取分组
  34. groupList := groupLists()
  35. self.pageSize = limit
  36. //查询条件
  37. filters := make([]interface{}, 0)
  38. filters = append(filters, "status", 1)
  39. if sourceName != "" {
  40. filters = append(filters, "source_name__icontains", sourceName)
  41. }
  42. result, count := backgroundm.ApiSourceGetList(page, self.pageSize, filters...)
  43. list := make([]map[string]interface{}, len(result))
  44. for k, v := range result {
  45. row := make(map[string]interface{})
  46. row["id"] = v.Id
  47. row["source_name"] = v.SourceName
  48. row["group_id"] = v.GroupId
  49. groupInfo := getGroupInfo(groupList, v.GroupId)
  50. row["group_name"] = groupInfo.GroupName
  51. row["create_time"] = beego.Date(time.Unix(v.CreateTime, 0), "Y-m-d H:i:s")
  52. row["update_time"] = beego.Date(time.Unix(v.UpdateTime, 0), "Y-m-d H:i:s")
  53. list[k] = row
  54. }
  55. self.ajaxList("成功", MSG_OK, count, list)
  56. }
  57. // //显示所有的接口详情
  58. // func (self *ApiController) Show() {
  59. // self.Data["ApiCss"] = true
  60. // id, _ := self.GetInt("id", 0)
  61. // sourceName := strings.TrimSpace(self.GetString("sourceName"))
  62. // self.Data["pageTitle"] = sourceName
  63. // detail, _ := models.ApiDetailsGetById(id)
  64. // list := make([]map[string]interface{}, len(detail))
  65. // for k, v := range detail {
  66. // row := make(map[string]interface{})
  67. // row["id"] = v.Id
  68. // row["source_id"] = v.SourceId
  69. // row["api_url"] = v.ApiUrl
  70. // row["api_name"] = v.ApiName
  71. // row["detail"] = v.Detail
  72. // row["status"] = v.Status
  73. // row["create_name"] = v.CreateName
  74. // row["update_name"] = v.UpdateName
  75. // row["audit_name"] = v.AuditName
  76. // row["audit_status"] = AUDIT_STATUS[v.Status]
  77. // row["method"] = REQUEST_METHOD[v.Method]
  78. // row["audit_time"] = beego.Date(time.Unix(v.AuditTime, 0), "Y-m-d H:i:s")
  79. // row["update_time"] = beego.Date(time.Unix(v.UpdateTime, 0), "Y-m-d H:i:s")
  80. // //参数
  81. // row["Params"], _ = models.ApiParamGetById(v.Id)
  82. // list[k] = row
  83. // }
  84. // self.Data["Detail"] = list
  85. // self.Data["sid"] = id
  86. // self.display("api/info")
  87. // }
  88. func (self *ApiSourceController) Add() {
  89. self.Data["pageTitle"] = "新增资源"
  90. //查询条件
  91. filters := make([]interface{}, 0)
  92. filters = append(filters, "status", 1)
  93. result, _ := backgroundm.GroupGetList(1, 1000, filters...)
  94. list := make([]map[string]interface{}, len(result))
  95. for k, v := range result {
  96. row := make(map[string]interface{})
  97. row["id"] = v.Id
  98. row["group_name"] = v.GroupName
  99. list[k] = row
  100. }
  101. self.Data["Groups"] = list
  102. self.display()
  103. }
  104. func (self *ApiSourceController) Edit() {
  105. self.Data["pageTitle"] = "编辑API"
  106. id, _ := self.GetInt("id", 0)
  107. Api, err := backgroundm.ApiSourceGetById(id)
  108. if err != nil {
  109. self.Ctx.WriteString("数据不存在")
  110. return
  111. }
  112. row := make(map[string]interface{})
  113. row["id"] = Api.Id
  114. row["source_name"] = Api.SourceName
  115. row["group_id"] = int(Api.GroupId)
  116. self.Data["Source"] = row
  117. filters := make([]interface{}, 0)
  118. filters = append(filters, "status", 1)
  119. result, _ := backgroundm.GroupGetList(1, 1000, filters...)
  120. list := make([]map[string]interface{}, len(result))
  121. for k, v := range result {
  122. row := make(map[string]interface{})
  123. row["id"] = v.Id
  124. row["group_name"] = v.GroupName
  125. list[k] = row
  126. }
  127. self.Data["Groups"] = list
  128. self.display()
  129. }
  130. //存储资源
  131. func (self *ApiSourceController) AjaxSave() {
  132. Api_id, _ := self.GetInt("id")
  133. if Api_id == 0 {
  134. Api := new(backgroundm.ApiSource)
  135. Api.SourceName = strings.TrimSpace(self.GetString("source_name"))
  136. Api.GroupId, _ = self.GetInt("group_id")
  137. Api.CreateId = self.userId
  138. Api.UpdateId = self.userId
  139. Api.CreateTime = time.Now().Unix()
  140. Api.UpdateTime = time.Now().Unix()
  141. Api.Status = 1
  142. // 检查登录名是否已经存在
  143. _, err := backgroundm.ApiSourceGetByName(Api.SourceName)
  144. if err == nil {
  145. self.ajaxMsg("资源名已经存在", MSG_ERR)
  146. }
  147. if _, err := backgroundm.ApiSourceAdd(Api); err != nil {
  148. self.ajaxMsg(err.Error(), MSG_ERR)
  149. }
  150. self.ajaxMsg("", MSG_OK)
  151. }
  152. ApiUpdate, _ := backgroundm.ApiSourceGetById(Api_id)
  153. // 修改
  154. ApiUpdate.SourceName = strings.TrimSpace(self.GetString("source_name"))
  155. ApiUpdate.GroupId, _ = self.GetInt("group_id")
  156. ApiUpdate.UpdateId = self.userId
  157. ApiUpdate.UpdateTime = time.Now().Unix()
  158. ApiUpdate.Status = 1
  159. if err := ApiUpdate.Update(); err != nil {
  160. self.ajaxMsg(err.Error(), MSG_ERR)
  161. }
  162. self.ajaxMsg("", MSG_OK)
  163. }
  164. func (self *ApiSourceController) AjaxDel() {
  165. Api_id, _ := self.GetInt("id")
  166. Api, _ := backgroundm.ApiSourceGetById(Api_id)
  167. Api.UpdateTime = time.Now().Unix()
  168. Api.UpdateId = self.userId
  169. Api.Status = 0
  170. Api.Id = Api_id
  171. //TODO 判断是否暂用API
  172. if err := Api.Update(); err != nil {
  173. self.ajaxMsg(err.Error(), MSG_ERR)
  174. }
  175. self.ajaxMsg("", MSG_OK)
  176. }
  177. //新增接口实例
  178. // func (self *ApiController) AddApi() {
  179. // self.Data["pageTitle"] = "添加接口"
  180. // source_id, _ := self.GetInt("sid")
  181. // self.Data["Sid"] = source_id
  182. // sourceName := strings.TrimSpace(self.GetString("sourceName"))
  183. // self.Data["pageTitle"] = sourceName + " > " + "添加接口"
  184. // self.Data["sourceName"] = sourceName
  185. // //查询条件
  186. // self.display()
  187. // }
  188. // //修改接口实例
  189. // func (self *ApiController) EditApi() {
  190. // id, _ := self.GetInt("id", 0)
  191. // detail, _ := models.ApiDetailGetById(id)
  192. // params, _ := models.ApiParamGetById(detail.Id)
  193. // self.Data["Detail"] = detail
  194. // self.Data["Params"] = params
  195. // self.Data["ParamsCount"] = len(params)
  196. // self.display()
  197. // }
  198. // func (self *ApiController) AjaxApiSave() {
  199. // Api_id, _ := self.GetInt("id")
  200. // if Api_id == 0 {
  201. // ApiDetail := new(models.ApiDetail)
  202. // ApiDetail.SourceId, _ = self.GetInt("source_id")
  203. // ApiDetail.Method, _ = self.GetInt("method")
  204. // ApiDetail.ApiName = strings.TrimSpace(self.GetString("api_name"))
  205. // ApiDetail.ApiUrl = strings.TrimSpace(self.GetString("api_url"))
  206. // ApiDetail.Detail = strings.TrimSpace(self.GetString("detail"))
  207. // ApiDetail.CreateId = self.userId
  208. // ApiDetail.UpdateId = self.userId
  209. // ApiDetail.CreateTime = time.Now().Unix()
  210. // ApiDetail.UpdateTime = time.Now().Unix()
  211. // ApiDetail.Status = 1
  212. // _, err := models.ApiDetailAdd(ApiDetail)
  213. // if err != nil {
  214. // self.ajaxMsg(err.Error(), MSG_ERR)
  215. // }
  216. // self.ajaxMsg("", MSG_OK)
  217. // }
  218. // //修改
  219. // ApiDetail, _ := models.ApiDetailGetById(Api_id)
  220. // ApiDetail.SourceId, _ = self.GetInt("source_id")
  221. // ApiDetail.Id, _ = self.GetInt("id")
  222. // ApiDetail.Method, _ = self.GetInt("method")
  223. // ApiDetail.ApiName = strings.TrimSpace(self.GetString("api_name"))
  224. // ApiDetail.ApiUrl = strings.TrimSpace(self.GetString("api_url"))
  225. // ApiDetail.Detail = strings.TrimSpace(self.GetString("detail"))
  226. // ApiDetail.UpdateId = self.userId
  227. // ApiDetail.UpdateTime = time.Now().Unix()
  228. // ApiDetail.Status, _ = self.GetInt("status")
  229. // if err := ApiDetail.Update(); err != nil {
  230. // self.ajaxMsg(err.Error(), MSG_ERR)
  231. // }
  232. // self.ajaxMsg("", MSG_OK)
  233. // }