蛋蛋星球 后台端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

187 lines
4.8 KiB

  1. package cloud_bundle
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. "applet/app/e"
  6. "applet/app/md"
  7. "applet/app/svc"
  8. "applet/app/utils"
  9. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  10. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  11. "fmt"
  12. "github.com/gin-gonic/gin"
  13. "io"
  14. "net/http"
  15. "strings"
  16. "time"
  17. )
  18. func List(c *gin.Context) {
  19. var req *md.CloudBundleReq
  20. if err := c.ShouldBindJSON(&req); err != nil {
  21. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  22. return
  23. }
  24. var resp md.CloudBundleResp
  25. noticeList := make([]md.CloudBundleList, 0)
  26. NewCloudBundleDb := implement.NewCloudBundleDb(db.Db)
  27. notice, total, _ := NewCloudBundleDb.FindCloudBundleAndTotal(req.Page, req.Limit)
  28. resp.Total = total
  29. if notice != nil {
  30. for _, v := range *notice {
  31. tmp := md.CloudBundleList{
  32. Id: utils.IntToStr(v.Id),
  33. BuildId: v.BuildId,
  34. Version: "v" + v.Version,
  35. Os: utils.IntToStr(v.Os),
  36. State: utils.IntToStr(v.State),
  37. Memo: v.Memo,
  38. Src: svc.GetOssUrl(v.Src),
  39. Bit: v.Bit,
  40. IsCombine: v.IsCombine,
  41. FinishAt: "--",
  42. IsAuditing: utils.IntToStr(v.IsAuditing),
  43. }
  44. if v.ApplyAt > 0 {
  45. tmp.ApplyAt = time.Unix(int64(v.ApplyAt), 0).Format("2006-01-02 15:04:05")
  46. }
  47. if v.FinishAt > 0 && v.State == 999 {
  48. tmp.FinishAt = time.Unix(int64(v.FinishAt), 0).Format("2006-01-02 15:04:05")
  49. }
  50. noticeList = append(noticeList, tmp)
  51. }
  52. }
  53. lastAndroid, _ := NewCloudBundleDb.GetCloudBundleLast("1")
  54. resp.LastBit = "64"
  55. resp.LastIsCombine = "0"
  56. if lastAndroid != nil {
  57. resp.LastAndroid = "v" + lastAndroid.Version
  58. resp.LastBit = lastAndroid.Bit
  59. resp.LastIsCombine = lastAndroid.IsCombine
  60. }
  61. lastIos, _ := NewCloudBundleDb.GetCloudBundleLast("2")
  62. if lastIos != nil {
  63. resp.ListIos = "v" + lastIos.Version
  64. }
  65. resp.List = noticeList
  66. e.OutSuc(c, resp, nil)
  67. return
  68. }
  69. func Build(c *gin.Context) {
  70. var req *md.CloudBundleBuildReq
  71. if err := c.ShouldBindJSON(&req); err != nil {
  72. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  73. return
  74. }
  75. count, _ := db.Db.Where("os=? and version=?", req.Os, req.Version).Count(&model.CloudBundle{})
  76. if count > 0 {
  77. e.OutErr(c, 400, e.NewErr(400, "版本已存在"))
  78. return
  79. }
  80. tmp := model.CloudBundle{
  81. Os: utils.StrToInt(req.Os),
  82. Version: req.Version,
  83. ApplyAt: int(time.Now().Unix()),
  84. Memo: req.Memo,
  85. AppletId: 0,
  86. IsCombine: req.IsCombine,
  87. NewBit: strings.Join(req.Bit, ","),
  88. }
  89. //TODO 调用打包机
  90. keys := "CloudBuild"
  91. if req.Os == "2" {
  92. keys = "CloudBuild-iOS"
  93. }
  94. token := "aaaaaa"
  95. masterKey := "master"
  96. domain := ""
  97. if cfg.Prd == false {
  98. masterKey = "dev"
  99. domain = ""
  100. }
  101. url := "http://120.76.175.204:8080/job/" + keys + "/buildWithParameters?timestamp=" + utils.Int64ToStr(time.Now().Unix()) + "&token=" + token + "&branch=" + masterKey + "&domain=" + domain
  102. tmp.Bit = strings.Join(req.Bit, ",")
  103. if req.Os == "2" {
  104. db.Db.Insert(&tmp)
  105. url += "&id=" + utils.IntToStr(tmp.Id)
  106. getcurl(url)
  107. } else {
  108. if req.IsCombine != "1" {
  109. for _, v := range req.Bit {
  110. for _, v1 := range req.Platform {
  111. tmp.Bit = v
  112. tmp.Platform = v1
  113. db.Db.Insert(&tmp)
  114. url += "&id=" + utils.IntToStr(tmp.Id)
  115. getcurl(url)
  116. }
  117. }
  118. } else {
  119. for _, v1 := range req.Platform {
  120. tmp.Platform = v1
  121. db.Db.Insert(&tmp)
  122. url += "&id=" + utils.IntToStr(tmp.Id)
  123. getcurl(url)
  124. }
  125. }
  126. }
  127. e.OutSuc(c, "success", nil)
  128. return
  129. }
  130. func getcurl(url string) (string, error) {
  131. client := &http.Client{}
  132. // 创建请求
  133. request, err := http.NewRequest("GET", url, nil)
  134. if err != nil {
  135. return "", err
  136. }
  137. // 设置用户名和密码,这里的"user:password"是用户名和密码的组合,中间不能有空格
  138. request.SetBasicAuth("fnuoos", "11ad0050eef8521bc85f1c06ebf018654b")
  139. // 发送请求
  140. response, err := client.Do(request)
  141. if err != nil {
  142. return "", err
  143. }
  144. // 读取响应内容
  145. defer response.Body.Close()
  146. body, err := io.ReadAll(response.Body)
  147. if err != nil {
  148. return "", err
  149. }
  150. fmt.Println(string(body))
  151. return string(body), nil
  152. }
  153. func Del(c *gin.Context) {
  154. var req *md.ArticleDelReq
  155. if err := c.ShouldBindJSON(&req); err != nil {
  156. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  157. return
  158. }
  159. db.Db.In("id", req.Id).Delete(&model.CloudBundle{})
  160. e.OutSuc(c, "success", nil)
  161. return
  162. }
  163. func AuditSet(c *gin.Context) {
  164. var req *md.CommReq
  165. if err := c.ShouldBindJSON(&req); err != nil {
  166. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  167. return
  168. }
  169. db.Db.Where("id=?", req.Id).Cols("is_auditing").Update(&model.CloudBundle{IsAuditing: 1})
  170. e.OutSuc(c, "success", nil)
  171. return
  172. }
  173. func AuditClear(c *gin.Context) {
  174. var req *md.AuditClearReq
  175. if err := c.ShouldBindJSON(&req); err != nil {
  176. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  177. return
  178. }
  179. db.Db.Where("os=?", req.Os).Cols("is_auditing").Update(&model.CloudBundle{IsAuditing: 0})
  180. e.OutSuc(c, "success", nil)
  181. return
  182. }