request-upFiles.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * 2019年4月7日14:17:12
  3. */
  4. let _defaultFile = {
  5. filesFilter: {
  6. 0: 'image',
  7. 1: 'video',
  8. 2: 'none'
  9. },
  10. upOpenDown: false
  11. }
  12. let _down = null; //下载文件对象
  13. import {
  14. RQ
  15. } from './request.js';
  16. class UpFiles extends RQ {
  17. constructor(...arg) {
  18. super(arg);
  19. this.defaultFile = _defaultFile;
  20. this.FunChunk = {
  21. 0: this.AppSelectFiles,
  22. 1: this.otherSelectFiles
  23. };
  24. this.proxy(this.defaultFile, (key, value) => {
  25. if (key === 'upOpenDown' && value === true) {
  26. if (_down !== null) {
  27. return console.error(`'${key}' property is set repeatedly`, this);
  28. }
  29. _down = require('./request-downFiles.js').df;
  30. }
  31. });
  32. }
  33. /**
  34. * 开始上传文件
  35. * 2019年4月7日14:55:15
  36. */
  37. startUpFiles({
  38. path = '',
  39. files = [],
  40. isUp = true,
  41. title = false,
  42. _ADMININDEX=null,
  43. extra = {},
  44. ...args
  45. } = {}, res) {
  46. return new Promise(async (resolve, reject) => {
  47. let upres = [];
  48. if (isUp) { //需要上传到服务器,然后再返回
  49. if (title) {
  50. uni.showLoading({
  51. title,
  52. mask: true,
  53. });
  54. }
  55. for (let i = 0; i < res.length; i++) {
  56. let fileName = files[i] != undefined ? files[i] : files[files.length - 1];
  57. try {
  58. upres.push(
  59. await this.ajaxFile({
  60. index: _ADMININDEX||i,
  61. path: path,
  62. title: false,
  63. filePath: res[i],
  64. fileName,
  65. extra,
  66. ...args
  67. })
  68. )
  69. } catch (e) {
  70. upres.push(e)
  71. }
  72. }
  73. if (title) {
  74. uni.hideLoading();
  75. }
  76. resolve({
  77. uploaded: true,
  78. upres
  79. });
  80. }
  81. return resolve(res);
  82. })
  83. }
  84. /**
  85. * 2019年4月7日15:07:54
  86. * 上传网络资源
  87. */
  88. upnNetRes({
  89. netPath = '',
  90. upPath = '',
  91. files = [],
  92. abort = (finsh,bt) => {},
  93. title = false,
  94. extra = {},
  95. ...args
  96. } = {}) {
  97. return new Promise(async (resolve, reject) => {
  98. const obj = {
  99. uploaded: true,
  100. upres:[],
  101. };
  102. if(netPath.constructor===String){
  103. netPath = netPath.split(',');
  104. }
  105. if(upPath.constructor===String){
  106. upPath=upPath.split(',');
  107. }
  108. for (let i = 0; i < netPath.length; i++) {
  109. try {
  110. const res = await _down.startDownFiles({
  111. path: [netPath[i]],
  112. ...args
  113. });
  114. const uploadRes = await this.startUpFiles({
  115. path: upPath[i]||upPath[upPath.length-1],
  116. _ADMININDEX:i,
  117. files,
  118. isUp: true,
  119. abort,
  120. title,
  121. extra,
  122. ...args
  123. }, res.FilePath);
  124. obj.upres.push(uploadRes.upres[0])
  125. } catch (e) {
  126. obj.upres.push(e)
  127. }
  128. }
  129. resolve(obj);
  130. })
  131. }
  132. /**
  133. * 2019年4月6日16:19:20
  134. * 选择文件
  135. */
  136. selectFiles({
  137. type = 2,
  138. isUp=true,
  139. maximum = 1,
  140. multiple = true,
  141. sizeType = ['original', 'compressed'],
  142. sourceType = ['album','camera'],
  143. upload = {
  144. path: '',
  145. files: [],
  146. isUp: false,
  147. title: false,
  148. abort: bt => {},
  149. extra: {}
  150. },
  151. ...extra
  152. } = {}) {
  153. return new Promise(async (resolve, reject) => {
  154. let merge = {
  155. type,
  156. maximum,
  157. sizeType,
  158. sourceType,
  159. multiple,
  160. ...extra,
  161. }
  162. const res = await this.FunChunk[this.platform](merge);
  163. if(!isUp){
  164. return resolve(res);
  165. }
  166. try {
  167. const uploadRes = await this.startUpFiles(upload, res);
  168. resolve(uploadRes);
  169. } catch (e) {
  170. reject(e);
  171. }
  172. })
  173. }
  174. /**
  175. * App选择文件
  176. */
  177. AppSelectFiles(queryInfo) {
  178. return new Promise(async (resolve, reject) => {
  179. plus.gallery.pick(path => {
  180. resolve(path.files);
  181. }, err => {
  182. reject(err);
  183. }, {
  184. filter: _defaultFile.filesFilter[queryInfo.type],
  185. system: false,
  186. ...queryInfo
  187. });
  188. })
  189. }
  190. /**
  191. * 其他小程序,h5
  192. */
  193. otherSelectFiles(queryInfo) {
  194. return new Promise(async (resolve, reject) => {
  195. uni.chooseImage({
  196. count: queryInfo.maximum,
  197. success: res => {
  198. resolve(res.tempFilePaths);
  199. },
  200. fail: err => {
  201. reject(err)
  202. },
  203. ...queryInfo
  204. });
  205. })
  206. }
  207. }
  208. export default new UpFiles();