jiazhengUpload.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <template>
  2. <view class="upload-btn" @click="showActionSheet">
  3. <text>+</text>
  4. </view>
  5. <viwe v-if="bottomText" class="bottom-text">
  6. {{bottomText}}
  7. </viwe>
  8. </template>
  9. <script>
  10. import {
  11. getAliyunPolicy
  12. } from "@/api/jiazheng.js"
  13. export default {
  14. props: {
  15. bottomText: { // 试题序号
  16. type: String
  17. }
  18. },
  19. methods: {
  20. showActionSheet() {
  21. uni.showActionSheet({
  22. itemList: ['拍照', '从相册选择'],
  23. success: (res) => {
  24. if (res.tapIndex === 0) {
  25. this.chooseImage('camera');
  26. } else if (res.tapIndex === 1) {
  27. this.chooseImage('album');
  28. }
  29. }
  30. });
  31. },
  32. chooseImage(sourceType) {
  33. uni.chooseImage({
  34. count: 1, // 只能选择一张图片
  35. sourceType: [sourceType], // 'camera' 或 'album'
  36. success: (res) => {
  37. const filePath = res.tempFilePaths[0];
  38. this.uploadFileToAliyun(filePath);
  39. }
  40. });
  41. },
  42. uploadFileToAliyun(filePath) {
  43. console.log('filePath', filePath);
  44. const loading = uni.showLoading({
  45. title: '上传中...',
  46. mask: true
  47. });
  48. try {
  49. const suffix = filePath.split('.').pop();
  50. let req = {
  51. prefix: 'resource/',
  52. suffix: suffix
  53. }
  54. getAliyunPolicy(req).then(res => {
  55. if (res.code == 0) {
  56. const policyData = res.data;
  57. console.log('policyData', policyData);
  58. const formData = {
  59. key: policyData.key,
  60. policy: policyData.policy,
  61. OSSAccessKeyId: policyData.accessid,
  62. signature: policyData.signature,
  63. success_action_status: '200',
  64. file: {
  65. name: policyData.key,
  66. uri: filePath
  67. }
  68. };
  69. uni.uploadFile({
  70. url: policyData.uploadUrl,
  71. filePath: filePath,
  72. name: 'file',
  73. formData: formData,
  74. header: {
  75. 'Content-Type': 'multipart/form-data'
  76. },
  77. success(uploadRes) {
  78. console.log('uploadRes', uploadRes);
  79. if (uploadRes.statusCode === 200) {
  80. const imageUrl = `${policyData.downloadUrl}/${policyData.key}`;
  81. uni.showToast({
  82. title: '上传成功',
  83. icon: 'success'
  84. });
  85. console.log('imageUrl', imageUrl);
  86. //this.$emit('getFileUrl', imageUrl);
  87. } else {
  88. uni.showToast({
  89. title: '上传失败',
  90. });
  91. return false
  92. }
  93. },
  94. fail(err) {
  95. console.log('err', err);
  96. }
  97. });
  98. } else {
  99. uni.showToast({
  100. title: '获取凭证失败',
  101. });
  102. return false
  103. }
  104. })
  105. } catch (error) {
  106. uni.showToast({
  107. title: '上传失败',
  108. icon: 'none'
  109. });
  110. console.error('上传失败:', error);
  111. } finally {
  112. uni.hideLoading();
  113. }
  114. },
  115. }
  116. };
  117. </script>
  118. <style scoped>
  119. .upload-btn {
  120. width: 100px;
  121. height: 100px;
  122. border: 1px dashed #ccc;
  123. display: flex;
  124. align-items: center;
  125. justify-content: center;
  126. font-size: 24px;
  127. color: #ccc;
  128. margin: 5px;
  129. border-radius: 5px;
  130. }
  131. .bottom-text {
  132. font-size: 12px;
  133. color: #bdbdbd;
  134. }
  135. </style>