多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
**重点看本例index.js代码** **页面目录结构如下:** ![](https://box.kancloud.cn/a6b46678b678946f6527452bbada7505_312x618.png) **页面渲染效果如下:** ![](https://box.kancloud.cn/a31f5393612dfc4f5615f3049b028790_401x713.png) ## 1.模板页template编写 ### 1.1template-star ~~~ <template name="templateStar"> <view class="images"> <block wx:for="{{stars}}" wx:key="index"> <image src="{{item?'/images/icon/star.png':'/images/icon/none-star.png'}}"></image> </block> </view> </template> ~~~ ~~~ .images>image{ width: 20rpx; height: 20rpx; } .images{ height: 60rpx; } ~~~ ### 2.1template-movies-item ~~~ <import src="../star/template-star"></import> <template name="templateMoviesItem"> <view class="movie-item"> <image class="mitem" src="{{imgUrl}}"></image> <title>{{title}}</title> <view class="average"> <template is="templateStar" data="{{stars}}"></template> <text>{{average}}</text> </view> </view> </template> ~~~ ~~~ @import "../star/template-star.wxss"; .mitem{ width: 220rpx; height: 280rpx; } .movie-item{ display: flex; flex-direction: column; } .movie-item>title{ font-size: 31rpx; margin-top: 15rpx; } .average{ display: flex; align-items: center; } .average>text{ margin-left: 12rpx; font-size: 25rpx; } ~~~ ### 1.3template-movies-detail ~~~ <import src="../movies-item/template-movies-item"></import> <template name="templateMoviesDetail"> <view class="head"> <text>{{title}}</text> <text class="more">更多> </text> </view> <view class="tmovie-itm"> <block wx:for="{{movies}}" wx:key="index"> <template is="templateMoviesItem" data="{{...item}}"></template> </block> </view> <view class="line"></view> </template> ~~~ ~~~ @import "../movies-item/template-movies-item.wxss"; .head{ display: flex; justify-content: space-between; padding: 25rpx 20rpx; font-size: 34rpx; } .head .more{ color: aqua; } .tmovie-itm{ display: flex; justify-content: space-around; } ~~~ ## 2.自定义方法util编写 ~~~ function http(url, callback, type) { wx.request({ url, // header: {}, // 设置请求的 header header: { 'Content-Type': 'json' }, success: function (res) { // success callback(res, type); } }) } function star(stars) { var value = stars.slice(0, 1); var arr = []; for (let i = 0; i < 5; i++) { if (i < value) { arr.push(1); } else { arr.push(0); } } return arr; } module.exports = { http: http, star:star } ~~~ ## 主页面index编写 ~~~ //index.js //获取应用实例 const app = getApp(); const douban = "http://douban.uieee.com/v2/movie/"; import utils from "../../utils/util"; const http = utils.http; const star = utils.star; Page({ data:{ in_theaters:{} }, onLoad() { var inTheatersurl = douban + "in_theaters" + "?start=0&count=3"; var comingSoonurl = douban + "coming_soon" + "?start=0&count=3"; var top250url = douban + "top250" + "?start=0&count=3"; http(inTheatersurl,this.handleData,"in_theaters"); http(comingSoonurl,this.handleData,"coming_soon"); http(top250url,this.handleData,"top250"); }, handleData(res,type) { var subjects = res.data.subjects; var title=res.data.title; var movies = []; subjects.forEach(ele => { var temp = { imgUrl: ele.images.small, title: ele.title, stars: star(ele.rating.stars), average: ele.rating.average, } movies.push(temp) }); var data={}; /* js给对象赋值属性方式:对象名[属性名]={} */ data[type]={ movies, type, title } /* console.log(data) console.log(data.in_theaters) */ /* this.setData({ 数组 }) */ /* this.setData({ 属性 }) */ /* this.setData({ 对象 }) */ /* this.setData(对象内容) */ this.setData( data ) } }) ~~~ ~~~ <!-- "pages/index/index.js" --> <import src="../../template/movies-detail/template-movies-detail"></import> <template is="templateMoviesDetail" data="{{...in_theaters}}"></template> <template is="templateMoviesDetail" data="{{...coming_soon}}"></template> <template is="templateMoviesDetail" data="{{...top250}}"></template> ~~~ ~~~ @import "../../template/movies-detail/template-movies-detail"; ~~~