Procházet zdrojové kódy

koa后端api接口

lybenson před 8 roky
rodič
revize
9e92254107

+ 7 - 8
bilibili-api/app/routes/banner.js

@@ -1,13 +1,12 @@
-import router from 'koa-router'
+import Router from 'koa-router'
 import axios from 'axios'
-const router = Router({
-  prefix: '/banner'
-})
-router.post('/list', async (ctx, next) => {
-  let response = await axios.get('')
-  ctx.body = response
-})
+import { banner } from './urlConfig'
 
+const router = Router()
 
+router.get('/banner', async (ctx, next) => {
+  let response = await axios.get(banner)
+  ctx.body = response.data
+})
 
 export default router

+ 13 - 0
bilibili-api/app/routes/ding.js

@@ -0,0 +1,13 @@
+import Router from 'koa-router'
+import axios from 'axios'
+import { ding } from './urlConfig'
+
+const router = Router()
+
+// 主内容
+router.get('/ding', async (ctx, next) => {
+  let response = await axios.get(ding)
+  ctx.body = response.data
+})
+
+export default router

+ 43 - 0
bilibili-api/app/routes/extra.js

@@ -0,0 +1,43 @@
+import Router from 'koa-router'
+import axios from 'axios'
+import { getSearchDefaultWords, topbg, hot, slideshow, season, recommend } from './urlConfig'
+
+const router = Router()
+
+// 默认搜索词
+router.get('/getSearchDefaultWords', async (ctx, next) => {
+  let response = await axios.get(ding)
+  ctx.body = response.data
+})
+// 顶部背景图
+router.get('/topbg', async (ctx, next) => {
+  let response = await axios.get(topbg)
+  ctx.body = response.data
+})
+
+// 各分类热门
+router.get('/hot', async (ctx, next) => {
+  let response = await axios.get(hot)
+  ctx.body = response.data
+})
+
+// 番剧下方更新列表右侧新番放送表上侧
+router.get('/slideshow', async (ctx, next) => {
+  let response = await axios.get(slideshow)
+  ctx.body = response.data
+})
+
+// 番剧下方更新列表右侧新番放送表下侧
+router.get('/season', async (ctx, next) => {
+  let response = await axios.get(season)
+  ctx.body = response.data
+})
+
+// 最底部特别推荐
+router.get('/recommend', async (ctx, next) => {
+  let response = await axios.get(recommend)
+  ctx.body = response.data
+})
+
+
+export default router

+ 12 - 13
bilibili-api/app/routes/index.js

@@ -1,22 +1,21 @@
 import Router from 'koa-router'
-
 import banner from './banner'
-
+import ding from './ding'
+import extra from './extra'
+import live from './live'
+import promote from './promote'
+import rank from './rank'
 
 const router = Router()
 
-// koa-router 支持中间件来处理路由的 通过use方法注册中间件
-
-// 任何路由都会匹配此中间件  并通过next传递给下一个
-router.use(async (ctx, next) => {
-	await next()
-})
-
-// 根据请求方法匹配中间件
-// router.get().post()
-
 // routes表示的是路由的嵌套处理
-router.use(banner.routes(), account.allowedMethods())
+router.use(banner.routes(), banner.allowedMethods())
+router.use(ding.routes(), ding.allowedMethods())
+router.use(extra.routes(), extra.allowedMethods())
+router.use(live.routes(), live.allowedMethods())
+router.use(promote.routes(), promote.allowedMethods())
+router.use(rank.routes(), rank.allowedMethods())
+
 
 export default router
 

+ 13 - 0
bilibili-api/app/routes/live.js

@@ -0,0 +1,13 @@
+import Router from 'koa-router'
+import axios from 'axios'
+import { live } from './urlConfig'
+
+const router = Router()
+
+// 直播
+router.get('/live', async (ctx, next) => {
+  let response = await axios.get(live)
+  ctx.body = response.data
+})
+
+export default router

+ 18 - 0
bilibili-api/app/routes/promote.js

@@ -0,0 +1,18 @@
+import Router from 'koa-router'
+import axios from 'axios'
+import { promote, promoteAd } from './urlConfig'
+
+const router = Router()
+
+// 推广
+router.get('/promote', async (ctx, next) => {
+  let response = await axios.get(promote)
+  ctx.body = response.data
+})
+
+router.get('/promoteAd', async (ctx, next) => {
+  let response = await axios.get(promoteAd)
+  ctx.body = response.data
+})
+
+export default router

