uni-events-helper-wx
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
5.8 KiB

3 years ago
  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. console.log('files888888',options.files);
  132. // 选择完成回调
  133. options.onSelectComplete && options.onSelectComplete(options.files);
  134. } catch (err) {
  135. this.requestError && this.requestError(err);
  136. return Promise.reject(err);
  137. }
  138. if (options.files) {
  139. return this.urlFileUpload(options);
  140. }
  141. }
  142. //本地服务器文件上传方法
  143. async urlFileUpload() {
  144. console.log('urlFileUpload');
  145. let requestInfo = {
  146. method: "FILE"
  147. };
  148. if (arguments[0]) {
  149. if (typeof(arguments[0]) == "string") {
  150. requestInfo.url = arguments[0];
  151. } else if (typeof(arguments[0]) == "object") {
  152. requestInfo = Object.assign(requestInfo, arguments[0]);
  153. }
  154. }
  155. if (arguments[1] && typeof(arguments[1]) == "object") {
  156. requestInfo = Object.assign(requestInfo, arguments[1]);
  157. }
  158. if (!requestInfo.url && this.defaultUploadUrl) {
  159. requestInfo.url = this.defaultUploadUrl;
  160. }
  161. // 请求数据
  162. // 是否运行过请求开始钩子
  163. let runRequestStart = false;
  164. try {
  165. if (!requestInfo.url) {
  166. throw {
  167. errMsg: "【request】文件上传缺失数据url",
  168. statusCode: 0,
  169. data: requestInfo.data,
  170. method: requestInfo.method,
  171. header: requestInfo.header,
  172. url: requestInfo.url,
  173. }
  174. }
  175. // 数据合并
  176. requestInfo = mergeConfig(this, requestInfo);
  177. // 代表之前运行到这里
  178. runRequestStart = true;
  179. //请求前回调
  180. if (this.requestStart) {
  181. let requestStart = this.requestStart(requestInfo);
  182. if (typeof requestStart == "object") {
  183. console.log('objects789');
  184. let changekeys = ["data", "header", "isPrompt", "load", "isFactory", "files"];
  185. changekeys.forEach(key => {
  186. requestInfo[key] = requestStart[key];
  187. });
  188. console.log('objects78910111213141516 requestInfo',requestInfo);
  189. } else {
  190. throw {
  191. errMsg: "【request】请求开始拦截器未通过",
  192. statusCode: 0,
  193. data: requestInfo.data,
  194. method: requestInfo.method,
  195. header: requestInfo.header,
  196. url: requestInfo.url,
  197. }
  198. }
  199. }
  200. console.log('789465');
  201. let requestResult = await urlUpload(requestInfo, this.dataFactory);
  202. console.log('321456',requestResult);
  203. return Promise.resolve(requestResult);
  204. } catch (err) {
  205. this.requestError && this.requestError(err);
  206. return Promise.reject(err);
  207. } finally {
  208. if (runRequestStart) {
  209. this.requestEnd && this.requestEnd(requestInfo);
  210. }
  211. }
  212. }
  213. }