Skip to content

Commit

Permalink
Merge pull request #2 from nowscott/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
nowscott authored Jun 30, 2024
2 parents 8518a23 + 24ed368 commit 3ed28f2
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 5 deletions.
140 changes: 138 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "IndWebIndex",
"version": "2.0.1",
"version": "2.1.0",
"scripts": {
"dev": "next dev",
"build": "next build",
Expand All @@ -9,6 +9,7 @@
"dependencies": {
"@notionhq/client": "^0.4.11",
"@vercel/analytics": "^1.3.1",
"mongodb": "^6.8.0",
"next": "^14.2.4",
"react": "^18.3.1",
"react-dom": "^18.3.1"
Expand Down
2 changes: 0 additions & 2 deletions pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import { Analytics } from '@vercel/analytics/react';
import '../public/css/style.css';
import '../public/css/context-menu.css';
import '../public/css/daytime.css';
import '../public/css/dark.css';

function MyApp({ Component, pageProps }) {
return (
Expand Down
1 change: 1 addition & 0 deletions pages/_document.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class MyDocument extends Document {
return (
<Html>
<Head>
<link id="darkcss" rel="stylesheet" href="/css/daytime.css" />
<link rel="shortcut icon" href="/images/favicon.ico" />
</Head>
<body>
Expand Down
44 changes: 44 additions & 0 deletions pages/api/visit-count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { MongoClient } from 'mongodb';

const uri = process.env.MONGODB_URI;
const client = new MongoClient(uri);

export default async function handler(req, res) {
if (req.method === 'GET') {
try {
await client.connect();
const database = client.db('indwebindex-count');
const collection = database.collection('visits');

console.log("Connected to database");

// 查找文档
let visit = await collection.findOne({ _id: 'visit_count' });

if (!visit) {
console.log("No document found, inserting initial document");
await collection.insertOne({ _id: 'visit_count', count: 1 });
visit = { count: 1 };
} else {
// 更新计数
visit = await collection.findOneAndUpdate(
{ _id: 'visit_count' },
{ $inc: { count: 1 } },
{ returnDocument: 'after' }
);
}

console.log('API response data:', visit);
res.status(200).json({ count: visit.value ? visit.value.count : visit.count });
} catch (error) {
console.error('读取或写入计数数据失败:', error.message);
res.status(200).json({ message: '未能读取或写入计数数据,但这不是致命错误。', error: error.message });
} finally {
await client.close();
console.log("Database connection closed");
}
} else {
res.setHeader('Allow', ['GET']);
res.status(405).end(`方法 ${req.method} 不被允许`);
}
}
17 changes: 17 additions & 0 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,29 @@ export default function Home({ initialPosts, lastFetched }) {
const [tags, setTags] = useState([]);
const [onList, setOnList] = useState([]);
const [filteredPosts, setFilteredPosts] = useState(initialPosts);
const [visitCount, setVisitCount] = useState(null); // 新增的状态用于存储访问次数

useEffect(() => {
initializeTheme();
initializeContextMenu();
console.log(`数据更新时间: ${new Date(lastFetched).toLocaleString()}`);
setPosts(initialPosts);

// 调用访问计数 API
fetch('/api/visit-count')
.then(response => response.json())
.then(data => {
if (data.count) {
console.log(`本页面已被访问 ${data.count} 次`);
setVisitCount(data.count);
} else {
console.log('访问计数数据不可用:', data.message);
}
})
.catch(error => {
console.error('Error fetching visit count:', error);
});

}, [initialPosts, lastFetched]);

useEffect(() => {
Expand Down

0 comments on commit 3ed28f2

Please sign in to comment.