微信小程序DEMO——面包旅行的代码
API
集合在一起写了一个页面,并导出,
const apiURL = 'http://xxx.xxxx.com'; const trip = {
hot(data,callback){
wx.request({
url: `${apiURL}/v2/index/`,
method: 'GET',
data,
header:{
Accept: 'application/json',
'Content-Type': 'application/json'
},
success(res) {
callback('success', res);
},
fail(res) {
callback('fail', res);
},
complete(res) {
callback('complete', res);
}
})
}
} module.exports = {
trip
}
请求的时候,采用 obj.xxx(data,(state,res)=>{}) 返回函数的形式挂载数据请求;
const api = require('../../utils/api.js'); Page({
data: { },
onLoad: function(options) {
let self = this;
let data = {
next_start: self.data.start
}
api.trip.hot(data,(state,res)=>{
if (state === 'success'){
// console.log(res.data);
let newList = res.data.data.elements;
newList.map((trip) => {
let item = trip;
item.data[0].date_added = formatTime(new Date(item.data[0].date_added * 1000),1)
return item;
});
if (needrefresh){
newList = self.data.trips.concat(newList);
};
self.setData({
trips: newList,
start: res.data.data.next_start,
loading: 'none'
});
// console.log(newList)
}
})
}
})
主页
主要写了上拉加载,下拉刷新数据,页面的跳转,多行文字的省略号。
下拉刷新,首页请求的数据中有一个 next_start 的字段。首次进入页面时,传入0,服务器返回数据和这个字段,记录这个字段,然后上拉刷新的时候,再把这个字段传给服务器,服务器会返回新的数据和新的字段。
上拉加载,还是这个 next_start 字段。首次获得数据之后,记录此字段,上拉加载时,传入此字段,返回新的数据和新的字段,新的数据需要和之前的数据合并,然后渲染。
页面的跳转,前往游记页面需要当前点击对象的 id 和 name 值,这个使用事件对象的 e.currentTarget.dataset 来获得,dataset 是由组件上以 data- 开头的属性集合。然后通过 wx.navigateTo() 跳转,路径中可以带参数(传值)。
多行文字的省略号,有些文字是需要省略写法的,单行又和多行不同。
<view data-id="{{item.data[0].id}}" data-name="{{item.data[0].name}}" bindtap='viewTrip'></view> viewTrip:function(e){
let dataTrip = e.currentTarget.dataset;
wx.navigateTo({
url: `../trip/trip?id=${dataTrip.id}&name=${dataTrip.name}`,
})
} .title{
font-size: 36rpx;
font-weight: bold;
line-height: 1.5;
overflow: hidden;
text-overflow: ellipsis;
-webkit-line-clamp: 2;
display: -webkit-box;
-webkit-box-orient: vertical;
}
游记页面
主要根据跳转过来的 id 和 name 来请求服务器数据,加载中画面,页面跳转。
请求数据,可以在生命周期 onload(options) 参数中获取打开当前页面路径中的参数,然后根据它请求服务器数据。
加载中画面,使用 wx.showToast() 和 wx.hideToast() 。
在加载数据的时候,页面可能会显示没有数据的代码,需要做判断处理,<view class='wrap' wx:if="{{ trip != null }}">,在数据没到之前,页面不渲染。
页面跳转和主页一样,使用 e.currentTarget.dataset ,wx.navigateTo() 和 onload(options) 。
onLoad: function(options) {
let self = this;
let id = options.id;
self.setData({
options
});
wx.showToast({
title: '正在加载',
icon: 'loading',
duration: 10000,
mask: true
});
api.trip.waypoint(id,(state,res) => {
if(state === 'success'){
let trip = res.data;
// console.log(trip);
self.setData({
trip
});
wx.hideToast();
}
})
},
路径页面
用户页面
发现页面
使用 swiper 组件,flex 布局
flex 布局,每行显示两个 item ,多的换行,这需要给 item 定宽。
.area{
display: flex;
justify-content:space-between;
align-items: center;
flex-direction: row;
flex-wrap:wrap;
padding: 0 20rpx;
}
.item{
width: 345rpx;
height: 345rpx;
margin-bottom: 20rpx;
}
目的地页面
各景区的得分(星星数量),这是一种很多地方都用的方法,最高5星,可以有半颗星。
- 先列个数组,值为12345,
- 首先依次判断当前 item 是否小于等于得分,然后渲染全颗黄星
- 然后依次判断当前 item 大于得分和 item-1 小于得分,然后渲染半颗黄星
- 最后依次判断当前 item 大于得分和 item-1 不小于得分,然后渲染灰色星。
这里还使用了模板的方式。需要引入和并且填写 name 值,传入数据需要用到 data 属性。
<template name='m-stars'>
<view class='m-stars'>
<block wx:for='{{[1,2,3,4,5]}}' wx:key='*this'>
<image class='stars' wx:if='{{item <= count}}' src='../../image/destination/star_s.png'></image>
<image class='stars' wx:if='{{item > count && item-1 < count}}' src='../../image/destination/semistar_s.png'></image>
<image class='stars' wx:if='{{item > count && !(item-1 < count)}}' src='../../image/destination/unstar_s.png'></image>
</block>
</view>
</template> <import src='../../component/stars/stars.wxml'/>
<template is='m-stars' data='{{count:attract.rating}}'></template>
最新文章
- Oracle中用随机数更新字段----将一张表的数据插入另一张表----环境设置
- object-c 系列教程
- Calculating simple running totals in SQL Server
- shell获取本地ip的三种方法
- python heapq
- BufferedInputStream 源码分析
- DES加密算法应用:分组加密模式
- 【转】RTP学习笔记
- echo '1'.print(2)+3; 的输出结果为什么是511
- windows7下的一个好玩的,你绝对不知道
- 商业化博客平台原型制作分享-TypePad
- vscode 学习笔记 —— 调试 (以 node 为例)
- RancherOS(ROS)如何安装到硬盘? 并设置为用户自动登录到系统? -a rancher.autologin=tty1
- geoserver 开发2
- 时间序列深度学习:seq2seq 模型预测太阳黑子
- MySQL的yum源
- HDU-4511-ac自动机+dp
- java23种设计模式之三: 适配器模式
- c#类中字段和方法中变量的声明问题
- linux安装配置阿里云的yum源和python3
热门文章
- Windows环境下的安装gcc(c语言环境)
- elementUI el-table合并单元格
- git如何将旧commit的相关notes复制到新commit?
- vue 事件结合双向数据绑定实现todolist
- 服务器端实时推送技术之SseEmitter的用法
- 【转载】 Bill Gates和Elon Musk推荐,人工智能必读的三本书 -《终极算法》,《超级智能》和《终极发明》
- no match for call to ‘(std::__cxx11::string {aka std::__cxx11::basic_string
- 123457123456#0#-----com.cym.shuXue02--前拼后广--开心学数学
- 【Leetcode_easy】977. Squares of a Sorted Array
- Docker入门之docker-compose