1. 程式人生 > >egg學習筆記(3)--MVC結構

egg學習筆記(3)--MVC結構

目錄結構

clipboard.png

頁面效果

title

clipboard.png

content

clipboard.png

controller

//controller/title.js

'use strict';

const Controller = require('egg').Controller;

class TitleController extends Controller {
    async index() {
        let list = await this.service.title.getTitleList();
        await this.ctx.render('title', {
            list: list,
        })
    }
    async content(){  
        let aid = this.ctx.query.aid;
        let content = await this.service.title.getContent(aid);
        console.log(content)
        await this.ctx.render('content',{
            content:content[0]
        })
    }
}

module.exports = TitleController;

service

//service/title.js

'use strict';

const Service = require('egg').Service;

class TitleService extends Service {
    async getTitleList() {
        let api = this.config.api + 'appapi.php?a=getPortalList&catid=20&page=1';
        let response = await this.ctx.curl(api);
        let data = JSON.parse(response.data);
        return data.result;
  }
  async getContent(aid){
      let api = this.config.api + 'appapi.php?a=getPortalArticle&aid=' + aid;
      let response = await this.ctx.curl(api);
      let data = JSON.parse(response.data);
      return data.result;
  }
}

module.exports = TitleService;

擴充套件

extend/helper.js

module.exports = {
    formatDate(time) {
        var date = new Date(time * 1000);
        var YYYY = date.getFullYear();
        var MM = date.getMonth() + 1;
        var DD = date.getDate();
        var hh = date.getHours();
        var mm = date.getMinutes();
        var ss = date.getSeconds();

        MM = MM < 10 ? ('0' + MM) : MM;
        DD = DD < 10 ? ('0' + DD) : DD;
        hh = hh < 10 ? ('0' + hh) : hh;
        mm = mm < 10 ? ('0' + mm) : mm;
        ss = ss < 10 ? ('0' + ss) : ss;

        return YYYY + '-' + MM + '-' + DD + ' ' + hh + ':' + mm + ':' + ss;
    }
}

config

//config/config.default.js

  config.api = 'http://www.phonegap100.com/';

router

  router.get('/title', controller.title.index);
  router.get('/content', controller.title.content);

view

//title.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h2>新聞列表</h2>
    <ul>
        <% for(var i = 0;i<list.length;i++) {%>
        <li>
            <a href="/content?aid=<%=list[i].aid%>"><%=list[i].title%>---<%= helper.formatDate(list[i].dateline)%></a>
        </li>
        <%}%>
    </ul>
</body>
</html>

// content.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h2><%= content.title%></h2>
    <div>
        <%- content.content%>
    </div>
</body>
</html>