jiazhengUpload.vue 3.3 KB

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