From 781fc300b046d3c0b3675c42b518c933dde1607c Mon Sep 17 00:00:00 2001 From: ddl Date: Sat, 25 Mar 2017 16:44:22 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E9=A6=96=E9=A1=B5=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 52 ++++---- app.json | 4 +- pages/blogpost/body.js | 2 +- pages/home/index.js | 63 ---------- pages/index/index.js | 197 +++++++++++++++++++++++++++++++ pages/{home => index}/index.json | 0 pages/{home => index}/index.wxml | 0 pages/{home => index}/index.wxss | 0 8 files changed, 226 insertions(+), 92 deletions(-) delete mode 100644 pages/home/index.js create mode 100644 pages/index/index.js rename pages/{home => index}/index.json (100%) rename pages/{home => index}/index.wxml (100%) rename pages/{home => index}/index.wxss (100%) diff --git a/app.js b/app.js index 824a3c3..01fa131 100644 --- a/app.js +++ b/app.js @@ -1,31 +1,31 @@ //app.js App({ - onLaunch: function () { - //调用API从本地缓存中获取数据 - var logs = wx.getStorageSync('logs') || [] - logs.unshift(Date.now()) - wx.setStorageSync('logs', logs) - }, - getUserInfo:function(cb){ - var that = this - if(this.globalData.userInfo){ - typeof cb == "function" && cb(this.globalData.userInfo) - }else{ - //调用登录接口 - wx.login({ - success: function () { - wx.getUserInfo({ - success: function (res) { - that.globalData.userInfo = res.userInfo - typeof cb == "function" && cb(that.globalData.userInfo) - } - }) + onLaunch: function () { + //调用API从本地缓存中获取数据 + var logs = wx.getStorageSync('logs') || [] + logs.unshift(Date.now()) + wx.setStorageSync('logs', logs) + }, + getUserInfo: function (cb) { + var that = this + if (this.globalData.userInfo) { + typeof cb == "function" && cb(this.globalData.userInfo) + } else { + //调用登录接口 + wx.login({ + success: function () { + wx.getUserInfo({ + success: function (res) { + that.globalData.userInfo = res.userInfo + typeof cb == "function" && cb(that.globalData.userInfo) + } + }) + } + }) } - }) + }, + globalData: { + userInfo: null, + access_token: "PVwCiV57iDOoNuFA-o97H1cpi0c7t_LIsgglxdmfzqCm_q2kP0hwxpy87lnbe-TbojGnzFkTg24dnwh7RTScCv0KHNN9Lhwyb6j4b1sK8c_oNvol957CggFqw06Ux457k_gRy9JzJccS2YBe1S_8SIwWDMDOmcQdvtpb7LE04Ynx9MKqH7eXA2odFymIUOE7KP5eaKrjz6TIlfUNTnXa9Q" } - }, - globalData:{ - userInfo:null, - access_token: "exhmkuTZljDgEfb0LMRetdsF2pB-6cgNIbxLCY8cuvZzXTF5EkHg7dTAeNelrpytn2P_ZyQvd9OREJvinUB5lud_G0r2dMpFt2EVuXANk4q24W2fx4ZL4VI62Hw36iKP9WjnWUI3xKsk4K0IAslspScppE-kyLf1C8NcR1nzKcDrf1YNhi4cFWFa3HhkNcGyJISyxoZccr7vFF-T51TACg" - } }) \ No newline at end of file diff --git a/app.json b/app.json index e5e01b7..01b66a6 100644 --- a/app.json +++ b/app.json @@ -1,6 +1,6 @@ { "pages": [ - "pages/home/index", + "pages/index/index", "pages/question/index", "pages/user/login", "pages/user/index", @@ -17,7 +17,7 @@ }, "tabBar": { "list": [{ - "pagePath": "pages/home/index", + "pagePath": "pages/index/index", "text": "首页" }, { diff --git a/pages/blogpost/body.js b/pages/blogpost/body.js index f9eca8e..8a8f894 100644 --- a/pages/blogpost/body.js +++ b/pages/blogpost/body.js @@ -20,7 +20,7 @@ Page({ article: res.data }) WxParse.wxParse('article', 'html', that.data.article, that, 5); - console.log(res.data); +// console.log(res.data); }, fail: function() { // fail diff --git a/pages/home/index.js b/pages/home/index.js deleted file mode 100644 index 8ab4cfe..0000000 --- a/pages/home/index.js +++ /dev/null @@ -1,63 +0,0 @@ -//index.js -//获取应用实例 -var app = getApp() -Page({ - data: { - tabs: ["博文", "精华区", "推荐新闻", "热门新闻", "本周新闻"], - activeIndex: 0, - sliderOffset: 0, - sliderLeft: 0, - - //首页博文列表 - blog_articles: [], - //精华区博文列表 - blog_pickeds: [], - //推荐新闻 - news_recommendeds: [], - //热门新闻 - news_hots: [], - //本周新闻 - news_hots_week: [] - }, - onLoad: function () { - var that = this; - - //首页博文列表 - wx.request({ - url: 'https://api.cnblogs.com/api/blogposts/@sitehome?pageIndex=1&pageSize=10', - method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT - header: { - "Content-Type":"application/x-www-form-urlencoded", - "Authorization":"Bearer " + app.globalData.access_token, - }, - success: function(res){ - that.setData({ - blog_articles: res.data - }) - }, - fail: function() { - // fail - }, - complete: function() { - // complete - } - }) - - }, - - onPullDownRefresh: function() { - console.log('下拉') - console.log(this.data.activeIndex); - }, - - onReachBottom: function() { - console.log('刷新') - }, - //tabs - tabClick: function (e) { - this.setData({ - sliderOffset: e.currentTarget.offsetLeft, - activeIndex: e.currentTarget.id - }); - } -}) diff --git a/pages/index/index.js b/pages/index/index.js new file mode 100644 index 0000000..8d15588 --- /dev/null +++ b/pages/index/index.js @@ -0,0 +1,197 @@ +//index.js +var pageSize = 10; +var postType = 'sitehome'; +var newsType = 'recommended'; + +//获取应用实例 +var app = getApp() +Page({ + data: { + tabs: ["博文", "精华区", "推荐新闻", "热门新闻", "本周新闻"], + activeIndex: 0, + sliderOffset: 0, + sliderLeft: 0, + + siteHomeIndex: 1, + pickedIndex: 1, + recommendedIndex: 1, + hotIndex: 1, + hot_weekIndex: 1, + + //首页博文列表 + blog_articles: [], + //精华区博文列表 + blog_pickeds: [], + //推荐新闻 + news_recommendeds: [], + //热门新闻 + news_hots: [], + //本周热门新闻 + news_hots_week: [] + }, + onLoad: function () { + //默认加载首页文章列表 + this.loadMoreBlogPosts(this, this.data.siteHomeIndex, postType); + }, + + onPullDownRefresh: function () { + console.log('--------下拉刷新-------') + wx.showNavigationBarLoading() + this.changeTab(this.data.activeIndex, 'pull'); + }, + + onReachBottom: function () { + console.log('--------上拉刷新-------') + this.changeTab(this.data.activeIndex, 'reach'); + }, + + //tabs + tabClick: function (e) { + this.setData({ + sliderOffset: e.currentTarget.offsetLeft, + activeIndex: e.currentTarget.id + }); + + this.changeTab(this.data.activeIndex); + }, + + //切换tab 加载相应tab绑定的类别数据 + changeTab: function (tabIndex, action) { + switch (parseInt(tabIndex)) { + case 0: + if (action === 'pull') { + this.setData({ + siteHomeIndex: 1, + }); + } + postType = 'sitehome'; //首页博文列表 + this.loadMoreBlogPosts(this, this.data.siteHomeIndex, postType); + break; + case 1: + if (action === 'pull') { + this.setData({ + pickedIndex: 1, + }); + } + postType = 'picked'; //精华区博文列表 + this.loadMoreBlogPosts(this, this.data.pickedIndex, postType); + break; + case 2: + if (action === 'pull') { + this.setData({ + recommendedIndex: 1, + }); + } + newsType = 'recommended';//推荐新闻 + this.loadMoreNews(this, this.data.recommendedIndex, newsType); + break; + case 3: + if (action === 'pull') { + this.setData({ + hotIndex: 1, + }); + } + newsType = 'hot';//热门新闻 + this.loadMoreNews(this, this.data.hotIndex, newsType); + break; + case 4: + if (action === 'pull') { + this.setData({ + hot_weekIndex: 1, + }); + } + newsType = 'hot-week';//本周热门新闻 + this.loadMoreNews(this, this.data.hot_weekIndex, newsType); + break; + default: + console.log(this.data.siteHomeIndex, 'siteHomeIndex'); + console.log(this.data.pickedIndex, 'pickedIndex'); + console.log(this.data.recommendedIndex, 'recommendedIndex'); + console.log(this.data.hotIndex, 'hotIndex'); + console.log(this.data.hot_weekIndex, 'hot_weekIndex'); + break; + } + }, + + //加载博文列表 + loadMoreBlogPosts: function (that, pageIndex, postType) { + wx.request({ + url: 'https://api.cnblogs.com/api/blogposts/@' + postType + '?pageIndex=' + pageIndex + '&pageSize=' + pageSize, + method: 'GET', + header: { + "Content-Type": "application/json; charset=utf-8", + "Authorization": "Bearer " + app.globalData.access_token, + }, + success: function (res) { + pageIndex++; + if (postType === 'sitehome') { + that.setData({ + blog_articles: res.data, + siteHomeIndex: pageIndex + }); + + } else if (postType === 'picked') { + that.setData({ + blog_pickeds: res.data, + pickedIndex: pageIndex + }); + + } + }, + fail: function () { + // fail + }, + complete: function () { + console.log('-------完成后---------'); + wx.hideNavigationBarLoading(); + wx.stopPullDownRefresh(); + } + }); + }, + + //加载新闻列表 + loadMoreNews: function (that, pageIndex, newsType) { + var url = ''; + if (newsType === 'recommended' || newsType === 'hot-week') { + url = 'https://api.cnblogs.com/api/newsitems/@' + newsType + '?pageIndex=' + pageIndex + '&pageSize=' + pageSize; + } else { + url = 'https://api.cnblogs.com/api/newsitems/@' + newsType + '?startDate=2017-03-24&endDate=2017-03-25&pageIndex=' + pageIndex + '&pageSize=' + pageSize; + } + + wx.request({ + url: url, + method: 'GET', + header: { + "Content-Type": "application/json; charset=utf-8", + "Authorization": "Bearer " + app.globalData.access_token, + }, + success: function (res) { + pageIndex++; + if (newsType === 'recommended') { + that.setData({ + news_recommendeds: res.data, + recommendedIndex: pageIndex + }); + } else if (newsType === 'hot') { + that.setData({ + news_hots: res.data, + hotIndex: pageIndex + }); + } else if (newsType === 'hot-week') { + that.setData({ + news_hots_week: res.data, + hot_weekIndex: pageIndex + }); + } + }, + fail: function () { + // fail + }, + complete: function () { + console.log('-------完成后---------'); + wx.hideNavigationBarLoading(); + wx.stopPullDownRefresh(); + } + }); + }, +}) diff --git a/pages/home/index.json b/pages/index/index.json similarity index 100% rename from pages/home/index.json rename to pages/index/index.json diff --git a/pages/home/index.wxml b/pages/index/index.wxml similarity index 100% rename from pages/home/index.wxml rename to pages/index/index.wxml diff --git a/pages/home/index.wxss b/pages/index/index.wxss similarity index 100% rename from pages/home/index.wxss rename to pages/index/index.wxss -- Gitee From bbb8d81d58f0db4ecfaf97376b77612189333b1c Mon Sep 17 00:00:00 2001 From: ddl Date: Sat, 25 Mar 2017 17:36:33 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=8D=9A=E6=96=87=E8=AF=84=E8=AE=BA?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/blogpost/body.js | 117 ++++++++++++++++++++++++++------------- pages/blogpost/body.wxml | 2 +- pages/index/index.js | 10 ++-- pages/index/index.wxml | 2 +- 4 files changed, 85 insertions(+), 46 deletions(-) diff --git a/pages/blogpost/body.js b/pages/blogpost/body.js index 8a8f894..020bae4 100644 --- a/pages/blogpost/body.js +++ b/pages/blogpost/body.js @@ -2,44 +2,83 @@ var WxParse = require('../../vendor/wxParse/wxParse.js') var app = getApp() Page({ - data:{ - article: "" - }, - onLoad:function(options){ - // 页面初始化 options为页面跳转所带来的参数 - var that = this; - wx.request({ - url: 'https://api.cnblogs.com/api/blogposts/'+ options.post_id +'/body', - method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT - header: { - "Content-Type":"application/x-www-form-urlencoded", - "Authorization":"Bearer " + app.globalData.access_token, - }, - success: function(res){ - that.setData({ - article: res.data + data: { + article: '', + comments: [], + pageIndex: 1, + pageSize: 10, + }, + onLoad: function (options) { + this.loadArticle(options); + }, + + loadArticle: function (options) { + // 页面初始化 options为页面跳转所带来的参数 + var that = this; + wx.request({ + url: 'https://api.cnblogs.com/api/blogposts/' + options.postId + '/body', + method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT + header: { + "Content-Type": "application/json; charset=utf-8", + "Authorization": "Bearer " + app.globalData.access_token, + }, + success: function (res) { + if (res) { + that.setData({ + article: res.data + }) + + WxParse.wxParse('article', 'html', that.data.article, that, 5); + } + + }, + fail: function () { + // fail + }, + complete: function () { + // complete + } }) - WxParse.wxParse('article', 'html', that.data.article, that, 5); -// console.log(res.data); - }, - fail: function() { - // fail - }, - complete: function() { - // complete - } - }) - }, - onReady:function(){ - // 页面渲染完成 - }, - onShow:function(){ - // 页面显示 - }, - onHide:function(){ - // 页面隐藏 - }, - onUnload:function(){ - // 页面关闭 - } + }, + + loadComments: function (options) { + var that = this; + wx.request({ + url: 'https://api.cnblogs.com/api/blogs/' + options.blogApp + '/posts/' + + options.postId + '/comments?pageIndex=' + this.data.pageSize + '&pageSize=' + this.data.pageSize, + method: 'GET', + header: { + "Content-Type": "application/json; charset=utf-8", + "Authorization": "Bearer " + app.globalData.access_token, + }, + success: function (res) { + if (res) { + that.setData({ + comments: res.data + }) + + WxParse.wxParse('article', 'html', that.data.article, that, 5); + } + + }, + fail: function () { + // fail + }, + complete: function () { + // complete + } + }) + }, + onReady: function () { + // 页面渲染完成 + }, + onShow: function () { + // 页面显示 + }, + onHide: function () { + // 页面隐藏 + }, + onUnload: function () { + // 页面关闭 + } }) \ No newline at end of file diff --git a/pages/blogpost/body.wxml b/pages/blogpost/body.wxml index 7a81362..73f74b5 100644 --- a/pages/blogpost/body.wxml +++ b/pages/blogpost/body.wxml @@ -1,3 +1,3 @@ -