91 lines
3.4 KiB
PHP
91 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace app\admin\server;
|
|
|
|
use app\admin\model\Moments;
|
|
use app\common\server\UrlServer;
|
|
use think\Db;
|
|
|
|
class MomentsServer
|
|
{
|
|
public static function getMoments()
|
|
{
|
|
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();
|
|
|
|
// 如果查询结果为空,尝试不使用关联查询
|
|
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;
|
|
}
|
|
}
|
|
} |