jiazhengUpload.vue 3.2 KB

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