upload.js 7.1 KB

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