Skip to content

Commit

Permalink
feat: PC客户端支持切换ip [实验性]
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jun 25, 2023
1 parent 87c2c52 commit de35c96
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 4 deletions.
3 changes: 3 additions & 0 deletions locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,7 @@ export default {
"share.link.expires.error":
"Please enter a number that must be legal and no older than 7 days! For perpetual effect, enter 0",
"go.home": "Go home",
"form.select": "Select",
"form.nodata": "No data",
"change.ip.title": "Change IP",
}
3 changes: 3 additions & 0 deletions locales/zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,7 @@ export default {
"blog.index.home.exists": "对不起,该页面已设置为主页,请先移除主页再取消分享",
"share.link.expires.error": "请输入必须为一个合法且不大于 7 天的数字!如需永久生效,请输入0",
"go.home": "打开主页",
"form.select": "请选择",
"form.nodata": "暂无数据",
"change.ip.title": "切换IP",
}
2 changes: 1 addition & 1 deletion nuxt.node.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export default defineNuxtConfig({
siyuanCookie: process.env.NUXT_SIYUAN_COOKIE,
public: {
defaultType: process.env.NUXT_PUBLIC_DEFAULT_TYPE ?? "siyuan",
siyuanApiUrl: process.env.NUXT_PUBLIC_SIYUAN_API_URL ?? "http://127.0.0.1:6808",
siyuanApiUrl: process.env.NUXT_PUBLIC_SIYUAN_API_URL ?? "http://127.0.0.1:6806",
waitTime: process.env.NUXT_PUBLIC_WAIT_TIME,
},
},
Expand Down
33 changes: 33 additions & 0 deletions pages/share.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { JsonUtil, StrUtil } from "zhi-common"
import { useMethodAsync } from "~/composables/useMethodAsync"
import { useMethod } from "~/composables/useMethod"
import { sendMessageToParent } from "~/utils/innerIframeEvent"
import { getAllIps } from "~/utils/urlUtil"
const logger = createAppLogger("share-page")
const { t } = useI18n()
Expand Down Expand Up @@ -40,6 +41,12 @@ const seoMeta = {
} as any
useSeoMeta(seoMeta)
// 初始化 ip
const url = new URL(origin.value)
const hostname = url.hostname
const ips = getAllIps()
ips.push(hostname)
// datas
const attrs = JsonUtil.safeParse<any>(post?.attrs ?? "{}", {})
const formData = reactive({
Expand All @@ -48,6 +55,10 @@ const formData = reactive({
optionEnabled: false,
expiredTime: attrs["custom-expires"] ?? "0",
isHome: setting.homePageId === id.value,
ip: hostname,
ipList: ips.map((ip: string) => {
return { value: ip, label: ip }
}),
})
const { optionState, optionToggle } = useShareOptionToggle(formData.optionEnabled)
Expand Down Expand Up @@ -135,6 +146,12 @@ const handleExpiresTime = async () => {
}
)
}
const handleIpChange = (val: string) => {
const url = new URL(formData.shareLink)
url.hostname = val
formData.shareLink = url.toString()
}
</script>

<template>
Expand Down Expand Up @@ -195,6 +212,20 @@ const handleExpiresTime = async () => {
<div class="item-right"></div>
</div>

<div v-if="optionState" class="share-item">
<div class="item-left">
<span class="change-ip-title">{{ t("change.ip.title") }}</span>
<el-select
v-model="formData.ip"
class="m-2"
:placeholder="t('form.select')"
no-data-text="t('form.nodata')"
@change="handleIpChange"
>
<el-option v-for="item in formData.ipList" :key="item.value" :label="item.label" :value="item.value" />
</el-select>
</div>
</div>
<div v-if="optionState" class="share-item expires-link-item">
<div class="expires-link expires-link-label">
{{ t("share.other.option.link.expires") }}
Expand Down Expand Up @@ -302,4 +333,6 @@ const handleExpiresTime = async () => {
.el-page-header__content
::v-deep(.el-switch)
display inline-flex
.change-ip-title
margin-right 10px
</style>
51 changes: 48 additions & 3 deletions utils/urlUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,64 @@
* questions.
*/

import { SiyuanDevice } from "zhi-device"
import { DeviceDetection, DeviceTypeEnum, SiyuanDevice } from "zhi-device"

const getIPv4List = () => {
const win = SiyuanDevice.siyuanWindow()
const os = win.require("os")
const interfaces = os.networkInterfaces()
const addresses = []

for (const k in interfaces) {
for (const k2 in interfaces[k]) {
const address = interfaces[k][k2]
if (address.family === "IPv4" && !address.internal) {
addresses.push(address.address)
}
}
}

if (addresses.length === 0) {
return "127.0.0.1"
}

return addresses
}

const getAvailableIP = (ips: string[]): string | null => {
const localIPs = ips.filter((ip) => ip !== "127.0.0.1")
return localIPs.length > 0 ? localIPs[0] : null
}

const getLocalIp = () => {
const win = SiyuanDevice.siyuanWindow()
const ips = win.siyuan.config.localIPs
const ips = getAllIps()
return getAvailableIP(ips)
}

export const getAllIps = () => {
if (typeof window === "undefined") {
return []
}
const win = window as any
if (typeof win.siyuan === "undefined" || typeof win.parent.siyuan === "undefined") {
return []
}
const syWin = SiyuanDevice.siyuanWindow()
const ips = syWin.siyuan.config.localIPs

const deviceType = DeviceDetection.getDevice()
if (
deviceType === DeviceTypeEnum.DeviceType_Siyuan_MainWin ||
deviceType === DeviceTypeEnum.DeviceType_Siyuan_Widget
) {
const ipv4s = getIPv4List()
// 将ipv4s中的所有元素添加到ips数组中
ips.push(...ipv4s)
}

return ips
}

export const getAvailableOrigin = () => {
const win = SiyuanDevice.siyuanWindow()
const origin = win.location.origin
Expand Down

0 comments on commit de35c96

Please sign in to comment.