upload.js 5.4 KB

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