upload.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import request from "./../core/request";
  2. import {
  3. chooseFile,
  4. chooseImage,
  5. chooseVideo,
  6. qiniuUpload,
  7. urlUpload
  8. } from "./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 qnChooseUpload(options = {}) {
  19. let files;
  20. try {
  21. files = await chooseFile(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 qnImgUpload(options = {}) {
  37. let files;
  38. try {
  39. files = await chooseImage(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 qnVideoUpload(options = {}) {
  55. let files;
  56. try {
  57. files = await chooseVideo(options);
  58. // 选择完成回调
  59. options.onSelectComplete && options.onSelectComplete(files);
  60. } catch (err) {
  61. this.requestError && this.requestError(err);
  62. return Promise.reject(err);
  63. }
  64. if (files) {
  65. return this.qnFileUpload({
  66. ...options,
  67. files: files
  68. });
  69. }
  70. }
  71. //七牛云文件上传(支持多张上传)
  72. async qnFileUpload(options = {}) {
  73. let requestInfo;
  74. try {
  75. // 数据合并
  76. requestInfo = {
  77. ...this.config,
  78. ...options,
  79. header: {},
  80. method: "FILE"
  81. };
  82. //请求前回调
  83. if (this.requestStart) {
  84. let requestStart = this.requestStart(requestInfo);
  85. if (typeof requestStart == "object") {
  86. let changekeys = ["load", "files"];
  87. changekeys.forEach(key => {
  88. requestInfo[key] = requestStart[key];
  89. });
  90. } else {
  91. throw {
  92. errMsg: "【request】请求开始拦截器未通过",
  93. statusCode: 0,
  94. data: requestInfo.data,
  95. method: requestInfo.method,
  96. header: requestInfo.header,
  97. url: requestInfo.url,
  98. }
  99. }
  100. }
  101. let requestResult = await qiniuUpload(requestInfo, this.getQnToken);
  102. return Promise.resolve(requestResult);
  103. } catch (err) {
  104. this.requestError && this.requestError(err);
  105. return Promise.reject(err);
  106. } finally {
  107. this.requestEnd && this.requestEnd(requestInfo);
  108. }
  109. }
  110. //本地服务器图片上传
  111. async urlImgUpload() {
  112. let options = {};
  113. if (arguments[0]) {
  114. if (typeof (arguments[0]) == "string") {
  115. options.url = arguments[0];
  116. } else if (typeof (arguments[0]) == "object") {
  117. options = Object.assign(options, arguments[0]);
  118. }
  119. }
  120. if (arguments[1] && typeof (arguments[1]) == "object") {
  121. options = Object.assign(options, arguments[1]);
  122. }
  123. try {
  124. options.files = await chooseImage(options);
  125. // 选择完成回调
  126. options.onSelectComplete && options.onSelectComplete(options.files);
  127. } catch (err) {
  128. this.requestError && this.requestError(err);
  129. return Promise.reject(err);
  130. }
  131. if (options.files) {
  132. return this.urlFileUpload(options);
  133. }
  134. }
  135. //本地服务器上传视频
  136. async urlVideoUpload() {
  137. let options = {};
  138. if (arguments[0]) {
  139. if (typeof (arguments[0]) == "string") {
  140. options.url = arguments[0];
  141. } else if (typeof (arguments[0]) == "object") {
  142. options = Object.assign(options, arguments[0]);
  143. }
  144. }
  145. if (arguments[1] && typeof (arguments[1]) == "object") {
  146. options = Object.assign(options, arguments[1]);
  147. }
  148. try {
  149. options.files = await chooseVideo(options);
  150. // 选择完成回调
  151. options.onSelectComplete && options.onSelectComplete(options.files);
  152. } catch (err) {
  153. this.requestError && this.requestError(err);
  154. return Promise.reject(err);
  155. }
  156. if (options.files) {
  157. return this.urlFileUpload(options);
  158. }
  159. }
  160. //本地服务器文件上传方法
  161. async urlFileUpload() {
  162. let requestInfo = {
  163. method: "FILE"
  164. };
  165. if (arguments[0]) {
  166. if (typeof (arguments[0]) == "string") {
  167. requestInfo.url = arguments[0];
  168. } else if (typeof (arguments[0]) == "object") {
  169. requestInfo = Object.assign(requestInfo, arguments[0]);
  170. }
  171. }
  172. if (arguments[1] && typeof (arguments[1]) == "object") {
  173. requestInfo = Object.assign(requestInfo, arguments[1]);
  174. }
  175. if (!requestInfo.url && this.defaultUploadUrl) {
  176. requestInfo.url = this.defaultUploadUrl;
  177. }
  178. // 请求数据
  179. // 是否运行过请求开始钩子
  180. let runRequestStart = false;
  181. try {
  182. if (!requestInfo.url) {
  183. throw {
  184. errMsg: "【request】文件上传缺失数据url",
  185. statusCode: 0,
  186. data: requestInfo.data,
  187. method: requestInfo.method,
  188. header: requestInfo.header,
  189. url: requestInfo.url,
  190. }
  191. }
  192. // 数据合并
  193. requestInfo = mergeConfig(this, requestInfo);
  194. // 代表之前运行到这里
  195. runRequestStart = true;
  196. //请求前回调
  197. if (this.requestStart) {
  198. let requestStart = this.requestStart(requestInfo);
  199. if (typeof requestStart == "object") {
  200. let changekeys = ["data", "header", "isPrompt", "load", "isFactory", "files"];
  201. changekeys.forEach(key => {
  202. requestInfo[key] = requestStart[key];
  203. });
  204. } else {
  205. throw {
  206. errMsg: "【request】请求开始拦截器未通过",
  207. statusCode: 0,
  208. data: requestInfo.data,
  209. method: requestInfo.method,
  210. header: requestInfo.header,
  211. url: requestInfo.url,
  212. }
  213. }
  214. }
  215. let requestResult = await urlUpload(requestInfo, this.dataFactory);
  216. return Promise.resolve(requestResult);
  217. } catch (err) {
  218. this.requestError && this.requestError(err);
  219. return Promise.reject(err);
  220. } finally {
  221. if (runRequestStart) {
  222. this.requestEnd && this.requestEnd(requestInfo);
  223. }
  224. }
  225. }
  226. }