|
|
import request from '../request.js'
|
|
|
import common from "../common.js"
|
|
|
import file from "./file.js"
|
|
|
|
|
|
// #ifdef H5
|
|
|
let jweixin = require('jweixin-module')
|
|
|
// #endif
|
|
|
|
|
|
/* 2022.12.12 兼容了微信小程序 */
|
|
|
|
|
|
let video = {}
|
|
|
|
|
|
/**
|
|
|
* 选择视频
|
|
|
* @param {array} sourceType album album 从相册选视频,camera 使用相机拍摄,默认为:['album', 'camera']
|
|
|
* @param {number} maxDuration 拍摄视频最长拍摄时间,单位秒。最长支持 60 秒。
|
|
|
* @param {boolean} isOriginalName false--随机文件名 true--原文件名
|
|
|
* 2022-12-12
|
|
|
*/
|
|
|
video.chooseVideoUpload = function(sourceType = ['album', 'camera'], maxDuration = 60, isOriginalName =
|
|
|
false) {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
// 拍摄视频或从手机相册中选视频
|
|
|
uni.chooseVideo({
|
|
|
// album 从相册选视频,camera 使用相机拍摄,默认为:['album', 'camera']
|
|
|
sourceType: sourceType,
|
|
|
// 拍摄视频最长拍摄时间,单位秒。最长支持 60 秒。
|
|
|
maxDuration: maxDuration,
|
|
|
success: (res) => {
|
|
|
/* res返回示例
|
|
|
微信小程序客户端:
|
|
|
{
|
|
|
errMsg: "chooseVideo:ok",
|
|
|
// 选定视频的临时文件路径
|
|
|
tempFilePath: "http://tmp/kSVDnhN7GX9v05f00f88df776700b0066e29be28a65f.mp4",
|
|
|
// 选定的视频文件
|
|
|
thumbTempFilePath: "http://tmp/Wqsssma5nNw51efac6cf58dc1d76e13b897cd09c1fbb.jpg",
|
|
|
size: 29192165,
|
|
|
name: "手机录频教程.mp4",
|
|
|
duration: 181.016,
|
|
|
height: 1280,
|
|
|
width: 720
|
|
|
}
|
|
|
|
|
|
H5端:
|
|
|
{
|
|
|
errMsg: "chooseVideo:ok",
|
|
|
// 选定视频的临时文件路径
|
|
|
tempFilePath: "blob:http://localhost:8080/6a835880-898e-48a4-a4b7-2e7f7fd2f794",
|
|
|
// 选定的视频文件
|
|
|
tempFile:{size: 29192165, lastModified: 1587106734791, name: "手机录频教程.mp4", type: "video/mp4"},
|
|
|
size: 29192165,
|
|
|
name: "手机录频教程.mp4",
|
|
|
duration: 181.015789,
|
|
|
height: 1280,
|
|
|
width: 720
|
|
|
}
|
|
|
*/
|
|
|
console.log('chooseVideoSuccess', res)
|
|
|
uni.showLoading({
|
|
|
mask: true,
|
|
|
title: '正在上传'
|
|
|
})
|
|
|
|
|
|
// 选定视频的临时文件路径 (本地路径)
|
|
|
let tempFiles = res.tempFilePath
|
|
|
// 待上传的文件列表
|
|
|
let files = []
|
|
|
|
|
|
// 原文件名称
|
|
|
let localFileName,
|
|
|
// 上传后文件名称
|
|
|
fileName
|
|
|
|
|
|
// 获取源文件名称
|
|
|
// #ifdef H5
|
|
|
localFileName = res.name
|
|
|
// #endif
|
|
|
// #ifdef APP-PLUS|MP-WEIXIN|MP-ALIPAY
|
|
|
let pos = res.tempFilePath.lastIndexOf('/')
|
|
|
if (pos != -1) {
|
|
|
localFileName = res.tempFilePath.substring(pos + 1)
|
|
|
}
|
|
|
// #endif
|
|
|
|
|
|
// 生成上传后文件名称
|
|
|
if (isOriginalName) {
|
|
|
// 原文件名
|
|
|
fileName = localFileName
|
|
|
} else {
|
|
|
// 随机文件名
|
|
|
let suffix = common.getSuffix(localFileName)
|
|
|
fileName = common.getRandomStr(10) + suffix
|
|
|
}
|
|
|
|
|
|
files.push({
|
|
|
// 图片的本地文件路径列表
|
|
|
path: res.tempFilePath,
|
|
|
// 上传后的文件名
|
|
|
fileName: fileName
|
|
|
})
|
|
|
|
|
|
// 上传到OSS
|
|
|
file.uploadFilesToOss(files, [], {}, isOriginalName, 'video').then(res => {
|
|
|
uni.hideLoading()
|
|
|
resolve(res)
|
|
|
}).catch(err => {
|
|
|
uni.hideLoading()
|
|
|
reject(err)
|
|
|
})
|
|
|
|
|
|
},
|
|
|
fail: (err) => {
|
|
|
reject(err)
|
|
|
}
|
|
|
})
|
|
|
})
|
|
|
}
|
|
|
|
|
|
export default video
|