common.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {
  2. store
  3. } from '../store/index'
  4. import {
  5. baseUrl
  6. } from './http.js'
  7. /**
  8. * count-- 可选张数
  9. * arr-- 最终结果 [{img1:'全路径',img2:'半路径'},{img1:'全路径',img2:'半路径'}]
  10. */
  11. export let upload = (count = 1) => {
  12. console.log(count);
  13. return new Promise((resolve, reject) => {
  14. uni.chooseImage({
  15. count: count,
  16. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  17. success: function(res) {
  18. uni.showLoading({
  19. title: '上传中'
  20. })
  21. let imgarr = res.tempFilePaths
  22. let arr = []
  23. imgarr.forEach(item => {
  24. uni.uploadFile({
  25. url: baseUrl + '/api/common/upload', //仅为示例,非真实的接口地址
  26. filePath: item,
  27. name: 'file',
  28. success: (res) => {
  29. uni.hideLoading()
  30. let obj = {
  31. img1: JSON.parse(res.data).data.fullurl,
  32. img2: JSON.parse(res.data).data.url
  33. }
  34. arr.push(obj)
  35. if (arr.length == imgarr.length) {
  36. resolve(arr)
  37. }
  38. },
  39. fail: () => {
  40. uni.showToast({
  41. title: '上传失败',
  42. icon: 'none'
  43. })
  44. }
  45. });
  46. })
  47. }
  48. });
  49. })
  50. }
  51. /**
  52. * arr-- 最终结果 [{img1:'全路径',img2:'半路径'},{img1:'全路径',img2:'半路径'}]
  53. */
  54. export let upvideo = () => {
  55. return new Promise((resolve, reject) => {
  56. uni.chooseVideo({
  57. count: 1,
  58. success: function(res) {
  59. uni.showLoading({
  60. title: '上传中'
  61. })
  62. let video = res.tempFilePath
  63. uni.uploadFile({
  64. url: baseUrl + '/api/common/uploadali', //仅为示例,非真实的接口地址
  65. filePath: video,
  66. name: 'file',
  67. success: (res) => {
  68. uni.hideLoading()
  69. console.log('ffffffffffff', res.data);
  70. if (JSON.parse(res.data).code == '4001') {
  71. uni.showToast({
  72. title: JSON.parse(res.data).msg,
  73. icon: 'none'
  74. })
  75. } else {
  76. let obj = {
  77. video1: JSON.parse(res.data).data.fullurl,
  78. video2: JSON.parse(res.data).data.url
  79. }
  80. resolve(obj)
  81. }
  82. },
  83. fail: () => {
  84. uni.showToast({
  85. title: '上传失败',
  86. icon: 'none'
  87. })
  88. }
  89. });
  90. }
  91. });
  92. })
  93. }