jiazhengUpload.vue 3.4 KB

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