upload.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import request from "./../core/request.js";
  2. import {
  3. chooseImage,
  4. chooseVideo,
  5. qiniuUpload,
  6. urlUpload
  7. } from './utils'
  8. import {
  9. mergeConfig
  10. } from "./../core/utils.js";
  11. export default class fileUpload extends request {
  12. constructor(props) {
  13. // 调用实现父类的构造函数
  14. super(props);
  15. }
  16. async ossImagUpload(options = {}) {
  17. try {
  18. const files = await chooseImage(options);
  19. const data = [];
  20. for (let item of files) {
  21. let params = {
  22. size: item.size,
  23. model: 'image',
  24. }
  25. if (item.name) {
  26. params.name = item.name;
  27. params.path = item.path;
  28. } else if (item.path) {
  29. if (item.path.indexOf('/') > -1) {
  30. let names = item.path.split('/');
  31. params.name = names[names.length - 1];
  32. }
  33. params.path = item.path;
  34. }
  35. // 选择完成回调
  36. options.onSelectComplete && options.onSelectComplete(files);
  37. data.push(params);
  38. }
  39. return Promise.resolve(data);
  40. } catch (err) {
  41. return Promise.reject(err);
  42. }
  43. }
  44. async ossVideoUpload(options = {}) {
  45. let files;
  46. try {
  47. files = await chooseVideo(options);
  48. let file = files[0];
  49. let params = {
  50. size: file.size,
  51. model: 'video',
  52. }
  53. if (file.name) {
  54. params.name = file.name;
  55. params.path = file.path;
  56. } else if (file.path) {
  57. if (file.path.indexOf('/') > -1) {
  58. let names = file.path.split('/');
  59. params.name = names[names.length - 1];
  60. }
  61. params.path = file.path;
  62. }
  63. // 选择完成回调
  64. options.onSelectComplete && options.onSelectComplete(files);
  65. return Promise.resolve(params);
  66. } catch (err) {
  67. return Promise.reject(err);
  68. }
  69. }
  70. //七牛云上传图片
  71. async qnImgUpload(options = {}) {
  72. let files;
  73. try {
  74. files = await chooseImage(options);
  75. // 选择完成回调
  76. options.onSelectComplete && options.onSelectComplete(files);
  77. } catch (err) {
  78. this.requestError && this.requestError(err);
  79. return Promise.reject(err);
  80. }
  81. if (files) {
  82. return this.qnFileUpload({
  83. ...options,
  84. files: files
  85. });
  86. }
  87. }
  88. //七牛云上传视频
  89. async qnVideoUpload(options = {}) {
  90. let files;
  91. try {
  92. files = await chooseVideo(options);
  93. // 选择完成回调
  94. options.onSelectComplete && options.onSelectComplete(files);
  95. } catch (err) {
  96. this.requestError && this.requestError(err);
  97. return Promise.reject(err);
  98. }
  99. if (files) {
  100. return this.qnFileUpload({
  101. ...options,
  102. files: files
  103. });
  104. }
  105. }
  106. //七牛云文件上传(支持多张上传)
  107. async qnFileUpload(options = {}) {
  108. let requestInfo;
  109. try {
  110. // 数据合并
  111. requestInfo = {
  112. ...this.config,
  113. ...options,
  114. header: {},
  115. method: "FILE"
  116. };
  117. //请求前回调
  118. if (this.requestStart) {
  119. let requestStart = this.requestStart(requestInfo);
  120. if (typeof requestStart == "object") {
  121. let changekeys = ["load", "files"];
  122. changekeys.forEach(key => {
  123. requestInfo[key] = requestStart[key];
  124. });
  125. } else {
  126. throw {
  127. errMsg: "【request】请求开始拦截器未通过",
  128. statusCode: 0,
  129. data: requestInfo.data,
  130. method: requestInfo.method,
  131. header: requestInfo.header,
  132. url: requestInfo.url,
  133. }
  134. }
  135. }
  136. let requestResult = await qiniuUpload(requestInfo, this.getQnToken);
  137. return Promise.resolve(requestResult);
  138. } catch (err) {
  139. this.requestError && this.requestError(err);
  140. return Promise.reject(err);
  141. } finally {
  142. this.requestEnd && this.requestEnd(requestInfo);
  143. }
  144. }
  145. //本地服务器图片上传
  146. async urlImgUpload() {
  147. let options = {};
  148. if (arguments[0]) {
  149. if (typeof(arguments[0]) == "string") {
  150. options.url = arguments[0];
  151. } else if (typeof(arguments[0]) == "object") {
  152. options = Object.assign(options, arguments[0]);
  153. }
  154. }
  155. if (arguments[1] && typeof(arguments[1]) == "object") {
  156. options = Object.assign(options, arguments[1]);
  157. }
  158. try {
  159. options.files = await chooseImage(options);
  160. // 选择完成回调
  161. options.onSelectComplete && options.onSelectComplete(options.files);
  162. } catch (err) {
  163. this.requestError && this.requestError(err);
  164. return Promise.reject(err);
  165. }
  166. if (options.files) {
  167. return this.urlFileUpload(options);
  168. }
  169. }
  170. //本地服务器上传视频
  171. async urlVideoUpload() {
  172. let options = {};
  173. if (arguments[0]) {
  174. if (typeof(arguments[0]) == "string") {
  175. options.url = arguments[0];
  176. } else if (typeof(arguments[0]) == "object") {
  177. options = Object.assign(options, arguments[0]);
  178. }
  179. }
  180. if (arguments[1] && typeof(arguments[1]) == "object") {
  181. options = Object.assign(options, arguments[1]);
  182. }
  183. try {
  184. options.files = await chooseVideo(options);
  185. // 选择完成回调
  186. options.onSelectComplete && options.onSelectComplete(options.files);
  187. } catch (err) {
  188. this.requestError && this.requestError(err);
  189. return Promise.reject(err);
  190. }
  191. if (options.files) {
  192. return this.urlFileUpload(options);
  193. }
  194. }
  195. //本地服务器文件上传方法
  196. async urlFileUpload() {
  197. let requestInfo = {
  198. method: "FILE"
  199. };
  200. if (arguments[0]) {
  201. if (typeof(arguments[0]) == "string") {
  202. requestInfo.url = arguments[0];
  203. } else if (typeof(arguments[0]) == "object") {
  204. requestInfo = Object.assign(requestInfo, arguments[0]);
  205. }
  206. }
  207. if (arguments[1] && typeof(arguments[1]) == "object") {
  208. requestInfo = Object.assign(requestInfo, arguments[1]);
  209. }
  210. if (!requestInfo.url && this.defaultUploadUrl) {
  211. requestInfo.url = this.defaultUploadUrl;
  212. }
  213. // 请求数据
  214. // 是否运行过请求开始钩子
  215. let runRequestStart = false;
  216. try {
  217. if (!requestInfo.url) {
  218. throw {
  219. errMsg: "【request】文件上传缺失数据url",
  220. statusCode: 0,
  221. data: requestInfo.data,
  222. method: requestInfo.method,
  223. header: requestInfo.header,
  224. url: requestInfo.url,
  225. }
  226. }
  227. // 数据合并
  228. requestInfo = mergeConfig(this, requestInfo);
  229. // 代表之前运行到这里
  230. runRequestStart = true;
  231. //请求前回调
  232. if (this.requestStart) {
  233. let requestStart = this.requestStart(requestInfo);
  234. if (typeof requestStart == "object") {
  235. let changekeys = ["data", "header", "isPrompt", "load", "isFactory", "files"];
  236. changekeys.forEach(key => {
  237. requestInfo[key] = requestStart[key];
  238. });
  239. } else {
  240. throw {
  241. errMsg: "【request】请求开始拦截器未通过",
  242. statusCode: 0,
  243. data: requestInfo.data,
  244. method: requestInfo.method,
  245. header: requestInfo.header,
  246. url: requestInfo.url,
  247. }
  248. }
  249. }
  250. let requestResult = await urlUpload(requestInfo, this.dataFactory);
  251. return Promise.resolve(requestResult);
  252. } catch (err) {
  253. this.requestError && this.requestError(err);
  254. return Promise.reject(err);
  255. } finally {
  256. if (runRequestStart) {
  257. this.requestEnd && this.requestEnd(requestInfo);
  258. }
  259. }
  260. }
  261. }