Commit 03c05558 authored by Allen Bai's avatar Allen Bai

feat: 字符串调整

parent ee97b824
/** @format */ /** @format */
type Optional<T> = { [P in keyof T]?: T[P] } type Optional<T> = { [P in keyof T]?: T[P] }
function request(option) {
if (String(option) !== '[object Object]') return undefined
option.method = option.method ? option.method.toUpperCase() : 'GET'
option.data = option.data || {}
var formData = []
for (var key in option.data) {
formData.push(''.concat(key, '=', option.data[key]))
}
option.data = formData.join('&')
if (option.method === 'GET') {
option.url += location.search.length === 0 ? ''.concat('?', option.data) : ''.concat('&', option.data)
}
var xhr = new XMLHttpRequest()
xhr.responseType = option.responseType || 'json'
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
if (option.success && typeof option.success === 'function') {
option.success(xhr.response)
}
} else {
if (option.error && typeof option.error === 'function') {
option.error()
}
}
}
}
xhr.open(option.method, option.url, true)
if (option.method === 'POST') {
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
}
xhr.send(option.method === 'POST' ? option.data : null)
}
function getIPCByDomain(domain: string) { function getIPCByDomain(domain: string) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
resolve('1231232123') request({
url: `http://activity.tuiatest.cn/land/getIcpInfo?domain=${encodeURIComponent(domain)}`,
method: 'GET',
success(res) {
console.log('当前域名的IPC备案为', res)
resolve(res)
},
error(err) {
reject(err)
}
})
}) })
} }
...@@ -44,7 +90,7 @@ const defaultBoardStyles: Optional<CSSStyleDeclaration> = { ...@@ -44,7 +90,7 @@ const defaultBoardStyles: Optional<CSSStyleDeclaration> = {
} }
async function mountIPCFooter(opts: IPCFooterOptions = {}) { async function mountIPCFooter(opts: IPCFooterOptions = {}) {
const icpNumber = await getIPCByDomain(opts.domain || location.host) const IPCNumberString = await getIPCByDomain(opts.domain || location.host)
const IPCFooter = document.createElement('div') const IPCFooter = document.createElement('div')
const IPCBoard = document.createElement('a') const IPCBoard = document.createElement('a')
const targetDom = opts.dom || document.querySelector('body') const targetDom = opts.dom || document.querySelector('body')
...@@ -52,7 +98,7 @@ async function mountIPCFooter(opts: IPCFooterOptions = {}) { ...@@ -52,7 +98,7 @@ async function mountIPCFooter(opts: IPCFooterOptions = {}) {
IPCBoard.id = '__IPC_board__' IPCBoard.id = '__IPC_board__'
addStyles(IPCFooter, { ...defaultFooterStyles, ...(opts.footerStyles || {}) }) addStyles(IPCFooter, { ...defaultFooterStyles, ...(opts.footerStyles || {}) })
addStyles(IPCBoard, { ...defaultBoardStyles, ...(opts.footerStyles || {}) }) addStyles(IPCBoard, { ...defaultBoardStyles, ...(opts.footerStyles || {}) })
IPCBoard.innerText = `浙ICP备${icpNumber}号` IPCBoard.innerText = (IPCNumberString as string) || ''
IPCFooter.appendChild(IPCBoard) IPCFooter.appendChild(IPCBoard)
targetDom.appendChild(IPCFooter) targetDom.appendChild(IPCFooter)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment