| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- <template>
- <view class="order-file-upload">
- <view class="upload-section">
- <view class="upload-title">
- <text class="title-text">{{title}}</text>
- <text class="upload-btn" @click="handleUpload">上传文件</text>
- </view>
- <view class="file-list" v-if="fileList.length > 0">
- <view class="file-item" v-for="(item, index) in fileList" :key="index">
- <view class="file-info">
- <text class="file-name">{{item.fileName}}</text>
- <text class="file-size">{{item.fileSize}}</text>
- </view>
- <view class="file-actions">
- <text class="delete-btn" @click="handleDelete(index)">删除</text>
- </view>
- </view>
- </view>
- <view class="upload-tip" v-else>
- <text class="tip-text">{{tipText}}</text>
- </view>
- </view>
- </view>
- </template>
- <script>
- export default {
- name: 'OrderFileUpload',
- props: {
- title: {
- type: String,
- default: '文件上传'
- },
- fileType: {
- type: String,
- required: true
- },
- fileList: {
- type: Array,
- default: () => []
- },
- tipText: {
- type: String,
- default: '点击上传文件'
- },
- maxSize: {
- type: Number,
- default: 10 * 1024 * 1024 // 10MB
- },
- allowTypes: {
- type: Array,
- default: () => ['pdf', 'doc', 'docx', 'txt', 'jpg', 'jpeg', 'png', 'gif', 'mp3', 'wav', 'mp4']
- }
- },
- data() {
- return {
- uploadList: []
- }
- },
- watch: {
- fileList: {
- handler(newVal) {
- this.uploadList = [...newVal];
- },
- immediate: true
- }
- },
- methods: {
- // 处理文件上传
- handleUpload() {
- const lemonjkFileSelect = uni.requireNativePlugin('lemonjk-FileSelect');
- //4.0.0+ 使用示例及高级筛选器配置示例
- lemonjkFileSelect.showNativePicker({
- pathScope: "/DCIM/Camera",
- mimeType: "*/*",
- utisType: "public.data",
- multi: 'yes'
- }, async result => {
- const {
- code,
- files
- } = result;
- if (code == 0) {
- // 选择的是否全部为图片
- const tempFilePaths = files.map(v => "file://" + v.filePath);
- Promise.all(tempFilePaths.map(v => this.uploadFile(v))).then(() => {
- uni.$u.toast('上传成功');
- }).catch(() => {
- uni.$u.toast("上传失败");
- })
- } else if (result.code == 1001) {
- uni.showModal({
- title: "需要文件访问权限",
- content: "您还未授权本应用读取文件。为保证您可以正常上传文件,请在权限设置页面打开文件访问权限(不同手机厂商表述可能略有差异)请根据自己手机品牌设置",
- confirmText: "去授权",
- cancelText: "算了",
- success(e) {
- if (e.confirm) {
- // 跳转到应用设置页
- lemonjkFileSelect.gotoSetting();
- }
- }
- })
- }
- })
- },
- // 上传文件
- async uploadFile(fileUrl) {
- try {
- uni.showLoading({
- title: '上传中...',
- mask: true
- });
- // 调用统一的上传接口
- const {
- data
- } = await uni.$u.api.uploadFile(fileUrl);
- const fileObj = {
- fileSize: data.fileSize,
- fileSuffix: data.fileSuffix,
- fileName: data.name,
- fileUrl: data.url,
- orderFileType : this.fileType
- };
- this.uploadList.push(fileObj);
- this.$emit('update:fileList', this.uploadList);
- this.$emit('change', this.uploadList);
- uni.hideLoading();
- } catch (error) {
- uni.hideLoading();
- console.error('文件上传失败:', error);
- uni.$u.toast('上传失败,请重试');
- }
- },
- // 删除文件
- handleDelete(index) {
- uni.showModal({
- title: '提示',
- content: '确定要删除该文件吗?',
- success: (res) => {
- if (res.confirm) {
- this.uploadList.splice(index, 1);
- this.$emit('update:fileList', this.uploadList);
- this.$emit('change', {
- type: this.fileType,
- fileList: this.uploadList
- });
- }
- }
- });
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .order-file-upload {
- .upload-section {
- background: #fff;
- margin-bottom: 20rpx;
- border-radius: 16rpx;
- overflow: hidden;
- }
- .upload-title {
- display: flex;
- justify-content: space-between;
- align-items: center;
- padding: 30rpx 40rpx;
- border-bottom: 1rpx solid #f5f5f5;
- .title-text {
- font-size: 32rpx;
- font-weight: 500;
- color: #333;
- }
- .upload-btn {
- padding: 12rpx 24rpx;
- background: #007aff;
- color: #fff;
- border-radius: 8rpx;
- font-size: 28rpx;
- }
- }
- .file-list {
- padding: 20rpx 40rpx;
- }
- .file-item {
- display: flex;
- justify-content: space-between;
- align-items: center;
- padding: 20rpx 0;
- border-bottom: 1rpx solid #f5f5f5;
- &:last-child {
- border-bottom: none;
- }
- }
- .file-info {
- flex: 1;
- min-width: 0; // 防止flex子元素溢出
- .file-name {
- display: block;
- font-size: 30rpx;
- color: #333;
- margin-bottom: 8rpx;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- max-width: 400rpx; // 设置最大宽度防止过长
- }
- .file-size {
- font-size: 26rpx;
- color: #999;
- }
- }
- .file-actions {
- .delete-btn {
- padding: 12rpx 24rpx;
- background: #ff4757;
- color: #fff;
- border-radius: 8rpx;
- font-size: 26rpx;
- }
- }
- .upload-tip {
- padding: 40rpx;
- text-align: center;
- .tip-text {
- font-size: 28rpx;
- color: #999;
- }
- }
- }
- </style>
|