mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-11-06 06:40:27 +08:00
增加在线检查最新版本功能
This commit is contained in:
@@ -18,4 +18,5 @@ const (
|
|||||||
CookieSID = "edgesid"
|
CookieSID = "edgesid"
|
||||||
|
|
||||||
SystemdServiceName = "edge-admin"
|
SystemdServiceName = "edge-admin"
|
||||||
|
UpdatesURL = "https://goedge.cn/api/boot/versions?os=${os}&arch=${arch}"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ func (this *Helper) BeforeAction(actionPtr actions.ActionWrapper) (goNext bool)
|
|||||||
}
|
}
|
||||||
tabbar.Add("安全设置", "", "/settings/security", "", this.tab == "security")
|
tabbar.Add("安全设置", "", "/settings/security", "", this.tab == "security")
|
||||||
tabbar.Add("IP库", "", "/settings/ip-library", "", this.tab == "ipLibrary")
|
tabbar.Add("IP库", "", "/settings/ip-library", "", this.tab == "ipLibrary")
|
||||||
|
tabbar.Add("检查更新", "", "/settings/updates", "", this.tab == "updates")
|
||||||
}
|
}
|
||||||
tabbar.Add("个人资料", "", "/settings/profile", "", this.tab == "profile")
|
tabbar.Add("个人资料", "", "/settings/profile", "", this.tab == "profile")
|
||||||
tabbar.Add("登录设置", "", "/settings/login", "", this.tab == "login")
|
tabbar.Add("登录设置", "", "/settings/login", "", this.tab == "login")
|
||||||
|
|||||||
115
internal/web/actions/default/settings/updates/index.go
Normal file
115
internal/web/actions/default/settings/updates/index.go
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||||
|
|
||||||
|
package updates
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
|
||||||
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
|
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type IndexAction struct {
|
||||||
|
actionutils.ParentAction
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *IndexAction) Init() {
|
||||||
|
this.Nav("", "updates", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *IndexAction) RunGet(params struct{}) {
|
||||||
|
this.Data["version"] = teaconst.Version
|
||||||
|
|
||||||
|
this.Show()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *IndexAction) RunPost(params struct {
|
||||||
|
}) {
|
||||||
|
type Response struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Data interface{} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var apiURL = teaconst.UpdatesURL
|
||||||
|
apiURL = strings.ReplaceAll(apiURL, "${os}", "linux") //runtime.GOOS)
|
||||||
|
apiURL = strings.ReplaceAll(apiURL, "${arch}", "amd64") // runtime.GOARCH)
|
||||||
|
resp, err := http.Get(apiURL)
|
||||||
|
if err != nil {
|
||||||
|
this.Data["result"] = maps.Map{
|
||||||
|
"isOk": false,
|
||||||
|
"message": "读取更新信息失败:" + err.Error(),
|
||||||
|
}
|
||||||
|
this.Success()
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
_ = resp.Body.Close()
|
||||||
|
}()
|
||||||
|
data, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
this.Data["result"] = maps.Map{
|
||||||
|
"isOk": false,
|
||||||
|
"message": "读取更新信息失败:" + err.Error(),
|
||||||
|
}
|
||||||
|
this.Success()
|
||||||
|
}
|
||||||
|
|
||||||
|
var apiResponse = &Response{}
|
||||||
|
err = json.Unmarshal(data, apiResponse)
|
||||||
|
if err != nil {
|
||||||
|
this.Data["result"] = maps.Map{
|
||||||
|
"isOk": false,
|
||||||
|
"message": "解析更新信息失败:" + err.Error(),
|
||||||
|
}
|
||||||
|
this.Success()
|
||||||
|
}
|
||||||
|
|
||||||
|
if apiResponse.Code != 200 {
|
||||||
|
this.Data["result"] = maps.Map{
|
||||||
|
"isOk": false,
|
||||||
|
"message": "解析更新信息失败:" + apiResponse.Message,
|
||||||
|
}
|
||||||
|
this.Success()
|
||||||
|
}
|
||||||
|
|
||||||
|
var m = maps.NewMap(apiResponse.Data)
|
||||||
|
var dlHost = m.GetString("host")
|
||||||
|
var versions = m.GetSlice("versions")
|
||||||
|
if len(versions) > 0 {
|
||||||
|
for _, version := range versions {
|
||||||
|
var vMap = maps.NewMap(version)
|
||||||
|
if vMap.GetString("code") == "admin" {
|
||||||
|
var latestVersion = vMap.GetString("version")
|
||||||
|
if stringutil.VersionCompare(teaconst.Version, latestVersion) < 0 {
|
||||||
|
this.Data["result"] = maps.Map{
|
||||||
|
"isOk": true,
|
||||||
|
"message": "有最新的版本v" + types.String(latestVersion) + "可以更新",
|
||||||
|
"hasNew": true,
|
||||||
|
"dlURL": dlHost + vMap.GetString("url"),
|
||||||
|
}
|
||||||
|
this.Success()
|
||||||
|
} else {
|
||||||
|
this.Data["result"] = maps.Map{
|
||||||
|
"isOk": true,
|
||||||
|
"message": "你安装已经是最新版本,无需更新",
|
||||||
|
}
|
||||||
|
this.Success()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.Data["result"] = maps.Map{
|
||||||
|
"isOk": false,
|
||||||
|
"message": "找不到更新信息",
|
||||||
|
}
|
||||||
|
this.Success()
|
||||||
|
|
||||||
|
this.Success()
|
||||||
|
}
|
||||||
19
internal/web/actions/default/settings/updates/init.go
Normal file
19
internal/web/actions/default/settings/updates/init.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package updates
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
|
||||||
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/settingutils"
|
||||||
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
|
||||||
|
"github.com/iwind/TeaGo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
TeaGo.BeforeStart(func(server *TeaGo.Server) {
|
||||||
|
server.
|
||||||
|
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeSetting)).
|
||||||
|
Helper(settingutils.NewHelper("updates")).
|
||||||
|
Prefix("/settings/updates").
|
||||||
|
GetPost("", new(IndexAction)).
|
||||||
|
EndAll()
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -118,6 +118,7 @@ import (
|
|||||||
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/server"
|
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/server"
|
||||||
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/transfer"
|
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/transfer"
|
||||||
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/ui"
|
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/ui"
|
||||||
|
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/updates"
|
||||||
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/upgrade"
|
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/upgrade"
|
||||||
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/user-ui"
|
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/user-ui"
|
||||||
|
|
||||||
|
|||||||
25
web/views/@default/settings/updates/index.html
Normal file
25
web/views/@default/settings/updates/index.html
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
{$layout}
|
||||||
|
|
||||||
|
<div class="ui margin"></div>
|
||||||
|
<form class="ui form">
|
||||||
|
<table class="ui table definition selectable">
|
||||||
|
<tr>
|
||||||
|
<td class="title">当前已安装版本</td>
|
||||||
|
<td>v{{version}}</td>
|
||||||
|
</tr>
|
||||||
|
<tr v-if="isStarted">
|
||||||
|
<td>最新版本</td>
|
||||||
|
<td>
|
||||||
|
<div v-if="isChecking">
|
||||||
|
<span class="blue">正在连接服务器检查更新...</span>
|
||||||
|
</div>
|
||||||
|
<div v-if="!isChecking">
|
||||||
|
<span class="green" v-if="result.isOk">{{result.message}}<span v-if="result.hasNew">:<br/><a :href="result.dlURL">下载地址:{{result.dlURL}}</a> </span></span>
|
||||||
|
<span class="red" v-if="!result.isOk">{{result.message}}</span>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<button class="ui button primary" type="button" @click.prevent="start">开始检查</button>
|
||||||
|
</form>
|
||||||
24
web/views/@default/settings/updates/index.js
Normal file
24
web/views/@default/settings/updates/index.js
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
Tea.context(function () {
|
||||||
|
this.isStarted = false
|
||||||
|
this.isChecking = true
|
||||||
|
this.result = {isOk: false, message: "", hasNew: false, dlURL: ""}
|
||||||
|
|
||||||
|
this.start = function () {
|
||||||
|
this.isStarted = true
|
||||||
|
this.isChecking = true
|
||||||
|
|
||||||
|
this.$delay(function () {
|
||||||
|
this.check()
|
||||||
|
}, 1000)
|
||||||
|
}
|
||||||
|
|
||||||
|
this.check = function () {
|
||||||
|
this.$post("$")
|
||||||
|
.success(function (resp) {
|
||||||
|
this.result = resp.data.result
|
||||||
|
})
|
||||||
|
.done(function () {
|
||||||
|
this.isChecking = false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user