程序思路:

  • 用微信自带组件swiper来实现轮播图
  • 用豆瓣提供的api(这里使用的电影api)来获取最近的电影数据【豆瓣api地址】
获取数据用微信的request方法,只需要提供豆瓣api的url链接,就能够get到数据
  • 用setData()方法来将数据存进对应的page里面,在视图层(html)用wx:for来进行列表渲染
  • 在渲染过程中加一个加载提示框(微信的showToast,API),等到数据请求并渲染完成后,结束提示框

1.app.js   获取用户登录状态并获取用户信息

//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)
}
})
}
})
}
},
globalData:{
userInfo:null
}
})

2.app.json

{
"pages":[
"pages/index/index",
"pages/logs/logs"
],
"window":{
"backgroundTextStyle":"light",
"color": "#fff",
"navigationBarBackgroundColor": "#000",
"navigationBarTitleText": "豆瓣",
"navigationBarTextStyle":"#fff"
},
"tabBar": {
"color": "#888",
"selectedColor": "#09bb07",
"backgroundColor": "#000",
"list": [{
"pagePath": "pages/index/index",
"text": "观看电影",
"iconPath": "icon/1.png",
"selectedIconPath": "icon/1.png"
},{
"pagePath": "pages/index/index",
"text": "当前热映",
"iconPath": "icon/2.png",
"selectedIconPath": "icon/2.png"
}]
}
}

3.app.wxss

/**app.wxss**/
.container {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
padding: 200rpx 0;
box-sizing: border-box;
}

4.until.js

function formatTime(date) {
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate() var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds() return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
//获取对象类型
function formatNumber(n) {
n = n.toString()
return n[1] ? n : '0' + n
} module.exports = {
formatTime: formatTime
}

5.index.wxml

<!--index.wxml-->
<view class="content">
<swiper indicator-dots="{{indicatorDots}}" autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}">
<block wx:for="{{imgUrls}}">
<swiper-item>
<image src="{{item}}" class="slide-image" width="355" height="150" />
</swiper-item>
</block>
</swiper>
<block wx:for="{{movie}}" wx:key="*this">
<view class="movie">
<view class="pic">
<image src="{{item.images.medium}}" mode="aspectFill"></image>
</view>
<view class="movie-info">
<view class="base-info">
<text>电影名字:{{item.title}}\n 导演:{{item.directors[0].name}}\n 演员:
<text wx:for="{{item.casts}}">{{item.name}} </text>
</text>
</view>
</view>
</view>
</block>
</view>

6.index.wxss

/**index.wxss**/
page {
height: 100%;
}
.content {
background-color: #3a3a3a;
min-height: 100%;
}
swiper-item image {
width: 100%;
}
.movie {
padding-top: 5px;
padding-bottom: 5px;
display: flex;
border-bottom: 1px solid #888;
}
.pic image {
width: 100px;
height: 150px;
vertical-align: top;
}
.movie-info {
padding-left: 20px;
}
.base-info {
color: #fff;
font-size: 12px;
padding-top: 20px;
line-height: 20px;
}

7.index.js

//index.js
//获取应用实例
var app = getApp()
Page({
data: {
imgUrls: [],
indicatorDots: false,
autoplay: true,
interval: 5000,
duration: 1000,
movie: null
},
//事件处理函数
bindViewTap: function () {
},
onLoad: function () {
this.loadMovie();
},
loadMovie() {
wx.showToast({
title: '正在加载',
icon: 'loading',
duration: 10000
});
let thispage = this;
wx.request({
url: 'http://api.douban.com/v2/movie/in_theaters',
method: 'GET',
header: { 'content-type': 'json' },
success: function (res) {
let subject = res.data.subjects;
thispage.setData({ movie: subject });
thispage.setData({
imgUrls: [
res.data.subjects[0].images.large,
res.data.subjects[1].images.large,
res.data.subjects[2].images.large
]
});
wx.hideToast();
}
});
}
})

8.logs.wxml

<!--logs.wxml-->
<view class="container log-list">
<block wx:for="{{logs}}" wx:for-item="log" wx:key="*this">
<text class="log-item">{{index + 1}}. {{log}}</text>
</block>
</view>

9.logs.wxss

.log-list {
display: flex;
flex-direction: column;
padding: 40rpx;
}
.log-item {
margin: 10rpx;
}

10.logs.json

{
"navigationBarTitleText": "查看启动日志"
}

11.logs.js

//logs.js
var util = require('../../utils/util.js')
Page({
data: {
logs: []
},
onLoad: function () {
this.setData({
logs: (wx.getStorageSync('logs') || []).map(function (log) {
return util.formatTime(new Date(log))
})
})
}
})

