Skip to content

Commit

Permalink
接口问题修复,新闻资讯,详情,评论动态渲染
Browse files Browse the repository at this point in the history
  • Loading branch information
ForeManWang committed Jan 3, 2019
1 parent 2a7da51 commit fe959cc
Show file tree
Hide file tree
Showing 13 changed files with 757 additions and 31 deletions.
523 changes: 509 additions & 14 deletions README.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -34,6 +34,7 @@
"dependencies": {
"bootstrap": "^3.3.7",
"mint-ui": "^2.2.9",
"moment": "^2.23.0",
"vue": "^2.5.2",
"vue-resource": "^1.5.1",
"vue-router": "^3.0.1"
Expand Down
1 change: 1 addition & 0 deletions src/App.vue
Expand Up @@ -40,6 +40,7 @@
<style lang="scss" scoped>
.app-container {
padding-top: 40px;
padding-bottom: 50px;
overflow-x: hidden;
}
Expand Down
78 changes: 78 additions & 0 deletions src/components/news/newsInfo.vue
@@ -0,0 +1,78 @@
<template>
<div>
<div class="newsinfo-container">
<h3 class="title">{{ newsinfo.title }}</h3>
<p class="subtitle">
<span>{{ newsinfo.add_time | dateFormat }}</span>
<span>点击: {{ newsinfo.click }} 次</span>
</p>
<hr>
<!-- 内容区域 -->
<div class="content" v-html="newsinfo.content"></div>
<!-- 评论子组件区域 -->
<comment-box :id="this.id"></comment-box>
</div>
</div>
</template>


<script>
// 1、导入评论子组件
import comment from '../subcomments/comment.vue'
import { Toast } from 'mint-ui'
export default {
data() {
return {
// 将 URL 地址中传递过来的 id 值挂载到 data上,方便以后调用
id: this.$route.params.id,
newsinfo: {}
}
},
created() {
this.getNewsInfo()
},
methods: {
getNewsInfo() {
// 获取新闻详情
this.$http.get('api/getnew/' + this.id).then(result => {
if (result.body.status === 0) {
this.newsinfo = result.body.message[0]
} else {
Toast('获取新闻详情失败')
}
})
}
},
// 2、注册评论组件
components: {
'comment-box': comment
}
}
</script>


<style lang="scss" scoped>
.newsinfo-container {
padding: 0 4px;
.title {
font-size: 16px;
text-align: center;
margin: 15px 0;
color: red;
}
.subtitle {
font-size: 13px;
color: #226aff;
display: flex;
justify-content: space-between;
}
.content {
img {
width: 100%;
}
}
}
</style>
64 changes: 64 additions & 0 deletions src/components/news/newsList.vue
@@ -0,0 +1,64 @@
<template>
<div>
<ul class="mui-table-view">
<li class="mui-table-view-cell mui-media" v-for="item in newslist" :key="item.id">
<router-link :to="'/home/newsinfo/' + item.id">
<img class="mui-media-object mui-pull-left" :src="item.img_url">
<div class="mui-media-body">
<h1>{{ item.title }}</h1>
<p class='mui-ellipsis'>{{ item.zhaiyao }}</p>
<p class="mui-ellipsis">
<span>发表时间: {{ item.add_time | dateFormat }}</span>
<span>点击: {{ item.click }} 次</span>
</p>
</div>
</router-link>
</li>
</ul>
</div>
</template>

<script>
import { Toast } from 'mint-ui'
export default {
data() {
return {
newslist: []
}
},
created() {
this.getNewsList()
},
methods: {
getNewsList() {
this.$http.get('api/getnewslist').then(result => {
if (result.body.status === 0) {
// 如果没有失败,应该把数据保存到data上
this.newslist = result.body.message
} else {
Toast('获取新闻列表失败')
}
})
}
}
}
</script>

<style lang="scss" scoped>
.mui-table-view {
li {
h1 {
font-size: 14px;
}
.mui-ellipsis {
font-size: 12px;
color: #226aff;
display: flex;
justify-content: space-between;
}
}
}
</style>
79 changes: 79 additions & 0 deletions src/components/subcomments/comment.vue
@@ -0,0 +1,79 @@
<template>
<div class="cmt-container">
<h3>发表评论</h3>
<hr>
<textarea placeholder="请输入评论内容,最多输入120字" maxlength="120"></textarea>
<mt-button type="primary" size="large">发表评论</mt-button>
<div class="cmt-list">
<div class="cmt-item" v-for="(item, i) in comments" :key="item.add_time">
<div class="cmt-title">第{{ i+1 }}楼&nbsp;&nbsp;用户: {{ item.uer_name }}&nbsp;&nbsp;发表时间: {{ item.add_time | dateFormat }}</div>
<div class="cmt-body">
{{ item.content === 'undefined' ? '此用户很懒,什么都没说' : item.content }}
</div>
</div>
</div>
<mt-button type="danger" size="large" plain @click="getMore">加载更多</mt-button>
</div>
</template>

<script>
import { Toast } from 'mint-ui'
export default {
data() {
return {
pageIndex: 1, // 默认展示第一页数据
comments: [] // 存储所有的评论数据
}
},
created() {
this.getComments()
},
methods: {
getComments() {
// 获取评论
this.$http.get("api/getcomments/" + this.id + "?pageindex=" + this.pageIndex).then(result => {
if (result.body.status === 0) {
// this.comments = result.body.message 这种直接覆盖,如果在加载更多中会覆盖掉前面的数据
// 每单获取新评论数据的时候,不要把老数据清空覆盖,而是应该以老数据拼接上新数据
this.comments = this.comments.concat(result.body.message)
} else {
Toast('评论数据加载失败')
}
})
},
getMore() {
// 加载更多
this.pageIndex++
this.getComments()
}
},
props: ["id"]
}
</script>

<style lang="scss" scoped>
.cmt-container {
h3 {
font-size: 18px;
}
textarea {
font-size: 14px;
height: 85px;
margin: 0;
}
.cmt-list {
margin: 5px 0;
.cmt-item {
font-size: 13px;
.cmt-title {
line-height: 30px;
background-color: #ccc;
}
.cmt-body {
line-height: 35px;
text-indent: 2em;
}
}
}
}
</style>
20 changes: 5 additions & 15 deletions src/components/tabbar/HomeContainer.vue
Expand Up @@ -5,17 +5,13 @@
<mt-swipe-item v-for="item in bannerList" :key="item.url">
<img :src="item.img" alt="">
</mt-swipe-item>
<mt-swipe-item>2</mt-swipe-item>
<mt-swipe-item>3</mt-swipe-item>
<mt-swipe-item>4</mt-swipe-item>
<mt-swipe-item>5</mt-swipe-item>
</mt-swipe>

<!-- 九宫格改造为六宫格 -->
<ul class="mui-table-view mui-grid-view mui-grid-9">
<li class="mui-table-view-cell mui-media mui-col-xs-4 mui-col-sm-3"><a href="#">
<img src="../../images/menu1(1).png" alt="">
<div class="mui-media-body">新闻资讯</div></a></li>
<li class="mui-table-view-cell mui-media mui-col-xs-4 mui-col-sm-3">
<router-link to="/home/newslist">
<img src="../../images/menu1.png" alt="">
<div class="mui-media-body">新闻资讯</div></router-link></li>
<li class="mui-table-view-cell mui-media mui-col-xs-4 mui-col-sm-3"><a href="#">
<img src="../../images/menu2.png" alt="">
<div class="mui-media-body">图片分享</div></a></li>
Expand Down Expand Up @@ -50,7 +46,7 @@
methods: {
getBanner() {
// 获取轮播图数据的方法
this.$http.get("http://vue.studyit.io/api/getlunbo").then(result => {
this.$http.get("api/getlunbo").then(result => {
if (result.body.status === 0) {
this.bannerList = result.body.message
} else {
Expand All @@ -74,12 +70,6 @@
}
&:nth-child(3) {
background-color: blue;
}
&:nth-child(4) {
background-color: cyan;
}
&:nth-child(5) {
background-color: pink;
}
img {
width: 100%;
Expand Down
Binary file added src/images/author.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
14 changes: 13 additions & 1 deletion src/main.js
Expand Up @@ -8,16 +8,28 @@ import VueRouter from 'vue-router'
// 1.2 安装路由
Vue.use(VueRouter)

// 3.1 导入时间插件
import moment from 'moment'
// 3.2 定义全局过滤器
Vue.filter('dateFormat', function (dataStr, pattern = "YYYY-MM-DD HH:mm:ss") {
return moment(dataStr).format(pattern)
})

// 2.1 导入 vue-resource
import VueResource from 'vue-resource'
// 2.2 安装 vue-resource
Vue.use(VueResource)

// 配置全局根路径
Vue.http.options.root = 'http://www.liulongbin.top:3005'

// Vue.http.options.emulateJSON = true
// 按需导入 Mint-UI 的组件
import { Header, Swipe, SwipeItem } from 'mint-ui'
import { Header, Swipe, SwipeItem, Button } from 'mint-ui'
Vue.component(Header.name, Header)
Vue.component(Swipe.name, Swipe)
Vue.component(SwipeItem.name, SwipeItem)
Vue.component(Button.name, Button)

// 1.3 创建自己的 router.js 模块
// 1.4 导入自己的 router.js 模块
Expand Down
8 changes: 7 additions & 1 deletion src/router.js
Expand Up @@ -6,6 +6,9 @@ import HomeContainer from './components/tabbar/HomeContainer.vue'
import MemberContainer from './components/tabbar/MemberContainer.vue'
import ShopcarContainer from './components/tabbar/ShopcarContainer.vue'
import SearchContainer from './components/tabbar/SearchContainer.vue'
import NewsList from './components/news/newsList.vue'
import NewsInfo from './components/news/newsInfo.vue'
// import Comment from './components/subcomments/comment.vue'

// 创建路由对象
var router = new VueRouter({
Expand All @@ -14,7 +17,10 @@ var router = new VueRouter({
{ path: '/home', component: HomeContainer },
{ path: '/member', component: MemberContainer },
{ path: '/shopcar', component: ShopcarContainer },
{ path: '/search', component: SearchContainer }
{ path: '/search', component: SearchContainer },
{ path: '/home/newslist', component: NewsList },
{ path: '/home/newsinfo/:id', component: NewsInfo }
// { path: '/home/newsinfo/', component: Comment }
],
linkActiveClass: 'mui-active' // 覆盖默认的路由高亮的类,默认的类叫做 router-link-active
})
Expand Down
Binary file removed ~$和api接口说明文档.docx
Binary file not shown.
Binary file modified 项目和api接口说明文档.docx
Binary file not shown.

0 comments on commit fe959cc

Please sign in to comment.