From e4c012cf2f83a7aa6f7338c48a84152f21186f27 Mon Sep 17 00:00:00 2001 From: marvzhang Date: Wed, 19 Feb 2020 13:16:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E7=89=88=E6=9C=AC=E6=A3=80?= =?UTF-8?q?=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/entity/version.go | 23 +++ backend/main.go | 3 +- backend/routes/version.go | 20 +++ backend/services/version.go | 29 ++++ frontend/src/App.vue | 30 +--- .../src/views/layout/components/Navbar.vue | 142 +++++++++++++++++- 6 files changed, 218 insertions(+), 29 deletions(-) create mode 100644 backend/entity/version.go create mode 100644 backend/routes/version.go create mode 100644 backend/services/version.go diff --git a/backend/entity/version.go b/backend/entity/version.go new file mode 100644 index 00000000..97a0278d --- /dev/null +++ b/backend/entity/version.go @@ -0,0 +1,23 @@ +package entity + +type Release struct { + Name string `json:"name"` + Draft bool `json:"draft"` + PreRelease bool `json:"pre_release"` + PublishedAt string `json:"published_at"` + Body string `json:"body"` +} + +type ReleaseSlices []Release + +func (r ReleaseSlices) Len() int { + return len(r) +} + +func (r ReleaseSlices) Less(i, j int) bool { + return r[i].PublishedAt < r[j].PublishedAt +} + +func (r ReleaseSlices) Swap(i, j int) { + r[i], r[j] = r[j], r[i] +} diff --git a/backend/main.go b/backend/main.go index 7e9d1369..6c00c797 100644 --- a/backend/main.go +++ b/backend/main.go @@ -133,7 +133,8 @@ func main() { anonymousGroup.PUT("/users", routes.PutUser) // 添加用户 anonymousGroup.GET("/setting", routes.GetSetting) // 获取配置信息 // release版本 - anonymousGroup.GET("/version", routes.GetVersion) // 获取发布的版本 + anonymousGroup.GET("/version", routes.GetVersion) // 获取发布的版本 + anonymousGroup.GET("/releases/latest", routes.GetLatestRelease) // 获取最近发布的版本 } authGroup := app.Group("/", middlewares.AuthorizationMiddleware()) { diff --git a/backend/routes/version.go b/backend/routes/version.go new file mode 100644 index 00000000..ec3b80c7 --- /dev/null +++ b/backend/routes/version.go @@ -0,0 +1,20 @@ +package routes + +import ( + "crawlab/services" + "github.com/gin-gonic/gin" + "net/http" +) + +func GetLatestRelease(c *gin.Context) { + latestRelease, err := services.GetLatestRelease() + if err != nil { + HandleError(http.StatusInternalServerError, c, err) + return + } + c.JSON(http.StatusOK, Response{ + Status: "ok", + Message: "success", + Data: latestRelease, + }) +} diff --git a/backend/services/version.go b/backend/services/version.go new file mode 100644 index 00000000..34df7b22 --- /dev/null +++ b/backend/services/version.go @@ -0,0 +1,29 @@ +package services + +import ( + "crawlab/entity" + "github.com/apex/log" + "github.com/imroc/req" + "runtime/debug" + "sort" +) + +func GetLatestRelease() (release entity.Release, err error) { + res, err := req.Get("https://api.github.com/repos/crawlab-team/crawlab/releases") + if err != nil { + log.Errorf(err.Error()) + debug.PrintStack() + return release, err + } + + var releaseDataList entity.ReleaseSlices + if err := res.ToJSON(&releaseDataList); err != nil { + log.Errorf(err.Error()) + debug.PrintStack() + return release, err + } + + sort.Sort(releaseDataList) + + return releaseDataList[len(releaseDataList)-1], nil +} diff --git a/frontend/src/App.vue b/frontend/src/App.vue index ec60d28b..d66691c4 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -35,33 +35,6 @@ export default { }, methods: {}, async mounted () { - // window.setUseStats = (value) => { - // document.querySelector('.el-message__closeBtn').click() - // if (value === 1) { - // this.$st.sendPv('/allow_stats') - // this.$st.sendEv('全局', '允许/禁止统计', '允许') - // } else { - // this.$st.sendPv('/disallow_stats') - // this.$st.sendEv('全局', '允许/禁止统计', '禁止') - // } - // localStorage.setItem('useStats', value) - // } - - // first-time user - // if (this.useStats === undefined || this.useStats === null) { - // this.$message({ - // type: 'info', - // dangerouslyUseHTMLString: true, - // showClose: true, - // duration: 0, - // message: '

' + this.$t('Do you allow us to collect some statistics to improve Crawlab?') + '

' + - // '
' + - // '' + - // '' + - // '
' - // }) - // } - // set uid if first visit if (this.uid === undefined || this.uid === null) { localStorage.setItem('uid', this.$utils.encrypt.UUID()) @@ -71,6 +44,9 @@ export default { if (this.sid === undefined || this.sid === null) { sessionStorage.setItem('sid', this.$utils.encrypt.UUID()) } + + // get latest version + await this.$store.dispatch('version/getLatestRelease') } } diff --git a/frontend/src/views/layout/components/Navbar.vue b/frontend/src/views/layout/components/Navbar.vue index 10c98427..72555bd7 100644 --- a/frontend/src/views/layout/components/Navbar.vue +++ b/frontend/src/views/layout/components/Navbar.vue @@ -1,5 +1,27 @@