如何用微信小程序模仿豆瓣首页的更多相关文章

  1. 微信小程序访问豆瓣api403问题解决方发法

    微信小程序访问豆瓣api403问题解决方法一览:通过豆瓣api可以获取很多电影.书籍等的数据信息.昨晚上用微信小程序请求豆瓣api,竟然被豆瓣拒绝了.(豆瓣设置了小程序的访问权限):下面就跟着小编一起 ...

  2. 图解微信小程序---scroll_view实现首页排行榜布局

    图解微信小程序---scroll_view实现首页排行榜布局 什么是scroll-view? 滚动视图可滚动视图区域.使用竖向滚动时,需要给scroll-view一个固定高度,通过 WXSS 设置 h ...

  3. 微信小程序demo豆瓣图书

    最近微信小程序被炒得很火热,本人也抱着试一试的态度下载了微信web开发者工具,开发工具比较简洁,功能相对比较少,个性化设置也没有.了解完开发工具之后,顺便看了一下小程序的官方开发文档,大概了解了小程序 ...

  4. 微信小程序访问豆瓣电影api400错误解决方法

    最近在跟着demo学习微信小程序,却卡在了第一步请求豆瓣电影api上,折腾了很久,代码如下: wx.request({ url : "https://api.douban.com/v2/mo ...

  5. 微信小程序 | 模仿百思不得其姐

    微信小程序 仿百思不得姐 设备 微信开发者工具 v1.02.1901230 扩展 修复了视频点击播放不流畅的问题 修复了视频的暂停够无法播放问题 优化了部分页面 接口 首页 http://api.bu ...

  6. 微信小程序 —— 仿制豆瓣(一)

    先预览一下效果 欢迎扫码查看 码云地址:https://gitee.com/mk_23/little_chen_xu.git 预览完成,首先进入app.json文件中配置参数,主要就是配置我们要用的页 ...

  7. 微信小程序之豆瓣电影

    此文是学习小程序第二天做出的一个小demo,调用了豆瓣电影的api,但是需要填上自己appId,现在项目的 目录如下图: 效果图如下: 在这个demo里面,我更改了小程序的navigationBar, ...

  8. 微信小程序访问豆瓣api报403错误解决方法

    通过豆瓣API可以获取很多电影.书籍的数据信息,今天在调用豆瓣正在上映电影接口的时候报403错误,原因是豆瓣设置了小程序的访问权限.如下: 解决方法是使用代理,将豆瓣API地址换成 https://d ...

  9. 如何用微信小程序,每天给自己赚个鸡腿?

    假期如果实在无聊的话,那跟随田同学的脚步上架一个小程序吧. 话说:谁不想拥有一个自己的小程序呢?既可以赚点小钱又可以长长见识. 不懂小程序的小白能不能做出来呢?那来对了,这个教程就是针对小白的. 今天 ...

随机推荐

  1. Java如何显示不同语言的时间?

    在Java中,如何显示不同语言的时间? 此示例使用DateFormat类以中文语言显示时间. package com.yiibai; import java.text.DateFormat; impo ...

  2. C# ?? 运算符是什么?

    ?? 运算符定义在将可空类型分配给非可空类型时返回的默认值. int? c = null; //若 c 为 null,则 d 为 -1,否则把 c 值赋予 dint d = c ?? -1;

  3. Python property,属性

    參考资料 http://www.ibm.com/developerworks/library/os-pythondescriptors/ 顾名思义,property用于生成一个属性.通过操作这个属性. ...

  4. SecureCRT同时发送命令到所有主机

    有时候我们需要在多台服务器上执行相同的命令,比如安装软件,复制,粘贴,删除等等,但一台一台的去操作工作量就太大了,我们可以借助SecureCRT这款客户端远程连接工具实现这样的要求! 相关阅读: 如何 ...

  5. Mysql:This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME 错误解决

    From: http://blog.chinaunix.net/uid-22414998-id-2945656.html This version of MySQL doesn’t yet suppo ...

  6. A:LinkedList实现了List接口; B: AbstractSet实现了Set接口; C: HashSet继承自AbstractSet基类; D: WeakMap继承自 AbstractMap

    List,Set,Map在java.util包下都是接口 List有两个实现类:ArrayList和LinkedListSet有两个实现类:HashSet和LinkedHashSetAbstractS ...

  7. java mysql 链接高版本出现SSL验证

    key1: String url="jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8 ...

  8. iOS 多线程简单使用的具体解释

    主线程 一个iOS程序执行后.默认会开启1条线程,称为"主线程"或"UI线程"(刷新UI界面最好在主线程中做.在子线程中可能会出现莫名其妙的BUG) 主线程的作 ...

  9. eclipse .setting下各文件详解

    Eclipse项目中系统文件介绍 一. 写在前面 文章较长,可以直接到感兴趣的段落,或者直接关键字搜索: 请原谅作者掌握的编程语言少,这里只研究Java相关的项目: 每一个文件仅仅做一个常见内容的简单 ...

  10. Android中利用C++处理Bitmap对象

    相信有些Android&图像算法开发者和我一样,遇到过这样的状况:要对Bitmap对象做一些密集计算(例如逐像素的滤波),但是在java层写循环代码来逐像素操作明显是不现实的,因为Java代码 ...