glsxUpload.js 2.59 KB
/** 上传
 * @param {*} baseURL method url 获取签名接口参数
 * @param {*} uploadEvent(eventType, callback)
 * eventType : media_upload 媒体上传完成,  media_progress 媒体上传进度,cover_upload 图片上传完成, cover_progress 图片上传进度
 * @returns params.url = 'rop-scrm-shopmall/router'  params.data.method = 'scrm.api.shopmall.livebroadcast.uploadsignature'
 */

//npm install vod-js-sdk-v6
import TcVod from 'vod-js-sdk-v6'
import { http } from '@/main'

// 请求配置
const SIGN = {
  v: '1.0.0',
  source: 'scm-home',
  format: 'json',
  appKey: '48e5e13229b82c1b4e6e8c96151f0637'
}
const SECRET = 'c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b'
const REQUEST = {
  timeout: 180000,
  responseType: 'json',
  baseURL: 'http://scrm.dev.glsx.com.cn',
  withCredentials: true
}
//   const RESPONSE = {
//     code_key: ['code', 'returnCode'],
//     code_message_map: {},
//     code_success_value: '0',
//     data_key: 'data',
//     errId_logout_value: '21',
//     message_key: 'message'
//   }

class GlsxUpload {
  constructor(params) {
    this.initParam = params
  }
  init() {
    return new Promise((resolve, reject) => {
      if (this.tcVod) return resolve({ status: 0, msg: 'init successful' })
      const { params: { data, url } = {} } = this.initParam || {}
      console.log(data)
      return http({
        data,
        url: url,
        returnAll: true
      })
        .then(res => {
          if (res.code == '0') {
            this.tcVod = new TcVod({
              getSignature: () => {
                return res.data
              }
            })
          }
          if (this.tcVod) return resolve({ status: 0, msg: 'init successful' })
          reject({ status: -1, msg: 'init failed' })
        })
        .catch(err => {
          reject({ status: -1, msg: 'init failed' })
        })
    })
  }
  uploaded(file, img) {
    return new Promise((resolve, reject) => {
      if (!file) return reject({ status: -1, msg: 'file is required' })
      let params = {
        mediaFile: file
      }
      if (img) params.coverFile = img
      this.uploader = this.tcVod.upload(params)
      this.uploader
        .done()
        .then(doneResult => {
          resolve(doneResult)
        })
        .catch(err => {
          reject(err)
        })
    })
  }
  uploadEvent(eventType, callback) {
    if (!eventType) callback({ status: -1, msg: 'eventType is required' })
    try {
      this.uploader.on(eventType, info => {
        callback(info)
      })
    } catch (err) {
      callback(err)
    }
  }
  uploadCancel() {
    this.uploader.cancel()
  }
}

export default GlsxUpload