关于微信小程序上传图片到阿里云oss?

这个功能是前一个开发写的,但是上线后测试反应有时候上传图片小程序会卡死
但是我这边模拟器缺演示不出来这个问题,我把代码贴在下面,大佬指点指点
这个是上传的方法

uploadImages: function () {
    const than = this
    const {
      arrList,
      personalPhotolist
    } = than.data
    wx.chooseImage({
      count: 1,
      sizeType: ['original'], //原图
      sourceType: ['album', 'camera'], //支持选取图片
      success(res) {
        console.log('1',res);
        arrList.push(res.tempFiles[0])
        ajax.upLoadFiles(res.tempFiles[0].path).then(res => {
          // const imgUrl = res
          const personalPhoto = {
            'picUrl': res
          }
          personalPhotolist.push(personalPhoto)
          if (res) {
            than.setData({
              picList: arrList,
              personalPhotolist: personalPhotolist
            })
          } else {
            return wx.showToast({
              title: '系统内部错误,请联系管理员',
              icon: 'none',
            })
          }
        })
      }
    })
  },

util.js

var getOssToken = function () {
  // const {
  //   baseUrl
  // } = this.globalData
  const nowTime = (Date.now() + '').substring(0, 11);
  const numNOWtime = Number(nowTime) + (1000 * 60 * 2)
  const numExpire = Number(app.globalData.ossToken.expire)
  const token = wx.getStorageSync('token');

  console.log('nowTime', nowTime);
  if (!numExpire || numNOWtime > numExpire) {
    wx.request({
      url: baseUrl + '/oss/getToken',
      header: {
        "token": token,
      },
      success: (res => {
        console.log("oss", res);
        const ossToken = JSON.parse(res.data.token)
        app.globalData.ossToken = ossToken
      })
    })
  }
  return _getOSS();
}

const _getOSS = function () {
  if (!app.globalData.ossToken || !app.globalData.ossToken.expire) {
    _sleep(100)
    return _getOSS();
  } else {
    return app.globalData.ossToken;
  }
}
// “休眠”函数
function _sleep(delay) {
  var start = (new Date()).getTime();
  while ((new Date()).getTime() - start < delay) {
    continue;
  }
}

上传到阿里云oss的函数

const getOssToken = require('./util.js');
console.log(getOssToken);
var upLoadFiles = function (filePath) {
  const ossToken = getOssToken.getOssToken()
  console.log(ossToken);
  const {
    host,
    accessKeyId,
    policy,
    signature,
    dir

  } = getOssToken.getOssToken()
  const path = dir
  const aliyunFileKey = path + new Date().getTime() + Math.floor(Math.random() * 150) + '.png';
  var promise = new Promise(function (resolve, reject) {
    wx.uploadFile({
      filePath: filePath,
      name: 'file',
      url: host,
      formData: {
        'key': aliyunFileKey,
        'policy': policy,
        'OSSAccessKeyId': accessKeyId,
        'signature': signature,
        // 'success_action_status': '200',
      },
      success: (res) => {
        console.log("成功", res);
        // const image = host + '/' + aliyunFileKey
        const image = host + aliyunFileKey
        resolve(image)
        // return image
      },
      fail: (err) => {
        reject(err)
        console.log('失败', err);
      }
    })
  })
  return promise
}
阅读 1.3k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