order-file-upload.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <template>
  2. <view class="order-file-upload">
  3. <view class="upload-section">
  4. <view class="upload-title">
  5. <text class="title-text">{{title}}</text>
  6. <text class="upload-btn" @click="handleUpload">上传文件</text>
  7. </view>
  8. <view class="file-list" v-if="fileList.length > 0">
  9. <view class="file-item" v-for="(item, index) in fileList" :key="index">
  10. <view class="file-info">
  11. <text class="file-name">{{item.fileName}}</text>
  12. <text class="file-size">{{item.fileSize}}</text>
  13. </view>
  14. <view class="file-actions">
  15. <text class="delete-btn" @click="handleDelete(index)">删除</text>
  16. </view>
  17. </view>
  18. </view>
  19. <view class="upload-tip" v-else>
  20. <text class="tip-text">{{tipText}}</text>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. name: 'OrderFileUpload',
  28. props: {
  29. title: {
  30. type: String,
  31. default: '文件上传'
  32. },
  33. fileType: {
  34. type: String,
  35. required: true
  36. },
  37. fileList: {
  38. type: Array,
  39. default: () => []
  40. },
  41. tipText: {
  42. type: String,
  43. default: '点击上传文件'
  44. },
  45. maxSize: {
  46. type: Number,
  47. default: 10 * 1024 * 1024 // 10MB
  48. },
  49. allowTypes: {
  50. type: Array,
  51. default: () => ['pdf', 'doc', 'docx', 'txt', 'jpg', 'jpeg', 'png', 'gif', 'mp3', 'wav', 'mp4']
  52. }
  53. },
  54. data() {
  55. return {
  56. uploadList: []
  57. }
  58. },
  59. watch: {
  60. fileList: {
  61. handler(newVal) {
  62. this.uploadList = [...newVal];
  63. },
  64. immediate: true
  65. }
  66. },
  67. methods: {
  68. // 处理文件上传
  69. handleUpload() {
  70. const lemonjkFileSelect = uni.requireNativePlugin('lemonjk-FileSelect');
  71. //4.0.0+ 使用示例及高级筛选器配置示例
  72. lemonjkFileSelect.showNativePicker({
  73. pathScope: "/DCIM/Camera",
  74. mimeType: "*/*",
  75. utisType: "public.data",
  76. multi: 'yes'
  77. }, async result => {
  78. const {
  79. code,
  80. files
  81. } = result;
  82. if (code == 0) {
  83. // 选择的是否全部为图片
  84. const tempFilePaths = files.map(v => "file://" + v.filePath);
  85. Promise.all(tempFilePaths.map(v => this.uploadFile(v))).then(() => {
  86. uni.$u.toast('上传成功');
  87. }).catch(() => {
  88. uni.$u.toast("上传失败");
  89. })
  90. } else if (result.code == 1001) {
  91. uni.showModal({
  92. title: "需要文件访问权限",
  93. content: "您还未授权本应用读取文件。为保证您可以正常上传文件,请在权限设置页面打开文件访问权限(不同手机厂商表述可能略有差异)请根据自己手机品牌设置",
  94. confirmText: "去授权",
  95. cancelText: "算了",
  96. success(e) {
  97. if (e.confirm) {
  98. // 跳转到应用设置页
  99. lemonjkFileSelect.gotoSetting();
  100. }
  101. }
  102. })
  103. }
  104. })
  105. },
  106. // 上传文件
  107. async uploadFile(fileUrl) {
  108. try {
  109. uni.showLoading({
  110. title: '上传中...',
  111. mask: true
  112. });
  113. // 调用统一的上传接口
  114. const {
  115. data
  116. } = await uni.$u.api.uploadFile(fileUrl);
  117. const fileObj = {
  118. fileSize: data.fileSize,
  119. fileSuffix: data.fileSuffix,
  120. fileName: data.name,
  121. fileUrl: data.url,
  122. orderFileType : this.fileType
  123. };
  124. this.uploadList.push(fileObj);
  125. this.$emit('update:fileList', this.uploadList);
  126. this.$emit('change', this.uploadList);
  127. uni.hideLoading();
  128. } catch (error) {
  129. uni.hideLoading();
  130. console.error('文件上传失败:', error);
  131. uni.$u.toast('上传失败,请重试');
  132. }
  133. },
  134. // 删除文件
  135. handleDelete(index) {
  136. uni.showModal({
  137. title: '提示',
  138. content: '确定要删除该文件吗?',
  139. success: (res) => {
  140. if (res.confirm) {
  141. this.uploadList.splice(index, 1);
  142. this.$emit('update:fileList', this.uploadList);
  143. this.$emit('change', {
  144. type: this.fileType,
  145. fileList: this.uploadList
  146. });
  147. }
  148. }
  149. });
  150. }
  151. }
  152. }
  153. </script>
  154. <style lang="scss" scoped>
  155. .order-file-upload {
  156. .upload-section {
  157. background: #fff;
  158. margin-bottom: 20rpx;
  159. border-radius: 16rpx;
  160. overflow: hidden;
  161. }
  162. .upload-title {
  163. display: flex;
  164. justify-content: space-between;
  165. align-items: center;
  166. padding: 30rpx 40rpx;
  167. border-bottom: 1rpx solid #f5f5f5;
  168. .title-text {
  169. font-size: 32rpx;
  170. font-weight: 500;
  171. color: #333;
  172. }
  173. .upload-btn {
  174. padding: 12rpx 24rpx;
  175. background: #007aff;
  176. color: #fff;
  177. border-radius: 8rpx;
  178. font-size: 28rpx;
  179. }
  180. }
  181. .file-list {
  182. padding: 20rpx 40rpx;
  183. }
  184. .file-item {
  185. display: flex;
  186. justify-content: space-between;
  187. align-items: center;
  188. padding: 20rpx 0;
  189. border-bottom: 1rpx solid #f5f5f5;
  190. &:last-child {
  191. border-bottom: none;
  192. }
  193. }
  194. .file-info {
  195. flex: 1;
  196. min-width: 0; // 防止flex子元素溢出
  197. .file-name {
  198. display: block;
  199. font-size: 30rpx;
  200. color: #333;
  201. margin-bottom: 8rpx;
  202. overflow: hidden;
  203. text-overflow: ellipsis;
  204. white-space: nowrap;
  205. max-width: 400rpx; // 设置最大宽度防止过长
  206. }
  207. .file-size {
  208. font-size: 26rpx;
  209. color: #999;
  210. }
  211. }
  212. .file-actions {
  213. .delete-btn {
  214. padding: 12rpx 24rpx;
  215. background: #ff4757;
  216. color: #fff;
  217. border-radius: 8rpx;
  218. font-size: 26rpx;
  219. }
  220. }
  221. .upload-tip {
  222. padding: 40rpx;
  223. text-align: center;
  224. .tip-text {
  225. font-size: 28rpx;
  226. color: #999;
  227. }
  228. }
  229. }
  230. </style>