sys_auto_code.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. package system
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/url"
  6. "os"
  7. "strings"
  8. "server/global"
  9. "server/model/common/response"
  10. "server/model/system"
  11. "server/utils"
  12. "github.com/gin-gonic/gin"
  13. "go.uber.org/zap"
  14. )
  15. type AutoCodeApi struct{}
  16. // PreviewTemp
  17. // @Tags AutoCode
  18. // @Summary 预览创建后的代码
  19. // @Security ApiKeyAuth
  20. // @accept application/json
  21. // @Produce application/json
  22. // @Param data body system.AutoCodeStruct true "预览创建代码"
  23. // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "预览创建后的代码"
  24. // @Router /autoCode/preview [post]
  25. func (autoApi *AutoCodeApi) PreviewTemp(c *gin.Context) {
  26. var a system.AutoCodeStruct
  27. _ = c.ShouldBindJSON(&a)
  28. if err := utils.Verify(a, utils.AutoCodeVerify); err != nil {
  29. response.FailWithMessage(err.Error(), c)
  30. return
  31. }
  32. a.Pretreatment() // 处理go关键字
  33. a.PackageT = utils.FirstUpper(a.Package)
  34. autoCode, err := autoCodeService.PreviewTemp(a)
  35. if err != nil {
  36. global.GVA_LOG.Error("预览失败!", zap.Error(err))
  37. response.FailWithMessage("预览失败", c)
  38. } else {
  39. response.OkWithDetailed(gin.H{"autoCode": autoCode}, "预览成功", c)
  40. }
  41. }
  42. // CreateTemp
  43. // @Tags AutoCode
  44. // @Summary 自动代码模板
  45. // @Security ApiKeyAuth
  46. // @accept application/json
  47. // @Produce application/json
  48. // @Param data body system.AutoCodeStruct true "创建自动代码"
  49. // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
  50. // @Router /autoCode/createTemp [post]
  51. func (autoApi *AutoCodeApi) CreateTemp(c *gin.Context) {
  52. var a system.AutoCodeStruct
  53. _ = c.ShouldBindJSON(&a)
  54. if err := utils.Verify(a, utils.AutoCodeVerify); err != nil {
  55. response.FailWithMessage(err.Error(), c)
  56. return
  57. }
  58. a.Pretreatment()
  59. var apiIds []uint
  60. var menuId uint
  61. if a.AutoCreateApiToSql {
  62. if ids, err := autoCodeService.AutoCreateApi(&a); err != nil {
  63. global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Error(err))
  64. c.Writer.Header().Add("success", "false")
  65. c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!"))
  66. return
  67. } else {
  68. apiIds = ids
  69. }
  70. }
  71. if a.AutoCreateMenuToSql {
  72. if id, err := autoCodeService.AutoCreateMenu(&a); err != nil {
  73. global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Error(err))
  74. c.Writer.Header().Add("success", "false")
  75. c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!"))
  76. } else {
  77. menuId = id
  78. }
  79. }
  80. a.PackageT = utils.FirstUpper(a.Package)
  81. err := autoCodeService.CreateTemp(a, menuId, apiIds...)
  82. if err != nil {
  83. if errors.Is(err, system.ErrAutoMove) {
  84. c.Writer.Header().Add("success", "true")
  85. c.Writer.Header().Add("msg", url.QueryEscape(err.Error()))
  86. } else {
  87. c.Writer.Header().Add("success", "false")
  88. c.Writer.Header().Add("msg", url.QueryEscape(err.Error()))
  89. _ = os.Remove("./ginvueadmin.zip")
  90. }
  91. } else {
  92. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; deptname=%s", "ginvueadmin.zip")) // fmt.Sprintf("attachment; deptname=%s", deptname)对下载的文件重命名
  93. c.Writer.Header().Add("Content-Type", "application/json")
  94. c.Writer.Header().Add("success", "true")
  95. c.File("./ginvueadmin.zip")
  96. _ = os.Remove("./ginvueadmin.zip")
  97. }
  98. }
  99. // GetDB
  100. // @Tags AutoCode
  101. // @Summary 获取当前所有数据库
  102. // @Security ApiKeyAuth
  103. // @accept application/json
  104. // @Produce application/json
  105. // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "获取当前所有数据库"
  106. // @Router /autoCode/getDatabase [get]
  107. func (autoApi *AutoCodeApi) GetDB(c *gin.Context) {
  108. businessDB := c.Query("businessDB")
  109. dbs, err := autoCodeService.Database(businessDB).GetDB(businessDB)
  110. var dbList []map[string]interface{}
  111. for _, db := range global.GVA_CONFIG.DBList {
  112. var item = make(map[string]interface{})
  113. item["aliasName"] = db.AliasName
  114. item["dbName"] = db.Dbname
  115. item["disable"] = db.Disable
  116. item["dbtype"] = db.Type
  117. dbList = append(dbList, item)
  118. }
  119. if err != nil {
  120. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  121. response.FailWithMessage("获取失败", c)
  122. } else {
  123. response.OkWithDetailed(gin.H{"dbs": dbs, "dbList": dbList}, "获取成功", c)
  124. }
  125. }
  126. // GetTables
  127. // @Tags AutoCode
  128. // @Summary 获取当前数据库所有表
  129. // @Security ApiKeyAuth
  130. // @accept application/json
  131. // @Produce application/json
  132. // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "获取当前数据库所有表"
  133. // @Router /autoCode/getTables [get]
  134. func (autoApi *AutoCodeApi) GetTables(c *gin.Context) {
  135. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  136. businessDB := c.Query("businessDB")
  137. tables, err := autoCodeService.Database(businessDB).GetTables(businessDB, dbName)
  138. if err != nil {
  139. global.GVA_LOG.Error("查询table失败!", zap.Error(err))
  140. response.FailWithMessage("查询table失败", c)
  141. } else {
  142. response.OkWithDetailed(gin.H{"tables": tables}, "获取成功", c)
  143. }
  144. }
  145. // GetColumn
  146. // @Tags AutoCode
  147. // @Summary 获取当前表所有字段
  148. // @Security ApiKeyAuth
  149. // @accept application/json
  150. // @Produce application/json
  151. // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "获取当前表所有字段"
  152. // @Router /autoCode/getColumn [get]
  153. func (autoApi *AutoCodeApi) GetColumn(c *gin.Context) {
  154. businessDB := c.Query("businessDB")
  155. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  156. tableName := c.Query("tableName")
  157. columns, err := autoCodeService.Database(businessDB).GetColumn(businessDB, tableName, dbName)
  158. if err != nil {
  159. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  160. response.FailWithMessage("获取失败", c)
  161. } else {
  162. response.OkWithDetailed(gin.H{"columns": columns}, "获取成功", c)
  163. }
  164. }
  165. // CreatePackage
  166. // @Tags AutoCode
  167. // @Summary 创建package
  168. // @Security ApiKeyAuth
  169. // @accept application/json
  170. // @Produce application/json
  171. // @Param data body system.SysAutoCode true "创建package"
  172. // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "创建package成功"
  173. // @Router /autoCode/createPackage [post]
  174. func (autoApi *AutoCodeApi) CreatePackage(c *gin.Context) {
  175. var a system.SysAutoCode
  176. _ = c.ShouldBindJSON(&a)
  177. if err := utils.Verify(a, utils.AutoPackageVerify); err != nil {
  178. response.FailWithMessage(err.Error(), c)
  179. return
  180. }
  181. // PackageName可能导致路径穿越的问题 / 和 \ 都要防止
  182. if strings.Contains(a.PackageName, "\\") || strings.Contains(a.PackageName, "/") || strings.Contains(a.PackageName, "..") {
  183. response.FailWithMessage("包名不合法", c)
  184. return
  185. }
  186. err := autoCodeService.CreateAutoCode(&a)
  187. if err != nil {
  188. global.GVA_LOG.Error("创建失败!", zap.Error(err))
  189. response.FailWithMessage("创建失败", c)
  190. } else {
  191. response.OkWithMessage("创建成功", c)
  192. }
  193. }
  194. // GetPackage
  195. // @Tags AutoCode
  196. // @Summary 获取package
  197. // @Security ApiKeyAuth
  198. // @accept application/json
  199. // @Produce application/json
  200. // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "创建package成功"
  201. // @Router /autoCode/getPackage [post]
  202. func (autoApi *AutoCodeApi) GetPackage(c *gin.Context) {
  203. pkgs, err := autoCodeService.GetPackage()
  204. if err != nil {
  205. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  206. response.FailWithMessage("获取失败", c)
  207. } else {
  208. response.OkWithDetailed(gin.H{"pkgs": pkgs}, "获取成功", c)
  209. }
  210. }
  211. // DelPackage
  212. // @Tags AutoCode
  213. // @Summary 删除package
  214. // @Security ApiKeyAuth
  215. // @accept application/json
  216. // @Produce application/json
  217. // @Param data body system.SysAutoCode true "创建package"
  218. // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "删除package成功"
  219. // @Router /autoCode/delPackage [post]
  220. func (autoApi *AutoCodeApi) DelPackage(c *gin.Context) {
  221. var a system.SysAutoCode
  222. _ = c.ShouldBindJSON(&a)
  223. err := autoCodeService.DelPackage(a)
  224. if err != nil {
  225. global.GVA_LOG.Error("删除失败!", zap.Error(err))
  226. response.FailWithMessage("删除失败", c)
  227. } else {
  228. response.OkWithMessage("删除成功", c)
  229. }
  230. }
  231. // AutoPlug
  232. // @Tags AutoCode
  233. // @Summary 创建插件模板
  234. // @Security ApiKeyAuth
  235. // @accept application/json
  236. // @Produce application/json
  237. // @Param data body system.SysAutoCode true "创建插件模板"
  238. // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "创建插件模板成功"
  239. // @Router /autoCode/createPlug [post]
  240. func (autoApi *AutoCodeApi) AutoPlug(c *gin.Context) {
  241. var a system.AutoPlugReq
  242. err := c.ShouldBindJSON(&a)
  243. if err != nil {
  244. response.FailWithMessage(err.Error(), c)
  245. return
  246. }
  247. if strings.Contains(a.PlugName, "\\") || strings.Contains(a.PlugName, "/") || strings.Contains(a.PlugName, "..") {
  248. response.FailWithMessage("插件名称不合法", c)
  249. return
  250. }
  251. a.Snake = strings.ToLower(a.PlugName)
  252. a.NeedModel = a.HasRequest || a.HasResponse
  253. err = autoCodeService.CreatePlug(a)
  254. if err != nil {
  255. global.GVA_LOG.Error("预览失败!", zap.Error(err))
  256. response.FailWithMessage("预览失败", c)
  257. return
  258. }
  259. response.Ok(c)
  260. }
  261. // InstallPlugin
  262. // @Tags AutoCode
  263. // @Summary 安装插件
  264. // @Security ApiKeyAuth
  265. // @accept multipart/form-data
  266. // @Produce application/json
  267. // @Param plug formData dept true "this is a test dept"
  268. // @Success 200 {object} response.Response{data=[]interface{},msg=string} "安装插件成功"
  269. // @Router /autoCode/installPlugin [post]
  270. func (autoApi *AutoCodeApi) InstallPlugin(c *gin.Context) {
  271. header, err := c.FormFile("plug")
  272. if err != nil {
  273. response.FailWithMessage(err.Error(), c)
  274. return
  275. }
  276. web, server, err := autoCodeService.InstallPlugin(header)
  277. webStr := "web插件安装成功"
  278. serverStr := "server插件安装成功"
  279. if web == -1 {
  280. webStr = "web端插件未成功安装,请按照文档自行解压安装,如果为纯后端插件请忽略此条提示"
  281. }
  282. if server == -1 {
  283. serverStr = "server端插件未成功安装,请按照文档自行解压安装,如果为纯前端插件请忽略此条提示"
  284. }
  285. if err != nil {
  286. response.FailWithMessage(err.Error(), c)
  287. return
  288. }
  289. response.OkWithData([]interface{}{
  290. gin.H{
  291. "code": web,
  292. "msg": webStr,
  293. },
  294. gin.H{
  295. "code": server,
  296. "msg": serverStr,
  297. }}, c)
  298. }
  299. // PubPlug
  300. // @Tags AutoCode
  301. // @Summary 打包插件
  302. // @Security ApiKeyAuth
  303. // @accept application/json
  304. // @Produce application/json
  305. // @Param data body system.SysAutoCode true "打包插件"
  306. // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "打包插件成功"
  307. // @Router /autoCode/pubPlug [get]
  308. func (autoApi *AutoCodeApi) PubPlug(c *gin.Context) {
  309. plugName := c.Query("plugName")
  310. zipPath, err := autoCodeService.PubPlug(plugName)
  311. if err != nil {
  312. global.GVA_LOG.Error("打包失败!", zap.Error(err))
  313. response.FailWithMessage("打包失败"+err.Error(), c)
  314. return
  315. }
  316. response.OkWithMessage(fmt.Sprintf("打包成功,文件路径为:%s", zipPath), c)
  317. }