From d70ccf541d87c43434514380dfd17bf5178f3e91 Mon Sep 17 00:00:00 2001 From: ddl Date: Sun, 23 Apr 2017 18:44:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E9=97=BB=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=AF=84=E8=AE=BA=EF=BC=8C=E6=97=A0=E9=99=90=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E6=94=B9=E8=AF=84=E8=AE=BA=EF=BC=8C=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E8=AF=84=E8=AE=BA=E3=80=82=E9=A1=B5=E9=9D=A2=E6=95=88?= =?UTF-8?q?=E6=9E=9C=E8=BF=98=E9=9C=80=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.wxss | 2 +- pages/blogpost/body.js | 3 +- pages/index/index.js | 70 +++++++++++++--------- pages/news/index.js | 129 +++++++++++++++++++++++++++++++++++------ pages/news/index.wxml | 22 ++++--- pages/news/index.wxss | 9 ++- pages/user/login.js | 2 +- 7 files changed, 179 insertions(+), 58 deletions(-) diff --git a/app.wxss b/app.wxss index b5823b8..1396a36 100644 --- a/app.wxss +++ b/app.wxss @@ -1,5 +1,5 @@ @import "style/weui.wxss"; - +@import "vendor/wxParse/wxParse.wxss"; page{ background-color: #F8F8F8; font-size: 16px; diff --git a/pages/blogpost/body.js b/pages/blogpost/body.js index 09d68ad..7aa04fa 100644 --- a/pages/blogpost/body.js +++ b/pages/blogpost/body.js @@ -58,7 +58,6 @@ Page({ "Authorization": "Bearer " + getApp().accessToken }, success: function (res) { - if (res) { that.setData({ comments: res.data @@ -80,7 +79,7 @@ Page({ url: 'https://api.cnblogs.com/api/blogs/' + this.data.blogApp + '/posts/' + this.data.postId + '/comments', method: 'POST', data: { - "content": this.data.commentInput + "": this.data.commentInput }, header: { "Content-Type": "application/x-www-form-urlencoded", diff --git a/pages/index/index.js b/pages/index/index.js index 55e2196..0b4df31 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -125,6 +125,10 @@ Page({ * @param int postType 加载博文类型 */ loadMoreBlogPosts: function (that, pageIndex, postType) { + wx.showLoading({ + title: '加载中', + }) + wx.request({ url: 'https://api.cnblogs.com/api/blogposts/@' + postType + '?pageIndex=' + pageIndex + '&pageSize=' + pageSize, method: 'GET', @@ -133,28 +137,30 @@ Page({ "Authorization": "Bearer " + getApp().accessToken }, success: function (res) { - pageIndex++; - if (postType === 'sitehome') { - that.setData({ - blog_articles: that.data.blog_articles.concat(res.data), - siteHomeIndex: pageIndex - }); + if (res.data.length > 0) { + pageIndex++; + if (postType === 'sitehome') { + that.setData({ + blog_articles: that.data.blog_articles.concat(res.data), + siteHomeIndex: pageIndex + }); - } else if (postType === 'picked') { - that.setData({ - blog_pickeds: that.data.blog_pickeds.concat(res.data), - pickedIndex: pageIndex - }); + } else if (postType === 'picked') { + that.setData({ + blog_pickeds: that.data.blog_pickeds.concat(res.data), + pickedIndex: pageIndex + }); + } } }, fail: function () { // fail }, complete: function () { - console.log('-------完成后---------'); wx.hideNavigationBarLoading(); wx.stopPullDownRefresh(); + wx.hideLoading(); } }); }, @@ -166,6 +172,10 @@ Page({ * @param int newsType 加载新闻类型 */ loadMoreNews: function (that, pageIndex, newsType) { + wx.showLoading({ + title: '加载中', + }) + var url = ''; if (newsType === 'recommended' || newsType === 'hot-week') { url = 'https://api.cnblogs.com/api/newsitems/@' + newsType + '?pageIndex=' + pageIndex + '&pageSize=' + pageSize; @@ -181,31 +191,33 @@ Page({ "Authorization": "Bearer " + getApp().accessToken }, success: function (res) { - pageIndex++; - if (newsType === 'recommended') { - that.setData({ - news_recommendeds: that.data.news_recommendeds.concat(res.data), - recommendedIndex: pageIndex - }); - } else if (newsType === 'hot') { - that.setData({ - news_hots: that.data.news_hots.concat(res.data), - hotIndex: pageIndex - }); - } else if (newsType === 'hot-week') { - that.setData({ - news_hots_week: that.data.news_hots_week.concat(res.data), - hotWeekIndex: pageIndex - }); + if (res.data.length > 0) { + pageIndex++; + if (newsType === 'recommended') { + that.setData({ + news_recommendeds: that.data.news_recommendeds.concat(res.data), + recommendedIndex: pageIndex + }); + } else if (newsType === 'hot') { + that.setData({ + news_hots: that.data.news_hots.concat(res.data), + hotIndex: pageIndex + }); + } else if (newsType === 'hot-week') { + that.setData({ + news_hots_week: that.data.news_hots_week.concat(res.data), + hotWeekIndex: pageIndex + }); + } } }, fail: function () { // fail }, complete: function () { - console.log('-------完成后---------'); wx.hideNavigationBarLoading(); wx.stopPullDownRefresh(); + wx.hideLoading(); } }); }, diff --git a/pages/news/index.js b/pages/news/index.js index e89c9ea..f1d631e 100644 --- a/pages/news/index.js +++ b/pages/news/index.js @@ -8,12 +8,28 @@ Page({ newsId: 0, blogApp: '', commentInput: '', + commentid: 0, pageIndex: 1, pageSize: 10, + user: [], + addStatus: true, }, onLoad: function (options) { - this.loadNews(options); - this.loadComments(options); + wx.showLoading({ + title: '加载中', + }) + + var that = this + that.setData({ + user: getApp().user + }) + + that.loadNews(options); + }, + + onReachBottom: function () { + //上拉加载分页评论数据 + this.loadComments(this.data.pageIndex); }, loadNews: function (options) { @@ -27,16 +43,20 @@ Page({ "Authorization": "Bearer " + getApp().accessToken }, success: function (res) { - if (res) { + if (res.data !== '') { that.setData({ news: res.data, newsId: options.newsId, -// blogApp: options.blogApp + blogApp: options.blogApp }) - WxParse.wxParse('news', 'html', that.data.news, that, 5); + WxParse.wxParse('news', 'html', that.data.news, that) + + //加载评论 + that.loadComments(that.data.pageIndex); } + }, fail: function () { // fail @@ -47,21 +67,26 @@ Page({ }) }, - loadComments: function (options) { - var that = this; + loadComments: function (pageIndex) { + wx.showLoading({ + title: '加载中', + }) + + var that = this wx.request({ - url: 'https://api.cnblogs.com/api/news/' + options.newsId + - '/comments?pageIndex=' + this.data.pageIndex + '&pageSize=' + this.data.pageSize, + url: 'https://api.cnblogs.com/api/news/' + that.data.newsId + + '/comments?pageIndex=' + pageIndex + '&pageSize=' + that.data.pageSize, method: 'GET', header: { "Content-Type": "application/json; charset=utf-8", "Authorization": "Bearer " + getApp().accessToken }, success: function (res) { - - if (res) { + if (res.data.length > 0) { + pageIndex++ that.setData({ - comments: res.data + comments: that.data.comments.concat(res.data), + pageIndex: pageIndex }) } }, @@ -69,30 +94,100 @@ Page({ // fail }, complete: function () { - // complete + wx.hideLoading() } }) }, - addComment: function (options) { - console.log(options); + //发布评论 + addComment: function () { wx.request({ - url: 'https://api.cnblogs.com/api/blogs/' + this.data.blogApp + '/posts/' + this.data.postId + '/comments', + url: 'https://api.cnblogs.com/api/news/' + this.data.newsId + '/comments', method: 'POST', data: { + "ParentId": 1, "content": this.data.commentInput }, header: { "Content-Type": "application/x-www-form-urlencoded", 'Authorization': 'Bearer ' + getApp().accessToken, }, + success: function (res) { + if (res.statusCode === 201) { + wx.showToast({title: "添加评论成功"}) + } else if (res.statusCode === 500) { + wx.showToast({title: res.data.Message}) + } + } + + }); + }, + + //修改评论 + editComment: function () { + var that = this + wx.request({ + url: 'https://api.cnblogs.com/api/newscomments/' + that.data.commentid, + method: 'PATCH', + data: { + "": this.data.commentInput + }, + header: { + "Content-Type": "application/x-www-form-urlencoded", + 'Authorization': 'Bearer ' + getApp().accessToken, + }, success: function (res) { console.log(res) + if (res.statusCode === 200) { + wx.showToast({title: "修改评论成功"}) + that.setData({ + commentInput: event.target.dataset.content, + addStatus: true + }) + } } + }); + }, + //删除评论 + deleteComment: function (event) { + var that = this + var commentId = event.target.dataset.commentid; + var commentsIndex = event.target.dataset.index; + wx.request({ + url: 'https://api.cnblogs.com/api/newscomments/' + commentId, + method: 'DELETE', + header: { + "Content-Type": "application/x-www-form-urlencoded", + 'Authorization': 'Bearer ' + getApp().accessToken, + }, + success: function (res) { + if (res.statusCode === 200) { + wx.showToast({title: "删除成功"}) + } else if (res.statusCode === 500) { + wx.showToast({title: res.data.ExceptionMessage}) + } + } }); }, - listenCommontInput: function (event) { + + editCommentStatus: function (event) { + this.setData({ + commentid: event.target.dataset.commentid, + commentInput: event.target.dataset.content, + addStatus: false + }) + }, + + cancelComment: function () { + this.setData({ + commentid: 0, + commentInput: '', + addStatus: true + }) + }, + + listenCommentInput: function (event) { this.data.commentInput = event.detail.value; }, onReady: function () { diff --git a/pages/news/index.wxml b/pages/news/index.wxml index 21a8b8f..5cc1eb8 100644 --- a/pages/news/index.wxml +++ b/pages/news/index.wxml @@ -3,18 +3,24 @@