小程序公共上传方法

 // 七牛云上传多张图片 默认上传图片 type = 1 视频
  qiniuToUpload(type, callFn){
    let that = this
    that.request('get', '/shifu/index/api_upload_qiniu_get_token', {}, function (res) {
      if (res.error_code == 0) {
        that.globalData.upload_host = res.data.upload_host
        that.globalData.img_domain = res.data.host 
        that.uploadFile(res.data.token, type, callFn)
      } else {
        wx.showToast({
          title: res.error_message,
          icon: 'none'
        })
      }
    }, (err) => {})
  }
// 上传 默认上传图片; type = 1 是视频
  uploadFile(token, type, callFn) {
    let that = this
    if (type == 1) {
      wx.chooseVideo({
        success(res) {
          const tempFilePaths = res.tempFilePath
          wx.showLoading({
            title: '拼命上传中',
          })
          wx.uploadFile({
            url: that.globalData.upload_host,
            filePath: tempFilePaths,
            name: 'file',
            formData: {
              'token': that.data.token
            },
            success(res) {
              const data = JSON.parse(res.data)
              callFn && callFn(data);
            },
            fail: (res) => {
              wx.showToast({
                title: res.errMsg,
                icon: 'none'
              })
            },
            complete() {
              wx.hideLoading()
            }
          })
        }
      })
    }
    else {
      wx.chooseImage({
        count: 9,
        success(res) {
          console.log(res)
          // tempFilePath可以作为img标签的src属性显示图片
          const tempFilePaths = res.tempFilePaths
          wx.showLoading({
            title: '拼命上传中',
          })
          for (let i = 0; i < tempFilePaths.length; i++) {
            wx.uploadFile({
              url: that.globalData.upload_host,
              filePath: tempFilePaths[i],
              name: 'file',
              formData: {
                'token': token,
              },
              success(res) {
                console.log(res)
                const data = JSON.parse(res.data)
                callFn && callFn(data);
              },
              fail: (res) => {
                wx.showToast({
                  title: res.errMsg,
                  icon: 'none'
                })
              },
              complete() {
                if (i == tempFilePaths.length - 1) {
                  wx.hideLoading()
                }
              }
            })
          }
        }
      })
    }
  }

// 请求方法封装
  request(type,url,obj,callback) {
    wx.showLoading({
      title: '拼命加载中',
    })
    let that = this
    let openid = wx.getStorageSync('openid') ? wx.getStorageSync('openid'): that.globalData.openid;
    let appid =  that.globalData.appid
    // 不是登录接口
    if(url!='/api/login') {
      // 视接口所需参数而定
      obj=Object.assign(obj,{'openid':openid});
      obj=Object.assign(obj,{'appid':appid});
    }
    let param=obj;
    wx.request({
      method: type,
      dataType: 'json',
      url: that.globalData.requestUrl + url,
      data: param,
      header: {
        'content-type': 'application/json',
      },
      success: function (res) {
        callback(res.data);
      },
      complete: function() {
        wx.hideLoading({
          success: (res) => {},
        })
      }
    })
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值