init: initial commit
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
const { getLayout } = require('../../utils/layout');
|
||||
// pages/lovewords/lovewords.js - 一起听过的歌
|
||||
const { api } = require('../../utils/request');
|
||||
Page({
|
||||
onResize: function() { this.setData(getLayout()); },
|
||||
data: { loading: true, loadError: false,
|
||||
list: [],
|
||||
showModal: false,
|
||||
isEdit: false,
|
||||
editId: '',
|
||||
form: { song_name: '', artist: '', note: '' }
|
||||
},
|
||||
|
||||
onLoad: function() {
|
||||
this.setData(getLayout());
|
||||
this.loadData();
|
||||
},
|
||||
onShow: function() { this.loadData(); },
|
||||
|
||||
onPullDownRefresh: async function() {
|
||||
await this.loadData();
|
||||
wx.stopPullDownRefresh();
|
||||
},
|
||||
|
||||
onShareAppMessage: function() {
|
||||
return { title: '让所有的爱有迹可循', path: '/pages/lovewords/lovewords', imageUrl: '/images/share.png' };
|
||||
},
|
||||
onShareTimeline: function() {
|
||||
return { title: '让所有的爱有迹可循', imageUrl: '/images/share.png' };
|
||||
},
|
||||
|
||||
loadData: async function() {
|
||||
if (this._pageLoading) return;
|
||||
this._pageLoading = true;
|
||||
this.setData({loading:true, loadError:false});
|
||||
try {
|
||||
const list = await api.songsList();
|
||||
const items = (list || []).map(function(item) {
|
||||
if (item.created_at) {
|
||||
var d = new Date(item.created_at);
|
||||
item.date_month = String(d.getMonth() + 1).padStart(2, '0');
|
||||
item.date_day = String(d.getDate()).padStart(2, '0');
|
||||
}
|
||||
return item;
|
||||
});
|
||||
this.setData({ list: items });
|
||||
} catch (e) {
|
||||
this.setData({loadError:true});
|
||||
console.error(e);
|
||||
wx.showToast({ title: '加载失败', icon: 'none' });
|
||||
}
|
||||
finally { this._pageLoading = false; this.setData({loading:false}); }
|
||||
},
|
||||
|
||||
noop: function() {},
|
||||
onSongMenu: function(e) {
|
||||
wx.showActionSheet({ itemList:['编辑歌曲', '删除歌曲'], success:(res) => {
|
||||
if (res.tapIndex === 0) this.editSong(e);
|
||||
else if (res.tapIndex === 1) this.deleteSong(e);
|
||||
}});
|
||||
},
|
||||
deleteSong: function(e) {
|
||||
var id = e.currentTarget.dataset.id;
|
||||
wx.showModal({
|
||||
title: '确认删除',
|
||||
content: '确定要删除这首歌吗?',
|
||||
success: async (res) => {
|
||||
if (res.confirm) {
|
||||
try {
|
||||
await api.songsDelete({ id: id });
|
||||
wx.showToast({ title: '已删除', icon: 'success' });
|
||||
this.loadData();
|
||||
} catch (err) { wx.showToast({ title: err.error || '删除失败', icon: 'none' }); }
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// 编辑歌曲:打开弹窗并带入原数据
|
||||
editSong: function(e) {
|
||||
var ds = e.currentTarget.dataset;
|
||||
this.setData({
|
||||
showModal: true,
|
||||
isEdit: true,
|
||||
editId: ds.id,
|
||||
form: { song_name: ds.name || '', artist: ds.artist || '', note: ds.note || '' }
|
||||
});
|
||||
},
|
||||
|
||||
// ===== 添加/编辑弹窗 =====
|
||||
showModal: function() {
|
||||
this.setData({ showModal: true, isEdit: false, editId: '', form: { song_name: '', artist: '', note: '' } });
|
||||
},
|
||||
hideModal: function() { this.setData({ showModal: false, isEdit: false, editId: '' }); },
|
||||
onSongNameInput: function(e) { this.setData({ 'form.song_name': e.detail.value }); },
|
||||
onArtistInput: function(e) { this.setData({ 'form.artist': e.detail.value }); },
|
||||
onNoteInput: function(e) { this.setData({ 'form.note': e.detail.value }); },
|
||||
|
||||
submit: async function() {
|
||||
var form = this.data.form;
|
||||
if (!form.song_name || !form.artist) {
|
||||
wx.showToast({ title: '请填写歌曲名和歌手名', icon: 'none' }); return;
|
||||
}
|
||||
try {
|
||||
if (this.data.isEdit && this.data.editId) {
|
||||
await api.songsUpdate({ id: this.data.editId, song_name: form.song_name, artist: form.artist, note: form.note || '' });
|
||||
wx.showToast({ title: '已修改 ✏️', icon: 'success' });
|
||||
} else {
|
||||
await api.songsAdd(form);
|
||||
wx.showToast({ title: '收藏成功 🎵', icon: 'success' });
|
||||
}
|
||||
this.hideModal();
|
||||
this.loadData();
|
||||
} catch (e) {
|
||||
wx.showToast({ title: e.error || '保存失败', icon: 'none' });
|
||||
}
|
||||
},
|
||||
|
||||
goToPublish: function() {
|
||||
this.showModal();
|
||||
},
|
||||
|
||||
goBack: function() { wx.navigateBack(); },
|
||||
goHome: function() { wx.redirectTo({ url: '/pages/index/index' }); },
|
||||
goProfile: function() { wx.redirectTo({ url: '/pages/profile/profile' }); }
|
||||
});
|
||||
Reference in New Issue
Block a user