application.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package controllers
  2. import (
  3. "context"
  4. "sparrow/pkg/models"
  5. "sparrow/pkg/rpcs"
  6. "sparrow/pkg/server"
  7. "sparrow/services/knowoapi/services"
  8. "strings"
  9. "github.com/opentracing/opentracing-go/ext"
  10. "github.com/opentracing/opentracing-go"
  11. "github.com/kataras/iris"
  12. )
  13. // AppController api
  14. type AppController struct {
  15. Ctx iris.Context
  16. Service services.ApplicationService
  17. Token Token
  18. }
  19. // Post 创建app
  20. // POST /application
  21. func (a *AppController) Post() {
  22. app := new(models.Application)
  23. if err := parseBody(a.Ctx, app); err != nil {
  24. badRequest(a.Ctx, err)
  25. return
  26. }
  27. app.VendorID = a.Token.getVendorID(a.Ctx)
  28. if app.AppIcon != "" {
  29. //move image
  30. args := &rpcs.ArgsMoveFile{
  31. Source: app.AppIcon,
  32. Target: "application",
  33. }
  34. reply := &rpcs.ReplyMoveFile{}
  35. span, ctx := opentracing.StartSpanFromContext(context.Background(), a.Ctx.Path())
  36. defer span.Finish()
  37. ext.SpanKindRPCClient.Set(span)
  38. ext.HTTPMethod.Set(span, a.Ctx.Method())
  39. err := server.RPCCallByName(ctx, "fileaccess", "FileAccess.MoveFile", args, reply)
  40. if err != nil {
  41. server.Log.Error(err)
  42. app.AppIcon = ""
  43. } else {
  44. app.AppIcon = reply.FilePath
  45. }
  46. }
  47. err := a.Service.Create(app)
  48. if err != nil {
  49. responseError(a.Ctx, ErrDatabase, err.Error())
  50. return
  51. }
  52. done(a.Ctx, app)
  53. }
  54. // Get 获取我的app
  55. // GET /application?pi=1&ps=&name=
  56. func (a *AppController) Get() {
  57. pi, err := a.Ctx.URLParamInt("pi")
  58. if err != nil {
  59. badRequest(a.Ctx, err)
  60. return
  61. }
  62. ps, err := a.Ctx.URLParamInt("ps")
  63. if err != nil {
  64. badRequest(a.Ctx, err)
  65. return
  66. }
  67. name := a.Ctx.URLParam("name")
  68. ds, total, err := a.Service.GetVendorApps(a.Token.getVendorID(a.Ctx), pi, ps, name)
  69. if err != nil {
  70. responseError(a.Ctx, ErrDatabase, err.Error())
  71. return
  72. }
  73. done(a.Ctx, map[string]interface{}{
  74. "list": ds,
  75. "total": total,
  76. })
  77. }
  78. // Delete 删除
  79. // DELETE /application
  80. func (a *AppController) Delete() {
  81. app := new(models.Application)
  82. if err := parseBody(a.Ctx, app); err != nil {
  83. badRequest(a.Ctx, err)
  84. return
  85. }
  86. err := a.Service.Delete(app)
  87. if err != nil {
  88. responseError(a.Ctx, ErrDatabase, err.Error())
  89. return
  90. }
  91. done(a.Ctx, "删除成功")
  92. }
  93. // GetBy 获取app信息
  94. // GET /application/{key}
  95. func (a *AppController) GetBy(key string) {
  96. app, err := a.Service.GetAppInfo(a.Token.getVendorID(a.Ctx), key)
  97. if err != nil {
  98. responseError(a.Ctx, ErrDatabase, err.Error())
  99. return
  100. }
  101. done(a.Ctx, app)
  102. }
  103. // Put 更新
  104. // PUT /application
  105. func (a *AppController) Put() {
  106. app := new(models.Application)
  107. if err := parseBody(a.Ctx, app); err != nil {
  108. badRequest(a.Ctx, err)
  109. return
  110. }
  111. // check update image
  112. if strings.Contains(app.AppIcon, "tmp") {
  113. //move image
  114. args := &rpcs.ArgsMoveFile{
  115. Source: app.AppIcon,
  116. Target: "application",
  117. }
  118. reply := &rpcs.ReplyMoveFile{}
  119. err := server.RPCCallByName(nil, "fileaccess", "FileAccess.MoveFile", args, reply)
  120. if err != nil {
  121. server.Log.Error(err)
  122. app.AppIcon = ""
  123. } else {
  124. app.AppIcon = reply.FilePath
  125. }
  126. }
  127. if app.AppIcon == "" {
  128. old, _ := a.Service.GetAppInfo(a.Token.getVendorID(a.Ctx), app.AppKey)
  129. arg := &rpcs.ArgsDeleteFile{
  130. FileName: old.AppIcon,
  131. }
  132. reply := &rpcs.ReplyEmptyResult{}
  133. server.RPCCallByName(nil, "fileaccess", "FileAccess.DeleteFile", arg, reply)
  134. }
  135. result, err := a.Service.Update(app)
  136. if err != nil {
  137. responseError(a.Ctx, ErrDatabase, err.Error())
  138. return
  139. }
  140. done(a.Ctx, result)
  141. }