Browse Source

move the easter_egg function to backend

master
Collapseyu 11 months ago
parent
commit
f5c909b1c3
  1. 2
      pages/admin/variables.tsx
  2. 67
      pages/index.tsx
  3. 6
      types/typings.d.ts
  4. 5
      utils/api.ts
  5. 2
      utils/global.ts

2
pages/admin/variables.tsx

@ -66,7 +66,7 @@ class AdminVariables extends React.Component<AdminVariablesProps, AdminVariables @@ -66,7 +66,7 @@ class AdminVariables extends React.Component<AdminVariablesProps, AdminVariables
vmess: '',
zhihu: '',
server_chan: '',
easter_egg: '',
// easter_egg: '',
},
};
}

67
pages/index.tsx

@ -6,18 +6,19 @@ import Link from 'next/link'; @@ -6,18 +6,19 @@ import Link from 'next/link';
import { Flex } from '@/components/container';
import PostList from '@/components/post_list';
import { Left } from '@/components/svg';
import { Left, Success } from '@/components/svg';
import PostSearch from '@/components/post_search';
import Card from '@/components/card';
import Button from '@/components/button';
import { EasterEgg } from '@/components/svg';
import { Context } from '@/utils/global';
import { indexPosts, tagsSearch } from '@/utils/api';
import { indexPosts, postEgg, tagsSearch } from '@/utils/api';
import { waitUntil } from '@/utils/debounce';
import styles from './index.module.scss';
import Notification from '@/components/notification';
import internal from 'stream';
interface IndexProps extends React.ComponentProps<'base'> {
posts: Blotter.PostCard[];
@ -38,6 +39,8 @@ interface IndexState { @@ -38,6 +39,8 @@ interface IndexState {
show: boolean;
search_height: number;
easter_egg: string;
easter_length: number;
easter_success:boolean;
}
class Index extends React.Component<IndexProps, IndexState> {
@ -69,6 +72,8 @@ class Index extends React.Component<IndexProps, IndexState> { @@ -69,6 +72,8 @@ class Index extends React.Component<IndexProps, IndexState> {
show: false,
search_height: 100,
easter_egg: '',
easter_length:1,
easter_success: false,
};
}
@ -79,45 +84,43 @@ class Index extends React.Component<IndexProps, IndexState> { @@ -79,45 +84,43 @@ class Index extends React.Component<IndexProps, IndexState> {
componentWillUnmount() {
this.easterEggDestory();
}
easterEggPost = (word:string) => {
postEgg(word).then(res => {
this.setState({easter_success:res.success,easter_egg: res.url, easter_length: res.length});
if (this.state.easter_success) {
Notification.message({
alertType: 'info',
title: '恭喜你,触发了一个彩蛋!',
content: '赶快去看一下彩蛋是什么吧',
});
}
console.log(this.state)
});
}
easterEggInit = () => {
const { easter_egg } = this.context;
console.log(easter_egg);
if (!!easter_egg) {
const easterEggDo = this.easterEggWrapper();
document.addEventListener('keyup', easterEggDo);
(this as any).easterEggDo = easterEggDo;
const easterEggDo = this.easterEggWrapper();
document.addEventListener('keyup', easterEggDo);
(this as any).easterEggDo = easterEggDo;
}
};
easterEggDestory = () => {
const { easterEggDo } = this as any;
if (!!easterEggDo) document.removeEventListener('keyup', easterEggDo);
};
easterEggWrapper = () => {
var cache = '';
const { easter_egg } = this.context;
const kv = easter_egg.split(/\s/).filter((s) => s.length > 0);
var mxL = 0;
var eggs = {};
var i = 0;
while (i + 1 < kv.length) {
eggs[kv[i]] = kv[i + 1];
mxL = Math.max(mxL, kv[i].length);
i += 2;
}
var mxL = this.state.easter_length;
return (e: KeyboardEvent) => {
cache += e.key;
if (cache.length >= mxL) {
this.easterEggPost(cache)
mxL = this.state.easter_length;
}
cache = cache.slice(-mxL);
console.log(cache);
for (const k of Object.keys(eggs))
if (cache.slice(-k.length) == k) {
Notification.message({
alertType: 'info',
title: '恭喜你,触发了一个彩蛋!',
content: '赶快去看一下彩蛋是什么吧',
});
this.setState({ easter_egg: eggs[k] });
}
console.log(cache,mxL);
};
};
@ -240,7 +243,7 @@ class Index extends React.Component<IndexProps, IndexState> { @@ -240,7 +243,7 @@ class Index extends React.Component<IndexProps, IndexState> {
</div>
</Card>
{!!this.state.easter_egg && (
{!!this.state.easter_success && (
<Notification
icon={<EasterEgg />}
title="你发现了一个彩蛋"
@ -250,7 +253,7 @@ class Index extends React.Component<IndexProps, IndexState> { @@ -250,7 +253,7 @@ class Index extends React.Component<IndexProps, IndexState> {
</Link>
}
onClose={() => {
this.setState({ easter_egg: '' });
this.setState({ easter_egg: '' ,easter_success:false});
}}
/>
)}

6
types/typings.d.ts vendored

@ -47,6 +47,11 @@ declare namespace Blotter { @@ -47,6 +47,11 @@ declare namespace Blotter {
title: string;
content: string;
}
interface EggResponse{
success: boolean;
length:number;
url:string;
}
interface Menu {
icon: string;
name: string;
@ -160,7 +165,6 @@ declare namespace Blotter { @@ -160,7 +165,6 @@ declare namespace Blotter {
vmess: string;
zhihu: string;
server_chan: string;
easter_egg: string;
}
interface City {

5
utils/api.ts

@ -108,7 +108,7 @@ export const layout = async ( @@ -108,7 +108,7 @@ export const layout = async (
email: string;
github: string;
friends: Blotter.FriendSimple[];
easter_eggs: string;
// easter_eggs: string;
notification: string;
}>,
) => {
@ -129,6 +129,9 @@ export const adminPost = async (url: string, callback?: RequestCallback<Blotter. @@ -129,6 +129,9 @@ export const adminPost = async (url: string, callback?: RequestCallback<Blotter.
return await request('get', '/api/admin/post', { url }, callback);
};
export const postEgg =async (word:string,callback?:RequestCallback<Blotter.EggResponse>) => {
return await request('get','/api/eggs',{word},callback)
}
export const comments = async (
url: string,
callback?: RequestCallback<{ total: number; comments: Blotter.Comment[] }>,

2
utils/global.ts

@ -45,7 +45,6 @@ export const defaultContext: GlobalProps = { @@ -45,7 +45,6 @@ export const defaultContext: GlobalProps = {
github_connected: false,
self: false,
},
easter_egg: '',
notification: '',
footer: '',
};
@ -77,7 +76,6 @@ export declare interface GlobalProps { @@ -77,7 +76,6 @@ export declare interface GlobalProps {
ad_text: string;
front_version: string;
back_version: string;
easter_egg: string;
notification: string;
footer: string;
}

Loading…
Cancel
Save