Commit d0e24c83 authored by shangbj's avatar shangbj

update

parent fa4bf9b0
...@@ -2,14 +2,64 @@ const { Router } = require('express') ...@@ -2,14 +2,64 @@ const { Router } = require('express')
const newsData = require('./data/news'); const newsData = require('./data/news');
const router = Router() const router = Router()
const total = newsData.length;
const newsDetails = {};
newsData.forEach((item)=>{
newsDetails[item.id]={
title:item.title,
time:item.time,
con:item.con,
id:item.id,
}
})
// const tetails
/* GET users listing. */ /* GET users listing. */
router.get('/news', function (req, res, next) { router.get('/news', function (req, res, next) {
const newsList = req.query.type==='simplify'?newsData.map((item)=>{
return({
title:item.title,
time:item.time,
id:item.id,
})
}):newsData;
if(req.query.pageSize){ if(req.query.pageSize){
res.json(newsData.slice(0,req.query.page)) if(req.query.pageIndex){
res.json({
total,
data:newsList.slice((req.query.pageIndex-1)*req.query.pageSize,(req.query.pageIndex-1)*req.query.pageSize+parseInt(req.query.pageSize)),
result:true
})
}else{ }else{
res.json(newsData) res.json({
total,
data:newsList.slice(0,req.query.pageSize),
result:true
})
}
}else{
res.json({
total:0,
data:[],
result:true
})
} }
}) })
router.get('/news/detail/:id', function (req, res, next) {
const id = parseInt(req.params.id)
if (id >= 0 && id < newsData.length) {
res.json({
data:newsDetails[id],
result:true
})
} else {
res.json({
data:null,
message:'该页面不存在',
result:false
})
}
})
module.exports = router module.exports = router
\ No newline at end of file
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<no-ssr> <no-ssr>
<vue-seamless-scroll :data="newsData" :class-option="optionTop" class="product-seamless-warp"> <vue-seamless-scroll :data="newsData" :class-option="optionTop" class="product-seamless-warp">
<ul class="item"> <ul class="item">
<li v-for="(item,index) in newsData" v-if="index<10" :key="item.id"> <li v-for="item in newsData" :key="item.id">
<nuxt-link :to="'/news/' + item.id"> <nuxt-link :to="'/news/' + item.id">
{{item.title}} {{item.title}}
</nuxt-link> </nuxt-link>
...@@ -20,8 +20,8 @@ ...@@ -20,8 +20,8 @@
<script> <script>
export default { export default {
async asyncData({ app }) { async asyncData({ app }) {
const newsData = await app.$axios.$get('/api/news?pageSize=10'); const newsData = await app.$axios.$get('/api/news?pageIndex=1&pageSize=20&type=simplify');
return { newsData: newsData } return { newsData: newsData.data||[] }
}, },
computed:{ computed:{
optionTop(){ optionTop(){
......
...@@ -103,9 +103,9 @@ import ContactUs from '~/components/ContactUs.vue' ...@@ -103,9 +103,9 @@ import ContactUs from '~/components/ContactUs.vue'
export default { export default {
async asyncData({ app }) { async asyncData({ app }) {
const newsData = await app.$axios.$get('/api/news?pageSize=10'); const newsData = await app.$axios.$get('/api/news?pageSize=10&type=simplify');
const productData = await app.$axios.$get('/api/product'); const productData = await app.$axios.$get('/api/product');
return { newsData: newsData,productData:[...productData.ozonizer.data,...productData.disinfectant.data,...productData.equipment.data] } return { newsData: newsData.data||[],productData:[...productData.ozonizer.data,...productData.disinfectant.data,...productData.equipment.data] }
}, },
data:function(){ data:function(){
return { return {
......
<template> <template>
<div class="currently-wrap"> <div class="currently-wrap">
<div class="currently-node">新闻动态</div> <div class="currently-node">新闻动态</div>
<div class="currently-title">{{title}}</div> <div class="currently-title">{{details.title}}</div>
<div class="currently-time">发布时间:{{time}}</div> <div class="currently-time">发布时间:{{details.time}}</div>
<div class="currently-text" v-html="con"></div> <div class="currently-text" v-html="details.con"></div>
</div> </div>
</template> </template>
<script> <script>
export default { export default {
asyncData({ params, store, error }) { // asyncData({ params, store, error }) {
const item = store.state.news.newsData.find((item) => String(item.id) === params.id) // const item = store.state.news.newsData.find((item) => String(item.id) === params.id)
if (!item) { // if (!item) {
// return error({ message: '页面不存在', statusCode: 404 })
// }
// return item
// },
async asyncData({ app, params, error }) {
const details = await app.$axios.$get(`/api/news/detail/${params.id}`);
if(details.result){
return {details: details.data};
}else{
return error({ message: '页面不存在', statusCode: 404 }) return error({ message: '页面不存在', statusCode: 404 })
} }
return item
},
head() {
return {
}
} }
} }
</script> </script>
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<div class="currently-node">新闻动态</div> <div class="currently-node">新闻动态</div>
<div class="currently-list"> <div class="currently-list">
<ul> <ul>
<li class="listItem" v-for="(item, index) in newData.slice(startPage,endPage)" :key="index"> <li class="listItem" v-for="(item, index) in newData" :key="index">
<nuxt-link :to="'/news/' + item.id" class="itemTitle">{{item.title}}</nuxt-link> <nuxt-link :to="'/news/' + item.id" class="itemTitle">{{item.title}}</nuxt-link>
<span class="contentTime">{{item.time}}</span> <span class="contentTime">{{item.time}}</span>
</li> </li>
...@@ -15,32 +15,32 @@ ...@@ -15,32 +15,32 @@
layout="prev, pager, next" layout="prev, pager, next"
prev-text="上一页" prev-text="上一页"
next-text="下一页" next-text="下一页"
:page-size="pageSize" :page-size='20'
@current-change="handleCurrentChange" @current-change="handleCurrentChange"
:total="newData.length"> :total="total">
</el-pagination> </el-pagination>
</div> </div>
</template> </template>
<script> <script>
export default { export default {
async asyncData({ app }) {
const newData = await app.$axios.$get('/api/news?pageIndex=1&pageSize=20&type=simplify');
return { newData: newData.data || [] ,total:newData.total}
},
data(){ data(){
return{ return{
newData:this.$store.state.news.newsData,
pageSize:20,
currentPage:1,
}
},
computed:{
startPage(){
return this.pageSize*(this.currentPage-1);
},
endPage(){
return this.startPage + this.pageSize;
} }
}, },
methods:{ methods:{
handleCurrentChange(val){ handleCurrentChange(page){
this.currentPage = val this.$axios.$get(`/api/news?pageIndex=${page}&pageSize=20&type=simplify`).then(res => {
if (res.result) {
this.newData = res.data;
} else {
this.newData = [];
}
});
} }
} }
} }
......
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment