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.

234 lines
6.8 KiB

3 years ago
  1. const qiniuUploader = require("./qiniuUploader");
  2. //七牛云上传文件命名
  3. export const randomChar = function(l, url = "") {
  4. const x = "0123456789qwertyuioplkjhgfdsazxcvbnm";
  5. let tmp = "";
  6. let time = new Date();
  7. for (let i = 0; i < l; i++) {
  8. tmp += x.charAt(Math.ceil(Math.random() * 100000000) % x.length);
  9. }
  10. return (
  11. "file/" +
  12. url +
  13. time.getTime() +
  14. tmp
  15. );
  16. }
  17. //图片选择
  18. export const chooseImage = function(data) {
  19. return new Promise((resolve, reject) => {
  20. uni.chooseImage({
  21. count: data.count || 9, //默认9
  22. sizeType: data.sizeType || ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  23. sourceType: data.sourceType || ['album', 'camera'], //从相册选择
  24. success: function(res) {
  25. resolve(res.tempFiles);
  26. },
  27. fail: err => {
  28. reject(err);
  29. }
  30. });
  31. });
  32. }
  33. //视频选择
  34. export const chooseVideo = function(data) {
  35. return new Promise((resolve, reject) => {
  36. uni.chooseVideo({
  37. sourceType: data.sourceType || ['album', 'camera'], //从相册选择
  38. compressed: data.compressed || false, //是否压缩所选的视频源文件,默认值为 true,需要压缩。
  39. maxDuration: data.maxDuration || 60, //拍摄视频最长拍摄时间,单位秒。最长支持 60 秒。
  40. camera: data.camera || 'back', //'front'、'back',默认'back'
  41. success: function(res) {
  42. let files = [{
  43. path: res.tempFilePath
  44. }];
  45. // #ifdef APP-PLUS || H5 || MP-WEIXIN
  46. files[0].duration = res.duration;
  47. files[0].size = res.size;
  48. files[0].height = res.height;
  49. files[0].width = res.width;
  50. // #endif
  51. // #ifdef H5
  52. files[0].name = res.name;
  53. // #endif
  54. resolve(files);
  55. },
  56. fail: err => {
  57. reject(err);
  58. }
  59. });
  60. });
  61. }
  62. // 七牛云上传
  63. export const qiniuUpload = function(requestInfo, getQnToken) {
  64. return new Promise((resolve, reject) => {
  65. if (Array.isArray(requestInfo.files)) {
  66. let len = requestInfo.files.length;
  67. let fileList = new Array;
  68. if (getQnToken) {
  69. getQnToken(qnRes => {
  70. /*
  71. *接口返回参数
  72. *visitPrefix:访问文件的域名
  73. *token:七牛云上传token
  74. *folderPath:上传的文件夹
  75. *region: 地区 默认为SCN
  76. */
  77. let prefixLen = qnRes.visitPrefix.length;
  78. if(qnRes.visitPrefix.charAt(prefixLen - 1) == '/'){
  79. qnRes.visitPrefix = qnRes.visitPrefix.substring(0, prefixLen - 1)
  80. }
  81. uploadFile(0);
  82. function uploadFile(i) {
  83. let item = requestInfo.files[i];
  84. let updateUrl = randomChar(10, qnRes.folderPath);
  85. let fileData = {
  86. fileIndex: i,
  87. files: requestInfo.files,
  88. ...item
  89. };
  90. if (item.name) {
  91. fileData.name = item.name;
  92. let nameArr = item.name.split(".");
  93. updateUrl += "." + nameArr[nameArr.length - 1];
  94. }
  95. // 交给七牛上传
  96. qiniuUploader.upload(item.path || item, (res) => {
  97. fileData.url = res.imageURL;
  98. requestInfo.onEachUpdate && requestInfo.onEachUpdate({
  99. url: res.imageURL,
  100. ...fileData
  101. });
  102. fileList.push(res.imageURL);
  103. if (len - 1 > i) {
  104. uploadFile(i + 1);
  105. } else {
  106. resolve(fileList);
  107. }
  108. }, (error) => {
  109. reject(error);
  110. }, {
  111. region: qnRes.region || 'SCN', //地区
  112. domain: qnRes.visitPrefix, // bucket 域名,下载资源时用到。
  113. key: updateUrl,
  114. uptoken: qnRes.token, // 由其他程序生成七牛 uptoken
  115. uptokenURL: 'UpTokenURL.com/uptoken' // 上传地址
  116. }, (res) => {
  117. console.log(requestInfo);
  118. requestInfo.onProgressUpdate && requestInfo.onProgressUpdate(Object.assign({}, fileData, res));
  119. // console.log('上传进度', res.progress)
  120. // console.log('已经上传的数据长度', res.totalBytesSent)
  121. // console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
  122. });
  123. }
  124. });
  125. } else {
  126. reject({
  127. errMsg: "请添加七牛云回调方法:getQnToken",
  128. statusCode: 0
  129. });
  130. }
  131. } else {
  132. reject({
  133. errMsg: "files 必须是数组类型",
  134. statusCode: 0
  135. });
  136. };
  137. });
  138. }
  139. // 服务器URL上传
  140. export const urlUpload = function(requestInfo, dataFactory) {
  141. console.log('urlUpload',requestInfo);
  142. return new Promise((resolve, reject) => {
  143. // 本地文件上传去掉默认Content-Type
  144. if (requestInfo.header['Content-Type']) {
  145. delete requestInfo.header['Content-Type'];
  146. }
  147. // 本地文件上传去掉默认Content-Type
  148. if (requestInfo.header['content-type']) {
  149. delete requestInfo.header['content-type'];
  150. }
  151. if (Array.isArray(requestInfo.files)) {
  152. console.log('requestInfo.files',requestInfo.files);
  153. // #ifdef MP
  154. console.log('456mp')
  155. const len = requestInfo.files.length - 1;
  156. let fileList = new Array;
  157. fileUpload(0);
  158. function fileUpload(i) {
  159. console.log('fileUpload');
  160. let item = requestInfo.files[i];
  161. let fileData = {
  162. fileIndex: i,
  163. files: requestInfo.files,
  164. ...item
  165. };
  166. let config = {
  167. url: requestInfo.url,
  168. filePath: item.path,
  169. files:requestInfo.files,
  170. header: requestInfo.header, //加入请求头
  171. name: requestInfo.name || "file",
  172. success: (response) => {
  173. console.log('456123');
  174. //是否用外部的数据处理方法
  175. if (requestInfo.isFactory && dataFactory) {
  176. console.log('waibu');
  177. //数据处理
  178. dataFactory({
  179. ...requestInfo,
  180. response: response,
  181. }).then(data => {
  182. console.log('data888',data);
  183. fileList.push(data);
  184. requestInfo.onEachUpdate && requestInfo.onEachUpdate({
  185. data: data,
  186. ...fileData
  187. });
  188. if (len <= i) {
  189. resolve(fileList);
  190. } else {
  191. fileUpload(i + 1);
  192. }
  193. },err => {
  194. reject(err);
  195. });
  196. } else {
  197. console.log('nb');
  198. requestInfo.onEachUpdate && requestInfo.onEachUpdate({
  199. data: response,
  200. ...fileData
  201. });
  202. fileList.push(response);
  203. if (len <= i) {
  204. resolve(fileList);
  205. } else {
  206. fileUpload(i + 1);
  207. }
  208. }
  209. },
  210. fail: (err) => {
  211. reject(err);
  212. }
  213. };
  214. if (requestInfo.data) {
  215. config.formData = requestInfo.data;
  216. }
  217. const uploadTask = uni.uploadFile(config);
  218. uploadTask.onProgressUpdate(res => {
  219. requestInfo.onProgressUpdate && requestInfo.onProgressUpdate(Object.assign({}, fileData, res));
  220. });
  221. }
  222. // #endif
  223. } else {
  224. reject({
  225. errMsg: "files 必须是数组类型",
  226. statusCode: 0
  227. });
  228. }
  229. });
  230. console.log('urlUploadEND');
  231. }