liiistem-miniapp/pages/takephoto/takephoto.js

405 lines
13 KiB
JavaScript
Raw Normal View History

2025-09-16 13:41:49 +08:00
import R from '../../utils/request';
import C from '../../utils/constant';
Page({
data: {
cdnHost: C.cdnHost,
cdnHost: C.cdnHost,
title: '',
tel: '',
content: '',
shortAddress: '',
address: '',
video: '',
position: {
longitude: 0,
latitude: 0,
},
ds: {
list: [],
refStatusNum: { finished: 0, failed: 0, process: 0 },
},
ts: {
refImgDescs: {},
refImgKeywords: {},
fileUploadList: [],
fileUploadList2: [],
isUploading: false,
timer: [],
},
stop: false,
gridConfig: {
column: 4,
width: 160,
height: 160,
},
config: {
count: 1,
},
phoneError: false,
//
videoFileList: [],
},
onLoad(options) {
const that = this;
R.get('/index.php/api/v1/my', {}).then(({ model }) => {
this.setData({ tel: model.phone });
});
wx.getFuzzyLocation({
type: 'wgs84',
success(res) {
const longitude = res.longitude
const latitude = res.latitude
that.setData({
position: {
longitude,
latitude,
},
});
R.get('/index.php/api/v1/gps', { lat: latitude, lon: longitude }).then(({ model }) => {
that.setData({ shortAddress: model.short_ad, address: model.full_ad });
});
}
})
},
onUnload() {
this.data.stop = true;
},
onShareAppMessage() {
},
choosePoi() {
const that = this;
wx.chooseLocation({
success({ latitude, longitude, name, address }) {
that.setData({
position: {
latitude,
longitude,
},
shortAddress: name,
address,
});
}
})
},
//
onPhoneInput(e) {
const { phoneError } = this.data;
const isPhoneNumber = /^[1][3,4,5,7,8,9][0-9]{9}$/.test(e.detail.value);
if (phoneError === isPhoneNumber) {
this.setData({
phoneError: !isPhoneNumber,
});
}
},
onSubmit() {
console.log('this.stat', this.data);
const { title, tel, shortAddress, address, position, ds, content, videoFileList } = this.data;
if (ds.refStatusNum.failed > 0) {
wx.showToast({
title: '请处理上传失败的图片',
icon: 'none',
});
return;
}
if (ds.refStatusNum.process > 0) {
wx.showToast({
title: '请等待图片上传完成',
icon: 'none',
});
return;
}
// 每个属性都判空,并且给出toast提示
if (!tel) {
wx.showToast({
title: '请输入手机号',
icon: 'none',
});
return;
}
if (!title) {
wx.showToast({
title: '请输入标题',
icon: 'none',
});
return;
}
if (!content) {
wx.showToast({
title: '请输入您的发件信息',
icon: 'none',
});
return;
}
if (!shortAddress) {
wx.showToast({
title: '请选择位置',
icon: 'none',
});
return;
}
if (ds.list.length === 0) {
wx.showToast({
title: '请上传图片',
icon: 'none',
});
return;
}
// 提交数据
R.post('/index.php/api/v1/submit', {
title,
tel,
shortAddress,
address,
content,
longitude: position.longitude,
latitude: position.latitude,
imgs: ds.list.map((o) => o.path),
video: videoFileList && videoFileList.length > 0 ? videoFileList[0].path : undefined,
}).then(({ model }) => {
wx.showToast({
title: '提交成功,感谢您的反馈',
icon: 'success',
});
setTimeout(() => {
wx.navigateBack();
}, 1000);
});
},
goBack() {
wx.navigateBack();
},
//
callStatusNum() {
const { ds } = this.data;
const s = { finished: 0, failed: 0, process: 0 };
ds.list.forEach((o) => {
if (o.failed) s.failed++;
else if (o.finished) s.finished++;
else s.process++;
});
this.setData({
'ds.refStatusNum': s,
});
},
imgHandleSuccess(e) {
console.log('imgHandleSuccess', e.detail);
// const { files } = e.detail;
// this.setData({
// originFiles: files,
// });
this.httpRequest();
},
imgHandleRemove(e) {
const { index } = e.detail;
console.log('imgHandleRemove', e.detail);
const { ds } = this.data;
ds.list.splice(index, 1);
this.setData({
'ds.list': ds.list,
});
},
imgHandleClick(e) {
console.log(e.detail.file);
},
beforUpload(e) {
const { files } = e.detail;
console.log('beforUpload', files);
const { ts } = this.data;
const { fileUploadList, fileUploadList2 } = ts;
files.map((file) => {
fileUploadList.push(file);
fileUploadList2.push(file);
})
this.setData({
'ts.fileUploadList': fileUploadList,
'ts.fileUploadList2': fileUploadList2,
});
},
startPreview() {
console.log('startPreview');
if (this.data.stop) return;
const { ts, ds } = this.data;
const { fileUploadList2 } = ts;
if (fileUploadList2.length > 0) {
const file = fileUploadList2.shift();
ds.list.push({
src: file.url,
url: file.url,
uid: file.name,
name: file.name,
percent: 0,
failed: false,
status: 'loading',
})
this.setData({
'ts.fileUploadList2': fileUploadList2,
'ds.list': ds.list,
});
setTimeout(() => {
this.startPreview();
}, 0);
} else {
setTimeout(() => {
this.startPreview();
}, 2000);
}
},
httpRequest(e) {
if (this.data.stop) return;
const { ts } = this.data;
if (ts.isUploading) return;
ts.isUploading = true;
console.log('httpRequest');
// 循环检测 是否有文件可以上传
setTimeout(() => {
this.uploadHandle();
this.startPreview();
}, 0);
},
uploadHandle() {
console.log('uploadHandle');
if (this.data.stop) return;
this.callStatusNum();
const { ts, ds } = this.data;
const { fileUploadList } = ts;
if (fileUploadList.length > 0) {
console.log('fileUploadList', fileUploadList);
const file = fileUploadList.shift();
// formData.append('imageData', file);
R.upload({ a: 1 }, 'img_file', file.url).catch((e) => e).then((response) => {
// console.log('response-response333', response);
ts.isUploading = false;
try {
response.data = JSON.parse(response.data);
} catch (error) {
response.data = null;
}
// console.log('asdfasd-response', response.data);
if (response.data && response.data.code == -1000) {
wx.showToast({
title: '登录超时,请重新登录',
icon: 'none',
});
setTimeout(() => {
wx.navigateTo({ url: '/pages/home/home' });
}, 500)
return;
}
else if (response.data && response.data.success) {
console.log('asdfasd-response2222', response.data);
const { data: { model: { path } } } = response;
this.setData({
'ds.list': ds.list.map((o) => {
if (file.name == o.uid) {
return { ...o, path, finished: true, percent: 100, status: 'done' };
}
return { ...o };
})
});
// this.setData({
// [`ds.list[${idx}].url`]: C.cdnHost + path,
// });
// R.getImg(4, imageId).then((path) => {
// const tlist = this.data.ds.list;
// let idx = 0;
// // console.log('path', path);
// tlist.forEach((c, i) => {
// if (c.imageId == imageId) {
// idx = i;
// }
// });
// if (idx) {
// this.setData({
// [`ds.list[${idx}].url`]: path,
// });
// }
// })
// wx.showToast({
// title: '图片:' + file.name + '已暂存',
// icon: 'success',
// });
// this.saveTmpData();
} else {
console.log(response, file.name);
let errorMsg = '上传失败,请移除后重试';
// if (response.data && response.data.status == 529) {
// errorMsg = '其他图集中已存在,请移除';
// }
this.setData({
'ds.list': ds.list.map((o) => {
if (file.name == o.uid) {
return { ...o, failed: errorMsg, status: 'failed' };
}
return { ...o };
}),
});
wx.showToast({
title: file.name + ',' + errorMsg,
icon: 'error',
});
// this.saveTmpData();
}
setTimeout(() => {
this.uploadHandle();
}, 0);
});
} else {
// setTimeout(() => {
// this.uploadHandle();
// }, 2000);
}
},
//video
handleVideoAdd(e) {
const { files } = e.detail;
files.forEach(file => this.uploadVideoFile(file))
},
uploadVideoFile(file) {
wx.getVideoInfo({ src: file.url }).then((res) => {
if (res.duration > 180) {
wx.showToast({
title: '视频时长不能超过3分钟',
icon: 'none',
});
return;
}
//
const { videoFileList } = this.data;
this.setData({
videoFileList: [...videoFileList, { ...file, path: '', status: 'loading' }],
});
const { length } = videoFileList;
R.upload({ a: 2 }, 'video_file', file.url, (res) => {
this.setData({
[`videoFileList[${length}].percent`]: res.progress,
});
}).catch((e) => e).then((response) => {
try {
response.data = JSON.parse(response.data);
} catch (error) {
response.data = null;
}
if (response.data && response.data.success) {
const { data: { model: { path } } } = response;
console.log('asdfasd-response2222', response.data, this.data.videoFileList, length);
this.setData({
[`videoFileList[${length}].status`]: 'done',
[`videoFileList[${length}].path`]: path,
});
}
});
});
},
handleVideoRemove(e) {
const { index } = e.detail;
const { videoFileList } = this.data;
videoFileList.splice(index, 1);
this.setData({
videoFileList,
});
},
})