Skip to content

Commit

Permalink
Merge pull request #67 from stack-wuh/feat-apiv2
Browse files Browse the repository at this point in the history
feat(fetch.prefix): ↪️  配合Nest应用,更换请求api的前缀
  • Loading branch information
stack-wuh authored Jan 15, 2022
2 parents 817b197 + 3cbe6e9 commit cf46cc6
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 41 deletions.
4 changes: 2 additions & 2 deletions components/post/body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const getBodyString = async (str: string): Promise<any> =>
const Body = () => {
const postRef = useRef<any>(null)
const data = usePostContext()
const [body, setbody] = useState(data.content)
const [body, setbody] = useState(data.body)
useEffect(() => {
getBodyString(data.content).then(res => setbody(res))
getBodyString(data.body).then(res => setbody(res))
}, [data])

useMount(() => {
Expand Down
2 changes: 1 addition & 1 deletion components/post/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const tagTypeMap: tagType = {
color: 'yuque',
icon: 'icon-yuque-fill1',
},
Github: {
github: {
color: 'github',
icon: 'icon-github',
},
Expand Down
3 changes: 1 addition & 2 deletions components/post/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ const usePostPages = (initialData: THomeIntialProps, count: number) => {
)
const isEmpty = data?.length
const allowLoadMore = size < Math.ceil(count / PAGE_SIZE)

const hits = (data || []).reduce(
(acc: [], curr: any) => acc.concat(curr.data.rows),
(acc: [], curr: any) => acc.concat(curr.hits),
[]
)

Expand Down
22 changes: 4 additions & 18 deletions constant/api.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
import { isDev } from '@/lib/index'
export const PREFIX = ''
export const DOMAIN = 'https://api.wuh.site'
export const VERCEL_DOMAIN =
'https://netease-cloud-music-api-umber-seven.vercel.app'
export const PREFIX = '/v2'
export const DOMAIN = isDev() ? 'http://localhost:3200' : 'https://api.wuh.site'
export const DOMAIN_STATIC = isDev()
? 'http://localhost:3000'
: 'https://wuh.site'

export const API_ARTICLE_LIST = `${DOMAIN}${PREFIX}/articles`
export const API_ARTICLE_LIST = `${DOMAIN}${PREFIX}/article`

export const API_ARTICLE_ITEM = `${DOMAIN}${PREFIX}/articles/`
export const API_ARTICLE_ITEM = `${DOMAIN}${PREFIX}/article/`

export const API_BANNER_HOME = `${DOMAIN_STATIC}/api/banner`

export const API_GALLERY_LIST = `${DOMAIN_STATIC}/api/gallery`

export const API_PLAY_LIST = `${DOMAIN_STATIC}/api/playlist`

export const API_VERCEL_LOGIN = `${VERCEL_DOMAIN}/login/cellphone?phone=18827057040&captcha=2974`

export const API_VERCEL_ACCOUNT = `${VERCEL_DOMAIN}/user/account`

export const API_VERCEL_LISTLIST = `${VERCEL_DOMAIN}/likelist?uid=398326271`

export const API_VERCEL_PLAYLIST =
`${VERCEL_DOMAIN}/playlist/detail?id=5417278232&timestamp=${new Date().getTime()}&cookie=` +
'MUSIC_A_T=1484056104529; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/eapi/feedback;;MUSIC_R_T=1484056123783; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/wapi/clientlog;;MUSIC_A_T=1484056104529; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/weapi/feedback;;__csrf=1413293f8c57b62b63a523d27adb53d2; Max-Age=1296010; Expires=Sun, 28 Nov 2021 13:21:15 GMT; Path=/;;MUSIC_R_T=1484056123783; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/wapi/feedback;;MUSIC_U=9de0b492647e466183adb0f6c6d71d023a72dffa63448f46408ff126231e692de865e922881deed94258bee06fad8fedabcbf7551dcd336e22fac3ef9d6f10fce758fe0f42e0166289e1a1143b71fa34c3061cd18d77b7a0; Max-Age=1296000; Expires=Sun, 28 Nov 2021 13:21:05 GMT; Path=/;;__remember_me=true; Max-Age=1296000; Expires=Sun, 28 Nov 2021 13:21:05 GMT; Path=/;;MUSIC_R_T=1484056123783; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/neapi/feedback;;MUSIC_A_T=1484056104529; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/eapi/clientlog;;MUSIC_R_T=1484056123783; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/openapi/clientlog;;MUSIC_A_T=1484056104529; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/weapi/clientlog;;MUSIC_A_T=1484056104529; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/neapi/clientlog;;MUSIC_R_T=1484056123783; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/eapi/clientlog;;MUSIC_R_T=1484056123783; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/eapi/feedback;;MUSIC_A_T=1484056104529; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/neapi/feedback;;MUSIC_A_T=1484056104529; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/wapi/clientlog;;MUSIC_A_T=1484056104529; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/api/clientlog;;MUSIC_A_T=1484056104529; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/openapi/clientlog;;MUSIC_A_T=1484056104529; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/api/feedback;;MUSIC_A_T=1484056104529; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/wapi/feedback;;MUSIC_R_T=1484056123783; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/weapi/clientlog;;MUSIC_R_T=1484056123783; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/api/feedback;;NMTID=00Oyj6D5UCW3PKCkURNtB04V1g5vY4AAAF9GXVqVQ; Max-Age=315360000; Expires=Tue, 11 Nov 2031 13:21:05 GMT; Path=/;;MUSIC_R_T=1484056123783; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/neapi/clientlog;;MUSIC_R_T=1484056123783; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/weapi/feedback;;MUSIC_R_T=1484056123783; Max-Age=2147483647; Expires=Thu, 1 Dec 2089 16:35:12 GMT; Path=/api/clientlog;'

export const API_VERCEL_SONG_URL = `${VERCEL_DOMAIN}/song/url?id=1370879975,1340543218,1465288704,3165930362967,1419346842,1485850351,1410415908,1382576444,36190432`
47 changes: 38 additions & 9 deletions lib/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
import fetch from 'isomorphic-fetch'
import { isDev } from './index'

export default function fetcher(url: string): Promise<any> {
return fetch(url, {
keepalive: true,
referrer: 'server',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json',
},
}).then(res => res.json())
export default async function fetcher(url: string): Promise<any> {
try {
const res = await fetch(url, {
keepalive: true,
referrer: 'wuh.site',
cache: 'no-cache',
mode: 'cors',
credentials: 'omit',
headers: {
'Content-Type': 'application/json',
},
referrerPolicy: 'no-referrer',
redirect: 'follow',
})
if (!res.status) {
throw Error('[Fetcher Error]: Response Fail')
}
const data = await res.json()

if (isDev()) {
console.group('Fetcher Response')
console.log('==== res', res)
console.log('==== data', data)
console.groupEnd()
}

/**
* 在v2中,请求成功的状态码换成了200
*/
if (data.code === 200) {
return data.data
}

return data
} catch (error) {
console.error('[Fetcher Error]: ', error)
}
}
2 changes: 1 addition & 1 deletion pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export async function getStaticProps() {
return {
props: {
initialData: article,
articleCount: article.data.count || 0,
articleCount: article.pager.count || 0,
bannerList: banner.data.rows || [],
},
}
Expand Down
12 changes: 8 additions & 4 deletions pages/post/[id].tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import useSWR from 'swr'
import { useUpdateEffect } from 'ahooks'
import fetcher from '@/lib/fetch'
import { useRouter } from 'next/router'
import { API_ARTICLE_ITEM } from '@/constant/api'
Expand Down Expand Up @@ -35,15 +36,18 @@ function useData(id: string | string[] | undefined) {
const Detail: React.FC<{}> = () => {
const router = useRouter()
const { data, error } = useData(router.query.id)

useUpdateEffect(() => {
hljs.formatter(router.asPath)
}, [])

if (error) return <div className="error">ERROR</div>
if (!data) return <Empty.Loading />

hljs.formatter(router.asPath)

return (
<div className="ww_detail">
<ErrorBoundary>
<PostContext value={data.data}>
<PostContext value={data.hits}>
{/* 文章详情页--页头meta处理 */}
<PostHeader />
{/* 文章详情页--文章主体 */}
Expand All @@ -52,7 +56,7 @@ const Detail: React.FC<{}> = () => {
{/* 版权声明 */}
<Divider />

<PostInfoList {...data.data} />
<PostInfoList {...data.hits} />

{/* 分享--按钮组 */}
<Divider size="small" />
Expand Down
11 changes: 7 additions & 4 deletions styles/post.scss
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,20 @@ $name: ww_post__;
&:hover {
&::before {
transform: rotate(360deg);
transform-origin: center;
transition: transform .5s ease;
}
}

&::before {
content: '*';
display: inline-block;
width: .3em;
height: .3em;
line-height: .5;
display: block;
width: .5em;
height: .5em;
text-align: center;
line-height: 1;
margin-right: .5em;
font-size: 1em;
}

&:not(:last-of-type) {
Expand Down

2 comments on commit cf46cc6

@vercel
Copy link

@vercel vercel bot commented on cf46cc6 Jan 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deployment failed with the following error:

The most recent charge for your active payment method has failed. Please update it here: https://vercel.com/teams/163m/settings/billing.

@vercel
Copy link

@vercel vercel bot commented on cf46cc6 Jan 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.