266 lines
8.3 KiB
PHP
266 lines
8.3 KiB
PHP
<?php
|
||
|
||
namespace app\admin\controller;
|
||
|
||
use app\admin\model\Staff;
|
||
use app\admin\server\MomentsServer;
|
||
use think\Db;
|
||
|
||
class Moments extends AdminBase
|
||
{
|
||
public function lists()
|
||
{
|
||
if ($this->request->isAjax()) {
|
||
$get = $this->request->get();
|
||
$list = MomentsServer::getMoments();
|
||
|
||
// 格式化数据为前端期望的格式
|
||
$data = [
|
||
'count' => count($list),
|
||
'lists' => $list
|
||
];
|
||
|
||
$this->_success('', $data);
|
||
}
|
||
return $this->fetch();
|
||
}
|
||
|
||
public function add()
|
||
{
|
||
if ($this->request->isAjax() && $this->request->isPost()) {
|
||
// 处理表单提交
|
||
$post = $this->request->post();
|
||
|
||
// 表单验证
|
||
if (empty($post['title'])) {
|
||
$this->_error('请输入标题');
|
||
}
|
||
if (empty($post['sid'])) {
|
||
$this->_error('请选择员工');
|
||
}
|
||
if (empty($post['goods_id'])) {
|
||
$this->_error('请选择关联产品');
|
||
}
|
||
|
||
// 处理图片字段(可能是数组格式 goods_image[])
|
||
$goods_images = [];
|
||
if (isset($post['goods_image'])) {
|
||
if (is_array($post['goods_image'])) {
|
||
// 数组格式,过滤空值
|
||
$goods_images = array_filter($post['goods_image']);
|
||
} else if (!empty($post['goods_image'])) {
|
||
// 字符串格式(逗号分隔),转换为数组
|
||
$goods_images = explode(',', $post['goods_image']);
|
||
$goods_images = array_filter($goods_images);
|
||
}
|
||
}
|
||
|
||
// 验证图片
|
||
if (empty($goods_images)) {
|
||
$this->_error('请选择图片');
|
||
}
|
||
|
||
// 将图片数组转换为逗号分隔的字符串
|
||
$post['goods_image'] = implode(',', $goods_images);
|
||
|
||
// 添加创建时间
|
||
$post['create_time'] = date('Y-m-d H:i:s',time());
|
||
$post['page_views'] = 0;
|
||
$post['like'] = 0;
|
||
$post['collection'] = 0;
|
||
// 保存数据
|
||
$result = Db::name('moments')->insert($post);
|
||
|
||
if ($result) {
|
||
$this->_success('添加成功');
|
||
} else {
|
||
$this->_error('添加失败');
|
||
}
|
||
}
|
||
|
||
//查询员工信息
|
||
$staff = Db::name('staff')->where('onwork',1)->field('id,name')->select();
|
||
//查询商品信息
|
||
$goods = Db::name('goods')
|
||
->where('status',1)
|
||
->order('code desc')
|
||
->field('id,name')->select();
|
||
$this->assign([
|
||
'staff' => $staff,
|
||
'goods' => $goods,
|
||
]);
|
||
return $this->fetch();
|
||
}
|
||
|
||
/**
|
||
* 查看详情
|
||
*/
|
||
public function info()
|
||
{
|
||
$id = $this->request->param('id', 0);
|
||
if (empty($id)) {
|
||
$this->_error('参数错误');
|
||
}
|
||
|
||
$moment = Db::name('moments')->where('id', $id)->find();
|
||
if (empty($moment)) {
|
||
$this->_error('数据不存在');
|
||
}
|
||
|
||
// 获取关联数据
|
||
$staff = null;
|
||
if (!empty($moment['sid'])) {
|
||
$staff = Db::name('staff')->where('id', $moment['sid'])->field('id,name')->find();
|
||
}
|
||
|
||
$goods = null;
|
||
if (!empty($moment['goods_id'])) {
|
||
$goods = Db::name('goods')->where('id', $moment['goods_id'])->field('id,name')->find();
|
||
}
|
||
|
||
// 处理图片(转换为完整URL)
|
||
$images = [];
|
||
if (!empty($moment['goods_image'])) {
|
||
$imageList = explode(',', $moment['goods_image']);
|
||
foreach ($imageList as $img) {
|
||
$img = trim($img);
|
||
if (!empty($img)) {
|
||
$images[] = \app\common\server\UrlServer::getFileUrl($img);
|
||
}
|
||
}
|
||
}
|
||
|
||
$this->assign([
|
||
'moment' => $moment,
|
||
'staff' => $staff,
|
||
'goods' => $goods,
|
||
'images' => $images,
|
||
]);
|
||
|
||
return $this->fetch();
|
||
}
|
||
|
||
/**
|
||
* 编辑
|
||
*/
|
||
public function edit()
|
||
{
|
||
$id = $this->request->param('id', 0);
|
||
if (empty($id)) {
|
||
$this->_error('参数错误');
|
||
}
|
||
|
||
if ($this->request->isAjax() && $this->request->isPost()) {
|
||
// 处理表单提交
|
||
$post = $this->request->post();
|
||
|
||
// 表单验证
|
||
if (empty($post['title'])) {
|
||
$this->_error('请输入标题');
|
||
}
|
||
if (empty($post['sid'])) {
|
||
$this->_error('请选择员工');
|
||
}
|
||
if (empty($post['goods_id'])) {
|
||
$this->_error('请选择关联产品');
|
||
}
|
||
|
||
// 处理图片字段(可能是数组格式 goods_image[])
|
||
$goods_images = [];
|
||
if (isset($post['goods_image'])) {
|
||
if (is_array($post['goods_image'])) {
|
||
// 数组格式,过滤空值
|
||
$goods_images = array_filter($post['goods_image']);
|
||
} else if (!empty($post['goods_image'])) {
|
||
// 字符串格式(逗号分隔),转换为数组
|
||
$goods_images = explode(',', $post['goods_image']);
|
||
$goods_images = array_filter($goods_images);
|
||
}
|
||
}
|
||
|
||
// 验证图片
|
||
if (empty($goods_images)) {
|
||
$this->_error('请选择图片');
|
||
}
|
||
|
||
// 将图片数组转换为逗号分隔的字符串
|
||
$post['goods_image'] = implode(',', $goods_images);
|
||
|
||
// 更新数据
|
||
$result = Db::name('moments')->where('id', $id)->update($post);
|
||
|
||
if ($result !== false) {
|
||
$this->_success('编辑成功');
|
||
} else {
|
||
$this->_error('编辑失败');
|
||
}
|
||
}
|
||
|
||
// 获取当前数据
|
||
$moment = Db::name('moments')->where('id', $id)->find();
|
||
if (empty($moment)) {
|
||
$this->_error('数据不存在');
|
||
}
|
||
|
||
// 处理图片(转换为完整URL用于显示,同时保存原始路径用于提交)
|
||
$images = [];
|
||
$imagePaths = [];
|
||
if (!empty($moment['goods_image'])) {
|
||
$imageList = explode(',', $moment['goods_image']);
|
||
foreach ($imageList as $img) {
|
||
$img = trim($img);
|
||
if (!empty($img)) {
|
||
$imagePaths[] = $img; // 保存原始路径用于提交
|
||
$images[] = \app\common\server\UrlServer::getFileUrl($img); // 转换为完整URL用于显示
|
||
}
|
||
}
|
||
}
|
||
|
||
//查询员工信息
|
||
$staff = Db::name('staff')->where('onwork',1)->field('id,name')->select();
|
||
//查询商品信息
|
||
$goodsList = Db::name('goods')
|
||
->where('status',1)
|
||
->order('code desc')
|
||
->field('id,name')->select();
|
||
|
||
$this->assign([
|
||
'moment' => $moment,
|
||
'staff' => $staff,
|
||
'goods' => $goodsList,
|
||
'images' => $images,
|
||
'imagePaths' => $imagePaths, // 传递原始路径
|
||
]);
|
||
return $this->fetch();
|
||
}
|
||
|
||
/**
|
||
* 删除
|
||
*/
|
||
public function del()
|
||
{
|
||
if ($this->request->isAjax() && $this->request->isPost()) {
|
||
$id = $this->request->post('id', 0);
|
||
if (empty($id)) {
|
||
$this->_error('参数错误');
|
||
}
|
||
|
||
// 检查数据是否存在
|
||
$moment = Db::name('moments')->where('id', $id)->find();
|
||
if (empty($moment)) {
|
||
$this->_error('数据不存在');
|
||
}
|
||
|
||
// 删除数据
|
||
$result = Db::name('moments')->where('id', $id)->delete();
|
||
|
||
if ($result) {
|
||
$this->_success('删除成功');
|
||
} else {
|
||
$this->_error('删除失败');
|
||
}
|
||
} else {
|
||
$this->_error('请求方式错误');
|
||
}
|
||
}
|
||
} |