Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Sep 21, 2024
2 parents 2cb7f16 + ca58e15 commit bd92370
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
40 changes: 40 additions & 0 deletions components/Coze.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { siteConfig } from '@/lib/config'
import { loadExternalResource } from '@/lib/utils'
import { useEffect } from 'react'

/**
* Coze-AI机器人
* @returns
*/
export default function Coze() {
const cozeSrc = siteConfig(
'COZE_SRC_URL',
'https://lf-cdn.coze.cn/obj/unpkg/flow-platform/chat-app-sdk/0.1.0-beta.6/libs/cn/index.js'
)
const title = siteConfig('COZE_TITLE', 'NotionNext助手')
const botId = siteConfig('COZE_BOT_ID')

const loadCoze = async () => {
await loadExternalResource(cozeSrc)
const CozeWebSDK = window?.CozeWebSDK
if (CozeWebSDK) {
const cozeClient = new CozeWebSDK.WebChatClient({
config: {
bot_id: botId
},
componentProps: {
title: title
}
})
console.log('coze', cozeClient)
}
}

useEffect(() => {
if (!botId) {
return
}
loadCoze()
}, [])
return <></>
}
3 changes: 3 additions & 0 deletions components/ExternalPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { convertInnerUrl } from '@/lib/notion/convertInnerUrl'
import { isBrowser, loadExternalResource } from '@/lib/utils'
import { useRouter } from 'next/router'
import { useEffect } from 'react'
import Coze from './Coze'
import { initGoogleAdsense } from './GoogleAdsense'

/**
Expand Down Expand Up @@ -65,6 +66,7 @@ const ExternalPlugin = props => {
const CUSTOM_EXTERNAL_JS = siteConfig('CUSTOM_EXTERNAL_JS')
// 默认关闭NProgress
const ENABLE_NPROGRSS = siteConfig('ENABLE_NPROGRSS', false)
const COZE_BOT_ID = siteConfig('COZE_BOT_ID')

// 自定义样式css和js引入
if (isBrowser) {
Expand Down Expand Up @@ -150,6 +152,7 @@ const ExternalPlugin = props => {
{ENABLE_NPROGRSS && <LoadingProgress />}
<AosAnimation />
{ANALYTICS_51LA_ID && ANALYTICS_51LA_CK && <LA51 />}
{COZE_BOT_ID && <Coze />}

{ANALYTICS_51LA_ID && ANALYTICS_51LA_CK && (
<>
Expand Down
37 changes: 17 additions & 20 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,50 +102,47 @@ export const siteConfig = (key, defaultVal = null, extendConfig = {}) => {
* @returns
*/
export const convertVal = val => {
// 如果传入参数本身就是obj、数组、boolean 就无需处理
// 如果传入参数本身就是 obj、数组、boolean就无需处理
if (typeof val !== 'string' || !val) {
return val
}

// 解析数字,parseInt将字符串转换为数字
// 检测是否数字并避免数值溢出
if (/^\d+$/.test(val)) {
return parseInt(val)
const parsedNum = Number(val)
// 如果数值大于 JavaScript 最大安全整数,则作为字符串返回
if (parsedNum > Number.MAX_SAFE_INTEGER) {
return val + ''
}
return parsedNum
}

// 检测是否url
if (isUrl(val)) {
return val
}
// 检测是否url
// 检测是否为布尔值
if (val === 'true' || val === 'false') {
return JSON.parse(val)
}

// 检测是否为 URL
if (isUrl(val)) {
return val
}

// 配置值前可能有污染的空格
if (val.indexOf('[') < 0 && val.indexOf('{') < 0) {
return val
}

// 转换 [] , {} , true/false 这类字符串为对象
// 转换 [] , {} 这类字符串为对象
try {
// 尝试解析json
const parsedJson = JSON.parse(val)
if (parsedJson !== null) {
return parsedJson
}
} catch (error) {
// try {
// // 尝试解析对象,对象解析能力不如上一步的json
// const evalObj = eval('(' + val + ')')
// if (evalObj !== null) {
// return evalObj
// }
// } catch (error) {
// // Ojbject 解析失败,返回原始字符串值
// return val
// }
// 解析失败,返回原始字符串
return val
}

return val
}

Expand Down

0 comments on commit bd92370

Please sign in to comment.