根据json返回个几分钟前

步骤

用方法wx.request 请求一个 url 地址。
返回需要的json数据,如下图所示:

代码

要点:先不去setData。在 wx.ruquest成功之后通过自定义的方法转换掉时间戳,再更新data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//index.js
//获取应用实例
var app = getApp()
Page({
data: {
imgUrls: [{
uri:'http://cdn-s3.si.com/s3fs-public/2017/02/27/james-harden-dwight-howard.jpg',
txt:'詹姆斯破茧重生',
url:'http://bbs.hupu.com/18657042.html',
},{
uri:'https://c2.hoopchina.com.cn/uploads/star/event/images/170307/38d602e779e53d8b8bd05f88d76b8563316d6267.jpg',
txt:'库班谈塞思-库里:我们希望他永远留在球队',
url:'http://bbs.hupu.com/18658465-2.html',
},{
uri:'http://cdn.fansided.com/wp-content/uploads/usat-images/2016/04/9874357-nba-san-antonio-spurs-at-new-york-knicks-850x560.jpeg',
txt:'卡梅罗-安东尼想赢下总冠军',
url:'http://bbs.hupu.com/18626439.html',
}],
indicatorDots: true,
autoplay: true,
interval: 5000,
duration: 1000,
circular: true,
zixunList: [],
},
swipeTo: function(res){
wx.navigateTo({
url: './details/details?id='+res.currentTarget.id,
})
},
//事件处理函数
bindViewTap: function() {
wx.navigateTo({
url: '../logs/logs'
})
},
getDateDiff : function(dateTimeStamp){
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var halfamonth = day * 15;
var month = day * 30;
var now = new Date().getTime();
var diffValue = now - dateTimeStamp;
if(diffValue < 0){return;}
var monthC =diffValue/month;
var weekC =diffValue/(7*day);
var dayC =diffValue/day;
var hourC =diffValue/hour;
var minC =diffValue/minute;
if(monthC>=1){
result="" + parseInt(monthC) + "月前";
}
else if(weekC>=1){
result="" + parseInt(weekC) + "周前";
}
else if(dayC>=1){
result=""+ parseInt(dayC) +"天前";
}
else if(hourC>=1){
result=""+ parseInt(hourC) +"小时前";
}
else if(minC>=1){
result=""+ parseInt(minC) +"分钟前";
}else
result="刚刚";
return result;
},
onLoad: function () {
var that = this;
wx.request({
url: "https://www.v2ex.com/api/topics/latest.json",
method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
// header: {}, // 设置请求的 header
success: function(res){
// success
for(var i in res.data){
res.data[i].chuo = that.getDateDiff(res.data[i].last_modified*1000);
}
that.setData({
zixunList:res.data
})
},
fail: function() {
// fail
alert('稍后重试');
}
});
//调用应用实例的方法获取全局数据
app.getUserInfo(function(userInfo){
//更新数据
that.setData({
userInfo:userInfo
})
})
}
})

效果

如下所示: