访客页面修改
This commit is contained in:
parent
9e3c698be9
commit
8d6c55007e
94
common/upload.js
Normal file
94
common/upload.js
Normal file
@ -0,0 +1,94 @@
|
||||
// utils/upload.js
|
||||
|
||||
export const uploadFiles = ({
|
||||
files = [],
|
||||
url = '',
|
||||
name = 'file',
|
||||
formData = {},
|
||||
fileType = 'image',
|
||||
vm, // 关键:传入 vm 以便访问 vuex_token、vuex_remember 等
|
||||
}) => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
if (!url) return reject(new Error('上传地址不能为空'));
|
||||
if (!Array.isArray(files) || files.length === 0) return reject(new Error('文件列表不能为空'));
|
||||
|
||||
const results = [];
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const filePath = files[i];
|
||||
try {
|
||||
const res = await uploadSingleFile({
|
||||
filePath,
|
||||
url,
|
||||
name,
|
||||
formData,
|
||||
fileType,
|
||||
vm
|
||||
});
|
||||
results.push(res);
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
resolve(results);
|
||||
});
|
||||
};
|
||||
|
||||
function uploadSingleFile({
|
||||
filePath,
|
||||
url,
|
||||
name,
|
||||
formData = {},
|
||||
fileType = 'image',
|
||||
vm
|
||||
}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const headers = {
|
||||
'x-requested-with': 'XMLHttpRequest',
|
||||
source: 'uniapp',
|
||||
clientId: 'dab457a1ea14411787c240db05bb0832',
|
||||
};
|
||||
|
||||
// 手动加上 Token
|
||||
if (vm?.vuex_token) {
|
||||
headers.Authorization = `Bearer ${vm.vuex_token}`;
|
||||
}
|
||||
|
||||
// 加上 rememberMe
|
||||
if (vm?.vuex_remember) {
|
||||
headers['x-remember'] = vm.vuex_remember;
|
||||
}
|
||||
console.log('request', headers);
|
||||
console.log('request:', {
|
||||
url: url,
|
||||
filePath: filePath,
|
||||
name: name,
|
||||
formData: formData
|
||||
});
|
||||
uni.uploadFile({
|
||||
url,
|
||||
filePath,
|
||||
name,
|
||||
formData,
|
||||
header: headers,
|
||||
fileType,
|
||||
success: (res) => {
|
||||
try {
|
||||
const data = JSON.parse(res.data);
|
||||
console.log('request', data);
|
||||
if (res.statusCode === 200) {
|
||||
resolve(data);
|
||||
} else {
|
||||
reject(data);
|
||||
}
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(err);
|
||||
console.log('request', err);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
@ -157,6 +157,9 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import {
|
||||
uploadFiles
|
||||
} from '@/common/upload.js';
|
||||
export default {
|
||||
data() {
|
||||
const now = new Date();
|
||||
@ -335,7 +338,7 @@
|
||||
},
|
||||
|
||||
// 提交表单
|
||||
submitForm() {
|
||||
async submitForm() {
|
||||
const errorMsg = this.validateForm();
|
||||
if (errorMsg) {
|
||||
uni.showToast({
|
||||
@ -346,7 +349,8 @@
|
||||
}
|
||||
|
||||
// 合并日期和时间
|
||||
this.formData.visitingBeginTime = `${this.formData.visitingBeginDate} ${this.formData.visitingBeginTime}`;
|
||||
this.formData.visitingBeginTime =
|
||||
`${this.formData.visitingBeginDate} ${this.formData.visitingBeginTime}`;
|
||||
this.formData.visitingEndTime = `${this.formData.visitingEndDate} ${this.formData.visitingEndTime}`;
|
||||
|
||||
|
||||
@ -356,128 +360,170 @@
|
||||
title: '提交中...',
|
||||
mask: true
|
||||
});
|
||||
const images = [this.formData.facePictures]
|
||||
|
||||
// 模拟API请求
|
||||
setTimeout(() => {
|
||||
uni.request({
|
||||
url: this.formData.facePictures
|
||||
})
|
||||
// .then(response => response.blob())
|
||||
.then(blob => {
|
||||
// 此时得到了原始Blob对象
|
||||
console.log(blob);
|
||||
const file = new File([blob], 'filename.png', {
|
||||
type: blob.type
|
||||
});
|
||||
// 构建FormData进行上传
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
console.log(formData)
|
||||
// 发送上传请求
|
||||
// this.$u.api.uploadimg()
|
||||
// 接上面的代码,假设Blob是图片类型
|
||||
uni.uploadFile({
|
||||
url: 'http://183.230.235.66:11010/api/resource/oss/qrupload', // 后端上传接口地址
|
||||
filePath: file.filePath, // 要上传的文件路径
|
||||
name: 'file', // 后端接收文件的参数名
|
||||
// FormData中的其他参数
|
||||
formData: {
|
||||
'code': this.formData.qrCodeId // 示例:其他表单字段
|
||||
},
|
||||
// // 上传进度回调
|
||||
// onProgressUpdate: (res) => {
|
||||
// this.progress = res.progress;
|
||||
// console.log('上传进度:' + res.progress);
|
||||
// },
|
||||
// 上传成功回调
|
||||
success: (res) => {
|
||||
console.log('上传成功', res);
|
||||
|
||||
// this.formData.facePictures = res.data.ossId;
|
||||
// 准备提交数据
|
||||
const submitData = {
|
||||
...this.formData,
|
||||
bookingParkingSpace: this.formData
|
||||
.bookingParkingSpace ? 0 : 1
|
||||
};
|
||||
|
||||
const parsedData = JSON.parse(res.data);
|
||||
if (parsedData.code == 200) {
|
||||
// 第二步:从解析后的数据中获取ossId
|
||||
const ossId = parsedData.data.ossId;
|
||||
console.log("ossId", ossId)
|
||||
submitData.facePictures = ossId;
|
||||
console.log(submitData)
|
||||
this.$u.api.fksub(submitData).then(res => {
|
||||
console.log(res)
|
||||
if (res.code == 200) {
|
||||
uni.showToast({
|
||||
title: "提交成功,请等待审核!",
|
||||
icon: "success"
|
||||
})
|
||||
}
|
||||
})
|
||||
}else{
|
||||
uni.showToast({
|
||||
title: '上传失败',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// uni.showToast({
|
||||
// title: '上传成功',
|
||||
// icon: 'success'
|
||||
// });
|
||||
},
|
||||
// 上传失败回调
|
||||
fail: (err) => {
|
||||
console.error('上传失败', err);
|
||||
uni.showToast({
|
||||
title: '上传失败',
|
||||
icon: 'none'
|
||||
});
|
||||
},
|
||||
// 无论成功失败都会执行
|
||||
complete: () => {
|
||||
this.progress = 0; // 重置进度
|
||||
}
|
||||
});
|
||||
// this.$u.api.uploadimg(formData).then(res => {
|
||||
// console.log(res)
|
||||
// if (res.code == 200) {
|
||||
// uni.showToast({
|
||||
// title: "提交成功,请等待审核!",
|
||||
// icon: "success"
|
||||
// })
|
||||
// } else {
|
||||
// uni.showToast({
|
||||
// title: "tp提交失败!",
|
||||
// icon: "error"
|
||||
// })
|
||||
// }
|
||||
// })
|
||||
|
||||
});
|
||||
|
||||
|
||||
uni.hideLoading();
|
||||
|
||||
// 显示成功提示
|
||||
const result = await uploadFiles({
|
||||
files: images,
|
||||
url: this.vuex_config.baseUrl + '/resource/oss/upload',
|
||||
name: 'file',
|
||||
vm: this // 关键:用于注入 token 等
|
||||
});
|
||||
let parsedData = result[0]
|
||||
if (parsedData.code == 200) {
|
||||
const submitData = {
|
||||
...this.formData,
|
||||
bookingParkingSpace: this.formData
|
||||
.bookingParkingSpace ? 0 : 1
|
||||
};
|
||||
// 第二步:从解析后的数据中获取ossId
|
||||
const ossId = parsedData.data.ossId;
|
||||
console.log("ossId", ossId)
|
||||
submitData.facePictures = ossId;
|
||||
console.log(submitData)
|
||||
this.$u.api.fksub(submitData).then(res => {
|
||||
console.log(res)
|
||||
if (res.code == 200) {
|
||||
this.resetForm();
|
||||
uni.showToast({
|
||||
title: "提交成功,请等待审核!",
|
||||
icon: "success"
|
||||
})
|
||||
}else {
|
||||
uni.showToast({
|
||||
title: "提交失败",
|
||||
icon: "none"
|
||||
})
|
||||
}
|
||||
})
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '提交成功',
|
||||
icon: 'success',
|
||||
duration: 2000
|
||||
title: '上传失败',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
uni.hideLoading();
|
||||
// return
|
||||
// 模拟API请求
|
||||
// setTimeout(() => {
|
||||
// uni.request({
|
||||
// url: this.formData.facePictures
|
||||
// })
|
||||
// // .then(response => response.blob())
|
||||
// .then(blob => {
|
||||
// // 此时得到了原始Blob对象
|
||||
// console.log(blob);
|
||||
// const file = new File([blob], 'filename.png', {
|
||||
// type: blob.type
|
||||
// });
|
||||
// // 构建FormData进行上传
|
||||
// const formData = new FormData();
|
||||
// formData.append('file', file);
|
||||
// console.log(formData)
|
||||
// 发送上传请求
|
||||
// this.$u.api.uploadimg()
|
||||
// 接上面的代码,假设Blob是图片类型
|
||||
// uni.uploadFile({
|
||||
// url: 'http://183.230.235.66:11010/api/resource/oss/upload', // 后端上传接口地址
|
||||
// filePath: this.formData.facePictures, // 要上传的文件路径
|
||||
// name: 'file', // 后端接收文件的参数名
|
||||
// // FormData中的其他参数
|
||||
// // formData: {
|
||||
// // 'code': this.formData.qrCodeId // 示例:其他表单字段
|
||||
// // },
|
||||
// // // 上传进度回调
|
||||
// // onProgressUpdate: (res) => {
|
||||
// // this.progress = res.progress;
|
||||
// // console.log('上传进度:' + res.progress);
|
||||
// // },
|
||||
// // 上传成功回调
|
||||
// success: (res) => {
|
||||
// console.log('上传成功', res);
|
||||
|
||||
// 提交成功后,可跳转到成功页面或重置表单
|
||||
setTimeout(() => {
|
||||
// 重置表单
|
||||
this.resetForm();
|
||||
// 返回到上一页或跳转到其他页面
|
||||
// uni.navigateBack();
|
||||
}, 2000);
|
||||
}, 1500);
|
||||
// // this.formData.facePictures = res.data.ossId;
|
||||
// // 准备提交数据
|
||||
// const submitData = {
|
||||
// ...this.formData,
|
||||
// bookingParkingSpace: this.formData
|
||||
// .bookingParkingSpace ? 0 : 1
|
||||
// };
|
||||
|
||||
// const parsedData = JSON.parse(res.data);
|
||||
// if (parsedData.code == 200) {
|
||||
// // 第二步:从解析后的数据中获取ossId
|
||||
// const ossId = parsedData.data.ossId;
|
||||
// console.log("ossId", ossId)
|
||||
// submitData.facePictures = ossId;
|
||||
// console.log(submitData)
|
||||
// this.$u.api.fksub(submitData).then(res => {
|
||||
// console.log(res)
|
||||
// if (res.code == 200) {
|
||||
// this.resetForm();
|
||||
// uni.showToast({
|
||||
// title: "提交成功,请等待审核!",
|
||||
// icon: "success"
|
||||
// })
|
||||
// }
|
||||
// })
|
||||
// }else{
|
||||
// uni.showToast({
|
||||
// title: '上传失败',
|
||||
// icon: 'none'
|
||||
// });
|
||||
// }
|
||||
|
||||
|
||||
// // uni.showToast({
|
||||
// // title: '上传成功',
|
||||
// // icon: 'success'
|
||||
// // });
|
||||
// },
|
||||
// // 上传失败回调
|
||||
// fail: (err) => {
|
||||
// console.error('上传失败', err);
|
||||
// uni.showToast({
|
||||
// title: '上传失败',
|
||||
// icon: 'none'
|
||||
// });
|
||||
// },
|
||||
// // 无论成功失败都会执行
|
||||
// complete: () => {
|
||||
// this.progress = 0; // 重置进度
|
||||
// }
|
||||
// });
|
||||
// this.$u.api.uploadimg(formData).then(res => {
|
||||
// console.log(res)
|
||||
// if (res.code == 200) {
|
||||
// uni.showToast({
|
||||
// title: "提交成功,请等待审核!",
|
||||
// icon: "success"
|
||||
// })
|
||||
// } else {
|
||||
// uni.showToast({
|
||||
// title: "tp提交失败!",
|
||||
// icon: "error"
|
||||
// })
|
||||
// }
|
||||
// })
|
||||
|
||||
// });
|
||||
|
||||
|
||||
|
||||
// 显示成功提示
|
||||
// uni.showToast({
|
||||
// title: '提交成功',
|
||||
// icon: 'success',
|
||||
// duration: 2000
|
||||
// });
|
||||
|
||||
// // 提交成功后,可跳转到成功页面或重置表单
|
||||
// setTimeout(() => {
|
||||
// // 重置表单
|
||||
// this.resetForm();
|
||||
// // 返回到上一页或跳转到其他页面
|
||||
// // uni.navigateBack();
|
||||
// }, 2000);
|
||||
// }, 1500);
|
||||
},
|
||||
delimg() {
|
||||
this.formData.facePictures = ''
|
||||
|
Loading…
Reference in New Issue
Block a user