ProductController.php
10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
namespace App\Http\Controllers\Api;
use App\Enums\Common\Code;
use App\Helper\Translate;
use App\Models\Domain\DomainInfo;
use App\Models\Product\Category;
use App\Models\Product\CategoryRelated;
use App\Models\Product\Product;
use App\Models\Project\ProjectAiSetting;
use App\Models\RouteMap\RouteMap;
use App\Services\CosService;
use App\Services\ProjectServer;
use App\Utils\LogUtils;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
/**
* Class ProductController
* @package App\Http\Controllers\Api
* @author zbj
* @date 2024/2/1
*/
class ProductController extends BaseController
{
/**
* 获取项目随机产品的图片
* @param Request $request
* @return void
*/
public function getImages(Request $request)
{
$project_id = $request->input('project_id');
$project = ProjectServer::useProject($project_id); $project = ProjectServer::useProject($project_id);
if (!$project) {
$this->response('项目不存在', Code::SYSTEM_ERROR);
}
try {
$info = Product::where('status', Product::STATUS_ON)->whereNotNull('gallery')->inRandomOrder()->select(['title', 'seo_mate', 'gallery'])->first();
$info['gallery'] = array_map(function ($item) use ($project) {
$item['url'] = getImageUrl($item['url'], $project['storage_type'], $project['project_location']);
return $item;
}, $info['gallery']);
} catch (\Exception $e) {
LogUtils::error('Project Id: ' . $project_id . ' getProductImages error:' . $e->getMessage());
$info = [];
}
$this->response('success', Code::SUCCESS, $info);
}
/**
* @remark :保存产品
* @name :saveProduct
* @author :lyh
* @method :post
* @time :2024/3/20 11:09
*/
public function saveProduct(){
$api_key = '8242LYUGaOfUQ1koc4Rq6MhEEOG7NW68oRaB7iO9coJDjG5L5gA1Q';
if($this->request->header('api-key') != $api_key){
$this->response('非法请求',Code::SYSTEM_ERROR);
}
$this->request->validate([
'project_id'=>'required',
'title'=>'required',
],[
'project_id.required' => 'project_id不能为空',
'title.required' => 'title不能为空',
]);
$project = ProjectServer::useProject($this->param['project_id']);
if (!$project) {
$this->response('项目不存在', Code::SYSTEM_ERROR);
}
//处理图片
$imageInfo = $this->handleImage($this->param['image'],$this->param['project_id']);
$thumb = $imageInfo['thumb'];
$gallery = $imageInfo['gallery'];
//处理分类
$category_id = $this->handleCategory($this->param['category_name'],$this->param['project_id']);
try {
$productModel = new Product();
$productInfo = $productModel->read(['title'=>$this->param['title']]);
if($productInfo === false){
$productData = [
'project_id'=>$this->param['project_id'],
'title'=>$this->param['title'],
'intro'=>$this->param['intro'] ?? '',
'content'=>$this->param['content'] ?? '',
'thumb'=>json_encode($thumb,true),
'gallery'=>json_encode($gallery,true),
'created_at'=>date('Y-m-d H:i:s'),
'updated_at'=>date('Y-m-d H:i:s')
];
if(!empty($category_id)){
$productData['category_id'] = ','.$category_id.',';
}
$product_id = $productModel->addReturnId($productData);
$route = RouteMap::setRoute($productData['title'], RouteMap::SOURCE_PRODUCT, $product_id, $this->param['project_id']);
$productModel->edit(['route'=>$route],['id'=>$product_id]);
//添加到关联分类
CategoryRelated::saveRelated($product_id, [$category_id]);
}
} catch (\Exception $e) {
LogUtils::error('Project Id: ' . $this->param['project_id'] . ' saveProduct error:' . $e->getMessage());
$this->response('保存失败,请联系管理员',Code::SYSTEM_ERROR);
}
$this->response('success');
}
/**
* @remark :处理图片
* @name :handleImage
* @author :lyh
* @method :post
* @time :2024/3/20 14:44
*/
public function handleImage($image,$project_id){
$gallery = [];
$thumb = [];
if(!empty($image) && is_array($image)){
foreach ($image as $k => $v){
//TODO::图片转存
$url = CosService::uploadRemote($project_id,'image_product',$v);
if($k == 0){
$thumb = ['url'=>$url,'alt'=>''];
}
$gallery[] = ['url'=>$url,'alt'=>''];
}
}
return ['thumb'=>$thumb,'gallery'=>$gallery];
}
/**
* @remark :处理分类
* @name :handleCategory
* @author :lyh
* @method :post
* @time :2024/3/20 14:48
*/
public function handleCategory($category_name,$project_id){
$category_id = '';
if(isset($category_name) && !empty($category_name)){
$categoryModel = new Category();
$cateInfo = $categoryModel->read(['title'=>$this->param['category_name']]);
if($cateInfo === false){
$cateData =[
'project_id'=>$project_id,
'title'=>$category_name,
'pid'=>0,
'created_at'=>date('Y-m-d H:i:s'),
'updated_at'=>date('Y-m-d H:i:s')
];
$cate_id = $categoryModel->addReturnId($cateData);
$route = RouteMap::setRoute($cateData['title'], RouteMap::SOURCE_PRODUCT_CATE, $cate_id, $project_id);
$categoryModel->edit(['route'=>$route],['id'=>$cate_id]);
}else{
$cate_id = $cateInfo['id'];
}
$category_id = $cate_id;
}
return $category_id;
}
/**
* @param Request $request
* @author zbj
* @date 2024/1/22
*/
protected function searchProduct(Request $request)
{
$project_id = $request->input('project_id');
$limit = $request->input('limit') ?: 5;
$text = $request->input('text');
$key = $request->input('key') ?: 'title';
$key_limit = $request->input('key_limit') ?: 15;
$project = ProjectServer::useProject($project_id);
if (!$project) {
$this->response('项目不存在', Code::SYSTEM_ERROR);
}
//匹配产品
$products = Product::with('category')
->where("title", 'like', $text . '%')
->where("status", 1)
->orderBy("id", "DESC")
->limit($limit)
->select('title', 'thumb', 'id', 'route')
->get()
->toArray();
//对应分类
$categories = [];
foreach ($products as &$product) {
foreach ($product['category'] as $category) {
$categories[$category['route']] = [
'title' => $category['title'],
'route' => '/' . $category['route'] . '/',
];
}
unset($product['id']);
unset($product['category']);
if(!empty($product['thumb']) && !empty($product['thumb']['url'])){
$product['thumb'] = getImageUrl($product['thumb']['url'],$project['storage_type'] ?? 0,$project['project_location']);
}
$product['route'] = '/' . $product['route'] . '/';
}
$data = [
'products' => $products,
'categories' => array_values($categories),
'suggestions' => $this->searchSuggestion($text, $key, $key_limit)
];
$this->response('success', Code::SUCCESS, $data);
}
protected function searchSuggestion($text, $key, $key_limit): array
{
$model = new Product();
$columns = $model->getConnection()->getSchemaBuilder()->getColumnListing($model->getTable());
//产品字段
if (in_array($key, $columns)) {
//匹配产品
$suggestions = Product::where("status", 1)
->where($key, 'like', $text . '%')
->orderBy("id", "DESC")
->limit($key_limit)
->select($key .' as title', 'route')
->get()
->toArray();
} else {
//扩展字段
$suggestions = Product::leftJoin('gl_product_extend_info as pei', 'gl_product.id', '=', 'pei.product_id')
->where('pei.values', 'like', $text . '%')
->where("gl_product.status", 1)
->orderBy("gl_product.id", "DESC")
->limit($key_limit)
->select('pei.values','gl_product.route')
->get()
->toArray();
}
foreach ($suggestions as &$suggestion){
$suggestion['route'] = '/' . $suggestion['route'] . '/';
}
return $suggestions;
}
/**
* 搜索给AI博客 产品推荐用
* 全文搜索 产品标题
* @author zbj
* @date 2025/5/8
*/
public function searchProductToAiBlog(Request $request){
$mch_id = $request->input('mch_id');
$key = $request->input('key');
$keyword = $request->input('keyword');
if(!$keyword){
$this->response('关键词不能为空', Code::SYSTEM_ERROR);
}
$project_id = ProjectAiSetting::where('mch_id', $mch_id)->where('key', $key)->value('project_id');
if(!$project_id){
$this->response('项目不存在', Code::SYSTEM_ERROR);
}
ProjectServer::useProject($project_id);
$product = Product::whereFullText('title', $keyword)->where('status', Product::STATUS_ON)->inRandomOrder()->select(['title', 'intro', 'thumb', 'id'])->first();
if($product){
ProjectServer::useProject($project_id);
$product['url'] = (new DomainInfo())->getProjectIdDomain($project_id) . getRouteMap(RouteMap::SOURCE_PRODUCT,$product->id);
}
$this->response('success', Code::SUCCESS, $product?:[]);
}
}