helper.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import config from '../config.js'
  2. export default {
  3. /**
  4. * 图片上传,支持多图
  5. * @author nullfeng
  6. * @param Array <images> 本地路径数组
  7. * @param Function [complete] 回调函数()
  8. * @return Promise
  9. */
  10. uploadFiles(images, type,data,complete){
  11. //console.log(config.apiUri)
  12. if(type=='images' || type=='avatar') var url=config.apiUri+'upload.php?act=uploadImage';
  13. else if(type=='Vedio')var url=config.apiUri+'upload.php?act=uploadVedio';
  14. else var url=config.apiUri+'upload.php?act=uploadFile';
  15. let queue = [];
  16. images.forEach(image=>{
  17. queue.push(new Promise((resolve,reject)=>{
  18. uni.uploadFile({
  19. url: url,
  20. filePath: image,
  21. name: 'file',
  22. fileType: type,
  23. formData:data,
  24. header: {
  25. },
  26. success: res => {
  27. let result = JSON.parse(res.data);
  28. if(result.uploaded) {
  29. var url=config.imgUri+result.url;
  30. resolve(url);
  31. } else {
  32. uni.showModal({
  33. content: result.message,
  34. showCancel: false
  35. });
  36. }
  37. },
  38. fail: err => {
  39. uni.showModal({
  40. content: err.errMsg,
  41. showCancel: false
  42. });
  43. reject();
  44. }
  45. });
  46. }));
  47. });
  48. if(!queue.length) queue.push(new Promise((resolve,reject)=>{resolve("")}));
  49. return new Promise((resolve,reject)=>{
  50. Promise.all(queue).then(res=>{
  51. complete && complete(res.join('|'));
  52. resolve(res.join('|'));
  53. }).catch(res=>{
  54. reject();
  55. });
  56. });
  57. },
  58. /**
  59. * 格式化时间
  60. */
  61. formatDate(date, fmt) {
  62. if(typeof(date)=="string") {
  63. date = new Date(date.replace('年','/').replace('月','/').replace('日','').replace(/-/g,'/'));
  64. }
  65. var o = {
  66. "m+": date.getMonth() + 1, //月份
  67. "d+": date.getDate(), //日
  68. "h+": date.getHours(), //小时
  69. "i+": date.getMinutes(), //分
  70. "s+": date.getSeconds(), //秒
  71. "q+": Math.floor((date.getMonth() + 3) / 3), //季度
  72. };
  73. if (/(y+)/.test(fmt))
  74. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  75. for (var k in o)
  76. if (new RegExp("(" + k + ")").test(fmt))
  77. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  78. return fmt;
  79. }
  80. };