Files
Blizzard b854d2873e feat: require couple binding and refine miniapp settings
Gate record features and storage entry points with invitation guidance; enforce record access in the backend. Add homepage greetings, right-swipe profile dismissal, and move the smoking switch to settings. Simplify API base configuration and remove the unused legacy tab bar component.
2026-11-04 21:27:03 +08:00

170 lines
5.5 KiB
JavaScript

const { withRecordAccess } = require('../../utils/record-access');
const { tryLoginRenewal } = require('../../utils/subscribe');
const { uploadReference } = require('../../utils/file-media');
const { getLayout } = require('../../utils/layout');
const { api, API_BASE, getImageUrl, uploadFileCompat } = require('../../utils/request');
const { guardStorageSpace } = require('../../utils/storage-guard');
var MOOD_OPTIONS = ['😊', '🥰', '😢', '😤', '😴', '🤩', '🥺', '😎', '🤔', '😭', '😋', '😌'];
var WEATHER_OPTIONS = ['☀️', '⛅', '☁️', '🌧️', '⛈️', '❄️', '🌤️', '🌥️', '🌈', '🌪️', '🌫️', '🌙'];
Page(withRecordAccess({
onLoginSubscribeTap: function() { return tryLoginRenewal(); },
onResize: function() { this.setData(getLayout()); },
data: {
editorContent: '', 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: String(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,
editorContent: content,
'form.lat': data.lat || 0,
'form.lng': data.lng || 0,
'form.location': data.location || '',
tempImages: tempImages,
existingImages: Array.isArray(data.image_file_ids) && data.image_file_ids.length === imgs.length ? data.image_file_ids : 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.data.form.content = val;
this.setData({ contentLength: val.length });
},
chooseImages: function() {
var self = this;
guardStorageSpace(0).then(function(ok) {
if (!ok) return;
wx.chooseImage({
count: 9 - self.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 || [];
// 已有图片通过原始引用识别;开发工具的本地图片也可能以 http 开头。
for (var i = 0; i < temps.length; i++) {
var p = temps[i];
if (existing[i] && getImageUrl(existing[i]) === p) {
// 已有图片,用原始路径
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) {
var data = await uploadFileCompat({
url: API_BASE + '/upload',
filePath: filePath,
name: 'file',
formData: { sub_dir: 'diaries' }
});
return uploadReference(data);
},
goBack: function() {
wx.navigateBack();
}
}));