submit.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import toast from '@/utils/tool/toast/index';
  2. export default {
  3. methods:{
  4. openSubmitLoading(value){
  5. this.submitStatus = true;
  6. clearTimeout(this.submitTimeUse);
  7. this.submitTimeUse = setTimeout(()=>{
  8. toast.loading(value || '提交中');
  9. },300);
  10. },
  11. closeSubmitLoading(value,type='fail'){
  12. this.submitStatus = false;
  13. clearTimeout(this.submitTimeUse);
  14. toast.hideLoading();
  15. value && toast[type](value);
  16. },
  17. // 上传所有图片
  18. uploadImageAll(imageKeys){
  19. let uploadImages = [];
  20. for (let key in imageKeys) {
  21. if (imageKeys.hasOwnProperty(key)) {
  22. if (typeof imageKeys[key] ==='string') { // @ts-ignore
  23. imageKeys[key] = [imageKeys[key]];
  24. }
  25. uploadImages.push(...imageKeys[key].map((item)=> this.uploadImage(item,key)));
  26. }
  27. }
  28. return new Promise(function (resolve,reject){
  29. Promise.all(uploadImages).then((data)=>{
  30. let imageKeyObject = {};
  31. data.map((item)=>{
  32. if (imageKeyObject[item.key] === undefined) {
  33. imageKeyObject[item.key] = [item.value];
  34. } else {
  35. imageKeyObject[item.key].push(item.value);
  36. }
  37. });
  38. for (let key in imageKeyObject) {
  39. if (imageKeyObject.hasOwnProperty(key)) {
  40. // @ts-ignore
  41. imageKeyObject[key] = imageKeyObject[key].join(',');
  42. }
  43. }
  44. resolve(imageKeyObject);
  45. }).catch(reject);
  46. });
  47. },
  48. uploadImage(url,key){
  49. return new Promise((resolve,reject)=>{
  50. if (this.imageCache[url]) return resolve({
  51. value:this.imageCache[url],
  52. key
  53. });
  54. this.$request({
  55. url:'common/upload',
  56. filePath:url,
  57. name:'file',
  58. token:true
  59. }).then((data)=>{
  60. if (data.isSuccess) {
  61. // 获取路径
  62. let path = data.data.url;
  63. // 植入缓存
  64. this.imageCache[url] = path;
  65. resolve({
  66. value:path,
  67. key
  68. });
  69. } else {
  70. reject();
  71. }
  72. }).catch(reject);
  73. });
  74. },
  75. },
  76. created(){
  77. this.imageCache = {};
  78. }
  79. }