前端

小程序请求带上传图片

http.js

//控制loading是因为有时一个页面会请求多个接口,出现多个loading体验不是很好

function get(url, params, onSuccess, loadding = true){
  request(url, params, "GET", onSuccess, loadding);
}

function post(url, params, onSuccess, loadding = true){
  request(url, params, "POST", onSuccess, loadding);
}

function upload(type = 'image', onSuccess) {

    var filePath = '';

    if (type == 'image') {
        wx.chooseImage({
            success (res) {
                wx.showLoading({
                    title: "上传中...",
                })
              const tempFilePaths = res.tempFilePaths
              filePath = tempFilePaths[0];
              uploadfile(filePath, function (res) {
                onSuccess(res);
              })
            }
          })
    }

    if (type == 'video') {
        wx.chooseVideo({
            maxDuration: 15,
            success (res) {
                wx.showLoading({
                    title: "上传中...",
                })
                filePath = res.tempFilePath;
                uploadfile(filePath, function (res) {
                    onSuccess(res);
                })
            }
        })
    }

}

function uploadfile(filePath, onSuccess){
    wx.uploadFile({
        url: get_url('api/index/upload'),
        filePath: filePath,
        name: 'file',
        header: {
            'content-type': 'application/json',
            'token' : wx.getStorageSync('token'),
            'sign' : '51eb229f5e0909c4e3c4bb1d31364d52'
        },
        success (res){
            wx.hideLoading();
            if (res.statusCode == 200) {
                let datas = JSON.parse(res.data)
                if (datas.code != 0) {
                    wx.showToast({
                        title: datas.msg,
                        icon: 'none',
                        duration: 3000
                    });return;
                }
                onSuccess(datas);
            } else {
                wx.showToast({
                    title: '上传失败',
                    icon: 'none',
                    duration: 3000
                });
            }
        },
        fail: function(error) {
            wx.hideLoading();
            console.log(error);
            wx.showToast({
                title: error.errMsg,
                icon: 'none',
                duration: 3000
            });
        }
    })
}

function request(url, params = {}, method = 'GET', onSuccess, loadding = true) {
  if (loadding==true) {
      wx.showLoading({
          title: "加载中...",
      })
  };
  wx.request({
      url: get_url(url),
      data: params,
      method: method,
      header: {
          'content-type': 'application/json',
          'token' : wx.getStorageSync('token'),
          'sign' : '51eb229f5e0909c4e3c4bb1d31364d52'
      },
      success: function(res) {
          if (loadding==true) {
              wx.hideLoading();
          }
          if (res.data) {
              if (res.statusCode == 200) {
                  if (res.data.code == 20000) {
                    wx.navigateTo({
                        url: '/pages/login/index',
                        events: {

                        },
                        success: function(res) {

                        }
                    });return;
                  }
                  onSuccess(res.data);
              } else {
                  console.log(res);
                  wx.showToast({
                      title: '请求失败',
                      icon: 'none',
                      duration: 3000
                  });
              }
          }
      },
      fail: function(error) {
          console.log(error);
          wx.showToast({
              title: error.errMsg,
              icon: 'none',
              duration: 3000
          });
      }
  })
}

function get_url(url) {
  var base_url = "https://subscribe.gxltcxtyzx.com/";
  return base_url + url;
}

//提供给外部使用
module.exports = {
  post: post,
  get: get,
  upload: upload,
}

外部使用

//引入
const http = require("../../utils/http");

上传图片
http.upload('image', function(res){
    var image = res.data.file_url;
});

上传视频
http.upload('video', function(res){
    var video = res.data.file_url;
});

类似文章