摘要: 本實例將演示從零開發(fā)一個微信應用號的過程娩嚼,頁面輪播與跳轉(zhuǎn)傳值殷绍,實現(xiàn)單元格自定義布局冠摄,全部源碼可通過github下載梗摇。
下載最新版的微信小程序開發(fā)工具拓哟,目前是v0.9.092300
下載地址:https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/download.html
官方文檔:https://mp.weixin.qq.com/debug/wxadoc/dev/index.html
git下載地址:http://git.oschina.net/dotton/news
先看下效果圖:
一、新建應用
1.內(nèi)測階段對于無內(nèi)測號的開發(fā)者伶授,請點無AppId断序。
2.然后選擇一個本地目錄作為工程目錄。
3.項目名稱任意糜烹,設置好目錄逢倍,勾上當前目錄創(chuàng)建quick start項目。如圖:
4.點擊添加項目景图,這時可以運行的效果。是自己的微信個人信息以及一HelloWorld文本框碉哑。
5.右邊是調(diào)試窗口挚币,有2個警告亮蒋,是由于沒有AppID導致的,可以暫時忽略妆毕,不影響開發(fā)慎玖。
6.提示一下,在app.json中設置debug:true笛粘,這樣控制臺看到實時的交互信息趁怔,以及將來在js文件中設置斷點,類似與Chrome的調(diào)試工具以及Firefox的Firebug薪前。
關(guān)于首頁配置:
{
"pages":[
"pages/index/index",
"pages/logs/logs"
],
"window":{
"backgroundTextStyle":"light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "WeChat",
"navigationBarTextStyle":"black"
},
"debug":true
}
其中pages屬性表明每一個頁面的存在润努,其中第一條為首頁,即pages/index/index
二示括、請求網(wǎng)絡API接口
1.前提條件:
這里需要用到聚合數(shù)據(jù)的新聞接口铺浇,前往:https://www.juhe.cn/docs/api/id/235 注冊、申請接口垛膝,拿到key鳍侣,我這里已經(jīng)申請到一個key:482e213ca7520ff1a8ccbb262c90320a,可以直接拿它做測試吼拥,然后就可以將它集成到自己的應用了倚聚。
2.使用微信小程序接口來訪問網(wǎng)絡:
改寫index.js文件:
//index.js
//獲取應用實例
var app = getApp()
Page({
data: {
motto: 'Hello World',
userInfo: {}
},
//事件處理函數(shù)
bindViewTap: function() {
wx.navigateTo({
url: '../logs/logs'
})
},
onLoad: function () {
// 訪問聚合數(shù)據(jù)的網(wǎng)絡接口
wx.request({
url: 'http://v.juhe.cn/toutiao/index',
data: {
type: '' ,
key: '482e213ca7520ff1a8ccbb262c90320a'
},
header: {
'Content-Type': 'application/json'
},
success: function(res) {
console.log(res.data)
}
})
console.log('onLoad')
var that = this
//調(diào)用應用實例的方法獲取全局數(shù)據(jù)
app.getUserInfo(function(userInfo){
//更新數(shù)據(jù)
that.setData({
userInfo:userInfo
})
})
}
})
3.查看效果,檢查Console控制臺凿可,得到以下信息:
說明已經(jīng)成功取得到了數(shù)據(jù)惑折。
三、將json格式的數(shù)據(jù)渲染到視圖
這里要用到swipe組件實現(xiàn)大圖輪播矿酵,文檔見:https://mp.weixin.qq.com/debug/wxadoc/dev/component/swiper.html
1.清空原index.wxml內(nèi)容唬复,加入如下代碼:
<swiper indicator-dots="true"
autoplay="true" interval="5000" duration="1000">
<block wx:for="{{topNews}}">
<swiper-item>
<image src="{{item.thumbnail_pic_s02}}" class="slide-image" width="355" height="150"/>
</swiper-item>
</block>
</swiper>
2.相應地在index.js文件的onLoad方法中加入如下代碼來獲取網(wǎng)絡數(shù)據(jù)
//index.js
//獲取應用實例
var app = getApp()
Page({
data: {
topNews:[],
techNews:[]
},
onLoad: function () {
var that = this
// 訪問聚合數(shù)據(jù)的網(wǎng)絡接口-頭條新聞
wx.request({
url: 'http://v.juhe.cn/toutiao/index',
data: {
type: 'topNews' ,
key: '482e213ca7520ff1a8ccbb262c90320a'
},
header: {
'Content-Type': 'application/json'
},
success: function(res) {
if (res.data.error_code == 0) {
that.setData({
topNews:res.data.result.data
})
} else {
console.log('獲取失敗');
}
}
})
}
})
3.看到輪播已經(jīng)成功的展示出來了
4.依樣畫葫蘆,同樣操作讀取列表新聞:
<view class="news-list">
<block wx:for="{{techNews}}">
<text class="news-item">{{index + 1}}. {{item.title}}</text>
</block>
</view>
配合樣式表全肮,不然列表文字排版是橫向的敞咧,將以下css加到index.wxss中:
.news-list {
display: flex;
flex-direction: column;
padding: 40rpx;
}
.news-item {
margin: 10rpx;
}
繼續(xù)美化,文字列表也采用縮略圖+大標題+出處+日期的形式
樣式表與布局文件 index.wxss
/**index.wxss**/
.news-list {
display: flex;
flex-direction: column;
padding: 40rpx;
}
.news-item {
display: flex;
flex-direction: row;
height:200rpx;
}
.news-text {
display: flex;
flex-direction: column;
}
.news-stamp {
font-size: 25rpx;
color:darkgray;
padding: 0 20rpx;
display: flex;
flex-direction: row;
justify-content:space-between;
}
.news-title {
margin: 10rpx;
font-size: 30rpx;
}
.container {
height: 5000rpx;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
/*padding: 200rpx 0;*/
box-sizing: border-box;
}
.list-image {
width:150rpx;
height:100rpx;
}
index.wxml
<!--index.wxml-->
<swiper indicator-dots="true"
autoplay="true" interval="5000" duration="1000">
<block wx:for="{{topNews}}">
<swiper-item>
<image src="{{item.thumbnail_pic_s02}}" mode="aspectFill" class="slide-image" width="375" height="250"/>
</swiper-item>
</block>
</swiper>
<view class="container news-list">
<block wx:for="{{techNews}}">
<view class="news-item">
<image src="{{item.thumbnail_pic_s}}" mode="aspectFill" class="list-image"/>
<view class="news-text">
<text class="news-title">{{item.title}}</text>
<view class="news-stamp">
<text>{{item.author_name}}</text>
<text>{{item.date}}</text>
</view>
</view>
</view>
</block>
</view>
四辜腺、跳轉(zhuǎn)詳情頁與傳值
保存當前點擊的新聞條目信息中的title休建,參見官方文檔:https://mp.weixin.qq.com/debug/wxadoc/dev/framework/view/wxml/event.html
傳值到詳情頁
<!--logs.wxml-->
<view class="container">
<text class="news-title">{{title}}</text>
<text class="news-info">暫時找不到WebView的組件接口,于是不能加載網(wǎng)頁數(shù)據(jù)</text>
</view>
//事件處理函數(shù)
bindViewTap: function(event) {
wx.navigateTo({
url: '../detail/detail?title='+event.currentTarget.dataset.newsTitle
})
}
//index.js
//事件處理函數(shù)
bindViewTap: function(event) {
wx.navigateTo({
url: '../detail/detail?title='+event.currentTarget.dataset.newsTitle
})
}
<!--index.wxml-->
//加入data-xxx元素來傳值
<view class="container news-list">
<block wx:for="{{techNews}}">
<view class="news-item" data-news-title="{{item.title}}" bindtap="bindViewTap">
<image src="{{item.thumbnail_pic_s}}" mode="aspectFill" class="list-image"/>
<view class="news-text">
<text class="news-title">{{item.title}}</text>
<view class="news-stamp">
<text>{{item.author_name}}</text>
<text>{{item.date}}</text>
</view>
</view>
</view>
</block>
</view>
當然也可以通過獲取全局的變量的方式傳值评疗,這里場景是由一個頁面與子頁面是一對一傳值關(guān)系测砂,所以不推薦,可參考quickStart項目中微信個人信息的傳值方式來做百匆。 app.js末尾加上
globalData:{
userInfo:null,
newsItem:null
}
})
由于未在官方文檔中找到WebView的組件砌些,所以詳情的網(wǎng)頁正文暫時無法實現(xiàn)。
結(jié)語
整體開發(fā)過程還是比較舒適的,上手難度不高存璃,過程中用到一定的CSS語法仑荐,本質(zhì)上還是體現(xiàn)了一個H5開發(fā)模式,WXML實質(zhì)上一種模板標簽語言纵东。