upload.js 5.5 KB

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