import {config} from '../config.js'
class HTTP{
request({ url, data = {}, method = 'GET' }){
return new Promise((resolve,reject)=>{
this._request(url, resolve, reject, data, method)
})
}
_request(url,resolve,reject,data={},method='GET'){
wx.request({
url:config.api_base_url+url,
method:method,
data:data,
header:{
'content-type':'application/json'
},
success:(res)=>{
resolve(res.data)
}
})
}
uploadimg({ url, filePath = '', name = 'file' }) {
return new Promise((resolve, reject) => {
this._uploadimg(url, resolve, reject, filePath, name)
})
}
_uploadimg(url, resolve, reject, filePath = '', name = 'file') {
wx.showLoading({
title: '上传中...',
})
wx.uploadFile({
url: config.api_base_url + url,
filePath: filePath,
name: name,
success: (res) => {
resolve(res.data)
wx.hideLoading()
}
})
}
}
export {HTTP}
使用
import { HTTP } from '../../utils/http.js'
class ListModel extends HTTP {
getAllvd() {
return this.request({
url: 'm=laodou&c=videolist'
})
}
getMorevd(s) {
return this.request({
url: 'm=laodou&c=videolist&start=' + s
})
}
}
export { ListModel }
使用
import {
ListModel
} from 'videolist-model.js'
import {
config
} from '../../config.js'
const listModel = new ListModel()
Page({
/**
* 页面的初始数据
*/
data: {
vddataArray: []
},
onLoad: function(e) {
this._loadData()
},
_loadData(e) {
wx.showLoading({
title: '加载中…',
})
const listvd = listModel.getAllvd()
Promise.all([listvd]).then(res => {
//console.log(res)
res[0][0].isPlaying = !0
this.vddataArray = res[0]
this.setData({
feeds: res[0],
})
wx.hideLoading()
wx.stopPullDownRefresh()
})
},
_loadMorevdData: function() {
const s = this.vddataArray.length
console.log(s)
const listmorevd = listModel.getMorevd(s)
Promise.all([listmorevd]).then(res => {
console.log(res[0])
const tempdataArray = this.vddataArray.concat(res[0])
console.log(tempdataArray)
this.vddataArray = tempdataArray
this.setData({
feeds: tempdataArray
})
})
},
onReady: function() {},
onShow: function() {},
onHide: function() {},
onUnload: function() {},
onPullDownRefresh: function() {
this._loadData()
},
onReachBottom: function() {
this._loadMorevdData()
},
onShareAppMessage: function(e) {
}
})