wx.request以及wx.uploadfile封装

1688源码 | 2020-02-17 22:00:04 |

 http.js

 

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) {

}
})
 

 

精品源码
  • 织梦万能API 百度小程序插件

    惊爆价¥400原价¥800

    立即购买
  • 我爱登报网 百度小程序源码 织梦CMS后台

    惊爆价¥520原价¥1040

    立即购买
  • 索娜根布黑枸杞 百度小程序源码 织梦CMS后台

    惊爆价¥520原价¥1040

    立即购买
视觉焦点

推荐文章

微信小程序插件使用错误cannotread property 'version' of undefined

微信小程序使用ES6构建面向对象的JS代码及访问服务器API获取数据

最新文章

关注我们

微信扫一扫,关注更多精彩

  • 1688源码公众号
    全面掌握源码一手资讯

  • 1688源码公众号
    精彩活动,推送提醒