upload.js 7.3 KB

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