实现URL跳转功能

This commit is contained in:
刘祥超
2021-01-10 17:34:30 +08:00
parent 0940fce743
commit e70ba457b2
12 changed files with 293 additions and 9 deletions

View File

@@ -0,0 +1,79 @@
package redirects
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
"net/url"
)
type CreatePopupAction struct {
actionutils.ParentAction
}
func (this *CreatePopupAction) Init() {
this.Nav("", "", "")
}
func (this *CreatePopupAction) RunGet(params struct {
}) {
this.Data["statusList"] = serverconfigs.AllHTTPRedirectStatusList()
this.Show()
}
func (this *CreatePopupAction) RunPost(params struct {
BeforeURL string
AfterURL string
Status int
Must *actions.Must
CSRF *actionutils.CSRF
}) {
params.Must.
Field("beforeURL", params.BeforeURL).
Require("请填写跳转前的URL")
// 校验格式
{
u, err := url.Parse(params.BeforeURL)
if err != nil {
this.FailField("beforeURL", "请输入正确的跳转前URL")
}
if (u.Scheme != "http" && u.Scheme != "https") ||
len(u.Host) == 0 {
this.FailField("beforeURL", "请输入正确的跳转前URL")
}
}
params.Must.
Field("afterURL", params.AfterURL).
Require("请填写跳转后URL")
// 校验格式
{
u, err := url.Parse(params.AfterURL)
if err != nil {
this.FailField("afterURL", "请输入正确的跳转后URL")
}
if (u.Scheme != "http" && u.Scheme != "https") ||
len(u.Host) == 0 {
this.FailField("afterURL", "请输入正确的跳转后URL")
}
}
params.Must.
Field("status", params.Status).
Gte(0, "请选择正确的跳转状态码")
this.Data["redirect"] = maps.Map{
"status": params.Status,
"beforeURL": params.BeforeURL,
"afterURL": params.AfterURL,
"isOn": true,
}
this.Success()
}

View File

@@ -0,0 +1,54 @@
package redirects
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "setting", "index")
this.SecondMenu("redirects")
}
func (this *IndexAction) RunGet(params struct {
ServerId int64
}) {
webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithServerId(this.AdminContext(), params.ServerId)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["webId"] = webConfig.Id
this.Data["redirects"] = webConfig.HostRedirects
this.Show()
}
func (this *IndexAction) RunPost(params struct {
ServerId int64
WebId int64
HostRedirectsJSON []byte
Must *actions.Must
}) {
defer this.CreateLogInfo("修改Web %d 的URL跳转设置", params.WebId)
_, err := this.RPC().HTTPWebRPC().UpdateHTTPWebHostRedirects(this.AdminContext(), &pb.UpdateHTTPWebHostRedirectsRequest{
WebId: params.WebId,
HostRedirectsJSON: params.HostRedirectsJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,20 @@
package redirects
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeServer)).
Helper(serverutils.NewServerHelper()).
Prefix("/servers/server/settings/redirects").
GetPost("", new(IndexAction)).
GetPost("/createPopup", new(CreatePopupAction)).
EndAll()
})
}

View File

