choose-and-upload-video.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (C) 2020 Tencent Cloud.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import uploadFile from './upload-file';
  17. /**
  18. * 选择并上传视频到腾讯云COS(需要调用云函数签名,请先配置好云函数)
  19. * @async
  20. * @return {Promise<string>} 返回成功上传到COS上的文件名称
  21. */
  22. export default async function chooseAndUploadVideo() {
  23. let [error, res] = await uni.chooseVideo();
  24. if (error) {
  25. throw error;
  26. }
  27. // #ifdef H5
  28. if (!/^video/.test(res.tempFile.type)) {
  29. throw new Error('文件类型错误');
  30. }
  31. // #endif
  32. uni.showLoading({
  33. mask: true,
  34. });
  35. try {
  36. let file = res.tempFilePath;
  37. // #ifdef H5
  38. file = res.tempFile;
  39. // #endif
  40. const key = await uploadFile(file);
  41. return key;
  42. } catch (error) {
  43. throw error;
  44. } finally {
  45. uni.hideLoading();
  46. }
  47. };