+ 21 - 0
bilibili-api/app/routes/rank.js

@@ -0,0 +1,21 @@
+import Router from 'koa-router'
+import axios from 'axios'
+import { ranking3, ranking7, randking } from './urlConfig'
+
+const router = Router()
+
+// 排行
+router.get('/ranking3', async (ctx, next) => {
+  let response = await axios.get(ranking3)
+  ctx.body = response.data
+})
+router.get('/ranking7', async (ctx, next) => {
+  let response = await axios.get(ranking7)
+  ctx.body = response.data
+})
+router.get('/randking', async (ctx, next) => {
+  let response = await axios.get(randking)
+  ctx.body = response.data
+})
+
+export default router

+ 5 - 5
bilibili-api/app/routes/urlConfig.js

@@ -2,22 +2,22 @@
 export const ding = 'http://www.bilibili.com/index/ding.json'
 
 // 轮播图
-export const banner = 'http://api.bilibili.com/x/web-show/res/loc?callback=jQuery17205969745067413896_1482805801285&jsonp=jsonp&pf=0&id=23&_=1482805801599'
+export const banner = 'http://api.bilibili.com/x/web-show/res/loc?pf=0&id=23'
 
 // 默认搜索单词
 export const getSearchDefaultWords = 'http://www.bilibili.com/widget/getSearchDefaultWords'
 
 // 主站头部背景图
-export const topbg = 'http://api.bilibili.com/x/web-show/res/loc?callback=jQuery1720029742745197591347_1488209619074&pf=0&id=142&jsonp=jsonp&_=1488209619792'
+export const topbg = 'http://api.bilibili.com/x/web-show/res/loc?pf=0&id=142'
 
 // 各分类热门内容
-export const hot = 'http://api.bilibili.com/x/web-show/res/locs?callback=jQuery1720029742745197591347_1488209619075&pf=0&ids=1550%2C1554%2C1556%2C1558%2C1560%2C1562%2C1564%2C1566%2C1568%2C1570%2C1572%2C1574%2C1624%2C1636&jsonp=jsonp&_=1488209619803'
+export const hot = 'http://api.bilibili.com/x/web-show/res/locs?pf=0&ids=1550,1554,1556,1558,1560,1562,1564,1566,1568,1570,1572,1574,1624,1636'
 
 // 推广
-export const promote = 'http://api.bilibili.com/x/web-show/res/loc?callback=jQuery1720029742745197591347_1488209619076&jsonp=jsonp&pf=0&id=34&_=1488209619911'
+export const promote = 'http://api.bilibili.com/x/web-show/res/loc?pf=0&id=34'
 
 // 推广右侧广告
-export const promoteAd = 'http://api.bilibili.com/x/web-show/res/loc?callback=jQuery1720029742745197591347_1488209619077&jsonp=jsonp&pf=0&id=29&_=1488209619914'
+export const promoteAd = 'http://api.bilibili.com/x/web-show/res/loc?pf=0&id=29'
 
 // 直播
 export const liveXhr = 'http://live.bilibili.com/bili/recom?callback=liveXhrDone'

+ 3 - 19
bilibili-api/app/server.js

@@ -14,24 +14,9 @@ import resource from 'koa-static' //静态资源托管
 
 import path from 'path'
 
-import router from './routes'
+import routes from './routes'
 import config from '../config/config';
 
-// 同步已定义的模型到数据库
-// models.sync({force: true}).then(function () {
-// 	console.log('Server Success')
-// }).catch (function (error) {
-// 	console.log('Server Failed' + error)
-// })
-
-// import router from './routes';
-
-// import middlewares from './middlewares';
-// import cacheMiddle from './middlewares/cache';
-
-// const redisStore = koaRedis({
-//   url: config.redisUrl
-// });
  
 const app = new Koa();
 
@@ -44,7 +29,6 @@ app.use(convert(logger()))
 app.use(bodyParser())
 
 app.use(resource(path.join(__dirname, '../public')))
-console.log(path.join(__dirname, '../public'))
 // app.use(json({ pretty: false, param: 'pretty' }))
 
 app.use(async (ctx, next) => {
@@ -54,8 +38,8 @@ app.use(async (ctx, next) => {
 	console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
 })
 
-// router
-app.use(router.routes(), router.allowedMethods());
+// routes
+app.use(routes.routes(), routes.allowedMethods());
 
 
 app.on('error', (error, ctx) => {