微信小程序-网络请求、上传接口封装(小程序端)

一、微信小程序网络请求、上传文件封装函数

//utils/api.js
const App = getApp();
const baseUrl = 'https://www.***.com';//域名
// 网络请求
const https = ({
    url = '',
    data = {},
    method
} = {}) => {
    wx.showLoading({
        title: '加载中...'
    });
    return new Promise((resolve, reject) => {
        wx.request({
            url: getUrl(url),
            method: method,
            data: data,
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            complete: (res) => {
                wx.hideLoading();
                console.log('返回数据');
                console.log(res)
                if (res.statusCode >= 200 && res.statusCode < 300) {
                    resolve(res.data)
                } else {
                    reject(res)
                }
            }
        });
    });
}
// 上传文件
const uploadFile = ({
    url = '',
    filePath,
    formData = {},
    name
} = {}) => {
    wx.showLoading({
        title: '加载中...'
    });
    return new Promise((resolve, reject) => {
        wx.uploadFile({
            url: getUrl(url),
            filePath: filePath,
            name: name,
            formData: formData,
            success: (res) => {
                console.log('上传接口数据', res);
                wx.hideLoading();
                if (res.statusCode >= 200 && res.statusCode < 300) {
                    resolve(res.data)
                } else {
                    reject(res)
                }
            }
        })
    });
}
// 请求网址
const getUrl = (url) => {
    if (url.indexOf('://') == -1) {
        url = baseUrl + url;
    }
    return url
}

// get方法
const _get = (url, data = {}) => {
    return https({
        url,
        method: 'GET',
        data
    })
}
// post方法
const _post = (url, data = {}) => {
    return https({
        url,
        method: 'POST',
        data
    })
}
// 上传录音
const uploadAudio = (filePath, formData = { 'token': App.globalData.token }) => {
    return uploadFile({
        url: '/index.php/Asset/Upload/plupload',
        filePath,
        formData,
        name: 'file'
    })
}
// 上传图片
const uploadImg = (filePath, formData = { 'token': App.globalData.token }) => {
    return uploadFile({
        url: '/index.php/Asset/Upload/advpPlupload',
        filePath,
        formData,
        name: 'file'
    })
}
// 上传视频
const uploadVideo = (filePath, formData = { 'token': App.globalData.token }) => {
    return uploadFile({
        url: '/index.php/Asset/Upload/advpPlupload',
        filePath,
        formData,
        name: 'file'
    })
}
module.exports = {
    baseUrl,
    _get,
    _post,
    uploadAudio,
    uploadImg,
    uploadVideo
}

二、接口使用方法

//index.js  获取应用实例
const Request = require('../../utils/api.js');
const App = getApp();
Page({
    data: {
        token: null,
        userInfo: {},
    },
    onLoad: function() {
        var _this = this;
        var g_userInfo = App.getStorageSync("g_userInfo");
        _this.setData({
            userInfo: g_userInfo
        });
    },
    onShow: function() {
        var _this = this;
        var g_userInfo = App.getStorageSync("g_userInfo");
        _this.setData({
            userInfo: g_userInfo
        });
        //使用实例
        // POST方式获取
        var data = {
            token: App.globalData.token
        }
        Request._post(
            '/index.php?g=User&m=Consumer&a=moneyAndAdv',
            data
        ).then(res => {
            console.log(res);
        }).catch(e => {
            console.log(e);
        });

        // GET方式获取
        var data = {
            token: App.globalData.token
        }
        Request._post(
            '/index.php?g=User&m=Consumer&a=moneyAndAdv',
            data
        ).then(res => {
            console.log(res);
        }).catch(e => {
            console.log(e);
        });

        //上传音频
        Request.uploadAudio(
            _this.data.touchFile //音频文件
        ).then(res => {
            //成功之后返回值
            console.log(res);
        }).catch(e => {
            //失败之后返回值
            console.log(e)
        });

        // 上传图片
        Request.uploadImg(
            _this.data.img_path //图片文件
        ).then(res => {
            console.log(res);
        }).catch(e => {
            console.log(e)
        });

        //上传视频
        Request.uploadVideo(
            _this.data.video_path //视频文件
        ).then(res => {
            console.log(res);    
        }).catch(e => {
            console.log(e)
        });

        //接口使用方法,按需使用
    },
});

 

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱宇阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值