jiazhengUpload.vue 3.5 KB

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