如何用微信小程序模仿豆瓣首页
程序思路:
- 用微信自带组件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))
})
})
}
})
如何用微信小程序模仿豆瓣首页的更多相关文章
- 微信小程序访问豆瓣api403问题解决方发法
微信小程序访问豆瓣api403问题解决方法一览:通过豆瓣api可以获取很多电影.书籍等的数据信息.昨晚上用微信小程序请求豆瓣api,竟然被豆瓣拒绝了.(豆瓣设置了小程序的访问权限):下面就跟着小编一起 ...
- 图解微信小程序---scroll_view实现首页排行榜布局
图解微信小程序---scroll_view实现首页排行榜布局 什么是scroll-view? 滚动视图可滚动视图区域.使用竖向滚动时,需要给scroll-view一个固定高度,通过 WXSS 设置 h ...
- 微信小程序demo豆瓣图书
最近微信小程序被炒得很火热,本人也抱着试一试的态度下载了微信web开发者工具,开发工具比较简洁,功能相对比较少,个性化设置也没有.了解完开发工具之后,顺便看了一下小程序的官方开发文档,大概了解了小程序 ...
- 微信小程序访问豆瓣电影api400错误解决方法
最近在跟着demo学习微信小程序,却卡在了第一步请求豆瓣电影api上,折腾了很久,代码如下: wx.request({ url : "https://api.douban.com/v2/mo ...
- 微信小程序 | 模仿百思不得其姐
微信小程序 仿百思不得姐 设备 微信开发者工具 v1.02.1901230 扩展 修复了视频点击播放不流畅的问题 修复了视频的暂停够无法播放问题 优化了部分页面 接口 首页 http://api.bu ...
- 微信小程序 —— 仿制豆瓣(一)
先预览一下效果 欢迎扫码查看 码云地址:https://gitee.com/mk_23/little_chen_xu.git 预览完成,首先进入app.json文件中配置参数,主要就是配置我们要用的页 ...
- 微信小程序之豆瓣电影
此文是学习小程序第二天做出的一个小demo,调用了豆瓣电影的api,但是需要填上自己appId,现在项目的 目录如下图: 效果图如下: 在这个demo里面,我更改了小程序的navigationBar, ...
- 微信小程序访问豆瓣api报403错误解决方法
通过豆瓣API可以获取很多电影.书籍的数据信息,今天在调用豆瓣正在上映电影接口的时候报403错误,原因是豆瓣设置了小程序的访问权限.如下: 解决方法是使用代理,将豆瓣API地址换成 https://d ...
- 如何用微信小程序,每天给自己赚个鸡腿?
假期如果实在无聊的话,那跟随田同学的脚步上架一个小程序吧. 话说:谁不想拥有一个自己的小程序呢?既可以赚点小钱又可以长长见识. 不懂小程序的小白能不能做出来呢?那来对了,这个教程就是针对小白的. 今天 ...
随机推荐
- Swing 是一个为Java设计的GUI工具包
Swing 是一个为Java设计的GUI工具包. Swing是JAVA基础类的一部分. Swing包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表. Swing提供许多比AWT更好的屏幕 ...
- Blog
http://www.cnblogs.com/digdeep/archive/2015/11/16/4968453.htmlhttp://it.dataguru.cn/article-8406-1.h ...
- Java 訪问权限控制:你真的了解 protected keyword吗?
摘要: 在一个类的内部,其成员(包含成员变量和成员方法)是否能被其它类所訪问,取决于该成员的修饰词:而一个类是否能被其它类所訪问,取决于该类的修饰词.Java的类成员訪问权限修饰词有四类:privat ...
- PHP进阶。
老手段,百度“PHP进阶” 不过,今天运气不错,搜到一个“PHP特级内容讲解”,地址是:http://wenku.baidu.com/course/view/fd8e591b6bd97f192279e ...
- This kernel requires an x86-64 CPU, but only detected an i686 CPU.
为了运行一款软件,我也是拼了.彻底的玩了一次,因为A需要B,我去下载B,结果B又需要C,我去下载C,结果……怎一个艰难了得.最关键的是,目前还没有达到目的!!! 先记录下过程,有时间再来一遍,也许我已 ...
- hive cst 时间转换
select from_unixtime(unix_timestamp(r.collecttime,'EEE MMM dd HH:mm:ss zzz yyyy'),'yyyy-MM-dd HH:mm: ...
- 基于HBase Hadoop 分布式集群环境下的MapReduce程序开发
HBase分布式集群环境搭建成功后,连续4.5天实验客户端Map/Reduce程序开发,这方面的代码网上多得是,写个测试代码非常容易,可是真正运行起来可说是历经挫折.下面就是我最终调通并让程序在集群上 ...
- VS常用快捷鍵
折疊所有方法 Ctrl +M +M 折疊單個方法 Ctrl +M +O 折疊單個方法
- python获取数组中最多的元素
获取数组中数量最多的元素,也就是最频繁的那个元素,方法有很多,下面是3种最简单的: 用max函数 sample = [1,2,3,3,3,4,5,5] max(set(sample), key=sam ...
- python连接mysql数据库封装
源码: import pymysql class MysqlConnect(object): # 魔术方法, 初始化, 构造函数 def __init__(self, host, user, pass ...