184 lines
5.7 KiB
JavaScript
184 lines
5.7 KiB
JavaScript
const { getLayout } = require('../../utils/layout');
|
|
const { api, API_BASE, getImageUrl, uploadFileCompat } = require('../../utils/request');
|
|
|
|
var MOOD_OPTIONS = ['😊', '🥰', '😢', '😤', '😴', '🤩', '🥺', '😎', '🤔', '😭', '😋', '😌'];
|
|
var WEATHER_OPTIONS = ['☀️', '⛅', '☁️', '🌧️', '⛈️', '❄️', '🌤️', '🌥️', '🌈', '🌪️', '🌫️', '🌙'];
|
|
|
|
Page({
|
|
onResize: function() { this.setData(getLayout()); },
|
|
data: { loading: true, loadError: false,
|
|
diaryId: 0,
|
|
form: { title: '', content: '' },
|
|
tempImages: [],
|
|
existingImages: [],
|
|
submitting: false,
|
|
contentLength: 0
|
|
},
|
|
|
|
onLoad: function(options) {
|
|
this.setData(getLayout());
|
|
var id = options.id;
|
|
if (!id) {
|
|
wx.showToast({ title: '参数错误', icon: 'none' });
|
|
return;
|
|
}
|
|
this.setData({ diaryId: parseInt(id) });
|
|
this.loadDiary();
|
|
},
|
|
|
|
loadDiary: async function() {
|
|
if (this._pageLoading) return;
|
|
this._pageLoading = true;
|
|
this.setData({loading:true, loadError:false});
|
|
try {
|
|
var data = await api.diaryDetail(this.data.diaryId);
|
|
var title = data.title || '';
|
|
var content = data.content || '';
|
|
var imgs = data.images || [];
|
|
if (typeof imgs === 'string') {
|
|
try { imgs = JSON.parse(imgs); } catch(e) { imgs = []; }
|
|
}
|
|
if (!Array.isArray(imgs)) imgs = [];
|
|
var tempImages = imgs.map(function(p) { return getImageUrl(p); });
|
|
this.setData({
|
|
'form.title': title,
|
|
'form.content': content,
|
|
'form.lat': data.lat || 0,
|
|
'form.lng': data.lng || 0,
|
|
'form.location': data.location || '',
|
|
tempImages: tempImages,
|
|
existingImages: imgs,
|
|
contentLength: content.length
|
|
});
|
|
} catch (e) {
|
|
this.setData({loadError:true});
|
|
wx.showToast({ title: '加载日记失败', icon: 'none' });
|
|
}
|
|
finally { this._pageLoading = false; this.setData({loading:false}); }
|
|
},
|
|
|
|
onTitleInput: function(e) {
|
|
this.setData({ 'form.title': e.detail.value });
|
|
},
|
|
onContentInput: function(e) {
|
|
var val = e.detail.value || '';
|
|
this.setData({ 'form.content': val, contentLength: val.length });
|
|
},
|
|
|
|
chooseImages: function() {
|
|
var self = this;
|
|
wx.chooseImage({
|
|
count: 9 - this.data.tempImages.length,
|
|
sizeType: ['compressed'],
|
|
sourceType: ['album', 'camera'],
|
|
success: function(res) {
|
|
self.setData({ tempImages: self.data.tempImages.concat(res.tempFilePaths) });
|
|
}
|
|
});
|
|
},
|
|
|
|
// ===== 删除单张图片 =====
|
|
removeImage: function(e) {
|
|
var index = parseInt(e.currentTarget.dataset.index);
|
|
var images = this.data.tempImages;
|
|
var existing = this.data.existingImages;
|
|
images.splice(index, 1);
|
|
existing.splice(index, 1);
|
|
this.setData({ tempImages: images, existingImages: existing });
|
|
},
|
|
|
|
submit: async function() {
|
|
if (this.data.loading || this.data.loadError) return;
|
|
if (this.data.submitting) return;
|
|
var content = this.data.form.content;
|
|
if (!content || !content.trim()) {
|
|
wx.showToast({ title: '请填写日记内容', icon: 'none' });
|
|
return;
|
|
}
|
|
this.setData({ submitting: true });
|
|
|
|
try {
|
|
var uploadPaths = [];
|
|
var existing = this.data.existingImages || [];
|
|
var temps = this.data.tempImages || [];
|
|
|
|
// 判断哪些是新上传的图片(tempFilePaths 以 http 开头的是已有图片,否则是新选的)
|
|
for (var i = 0; i < temps.length; i++) {
|
|
var p = temps[i];
|
|
if (p.indexOf('http') === 0) {
|
|
// 已有图片,用原始路径
|
|
uploadPaths.push(existing[i] || '');
|
|
} else {
|
|
// 新图片,需要上传
|
|
wx.showLoading({ title: '上传图片中...' });
|
|
var res = await this._uploadImage(p);
|
|
wx.hideLoading();
|
|
if (res) uploadPaths.push(res);
|
|
}
|
|
}
|
|
|
|
await api.diaryUpdate({
|
|
id: this.data.diaryId,
|
|
title: this.data.form.title || '',
|
|
content: content.trim(),
|
|
images: uploadPaths,
|
|
// 保留原有地理位置(编辑时位置不变)
|
|
lat: this.data.form.lat || 0,
|
|
lng: this.data.form.lng || 0,
|
|
location: this.data.form.location || ''
|
|
});
|
|
|
|
// 清除详情页缓存
|
|
|
|
wx.showToast({ title: '修改成功 ✨', icon: 'success' });
|
|
setTimeout(function() { wx.navigateBack(); }, 1500);
|
|
} catch (e) {
|
|
wx.hideLoading();
|
|
wx.showToast({ title: (e && (e.error || e.message || e.errMsg)) || '修改失败', icon: 'none' });
|
|
} finally {
|
|
this.setData({ submitting: false });
|
|
}
|
|
},
|
|
|
|
_uploadImage: async function(filePath) {
|
|
try {
|
|
var data = await uploadFileCompat({
|
|
url: API_BASE + '/upload',
|
|
filePath: filePath,
|
|
name: 'file',
|
|
formData: { sub_dir: 'diaries' }
|
|
});
|
|
var path = data && (data.path || data.url || '');
|
|
if (!path) {
|
|
// 内容安全检测失败:抛出错误提示用户
|
|
if (data && data.error) {
|
|
throw data;
|
|
}
|
|
try {
|
|
var retryData = await uploadFileCompat({
|
|
url: API_BASE + '/upload',
|
|
filePath: filePath,
|
|
name: 'file',
|
|
formData: { sub_dir: 'diaries' }
|
|
});
|
|
path = retryData && (retryData.path || retryData.url || '');
|
|
} catch(retryErr) {
|
|
console.error('图片上传重试失败', retryErr);
|
|
}
|
|
}
|
|
return path;
|
|
} catch(e) {
|
|
// 内容安全违规错误:直接抛出,中断发布并提示用户
|
|
if (e && e.error) {
|
|
throw e;
|
|
}
|
|
console.error('图片上传失败', e);
|
|
return '';
|
|
}
|
|
},
|
|
|
|
goBack: function() {
|
|
wx.navigateBack();
|
|
}
|
|
});
|