首先感谢cnode社区提供的api,本次实现了简单的cnode社区应用号制做。 实现了数据的读取、展现, 实现了简单的布局, 实现了下一页功能。
html
下面就说说我作这个的过程,不足之处,请多多指教,只愿为进步。node
首先,在官网下载工具,下载地址 个人是选择mac版本0.9.092300。react
而后跟着官方的简版教程 建立一个项目。jquery
默认的项目里已经没有关于tabBar的配置信息,因此为了学习,我把这个配置进行了修改。git
首先关于配置的说明一样来自于官方github
注意:官方的代码有些字段是不同的,当心被坑。web
{ "pages":[ "pages/index/index", "pages/logs/logs" ], "window":{ "backgroundTextStyle":"light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "WeChat", "navigationBarTextStyle":"black" }, "tabBar":{ "list": [{ "pagePath": "pages/index/index", "text": "首页" }, { "pagePath": "pages/logs/logs", "text": "日志" }] } }
增长了tabBar, 查看调试 看界面是如此的简陋,为此针对tabBar参考官方说明进行了简单的美化。小程序
"tabBar":{ "color":"#272636", "selectedColor":"#80bd01", "backgroundColor":"#fff", "borderStyle":"white", "list":[{ "pagePath":"pages/index/index", "text":"首页", "iconPath":"images/tabBar/my.png", "selectedIconPath":"images/tabBar/my_hl.png" },{ "pagePath":"pages/index/index", "text":"个人", "iconPath":"images/tabBar/list.png", "selectedIconPath":"images/tabBar/list_hl.png" }] }
效果如图 最后根据文档,对默认页面的窗口表现进行了修改api
"window":{ "backgroundTextStyle":"black", "backgroundColor":"#fff", "navigationBarBackgroundColor":"#000", "navigationBarTitleText":"CNODE 应用号版", "navigationBarTextStyle":"white", "enablePullDownRefresh":"true" },
效果如图 总体配置文件为数组
{ "pages":[ "pages/index/index", "pages/logs/logs" ], "window":{ "backgroundTextStyle":"black", "backgroundColor":"#fff", "navigationBarBackgroundColor":"#000", "navigationBarTitleText":"CNODE 应用号版", "navigationBarTextStyle":"white", "enablePullDownRefresh":"true" }, "tabBar":{ "color":"#272636", "selectedColor":"#80bd01", "backgroundColor":"#fff", "borderStyle":"white", "list":[{ "pagePath":"pages/index/index", "text":"首页", "iconPath":"images/tabBar/my.png", "selectedIconPath":"images/tabBar/my_hl.png" },{ "pagePath":"pages/index/index", "text":"个人", "iconPath":"images/tabBar/list.png", "selectedIconPath":"images/tabBar/list_hl.png" }] } }
根据官方要求,我在pages文件夹内建立了topics文件夹,并建立了对应了 topics.js、topics.wxml、topics.wxss 三个文件。
首先在配置文件里注册这个topics,
"pages":[ "pages/topics/topics", "pages/index/index", "pages/logs/logs" ],
而且制定tabBar点击跳到对应的topics页面
"tabBar":{ "color":"#272636", "selectedColor":"#80bd01", "backgroundColor":"#fff", "borderStyle":"white", "list":[{ "pagePath":"pages/topics/topics", "text":"首页", "iconPath":"images/tabBar/my.png", "selectedIconPath":"images/tabBar/my_hl.png" },{ "pagePath":"pages/index/index", "text":"个人", "iconPath":"images/tabBar/list.png", "selectedIconPath":"images/tabBar/list_hl.png" }] }
"pages/topics/topics"
放到了"pages/index/index"
的前面而后编写topics.js
Page({ data: { title: '首页列表' }, onLoad: function () { console.log('onLoad by topics'); } });
以及topics.wxml文件
<view class="topics-main"> 测试首页列表界面 </view>
和topics.wxss文件
.topics-main { background: #f60; height: 100%; }
最后效果如图
根据文档请求数据,在util文件夹内建立一个api.js文件,专门进行数据请求处理。
'use strict'; var HOST_URI = 'https://cnodejs.org/api/v1'; var GET_TOPICS = '/topics'; var GET_TOPIC_BY_ID = '/topic/'; function obj2uri (obj) { return Object.keys(obj).map(function (k) { return encodeURIComponent(k) + '=' + encodeURIComponent(obj[k]); }).join('&'); } module.exports = { // 获取列表数据 getTopics: function (obj) { return HOST_URI + GET_TOPICS + '?' + obj2uri(obj); }, // 获取内容页数据 getTopicByID: function (id, obj) { return HOST_URI + GET_TOPIC_BY_ID + id + '?' + obj2uri(obj); } };
修改topics.js
var Api = require('../../utils/api.js'); Page({ data: { title: '首页列表' }, onLoad: function () { console.log('onLoad by topics'); this.fetchData();// 获取数据 }, fetchData: function (data) { // 处理参数 if (!data) data = {}; if (!data.page) data.page = 1; wx.request({ url: Api.getTopics(data), success: function (res) { console.log(res); } }); } });
效果如图 成功拿到了数据。
拿到了数据,也能修改界面,那么就直接完善这个首页吧
代码就不放了,直接上图 我认为这里值得说的大概只有loading、循环、传参、下一页和页面跳转了。
<loading hidden="{{hidden}}"> 加载中... </loading>
在topics.wxml中写官方提供的loading组件,根据在topics.js中对hidden值的修改,来触发loading。
文档提供了列表渲染
经过wx:for
实现列表的渲染。
<block wx:for="{{postsList}}"> <view class="posts-item" index="{{index}}" id="{{item.id}}" catchtap="redictDetail"> <view class="author"> <image class="author-avatar" src="{{item.author.avatar_url}}"></image> <view class="author-name">{{item.author.loginname}}</view> <view class="posts-tag hot" wx:if="{{item.top === true}}">置顶</view> <view class="posts-tag" wx:if="{{item.good === true}}">精华</view> <view class="posts-last-reply">{{item.last_reply_at}}</view> </view> <view class="posts-title">{{item.title}}</view> <view class="bar-info"> <view class="bar-info-item"> <image class="bar-info-item-icon" src="/images/icon/reply.png"></image> <view class="bar-info-item-number">{{item.reply_count}}</view> </view> <view class="bar-info-item"> <image class="bar-info-item-icon" src="/images/icon/visit.png"></image> <view class="bar-info-item-number">{{item.visit_count}}</view> </view> </view> </view> </block>
附上一个没有样式的列表展示
根据cnode的api能够知道经过tab不一样的值,得到到不一样标签下的内容列表。
因此 在页面的最上面 tab 栏中
<view class="top-bar"> <view class="top-bar-item" id="all" catchtap="onTapTag">所有</view> <view class="top-bar-item" id="good" catchtap="onTapTag">精华</view> <view class="top-bar-item" id="share" catchtap="onTapTag">分享</view> <view class="top-bar-item" id="ask" catchtap="onTapTag">问答</view> <view class="top-bar-item" id="job" catchtap="onTapTag">招聘</view> </view>
将id进行定义,经过获取id拿到对应的tab类型。
其中catchtap
是事件绑定。
bind事件绑定不会阻止冒泡事件向上冒泡,catch事件绑定能够阻止冒泡事件向上冒泡。
在topics.js获取
onTapTag: function (e) { var self = this; var tab = e.currentTarget.id; // 这里就能获取到不一样的tab值了 self.setData({ tab: tab }); if (tab !== 'all') { this.fetchData({tab: tab}); } else { this.fetchData(); } },
根据文档,组件的视图容器中有scroll-view这个可滚动视图区域。
<scroll-view class="posts-list" style="height:100%" scroll-y="true" bindscrolltolower="lower"> <block wx:for="{{postsList}}"> ... </block> </scroll-view>
topics.js文件
lower: function (e) { var self = this; // 修改当前页码 self.setData({ page: self.data.page + 1 }); // 判断当前页的tab值 进行请求数据 if (self.data.tab !== 'all') { this.fetchData({tab: self.data.tab, page: self.data.page}); } else { this.fetchData({page: self.data.page}); } }
在posts-item
中已经进行了事件绑定。利用wx.navigateTo
实现页面的跳转。
redictDetail: function (e) { console.log('我要看详情'); var id = e.currentTarget.id, url = '../detail/detail?id=' + id; // 这里的detail是须要建立对应的文件,以及页面注册的 wx.navigateTo({ url: url }) },
一样的原理,建立detail文件,并注册,获取数据,并美化页面。
放上个人github地址 https://github.com/coolfishstudio/wechat-webapp-cnode
最后感谢:cnode社区和博卡君
附上 博卡君的教程