api.go.tpl 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package api
  2. import (
  3. {{if not .OnlyTemplate}}
  4. "{{.Module}}/global"
  5. "{{.Module}}/model/common/response"
  6. "{{.Module}}/plugin/{{.Package}}/model"
  7. {{- if not .IsTree}}
  8. "{{.Module}}/plugin/{{.Package}}/model/request"
  9. {{- end }}
  10. "github.com/gin-gonic/gin"
  11. "go.uber.org/zap"
  12. {{- if .AutoCreateResource}}
  13. "{{.Module}}/utils"
  14. {{- end }}
  15. {{- else }}
  16. "{{.Module}}/model/common/response"
  17. "github.com/gin-gonic/gin"
  18. {{- end }}
  19. )
  20. var {{.StructName}} = new({{.Abbreviation}})
  21. type {{.Abbreviation}} struct {}
  22. {{if not .OnlyTemplate}}
  23. // Create{{.StructName}} 创建{{.Description}}
  24. // @Tags {{.StructName}}
  25. // @Summary 创建{{.Description}}
  26. // @Security ApiKeyAuth
  27. // @Accept application/json
  28. // @Produce application/json
  29. // @Param data body model.{{.StructName}} true "创建{{.Description}}"
  30. // @Success 200 {object} response.Response{msg=string} "创建成功"
  31. // @Router /{{.Abbreviation}}/create{{.StructName}} [post]
  32. func (a *{{.Abbreviation}}) Create{{.StructName}}(c *gin.Context) {
  33. // 创建业务用Context
  34. ctx := c.Request.Context()
  35. var info model.{{.StructName}}
  36. err := c.ShouldBindJSON(&info)
  37. if err != nil {
  38. response.FailWithMessage(err.Error(), c)
  39. return
  40. }
  41. {{- if .AutoCreateResource }}
  42. info.CreatedBy = utils.GetUserID(c)
  43. {{- end }}
  44. err = service{{ .StructName }}.Create{{.StructName}}(ctx,&info)
  45. if err != nil {
  46. global.GVA_LOG.Error("创建失败!", zap.Error(err))
  47. response.FailWithMessage("创建失败:" + err.Error(), c)
  48. return
  49. }
  50. response.OkWithMessage("创建成功", c)
  51. }
  52. // Delete{{.StructName}} 删除{{.Description}}
  53. // @Tags {{.StructName}}
  54. // @Summary 删除{{.Description}}
  55. // @Security ApiKeyAuth
  56. // @Accept application/json
  57. // @Produce application/json
  58. // @Param data body model.{{.StructName}} true "删除{{.Description}}"
  59. // @Success 200 {object} response.Response{msg=string} "删除成功"
  60. // @Router /{{.Abbreviation}}/delete{{.StructName}} [delete]
  61. func (a *{{.Abbreviation}}) Delete{{.StructName}}(c *gin.Context) {
  62. // 创建业务用Context
  63. ctx := c.Request.Context()
  64. {{.PrimaryField.FieldJson}} := c.Query("{{.PrimaryField.FieldJson}}")
  65. {{- if .AutoCreateResource }}
  66. userID := utils.GetUserID(c)
  67. {{- end }}
  68. err := service{{ .StructName }}.Delete{{.StructName}}(ctx,{{.PrimaryField.FieldJson}} {{- if .AutoCreateResource -}},userID{{- end -}})
  69. if err != nil {
  70. global.GVA_LOG.Error("删除失败!", zap.Error(err))
  71. response.FailWithMessage("删除失败:" + err.Error(), c)
  72. return
  73. }
  74. response.OkWithMessage("删除成功", c)
  75. }
  76. // Delete{{.StructName}}ByIds 批量删除{{.Description}}
  77. // @Tags {{.StructName}}
  78. // @Summary 批量删除{{.Description}}
  79. // @Security ApiKeyAuth
  80. // @Accept application/json
  81. // @Produce application/json
  82. // @Success 200 {object} response.Response{msg=string} "批量删除成功"
  83. // @Router /{{.Abbreviation}}/delete{{.StructName}}ByIds [delete]
  84. func (a *{{.Abbreviation}}) Delete{{.StructName}}ByIds(c *gin.Context) {
  85. // 创建业务用Context
  86. ctx := c.Request.Context()
  87. {{.PrimaryField.FieldJson}}s := c.QueryArray("{{.PrimaryField.FieldJson}}s[]")
  88. {{- if .AutoCreateResource }}
  89. userID := utils.GetUserID(c)
  90. {{- end }}
  91. err := service{{ .StructName }}.Delete{{.StructName}}ByIds(ctx,{{.PrimaryField.FieldJson}}s{{- if .AutoCreateResource }},userID{{- end }})
  92. if err != nil {
  93. global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
  94. response.FailWithMessage("批量删除失败:" + err.Error(), c)
  95. return
  96. }
  97. response.OkWithMessage("批量删除成功", c)
  98. }
  99. // Update{{.StructName}} 更新{{.Description}}
  100. // @Tags {{.StructName}}
  101. // @Summary 更新{{.Description}}
  102. // @Security ApiKeyAuth
  103. // @Accept application/json
  104. // @Produce application/json
  105. // @Param data body model.{{.StructName}} true "更新{{.Description}}"
  106. // @Success 200 {object} response.Response{msg=string} "更新成功"
  107. // @Router /{{.Abbreviation}}/update{{.StructName}} [put]
  108. func (a *{{.Abbreviation}}) Update{{.StructName}}(c *gin.Context) {
  109. // 创建业务用Context
  110. ctx := c.Request.Context()
  111. var info model.{{.StructName}}
  112. err := c.ShouldBindJSON(&info)
  113. if err != nil {
  114. response.FailWithMessage(err.Error(), c)
  115. return
  116. }
  117. {{- if .AutoCreateResource }}
  118. info.UpdatedBy = utils.GetUserID(c)
  119. {{- end }}
  120. err = service{{ .StructName }}.Update{{.StructName}}(ctx,info)
  121. if err != nil {
  122. global.GVA_LOG.Error("更新失败!", zap.Error(err))
  123. response.FailWithMessage("更新失败:" + err.Error(), c)
  124. return
  125. }
  126. response.OkWithMessage("更新成功", c)
  127. }
  128. // Find{{.StructName}} 用id查询{{.Description}}
  129. // @Tags {{.StructName}}
  130. // @Summary 用id查询{{.Description}}
  131. // @Security ApiKeyAuth
  132. // @Accept application/json
  133. // @Produce application/json
  134. // @Param {{.PrimaryField.FieldJson}} query {{.PrimaryField.FieldType}} true "用id查询{{.Description}}"
  135. // @Success 200 {object} response.Response{data=model.{{.StructName}},msg=string} "查询成功"
  136. // @Router /{{.Abbreviation}}/find{{.StructName}} [get]
  137. func (a *{{.Abbreviation}}) Find{{.StructName}}(c *gin.Context) {
  138. // 创建业务用Context
  139. ctx := c.Request.Context()
  140. {{.PrimaryField.FieldJson}} := c.Query("{{.PrimaryField.FieldJson}}")
  141. re{{.Abbreviation}}, err := service{{ .StructName }}.Get{{.StructName}}(ctx,{{.PrimaryField.FieldJson}})
  142. if err != nil {
  143. global.GVA_LOG.Error("查询失败!", zap.Error(err))
  144. response.FailWithMessage("查询失败:" + err.Error(), c)
  145. return
  146. }
  147. response.OkWithData(re{{.Abbreviation}}, c)
  148. }
  149. {{- if .IsTree }}
  150. // Get{{.StructName}}List 分页获取{{.Description}}列表
  151. // @Tags {{.StructName}}
  152. // @Summary 分页获取{{.Description}}列表
  153. // @Security ApiKeyAuth
  154. // @Accept application/json
  155. // @Produce application/json
  156. // @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
  157. // @Router /{{.Abbreviation}}/get{{.StructName}}List [get]
  158. func (a *{{.Abbreviation}}) Get{{.StructName}}List(c *gin.Context) {
  159. // 创建业务用Context
  160. ctx := c.Request.Context()
  161. list, err := service{{ .StructName }}.Get{{.StructName}}InfoList(ctx)
  162. if err != nil {
  163. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  164. response.FailWithMessage("获取失败:" + err.Error(), c)
  165. return
  166. }
  167. response.OkWithDetailed(list, "获取成功", c)
  168. }
  169. {{- else }}
  170. // Get{{.StructName}}List 分页获取{{.Description}}列表
  171. // @Tags {{.StructName}}
  172. // @Summary 分页获取{{.Description}}列表
  173. // @Security ApiKeyAuth
  174. // @Accept application/json
  175. // @Produce application/json
  176. // @Param data query request.{{.StructName}}Search true "分页获取{{.Description}}列表"
  177. // @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
  178. // @Router /{{.Abbreviation}}/get{{.StructName}}List [get]
  179. func (a *{{.Abbreviation}}) Get{{.StructName}}List(c *gin.Context) {
  180. // 创建业务用Context
  181. ctx := c.Request.Context()
  182. var pageInfo request.{{.StructName}}Search
  183. err := c.ShouldBindQuery(&pageInfo)
  184. if err != nil {
  185. response.FailWithMessage(err.Error(), c)
  186. return
  187. }
  188. list, total, err := service{{ .StructName }}.Get{{.StructName}}InfoList(ctx,pageInfo)
  189. if err != nil {
  190. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  191. response.FailWithMessage("获取失败:" + err.Error(), c)
  192. return
  193. }
  194. response.OkWithDetailed(response.PageResult{
  195. List: list,
  196. Total: total,
  197. Page: pageInfo.Page,
  198. PageSize: pageInfo.PageSize,
  199. }, "获取成功", c)
  200. }
  201. {{- end }}
  202. {{- if .HasDataSource }}
  203. // Get{{.StructName}}DataSource 获取{{.StructName}}的数据源
  204. // @Tags {{.StructName}}
  205. // @Summary 获取{{.StructName}}的数据源
  206. // @Accept application/json
  207. // @Produce application/json
  208. // @Success 200 {object} response.Response{data=object,msg=string} "查询成功"
  209. // @Router /{{.Abbreviation}}/get{{.StructName}}DataSource [get]
  210. func (a *{{.Abbreviation}}) Get{{.StructName}}DataSource(c *gin.Context) {
  211. // 创建业务用Context
  212. ctx := c.Request.Context()
  213. // 此接口为获取数据源定义的数据
  214. dataSource, err := service{{ .StructName }}.Get{{.StructName}}DataSource(ctx)
  215. if err != nil {
  216. global.GVA_LOG.Error("查询失败!", zap.Error(err))
  217. response.FailWithMessage("查询失败:" + err.Error(), c)
  218. return
  219. }
  220. response.OkWithData(dataSource, c)
  221. }
  222. {{- end }}
  223. {{- end }}
  224. // Get{{.StructName}}Public 不需要鉴权的{{.Description}}接口
  225. // @Tags {{.StructName}}
  226. // @Summary 不需要鉴权的{{.Description}}接口
  227. // @Accept application/json
  228. // @Produce application/json
  229. // @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
  230. // @Router /{{.Abbreviation}}/get{{.StructName}}Public [get]
  231. func (a *{{.Abbreviation}}) Get{{.StructName}}Public(c *gin.Context) {
  232. // 创建业务用Context
  233. ctx := c.Request.Context()
  234. // 此接口不需要鉴权 示例为返回了一个固定的消息接口,一般本接口用于C端服务,需要自己实现业务逻辑
  235. service{{ .StructName }}.Get{{.StructName}}Public(ctx)
  236. response.OkWithDetailed(gin.H{"info": "不需要鉴权的{{.Description}}接口信息"}, "获取成功", c)
  237. }