jiazhengUpload.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <view v-if="changjingType =='shenfenzheng'" class="sfz-upload-box" @click="showActionSheet">
  3. <view v-if="!imageUrl" class="sfz-content"><text>+</text> 添加身份证</view>
  4. <image class="sfz-image" v-else :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 class="other-image" :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. // 判断是否是H5环境
  51. // #ifdef H5
  52. this.chooseImage('album'); // H5环境下直接选择相册
  53. // #endif
  54. // #ifndef H5
  55. uni.showActionSheet({
  56. itemList: ['拍照', '从相册选择'],
  57. success: (res) => {
  58. if (res.tapIndex === 0) {
  59. this.chooseImage('camera');
  60. } else if (res.tapIndex === 1) {
  61. this.chooseImage('album');
  62. }
  63. }
  64. });
  65. // #endif
  66. },
  67. chooseImage(sourceType) {
  68. uni.chooseImage({
  69. count: 1, // 只能选择一张图片
  70. sourceType: [sourceType], // 'camera' 或 'album'
  71. success: (res) => {
  72. const filePath = res.tempFilePaths[0];
  73. this.uploadFileToAliyun(filePath);
  74. }
  75. });
  76. },
  77. uploadFileToAliyun(filePath) {
  78. console.log('filePath', filePath);
  79. const loading = uni.showLoading({
  80. title: '上传中...',
  81. mask: true
  82. });
  83. try {
  84. const suffix = filePath.split('.').pop();
  85. let req = {
  86. prefix: 'resource/',
  87. suffix: suffix
  88. }
  89. getAliyunPolicy(req).then(res => {
  90. let that = this;
  91. if (res.code == 0) {
  92. const policyData = res.data;
  93. console.log('policyData', policyData);
  94. // #ifdef H5
  95. const formData = new FormData();
  96. formData.append('key', policyData.key);
  97. formData.append('policy', policyData.policy);
  98. formData.append('OSSAccessKeyId', policyData.accessid);
  99. formData.append('signature', policyData.signature);
  100. formData.append('success_action_status', '200');
  101. fetch(filePath)
  102. .then(response => response.blob())
  103. .then(blob => {
  104. formData.append('file', blob, policyData.key);
  105. fetch(policyData.uploadUrl, {
  106. method: 'POST',
  107. body: formData
  108. })
  109. .then(response => {
  110. if (response.ok) {
  111. that.imageUrl = `${policyData.downloadUrl}/${policyData.key}`;
  112. uni.showToast({
  113. title: '上传成功',
  114. icon: 'success'
  115. });
  116. that.$emit('getFileUrl', that.imageUrl);
  117. } else {
  118. uni.showToast({
  119. title: '上传失败',
  120. });
  121. }
  122. })
  123. .catch(error => {
  124. console.error('上传失败:', error);
  125. uni.showToast({
  126. title: '上传失败',
  127. icon: 'none'
  128. });
  129. });
  130. })
  131. .catch(error => {
  132. console.error('文件转换失败:', error);
  133. uni.showToast({
  134. title: '文件转换失败',
  135. icon: 'none'
  136. });
  137. });
  138. // #endif
  139. // #ifndef H5
  140. // 非 H5 环境下使用 uni.uploadFile 上传
  141. uni.uploadFile({
  142. url: policyData.uploadUrl,
  143. filePath: filePath,
  144. name: 'file',
  145. formData: {
  146. key: policyData.key,
  147. policy: policyData.policy,
  148. OSSAccessKeyId: policyData.accessid,
  149. signature: policyData.signature,
  150. success_action_status: '200'
  151. },
  152. header: {
  153. 'Content-Type': 'multipart/form-data'
  154. },
  155. success(uploadRes) {
  156. console.log('uploadRes', uploadRes);
  157. if (uploadRes.statusCode === 200) {
  158. that.imageUrl = `${policyData.downloadUrl}/${policyData.key}`;
  159. uni.showToast({
  160. title: '上传成功',
  161. icon: 'success'
  162. });
  163. that.$emit('getFileUrl', that.imageUrl);
  164. } else {
  165. uni.showToast({
  166. title: '上传失败',
  167. });
  168. }
  169. },
  170. fail(err) {
  171. console.log('err', err);
  172. }
  173. });
  174. // #endif
  175. } else {
  176. uni.showToast({
  177. title: '获取凭证失败',
  178. });
  179. }
  180. });
  181. } catch (error) {
  182. uni.showToast({
  183. title: '上传失败',
  184. icon: 'none'
  185. });
  186. console.error('上传失败:', error);
  187. } finally {
  188. uni.hideLoading();
  189. }
  190. }
  191. // uploadFileToAliyun(filePath) {
  192. // console.log('filePath', filePath);
  193. // const loading = uni.showLoading({
  194. // title: '上传中...',
  195. // mask: true
  196. // });
  197. // try {
  198. // const suffix = filePath.split('.').pop();
  199. // let req = {
  200. // prefix: 'resource/',
  201. // suffix: suffix
  202. // }
  203. // getAliyunPolicy(req).then(res => {
  204. // let that = this
  205. // if (res.code == 0) {
  206. // const policyData = res.data;
  207. // console.log('policyData', policyData);
  208. // const formData = {
  209. // key: policyData.key,
  210. // policy: policyData.policy,
  211. // OSSAccessKeyId: policyData.accessid,
  212. // signature: policyData.signature,
  213. // success_action_status: '200',
  214. // file: {
  215. // name: policyData.key,
  216. // uri: filePath
  217. // }
  218. // };
  219. // uni.uploadFile({
  220. // url: policyData.uploadUrl,
  221. // filePath: filePath,
  222. // name: 'file',
  223. // formData: formData,
  224. // header: {
  225. // 'Content-Type': 'multipart/form-data'
  226. // },
  227. // success(uploadRes) {
  228. // console.log('uploadRes', uploadRes);
  229. // if (uploadRes.statusCode === 200) {
  230. // that.imageUrl = `${policyData.downloadUrl}/${policyData.key}`;
  231. // uni.showToast({
  232. // title: '上传成功',
  233. // icon: 'success'
  234. // });
  235. // console.log('imageUrl', that.imageUrl);
  236. // that.$emit('getFileUrl', that.imageUrl);
  237. // } else {
  238. // uni.showToast({
  239. // title: '上传失败',
  240. // });
  241. // return false
  242. // }
  243. // },
  244. // fail(err) {
  245. // console.log('err', err);
  246. // }
  247. // });
  248. // } else {
  249. // uni.showToast({
  250. // title: '获取凭证失败',
  251. // });
  252. // return false
  253. // }
  254. // })
  255. // } catch (error) {
  256. // uni.showToast({
  257. // title: '上传失败',
  258. // icon: 'none'
  259. // });
  260. // console.error('上传失败:', error);
  261. // } finally {
  262. // uni.hideLoading();
  263. // }
  264. // },
  265. }
  266. };
  267. </script>