// app.js
App({
onLaunch() {
// 展示本地存储能力
const logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
}
})
},
globalData: {
url: 'https://zyz.hdagyj.com/api/',
imgUrl: 'http://zyz.hdagyj.com/static/wechat/',
userInfo: null
},
/**
* 封装调用接口方法
* @param {路径} url
* @param {get post} method
* @param {成功后回调} success
*/
myRequest: function(param){
var that = this;
wx.showLoading({
title: '加载中',
})
wx.request({
url: that.globalData.url + param.url,
data: param.data,
method: param.method?param.method:'POST',
success: function(res){
wx.hideLoading({})
if(res.data.errcode == 0){
param.success(res);
}else{
wx.showModal({
showCancel: false,
title: JSON.stringify(res)
})
if(param.error){
param.error(res);
}
}
},
fail(res){
if(param.fail){
param.fail(res);
}
}
})
},
/**
* 加载更多 isLoading isLoadOk
* @param that 调用this
* @param url 请求数据url
* @param params 请求数据参数 有没有page都可以
* @param name 页面数据名称
* @param resName 返回数据名称
* @param countName 返回数据数量名称
* @param method 请求方式 GET POST 默认POST
*/
getMore: function (that, url, params, name, resName, countName='count', method='POST') {
// countName = countName?countName:'count';
// method = method?method:'POST';
if (!that.data.isLoading) {
if (that.data.isLoadOk) {
wx.showToast({
icon: 'none',
title: '到底了'
});
return;
}
that.setData({
isLoading: true,
page: that.data.page + 1
})
let param = JSON.parse(JSON.stringify(params));
param.page = that.data.page;
wx.request({
url: url,
method: method,
data: param,
success: function (res) {
if (res.data.errcode == 0) {
that.setData({
[name]: that.data[name].concat(res.data.str[resName])
})
if (that.data[name].length >= res.data.str[countName]) {
that.setData({
isLoadOk: true
})
}
} else {
wx.showToast({
title: res.data.msg
})
}
that.setData({
isLoading: false
})
}
})
}
},
})