const { getLayout } = require('../../utils/layout'); // pages/albums/albums.js const { api, getImageUrl } = require('../../utils/request'); Page({ onResize: function() { this.setData(getLayout()); }, data: { loading: true, loadError: false, albums: [], uploadMode:false, showMoreModal: false, showEditModal: false, currentAlbum: null, editForm: { title: '', desc: '' } }, onLoad: function(options) { this.setData(getLayout()); this.setData({uploadMode: !!(options && options.upload === '1')}); }, onShow: function() { this.loadAlbums(); }, onPullDownRefresh: async function() { await this.loadAlbums(); wx.stopPullDownRefresh(); }, // 分享到好友 onShareAppMessage: function() { return { title: '让所有的爱有迹可循', path: '/pages/albums/albums', imageUrl: '/images/share.png' }; }, // 分享到朋友圈 onShareTimeline: function() { return { title: '让所有的爱有迹可循', imageUrl: '/images/share.png' }; }, noop: function() {}, loadAlbums: async function() { if (this._loading) return; this._loading = true; this.setData({ loading: true, loadError: false }); try { var list = await api.albumList(null, 'GET', true); list = (list || []).map(function(a) { a.cover = getImageUrl(a.cover); a.photo_count = a.photo_count || 0; // 地理位置:优先显示地址名称,其次显示经纬度 a.location_text = a.location || ((a.lat && a.lng) ? (a.lat + ', ' + a.lng) : ''); return a; }); this.setData({ albums: list }); } catch(e) { console.error(e); this.setData({ loadError: true }); } finally { this._loading = false; this.setData({ loading: false }); } }, openAlbum: function(e) { var id = e.currentTarget.dataset.id; wx.navigateTo({ url: '/pages/album/album?album_id=' + id + (this.data.uploadMode ? '&upload=1' : '') }); }, // 显示更多菜单 showMoreMenu: function(e) { var id = e.currentTarget.dataset.id; var title = e.currentTarget.dataset.title || '默认相册'; var desc = e.currentTarget.dataset.desc || ''; var cover = e.currentTarget.dataset.cover || ''; this.setData({ showMoreModal: true, currentAlbum: { id: id, title: title, description: desc, cover: cover } }); }, hideMoreMenu: function() { this.setData({ showMoreModal: false }); }, // 编辑相册 editAlbum: function() { var album = this.data.currentAlbum; if (!album) return; this.setData({ showMoreModal: false, showEditModal: true, editForm: { title: album.title, desc: album.description || '' } }); }, hideEditModal: function() { this.setData({ showEditModal: false }); }, onEditTitleInput: function(e) { this.setData({ 'editForm.title': e.detail.value }); }, onEditDescInput: function(e) { this.setData({ 'editForm.desc': e.detail.value }); }, saveAlbumEdit: async function() { var self = this; var album = this.data.currentAlbum; if (!album) return; var title = this.data.editForm.title; if (!title) { wx.showToast({ title: '请输入相册标题', icon: 'none' }); return; } try { wx.showLoading({ title: '保存中...' }); await api.albumUpdate({ id: album.id, title: title, description: this.data.editForm.desc || '', // 保留原有地理位置 lat: album.lat || 0, lng: album.lng || 0, location: album.location || '' }); wx.hideLoading(); wx.showToast({ title: '已保存 ✨', icon: 'success' }); this.hideEditModal(); this.loadAlbums(); } catch(e) { wx.hideLoading(); wx.showToast({ title: e.error || '保存失败', icon: 'none' }); } }, // 删除相册 deleteAlbum: function() { var self = this; var album = this.data.currentAlbum; if (!album) return; // 从相册列表中查找照片数量 var albums = this.data.albums; var targetAlbum = albums.find(function(a) { return String(a.id) === String(album.id); }); var count = targetAlbum ? (targetAlbum.photo_count || 0) : 0; var title = album.title || '该相册'; this.setData({ showMoreModal: false }); if (count > 0) { wx.showModal({ title: '无法删除', content: '「' + title + '」中还有 ' + count + ' 张照片,请先删除里面的全部照片后再删除相册', showCancel: false, confirmText: '知道了' }); return; } wx.showModal({ title: '确认删除', content: '确定要删除「' + title + '」吗?', success: function(res) { if (res.confirm) { wx.showLoading({ title: '删除中...' }); api.albumDelete({ id: album.id }).then(function() { wx.hideLoading(); wx.showToast({ title: '已删除', icon: 'success' }); self.loadAlbums(); }).catch(function(err) { wx.hideLoading(); wx.showToast({ title: err.error || '删除失败', icon: 'none' }); }); } } }); }, goToPublish: function() { wx.navigateTo({ url: '/pages/publish/publish?type=album' }); }, goHome: function() { wx.redirectTo({ url: '/pages/index/index' }); }, goBack: function() { wx.navigateBack(); }, goToProfile: function() { wx.redirectTo({ url: '/pages/profile/profile' }); } });