upFile.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import * as qiniu from 'qiniu-js'
  2. import request from '@/utils/request'
  3. export function toUpLnImg(files, fun) {
  4. uni.showLoading({
  5. title: '上传中...'
  6. })
  7. for (let i = 0; i < files.length; i++) {
  8. let file = files[i];
  9. let imageType = /^image\//;
  10. //判断是否为图片不是则跳过不执行下面语句
  11. if (!imageType.test(file.type)) {
  12. uni.hideLoading()
  13. this.$api.msg('格式错误')
  14. continue;
  15. }
  16. request({
  17. url: "/Upload/wxUploudtoken",
  18. method: 'post',
  19. data: {}
  20. }).then(({
  21. data
  22. }) => {
  23. console.log(data, 99);
  24. let observable = qiniu.upload(file, file.name, data.token, {
  25. fname: "",
  26. params: {},
  27. mimeType: null
  28. }, {
  29. useCdnDomain: true,
  30. region: qiniu.region.z0
  31. });
  32. let observer = {
  33. next(res) {
  34. console.log(res);
  35. // ...
  36. },
  37. error(err) {
  38. console.log(err);
  39. // ...
  40. },
  41. complete(res) {
  42. uni.hideLoading()
  43. this.$api.msg('上传成功')
  44. fun('https://limg.liuniu946.com/' + res.key)
  45. // ...
  46. }
  47. }
  48. let subscription = observable.subscribe(observer) // 上传开始
  49. })
  50. }
  51. }
  52. /**
  53. * @param {Object} files 要上传的文件
  54. * @param {Function} fun 上传成功后的回调
  55. */
  56. export function toUpVideo(files, fun) {
  57. Indicator.open({
  58. text: '上传中...',
  59. spinnerType: 'fading-circle'
  60. });
  61. for (let i = 0; i < files.length; i++) {
  62. let file = files[i];
  63. let Type = /^video\//;
  64. //判断是否为图片不是则跳过不执行下面语句
  65. if (!Type.test(file.type)) {
  66. uni.hideLoading()
  67. this.$api.msg('格式错误')
  68. continue;
  69. }
  70. request({
  71. url: "/Upload/wxUploudtoken",
  72. method: 'post',
  73. data: {}
  74. }).then(({
  75. data
  76. }) => {
  77. // console.log(data);
  78. let observable = qiniu.upload(file, file.name, data.token, {
  79. fname: "",
  80. params: {},
  81. mimeType: null
  82. }, {
  83. useCdnDomain: true,
  84. region: qiniu.region.z0
  85. });
  86. let observer = {
  87. next(res) {
  88. console.log(res);
  89. // ...
  90. },
  91. error(err) {
  92. console.log(err);
  93. // ...
  94. },
  95. complete(res) {
  96. uni.hideLoading()
  97. this.$api.msg('上传成功')
  98. fun('https://limg.liuniu946.com/' + res.key)
  99. // ...
  100. }
  101. }
  102. let subscription = observable.subscribe(observer) // 上传开始
  103. })
  104. }
  105. }
  106. export default {
  107. toUpLnImg,
  108. toUpVideo
  109. };