api.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**********************************************
  2. ** @Des: This file ...
  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. "fmt"
  11. "strings"
  12. "time"
  13. "github.com/astaxie/beego"
  14. "wuyebaoxiuapi/models/backgroundm"
  15. )
  16. var (
  17. AUDIT_STATUS = [5]string{"<span class='layui-badge layui-bg-black'>暂停使用</span>",
  18. "<span class='layui-badge layui-bg-orange'>正在开发</span>",
  19. "<span class='layui-badge layui-bg-blue'>正在审核</span>",
  20. "<span class='layui-badge layui-bg-green'>审核通过</span>",
  21. "<span class='layui-badge'>未通过</span>"}
  22. AUDIT_STATUS_TEXT = [5]string{"暂停使用",
  23. "正在开发",
  24. "正在审核",
  25. "审核通过",
  26. "未通过"}
  27. REQUEST_METHOD = [6]string{"未知", "GET", "POST", "PUT", "PATCH", "DELETE"}
  28. )
  29. type ApiController struct {
  30. BaseController
  31. }
  32. func (self *ApiController) List() {
  33. self.Data["pageTitle"] = "API接口"
  34. self.Data["ApiCss"] = true
  35. self.Data["auditStatus"] = AUDIT_STATUS_TEXT
  36. self.display()
  37. }
  38. func (self *ApiController) Table() {
  39. //列表
  40. page, err := self.GetInt("page")
  41. if err != nil {
  42. page = 1
  43. }
  44. limit, err := self.GetInt("limit")
  45. if err != nil {
  46. limit = 30
  47. }
  48. apiName := strings.TrimSpace(self.GetString("apiName"))
  49. status, _ := self.GetInt("status", -1)
  50. //获取分组
  51. sourceList := sourceLists()
  52. self.pageSize = limit
  53. //查询条件
  54. filters := make([]interface{}, 0)
  55. if status != -1 {
  56. filters = append(filters, "status", status)
  57. } else {
  58. filters = append(filters, "status__in", []int{0, 1, 2, 3, 4})
  59. }
  60. if apiName != "" {
  61. filters = append(filters, "api_name__icontains", apiName)
  62. }
  63. result, count := backgroundm.ApiDetailGetList(page, self.pageSize, filters...)
  64. list := make([]map[string]interface{}, len(result))
  65. for k, v := range result {
  66. row := make(map[string]interface{})
  67. row["id"] = v.Id
  68. row["api_name"] = v.ApiName
  69. row["api_url"] = v.ApiUrl
  70. row["status_text"] = AUDIT_STATUS[v.Status]
  71. row["status"] = v.Status
  72. row["method"] = REQUEST_METHOD[v.Method]
  73. sourceInfo := getSourceInfo(sourceList, v.SourceId)
  74. row["source_name"] = sourceInfo.SourceName
  75. row["create_time"] = beego.Date(time.Unix(v.CreateTime, 0), "Y-m-d H:i:s")
  76. row["update_time"] = beego.Date(time.Unix(v.UpdateTime, 0), "Y-m-d H:i:s")
  77. list[k] = row
  78. }
  79. self.ajaxList("成功", MSG_OK, count, list)
  80. }
  81. //查看详情
  82. func (self *ApiController) Detail() {
  83. self.Data["ApiCss"] = true
  84. id, _ := self.GetInt("id", 0)
  85. detail, _ := backgroundm.ApiFullDetailById(id)
  86. row := make(map[string]interface{})
  87. row["id"] = detail.Id
  88. row["source_id"] = detail.SourceId
  89. row["api_url"] = detail.ApiUrl
  90. row["api_name"] = detail.ApiName
  91. row["detail"] = detail.Detail
  92. row["status"] = detail.Status
  93. row["create_name"] = detail.CreateName
  94. row["update_name"] = detail.UpdateName
  95. row["audit_name"] = detail.AuditName
  96. row["audit_status"] = AUDIT_STATUS[detail.Status]
  97. row["method"] = REQUEST_METHOD[detail.Method]
  98. row["audit_time"] = beego.Date(time.Unix(detail.AuditTime, 0), "Y-m-d H:i:s")
  99. row["update_time"] = beego.Date(time.Unix(detail.UpdateTime, 0), "Y-m-d H:i:s")
  100. self.Data["pageTitle"] = "查看 " + detail.ApiName
  101. self.Data["Detail"] = row
  102. self.display()
  103. }
  104. //审核
  105. func (self *ApiController) Audit() {
  106. self.Data["ApiCss"] = true
  107. id, _ := self.GetInt("id", 0)
  108. detail, _ := backgroundm.ApiFullDetailById(id)
  109. row := make(map[string]interface{})
  110. row["id"] = detail.Id
  111. row["source_id"] = detail.SourceId
  112. row["api_url"] = detail.ApiUrl
  113. row["api_name"] = detail.ApiName
  114. row["detail"] = detail.Detail
  115. row["status"] = detail.Status
  116. row["create_name"] = detail.CreateName
  117. row["update_name"] = detail.UpdateName
  118. row["audit_name"] = detail.AuditName
  119. row["audit_status"] = AUDIT_STATUS[detail.Status]
  120. row["method"] = REQUEST_METHOD[detail.Method]
  121. row["audit_time"] = beego.Date(time.Unix(detail.AuditTime, 0), "Y-m-d H:i:s")
  122. row["update_time"] = beego.Date(time.Unix(detail.UpdateTime, 0), "Y-m-d H:i:s")
  123. self.Data["pageTitle"] = "审核 " + detail.ApiName
  124. self.Data["Detail"] = row
  125. self.display()
  126. }
  127. func (self *ApiController) AjaxDel() {
  128. Api_id, _ := self.GetInt("id")
  129. Api, _ := backgroundm.ApiDetailGetById(Api_id)
  130. Api.UpdateTime = time.Now().Unix()
  131. Api.UpdateId = self.userId
  132. if Api.Status == 0 {
  133. Api.Status = 1
  134. } else {
  135. Api.Status = 0
  136. }
  137. Api.Id = Api_id
  138. //TODO 判断是否暂用API
  139. if err := Api.Update(); err != nil {
  140. self.ajaxMsg(err.Error(), MSG_ERR)
  141. }
  142. self.ajaxMsg("", MSG_OK)
  143. }
  144. func (self *ApiController) AjaxChangeStatus() {
  145. Api_ids := strings.TrimSpace(self.GetString("ids"))
  146. status, _ := self.GetInt("status")
  147. if status == 1 {
  148. status = 2
  149. }
  150. _, err := backgroundm.ApiChangeStatus(Api_ids, status)
  151. if err != nil {
  152. self.ajaxMsg(err.Error(), MSG_ERR)
  153. }
  154. self.ajaxMsg("", MSG_OK)
  155. }
  156. //新增接口实例
  157. func (self *ApiController) Add() {
  158. self.Data["pageTitle"] = "添加接口"
  159. // //分组
  160. // groupList := groupLists()
  161. // self.Data["groupList"] = groupList
  162. //资源
  163. sourceList := sourceLists()
  164. self.Data["sourceList"] = sourceList
  165. fmt.Println(sourceList)
  166. tmplates := templateLists()
  167. self.Data["templates"] = tmplates
  168. self.display()
  169. }
  170. //修改接口实例
  171. func (self *ApiController) Edit() {
  172. self.Data["ApiCss"] = true
  173. id, _ := self.GetInt("id", 0)
  174. detail, _ := backgroundm.ApiFullDetailById(id)
  175. row := make(map[string]interface{})
  176. row["id"] = detail.Id
  177. row["source_id"] = detail.SourceId
  178. row["api_url"] = detail.ApiUrl
  179. row["api_name"] = detail.ApiName
  180. row["detail"] = detail.Detail
  181. row["status"] = detail.Status
  182. row["create_name"] = detail.CreateName
  183. row["update_name"] = detail.UpdateName
  184. row["audit_name"] = detail.AuditName
  185. row["audit_status"] = AUDIT_STATUS[detail.Status]
  186. row["method"] = detail.Method
  187. row["audit_time"] = beego.Date(time.Unix(detail.AuditTime, 0), "Y-m-d H:i:s")
  188. row["update_time"] = beego.Date(time.Unix(detail.UpdateTime, 0), "Y-m-d H:i:s")
  189. self.Data["pageTitle"] = "查看 " + detail.ApiName
  190. self.Data["Detail"] = row
  191. sourceList := sourceLists()
  192. self.Data["sourceList"] = sourceList
  193. tmplates := templateLists()
  194. self.Data["templates"] = tmplates
  195. self.display()
  196. }
  197. func (self *ApiController) AjaxSave() {
  198. Api_id, _ := self.GetInt("id")
  199. if Api_id == 0 {
  200. ApiDetail := new(backgroundm.ApiDetail)
  201. ApiDetail.SourceId, _ = self.GetInt("source_id")
  202. ApiDetail.Method, _ = self.GetInt("method")
  203. ApiDetail.ApiName = strings.TrimSpace(self.GetString("api_name"))
  204. ApiDetail.ApiUrl = strings.TrimSpace(self.GetString("api_url"))
  205. ApiDetail.Detail = strings.TrimSpace(self.GetString("detail"))
  206. ApiDetail.CreateId = self.userId
  207. ApiDetail.UpdateId = self.userId
  208. ApiDetail.CreateTime = time.Now().Unix()
  209. ApiDetail.UpdateTime = time.Now().Unix()
  210. ApiDetail.Status = 1
  211. _, err := backgroundm.ApiDetailAdd(ApiDetail)
  212. if err != nil {
  213. self.ajaxMsg(err.Error(), MSG_ERR)
  214. }
  215. self.ajaxMsg("", MSG_OK)
  216. }
  217. //修改
  218. ApiDetail, _ := backgroundm.ApiDetailGetById(Api_id)
  219. ApiDetail.SourceId, _ = self.GetInt("source_id")
  220. ApiDetail.Id, _ = self.GetInt("id")
  221. ApiDetail.Method, _ = self.GetInt("method")
  222. ApiDetail.ApiName = strings.TrimSpace(self.GetString("api_name"))
  223. ApiDetail.ApiUrl = strings.TrimSpace(self.GetString("api_url"))
  224. ApiDetail.Detail = strings.TrimSpace(self.GetString("detail"))
  225. ApiDetail.UpdateId = self.userId
  226. ApiDetail.UpdateTime = time.Now().Unix()
  227. ApiDetail.Status, _ = self.GetInt("status")
  228. ApiDetail.Status = 1
  229. if err := ApiDetail.Update(); err != nil {
  230. self.ajaxMsg(err.Error(), MSG_ERR)
  231. }
  232. self.ajaxMsg("", MSG_OK)
  233. }