jiazhengUpload.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 axios from 'axios';
  19. import {
  20. getAliyunPolicy
  21. } from "@/api/jiazheng.js"
  22. import cacheManager from "@/utils/cacheManager.js";
  23. export default {
  24. emits: ['getFileUrl'], // 声明自定义事件
  25. data() {
  26. return {
  27. imageUrl: ""
  28. }
  29. },
  30. props: {
  31. bottomText: { // 试题序号
  32. type: String
  33. },
  34. changjingType: {
  35. type: String,
  36. default: ''
  37. },
  38. img: {
  39. type: String,
  40. default: ''
  41. }
  42. },
  43. watch: {
  44. img: {
  45. handler(newVal) {
  46. this.imageUrl = newVal
  47. }
  48. },
  49. },
  50. methods: {
  51. showActionSheet() {
  52. // 判断是否是H5环境
  53. // #ifdef H5
  54. this.chooseImage('album'); // H5环境下直接选择相册
  55. // #endif
  56. // #ifndef H5
  57. uni.showActionSheet({
  58. itemList: ['拍照', '从相册选择'],
  59. success: (res) => {
  60. if (res.tapIndex === 0) {
  61. this.chooseImage('camera');
  62. } else if (res.tapIndex === 1) {
  63. this.chooseImage('album');
  64. }
  65. }
  66. });
  67. // #endif
  68. },
  69. chooseImage(sourceType) {
  70. uni.chooseImage({
  71. count: 1, // 只能选择一张图片
  72. sourceType: [sourceType], // 'camera' 或 'album'
  73. success: (res) => {
  74. console.log('res', res);
  75. // #ifndef H5
  76. const filePath = res.tempFilePaths[0];
  77. this.uploadFileToAliyun(filePath);
  78. // #endif
  79. // #ifdef H5
  80. const filePath = res.tempFiles[0];
  81. this.uploadFileToAliyun(filePath);
  82. // #endif
  83. }
  84. });
  85. },
  86. uploadFileToAliyun(filePath) {
  87. console.log('filePath', filePath);
  88. const loading = uni.showLoading({
  89. title: '上传中...',
  90. mask: true
  91. });
  92. try {
  93. // #ifdef H5
  94. const suffixList = filePath.name.split('.');
  95. let req = {
  96. prefix: 'resource/',
  97. suffix: suffixList[suffixList.length - 1],
  98. }
  99. // #endif
  100. // #ifndef H5
  101. const suffix = filePath.split('.').pop();
  102. let req = {
  103. prefix: 'resource/',
  104. suffix: suffix
  105. }
  106. // #endif
  107. getAliyunPolicy(req).then(res => {
  108. let that = this;
  109. if (res.code == 0) {
  110. const policyData = res.data;
  111. console.log('policyData', policyData);
  112. // #ifdef H5
  113. const formData = new FormData();
  114. formData.append('key', policyData.key);
  115. formData.append('policy', policyData.policy);
  116. formData.append('OSSAccessKeyId', policyData.accessid);
  117. formData.append('signature', policyData.signature);
  118. formData.append('success_action_status', '200');
  119. formData.append('file', filePath);
  120. axios.post(policyData.uploadUrl, formData)
  121. .then(alRes => {
  122. if (alRes.status === 200) {
  123. that.imageUrl =
  124. `${policyData.downloadUrl}/${policyData.key}`;
  125. uni.showToast({
  126. title: '上传成功',
  127. icon: 'success'
  128. });
  129. that.$emit('getFileUrl', that.imageUrl);
  130. } else {
  131. uni.showToast({
  132. title: '上传失败',
  133. });
  134. }
  135. })
  136. // #endif
  137. // #ifndef H5
  138. console.log('appappappappappapp');
  139. // 非 H5 环境下使用 uni.uploadFile 上传
  140. uni.uploadFile({
  141. url: policyData.uploadUrl,
  142. filePath: filePath,
  143. name: 'file',
  144. formData: {
  145. key: policyData.key,
  146. policy: policyData.policy,
  147. OSSAccessKeyId: policyData.accessid,
  148. signature: policyData.signature,
  149. success_action_status: '200'
  150. },
  151. header: {
  152. 'Content-Type': 'multipart/form-data'
  153. },
  154. success(uploadRes) {
  155. console.log('uploadRes', uploadRes);
  156. if (uploadRes.statusCode === 200) {
  157. that.imageUrl = `${policyData.downloadUrl}/${policyData.key}`;
  158. uni.showToast({
  159. title: '上传成功',
  160. icon: 'success'
  161. });
  162. that.$emit('getFileUrl', that.imageUrl);
  163. } else {
  164. uni.showToast({
  165. title: '上传失败',
  166. });
  167. }
  168. },
  169. fail(err) {
  170. console.log('err', err);
  171. }
  172. });
  173. // #endif
  174. } else {
  175. uni.showToast({
  176. title: '获取凭证失败',
  177. });
  178. }
  179. });
  180. } catch (error) {
  181. uni.showToast({
  182. title: '上传失败',
  183. icon: 'none'
  184. });
  185. console.error('上传失败:', error);
  186. } finally {
  187. uni.hideLoading();
  188. }
  189. }
  190. // uploadFileToAliyun(filePath) {
  191. // console.log('filePath', filePath);
  192. // const loading = uni.showLoading({
  193. // title: '上传中...',
  194. // mask: true
  195. // });
  196. // try {
  197. // const suffix = filePath.split('.').pop();
  198. // let req = {
  199. // prefix: 'resource/',
  200. // suffix: suffix
  201. // }
  202. // getAliyunPolicy(req).then(res => {
  203. // let that = this
  204. // if (res.code == 0) {
  205. // const policyData = res.data;
  206. // console.log('policyData', policyData);
  207. // const formData = {
  208. // key: policyData.key,
  209. // policy: policyData.policy,
  210. // OSSAccessKeyId: policyData.accessid,
  211. // signature: policyData.signature,
  212. // success_action_status: '200',
  213. // file: {
  214. // name: policyData.key,
  215. // uri: filePath
  216. // }
  217. // };
  218. // uni.uploadFile({
  219. // url: policyData.uploadUrl,
  220. // filePath: filePath,
  221. // name: 'file',
  222. // formData: formData,
  223. // header: {
  224. // 'Content-Type': 'multipart/form-data'
  225. // },
  226. // success(uploadRes) {
  227. // console.log('uploadRes', uploadRes);
  228. // if (uploadRes.statusCode === 200) {
  229. // that.imageUrl = `${policyData.downloadUrl}/${policyData.key}`;
  230. // uni.showToast({
  231. // title: '上传成功',
  232. // icon: 'success'
  233. // });
  234. // console.log('imageUrl', that.imageUrl);
  235. // that.$emit('getFileUrl', that.imageUrl);
  236. // } else {
  237. // uni.showToast({
  238. // title: '上传失败',
  239. // });
  240. // return false
  241. // }
  242. // },
  243. // fail(err) {
  244. // console.log('err', err);
  245. // }
  246. // });
  247. // } else {
  248. // uni.showToast({
  249. // title: '获取凭证失败',
  250. // });
  251. // return false
  252. // }
  253. // })
  254. // } catch (error) {
  255. // uni.showToast({
  256. // title: '上传失败',
  257. // icon: 'none'
  258. // });
  259. // console.error('上传失败:', error);
  260. // } finally {
  261. // uni.hideLoading();
  262. // }
  263. // },
  264. }
  265. };
  266. </script>