后台:
添加服务逻辑的编辑 接口: 添加服务列表 添加服务详情 添加服务点赞、收藏
This commit is contained in:
@@ -2,23 +2,90 @@
|
||||
|
||||
namespace app\admin\server;
|
||||
|
||||
use app\admin\model\MomentsModel;
|
||||
use app\admin\model\Moments;
|
||||
use app\common\server\UrlServer;
|
||||
use think\Db;
|
||||
|
||||
class MomentsServer
|
||||
{
|
||||
public static function getMoments()
|
||||
{
|
||||
$data = MomentsModel::with([
|
||||
'getStaff'=>function ($query) {
|
||||
$query->field('id,name');
|
||||
},
|
||||
'getOrder'=>function ($query) {
|
||||
$query->field('id,order_sn');
|
||||
}
|
||||
try {
|
||||
$data = Moments::with([
|
||||
'getStaff'=>function ($query) {
|
||||
$query->field('id,name');
|
||||
},
|
||||
'getOrder'=>function ($query) {
|
||||
$query->field('id,order_sn');
|
||||
},
|
||||
'getProduct'=>function ($query) {
|
||||
$query->field('id,name');
|
||||
}
|
||||
])
|
||||
->order('id desc')
|
||||
->select();
|
||||
return $data;
|
||||
|
||||
// 如果查询结果为空,尝试不使用关联查询
|
||||
if (empty($data)) {
|
||||
$data = Moments::order('id desc')->select();
|
||||
}
|
||||
//处理数据
|
||||
$result = [];
|
||||
if ($data != null){
|
||||
foreach ($data as $value) {
|
||||
// 从 goods_image 字段中取第一张图片作为封面
|
||||
$img = '';
|
||||
if (!empty($value['goods_image'])) {
|
||||
$images = explode(',', $value['goods_image']);
|
||||
$img = trim($images[0]);
|
||||
$img = UrlServer::getFileUrl($img);
|
||||
}
|
||||
|
||||
$result[] = [
|
||||
'id' => $value['id'] ?? 0,
|
||||
'title' => $value['title'] ?? '',
|
||||
'order_sn' => $value['order_id'],
|
||||
'img' => $img,
|
||||
'create_time' => $value['create_time'] ?? '',
|
||||
'staff_name' => $value['getStaff']['name'] ?? null,
|
||||
'goods_name' => $value['getProduct']['name'] ?? null,
|
||||
'collection' => $value['collection'] ?? 0,
|
||||
'page_views' => $value['page_views'] ?? 0,
|
||||
'like' => $value['like'] ?? 0,
|
||||
'state' => $value['state'] ?? 1,
|
||||
];
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
} catch (\Exception $e) {
|
||||
// 如果关联查询出错,使用简单查询
|
||||
$data = Moments::order('id desc')->select();
|
||||
$result = [];
|
||||
foreach ($data as $value) {
|
||||
// 从 goods_image 字段中取第一张图片作为封面
|
||||
$img = '';
|
||||
if (!empty($value['goods_image'])) {
|
||||
$images = explode(',', $value['goods_image']);
|
||||
$firstImage = trim($images[0]);
|
||||
// 转换为完整的图片URL
|
||||
$img = UrlServer::getFileUrl($firstImage);
|
||||
}
|
||||
|
||||
$result[] = [
|
||||
'id' => $value['id'],
|
||||
'title' => $value['title'],
|
||||
'order_sn' => $value['order_id'] ?? '',
|
||||
'img' => $img,
|
||||
'create_time' => $value['create_time'],
|
||||
'staff_name' => null,
|
||||
'goods_name' => null,
|
||||
'collection' => $value['collection'] ?? 0,
|
||||
'page_views' => $value['page_views'] ?? 0,
|
||||
'like' => $value['like'] ?? 0,
|
||||
'state' => $value['state'] ?? 1,
|
||||
];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user