@@ -204,6 +204,12 @@ func (this *ServerHelper) createSettingsMenu(secondMenuItem string, serverIdStri
"url": "",
"isActive": false,
})
menuItems = append(menuItems, maps.Map{
"name": "URL跳转",
"url": "/servers/server/settings/redirects?serverId=" + serverIdString,
"isActive": secondMenuItem == "redirects",
"isOn": serverConfig.Web != nil && len(serverConfig.Web.HostRedirects) > 0,
})
menuItems = append(menuItems, maps.Map{
"name": "路径规则",
"url": "/servers/server/settings/locations?serverId=" + serverIdString,

View File

@@ -63,6 +63,7 @@ import (
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/locations/websocket"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/origins"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/pages"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/redirects"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/reverseProxy"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/rewrite"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/serverNames"

10
web/public/js/axios.min.js vendored Executable file → Normal file

File diff suppressed because one or more lines are too long

2
web/public/js/axios.min.map Executable file → Normal file

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,61 @@
Vue.component("http-host-redirect-box", {
props: ["v-redirects"],
data: function () {
let redirects = this.vRedirects
if (redirects == null) {
redirects = []
}
return {
redirects: redirects,
statusOptions: [
{"code": 301, "text": "Moved Permanently"},
{"code": 308, "text": "Permanent Redirect"},
{"code": 302, "text": "Found"},
{"code": 303, "text": "See Other"},
{"code": 307, "text": "Temporary Redirect"}
]
}
},
methods: {
add: function () {
let that = this
window.UPDATING_REDIRECT = null
teaweb.popup("/servers/server/settings/redirects/createPopup", {
height: "22em",
callback: function (resp) {
that.redirects.push(resp.data.redirect)
}
})
},
update: function (index, redirect) {
let that = this
window.UPDATING_REDIRECT = redirect
teaweb.popup("/servers/server/settings/redirects/createPopup", {
height: "22em",
callback: function (resp) {
Vue.set(that.redirects, index, resp.data.redirect)
}
})
},
remove: function (index) {
this.redirects.$remove(index)
}
},
template: `<div>
<input type="hidden" name="hostRedirectsJSON" :value="JSON.stringify(redirects)"/>
<!-- TODO 将来支持排序并支持isOn切换 -->
<div v-if="redirects.length > 0">
<div v-for="(redirect, index) in redirects" class="ui label basic small" style="margin-bottom: 0.5em;margin-top: 0.5em">
<span v-if="redirect.status > 0">[{{redirect.status}}]</span> {{redirect.beforeURL}} -&gt; {{redirect.afterURL}} <a href="" @click.prevent="update(index, redirect)" title="修改"><i class="icon pencil small"></i></a> &nbsp; <a href="" @click.prevent="remove(index)" title="删除"><i class="icon remove"></i></a>
</div>
<div class="ui divider"></div>
</div>
<div>
<button type="button" class="ui button tiny" @click.prevent="add">+</button>
</div>
</div>`
})

View File

@@ -0,0 +1,36 @@
{$layout "layout_popup"}
<h3 v-if="isCreating">添加URL跳转</h3>
<h3 v-if="!isCreating">修改URL跳转</h3>
<form class="ui form" data-tea-success="success" data-tea-action="$">
<csrf-token></csrf-token>
<table class="ui table definition selectable">
<tr>
<td>跳转前URL *</td>
<td>
<input type="text" name="beforeURL" placeholder="比如 http://www.url1.com" v-model="redirect.beforeURL" ref="focus"/>
<p class="comment">需要填写完整的URL包括<code-label>http://</code-label>或者<code-label>https://</code-label>,如果有非默认端口,也需要带上端口号。</p>
</td>
</tr>
<tr>
<td>跳转后URL *</td>
<td>
<input type="text" name="afterURL" placeholder="比如 https://www.url2.cn" v-model="redirect.afterURL"/>
<p class="comment">需要填写完整的URL包括<code-label>http://</code-label>或者<code-label>https://</code-label>,如果有非默认端口,也需要带上端口号。</p>
</td>
</tr>
<tr>
<td class="title">跳转状态码</td>
<td>
<select class="ui dropdown auto-width" name="status" v-model="redirect.status">
<option value="0">[默认]</option>
<option v-for="status in statusList" :value="status.code">{{status.code}} - {{status.text}}</option>
</select>
</td>
</tr>
</table>
<submit-btn></submit-btn>
</form>

View File

@@ -0,0 +1,13 @@
Tea.context(function () {
this.isCreating = true
if (window.parent.UPDATING_REDIRECT != null) {
this.isCreating = false
this.redirect = window.parent.UPDATING_REDIRECT
} else {
this.redirect = {
status: 0,
beforeURL: "",
afterURL: ""
}
}
})

View File

@@ -0,0 +1,17 @@
{$layout}
{$template "/left_menu"}
<div class="right-box">
<form class="ui form" method="post" data-tea-action="$" data-tea-success="success">
<input type="hidden" name="webId" :value="webId"/>
<table class="ui table selectable definition">
<tr>
<td class="title">URL跳转设置</td>
<td>
<http-host-redirect-box :v-redirects="redirects"></http-host-redirect-box>
</td>
</tr>
</table>
<submit-btn></submit-btn>
</form>
</div>

View File

@@ -0,0 +1,3 @@
Tea.context(function () {
this.success = NotifyReloadSuccess("保存成功")
})