// pages/admin/movies/movies.js
Page({
data: {
movies: [], // 电影列表
loading: false, // 是否加载中
hasMore: true, // 是否还有更多数据
page: 1, // 当前页码
pageSize: 10, // 每页数量
searchValue: '', // 搜索框值
showAddModal: false, // 是否显示添加电影模态框
movieForm: {
name: '',
poster: '',
type: '',
director: '',
actors: '',
description: '',
rating: 0,
releaseDate: '',
duration: '',
status: 1
},
formErrors: {} // 表单错误信息
},
onLoad: function(options) {
this.getMovies();
},
// 获取电影列表
getMovies() {
if (this.data.loading || !this.data.hasMore) return;
this.setData({ loading: true });
wx.request({
url: `${getApp().globalData.baseUrl}/admin/api/movies`,
data: {
page: this.data.page,
pageSize: this.data.pageSize,
keyword: this.data.searchValue
},
success: (res) => {
if (res.data.code === 0) {
const newMovies = res.data.data.list;
const hasMore = res.data.data.list.length >= this.data.pageSize;
this.setData({
movies: this.data.page === 1 ? newMovies : [...this.data.movies, ...newMovies],
hasMore,
page: this.data.page + 1
});
}
},
complete: () => {
this.setData({ loading: false });
}
});
},
// 搜索框输入
onSearchInput(e) {
this.setData({
searchValue: e.detail.value
});
},
// 搜索确认
onSearchConfirm() {
this.setData({
page: 1,
hasMore: true,
movies: []
});
this.getMovies();
},
// 显示添加电影模态框
showAddMovieModal() {
this.setData({
showAddModal: true,
movieForm: {
name: '',
poster: '',
type: '',
director: '',
actors: '',
description: '',
rating: 0,
releaseDate: '',
duration: '',
status: 1
},
formErrors: {}
});
},
// 关闭添加电影模态框
closeAddMovieModal() {
this.setData({
showAddModal: false
});
},
// 表单输入
onFormInput(e) {
const { field } = e.currentTarget.dataset;
const { value } = e.detail;
this.setData({
[`movieForm.${field}`]: value
});
},
// 日期选择
onDateChange(e) {
this.setData({
'movieForm.releaseDate': e.detail.value
});
},
// 评分选择
onRatingChange(e) {
this.setData({
'movieForm.rating': e.detail.value
});
},
// 提交表单
submitForm() {
const { movieForm } = this.data;
const errors = {};
// 表单验证
if (!movieForm.name.trim()) {
errors.name = '请输入电影名称';
}
if (!movieForm.poster.trim()) {
errors.poster = '请上传电影海报';
}
if (!movieForm.type.trim()) {
errors.type = '请输入电影类型';
}
if (!movieForm.director.trim()) {
errors.director = '请输入导演';
}
if (!movieForm.actors.trim()) {
errors.actors = '请输入主演';
}
if (!movieForm.description.trim()) {
errors.description = '请输入电影简介';
}
if (!movieForm.releaseDate) {
errors.releaseDate = '请选择上映日期';
}
if (!movieForm.duration.trim()) {
errors.duration = '请输入电影时长';
}
if (Object.keys(errors).length > 0) {
this.setData({ formErrors: errors });
return;
}
// 提交表单
wx.showLoading({
title: '提交中...',
mask: true
});
wx.request({
url: `${getApp().globalData.baseUrl}/admin/api/movies`,
method: 'POST',
data: movieForm,
success: (res) => {
if (res.data.code === 0) {
wx.showToast({
title: '添加成功',
icon: 'success'
});
// 刷新电影列表
this.setData({
page: 1,
hasMore: true,
movies: [],
showAddModal: false
});
this.getMovies();
} else {
wx.showToast({
title: res.data.message || '添加失败',
icon: 'none'
});
}
},
fail: () => {
wx.showToast({
title: '网络错误,请稍后重试',
icon: 'none'
});
},
complete: () => {
wx.hideLoading();
}
});
},
// 上传海报
uploadPoster() {
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: (res) => {
const tempFilePaths = res.tempFilePaths;
// 上传图片到服务器
wx.showLoading({
title: '上传中...',
});
wx.uploadFile({
url: `${getApp().globalData.baseUrl}/admin/api/upload`,
filePath: tempFilePaths[0],
name: 'file',
success: (res) => {
const data = JSON.parse(res.data);
if (data.code === 0) {
this.setData({
'movieForm.poster': data.data.url
});
wx.showToast({
title: '上传成功',
icon: 'success'
});
} else {
wx.showToast({
title: data.message || '上传失败',
icon: 'none'
});
}
},
fail: () => {
wx.showToast({
title: '网络错误,请稍后重试',
icon: 'none'
});
},
complete: () => {
wx.hideLoading();
}
});
}
});
},
// 编辑电影
editMovie(e) {
const index = e.currentTarget.dataset.index;
const movie = this.data.movies[index];
this.setData({
showAddModal: true,
movieForm: {
id: movie.id,
name: movie.name,
poster: movie.poster,
type: movie.type,
director: movie.director,
actors: movie.actors,
description: movie.description,
rating: movie.rating,
releaseDate: movie.releaseDate,
duration: movie.duration,
status: movie.status
},
formErrors: {}
});
},
// 删除电影
deleteMovie(e) {
const index = e.currentTarget.dataset.index;
const movieId = this.data.movies[index].id;
wx.showModal({
title: '确认删除',
content: '您确定要删除这部电影吗?',
success: (res) => {
if (res.confirm) {
wx.showLoading({
title: '删除中...',
});
wx.request({
url: `${getApp().globalData.baseUrl}/admin/api/movies/${movieId}`,
method: 'DELETE',
success: (res) => {
if (res.data.code === 0) {
// 从列表中删除电影
const movies = [...this.data.movies];
movies.splice(index, 1);
this.setData({ movies });
wx.showToast({
title: '删除成功',
icon: 'success'
});
} else {
wx.showToast({
title: res.data.message || '删除失败',
icon: 'none'
});
}
},
fail: () => {
wx.showToast({
title: '网络错误,请稍后重试',
icon: 'none'
});
},
complete: () => {
wx.hideLoading();
}
});
}
}
});
},
// 更改电影状态
changeMovieStatus(e) {
const index = e.currentTarget.dataset.index;
const movie = this.data.movies[index];
const newStatus = movie.status === 1 ? 0 : 1;
wx.showLoading({
title: '更新中...',
});
wx.request({
url: `${getApp().globalData.baseUrl}/admin/api/movies/${movie.id}/status`,
method: 'POST',
data: { status: newStatus },
success: (res) => {
if (res.data.code === 0) {
// 更新列表中的电影状态
const movies = [...this.data.movies];
movies[index].status = newStatus;
this.setData({ movies });
wx.showToast({
title: newStatus === 1 ? '已上架' : '已下架',
icon: 'success'
});
} else {
wx.showToast({
title: res.data.message || '更新失败',
icon: 'none'
});
}
},
fail: () => {
wx.showToast({
title: '网络错误,请稍后重试',
icon: 'none'
});
},
complete: () => {
wx.hideLoading();
}
});
}
}); 有没有问题
最新发布