TranslateController.php
19.8 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<?php
/**
* @remark :
* @name :TranslateController.php
* @author :lyh
* @method :post
* @time :2024/1/12 9:39
*/
namespace App\Http\Controllers\Bside\Setting;
use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\Setting\TranslateLogic;
use App\Models\Blog\Blog;
use App\Models\CustomModule\CustomModule;
use App\Models\CustomModule\CustomModuleCategory;
use App\Models\CustomModule\CustomModuleContent;
use App\Models\News\News;
use App\Models\Product\Category;
use App\Models\Product\CategoryRelated;
use App\Models\Product\Product;
use App\Models\Project\Project;
use App\Models\RouteMap\RouteMap;
use App\Models\WebSetting\SettingNum;
use App\Models\WebSetting\Translate;
use App\Models\WebSetting\TranslateKey;
use Illuminate\Support\Facades\Cache;
class TranslateController extends BaseController
{
/**
* @remark :获取翻译校队列表
* @name :lists
* @author :lyh
* @method :post
* @time :2024/1/12 9:39
*/
public function lists(TranslateLogic $logic){
$this->request->validate([
'type'=>'required',
'language_id'=>'required',
'url'=>'required',
],[
'type.required' => 'type不能为空',
'language_id.required' => 'language_id不能为空',
'url.required' => 'url不能为空',
]);
$data = $logic->getTranslateList();
$this->response('success',Code::SUCCESS,$data);
}
/**
* @remark :根据语种获取翻译校队内容
* @name :getLanguageList
* @author :lyh
* @method :post
* @time :2024/5/8 16:08
*/
public function getLanguageList(Translate $translate){
$this->request->validate([
'language_id'=>'required',
],[
'language_id.required' => 'language_id不能为空',
]);
//上线后放开
// $is_trans_proof = $this->project['deploy_build']['is_trans_proof'] ?? 0;
// if($is_trans_proof == 0){
// $this->response('未开启导出功能。请联系管理员开启。',Code::SYSTEM_ERROR);
// }
$res_data = [];
$res_data['language_id'] = $this->param['language_id'];
$this->map['type'] = 1;
$data = $translate->formatQuery($this->map)->with('translate_data')->get()->toArray();
if(empty($data)){
$keyModel = new TranslateKey();
$keyList = $keyModel->list([],'id',['*'],'asc');
if(empty($keyList)){
$this->response('导出数据为空',Code::SYSTEM_ERROR);
}
$res_data = $this->resEmptyData($keyList);
$this->response('success',Code::SUCCESS,$res_data);
}
$res_data = $this->resData($data);
$this->response('success',Code::SUCCESS,$res_data);
}
/**
* @remark :都不为处理数据
* @name :resData
* @author :lyh
* @method :post
* @time :2024/11/22 17:50
*/
public function resData($data){
$result_id_data = [];
$keyModel = new TranslateKey();
foreach ($data as $values){
$resData['route'] = $values['url'];
$resData['page'] = $values['page'];
$resData['data'] = [];
$keyInfo = $keyModel->read(['url'=>$values['url'],'page'=>$values['page']],['id','proof_key']);
if($keyInfo !== false){
$result_id_data[] = $keyInfo['id'];
$translate_data = json_decode($values['translate_data']['data'],JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$translate_data_keys = array_keys($translate_data);
$proof_key_data = json_decode($keyInfo['proof_key'],JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
foreach ($proof_key_data as $value){
if(!in_array($value,$translate_data_keys)){
$translate_data[$value] = '';
}
}
}else{
$translate_data = json_decode($values['translate_data']['data'],JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
$resData['data'] = $translate_data ?? [];
$res_data[] = $resData;
}
$keyList = $keyModel->list(['id'=>['not in',$result_id_data]],'id',['id','proof_key','url','page']);
if(!empty($keyList)){
foreach ($keyList as $val){
$resData['route'] = $val['url'];
$resData['page'] = $val['page'];
$proof_key_data = json_decode($val['proof_key'],JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
foreach ($proof_key_data as $value){
$resData[$value] = '';
}
$res_data[] = $resData;
}
}
return $res_data;
}
/**
* @remark :data值为空 按原key返回
* @name :resEmptyKeyList
* @author :lyh
* @method :post
* @time :2024/11/22 17:45
*/
public function resEmptyData($keyList){
$res_data = [];
foreach ($keyList as $values){
$key_data = json_decode($values['proof_key'],JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$resData['route'] = $values['url'];
$resData['page'] = $values['page'];
foreach ($key_data as $key_val){
$resData['data'][] = [$key_val => ''];
}
$res_data[] = $resData;
}
return $res_data;
}
/**
* @remark :图片列表
* @name :imageList
* @author :lyh
* @method :post
* @time :2024/1/12 11:18
*/
public function imageList(TranslateLogic $logic){
$this->request->validate([
'type'=>'required',
'language_id'=>'required',
'url'=>'required',
],[
'type.required' => 'type不能为空',
'language_id.required' => 'language_id不能为空',
'url.required' => 'url不能为空',
]);
$data = $logic->getTranslateImageList();
$this->response('success',Code::SUCCESS,$data);
}
/**
* @remark :保存数据
* @name :save
* @author :lyh
* @method :post
* @time :2024/1/12 11:19
*/
public function save(TranslateLogic $logic){
$this->request->validate([
'type'=>'required',
'language_id'=>'required',
'url'=>'required',
'alias'=>'required',
],[
'type.required' => 'type不能为空',
'language_id.required' => 'language_id不能为空',
'url.required' => 'url不能为空',
'alias.required' => 'url不能为空',
]);
$rs = $logic->translateSave();
$this->response('success',Code::SUCCESS,$rs);
}
/**
* @remark :获取所有路由
* @name :getUrl
* @author :lyh
* @method :post
* @time :2024/1/12 14:34
*/
public function getUrl(){
//查看项目是5.0还是6.0
if($this->user['is_upgrade'] == Project::IS_UPGRADE_TRUE){//升级项目
$data = $this->getFiveUrl($this->map);
}else {
$data = $this->getSixUrl($this->map);
}
$this->response('success',Code::SUCCESS,$data);
}
/**
* @remark :5.0路由
* @name :getFiveUrl
* @author :lyh
* @method :post
* @time :2024/1/17 16:27
*/
public function getFiveUrl($map){
$data = [];
$routeMapModel = new RouteMap();
$list = $routeMapModel->list($map);
foreach ($list as $k => $v){
switch ($v['source']){
case RouteMap::SOURCE_PAGE:
if($v['route'] == 'index'){
$data[] = '/';
}else{
$data[] = $v['route'];
}
break;
case RouteMap::SOURCE_PRODUCT_CATE:
//获取当前产品分类关联多少产品
$productModel = new Product();
if($v['route'] == 'products'){
$count = $productModel->formatQuery(['status'=>1])->count();
}else{
$count = $productModel->formatQuery(['category_id'=>['like','%,'.$v['source_id'].',%'],'status'=>1])->count();
}
$this->pageList($data,$count,$v,1,15);
break;
case RouteMap::SOURCE_BLOG:
$data[] = 'blogs/'.$v['route'];
break;
case RouteMap::SOURCE_NEWS:
$data[] = 'news/'.$v['route'];
break;
case RouteMap::SOURCE_BLOG_CATE:
$blogModel = new Blog();
if($v['route'] == 'blog'){
$count = $blogModel->formatQuery(['status'=>1])->count();
}else{
$count = $blogModel->formatQuery(['category_id'=>['like','%,'.$v['source_id'].',%'],'status'=>1])->count();
}
$this->pageList($data,$count,$v,2,10);
break;
case RouteMap::SOURCE_NEWS_CATE:
$newsModel = new News();
if($v['route'] == 'news'){
$count = $newsModel->formatQuery(['status'=>1])->count();
}else{
$count = $newsModel->formatQuery(['category_id'=>['like','%,'.$v['source_id'].',%'],'status'=>1])->count();
}
$this->pageList($data,$count,$v,3,10);
break;
case RouteMap::SOURCE_MODULE:
$customModel = new CustomModuleContent();
$contentInfo = $customModel->read(['id'=>$v['source_id']]);
if(!empty($contentInfo) && !empty($contentInfo['category_id'])){
$categoryIdArr = $contentInfo['category_id'];
$cate_id = (int)array_shift($categoryIdArr);
$routeInfo = $routeMapModel->read(['source'=>RouteMap::SOURCE_MODULE_CATE,'source_id'=>$cate_id]);
$v['path'] = ($routeInfo['route'] ?? '');
if(!empty($v['path'])){
$v['path'] = $v['path'].'/';
}
}
$data[] = $v['path'].$v['route'];
break;
case RouteMap::SOURCE_MODULE_CATE:
$customModel = new CustomModuleContent();
$count = $customModel->formatQuery(['category_id'=>['like','%,'.$v['source_id'].',%']])->count();
//获取module_id
$customCateModel = new CustomModuleCategory();
$cateInfo = $customCateModel->read(['route'=>$v['route']],['module_id']);
if($cateInfo !== false){
$model = new CustomModule();
$moduleInfo = $model->read(['id'=>$cateInfo['module_id']],['route']);
if($moduleInfo !== false){
if($v['route'] != $moduleInfo['route']){
$v['route'] = $moduleInfo['route'].'/'.$v['route'];
}
}
}
$this->pageList($data,$count,$v,3,10);
break;
default:
$data[] = $v['route'];
break;
}
}
return $this->success($data);
}
/**
* @remark :获取6.0的url
* @name :getSixUrl
* @author :lyh
* @method :post
* @time :2024/1/17 15:04
*/
public function getSixUrl($map){
$data = [];
$routeMapModel = new RouteMap();
//查询路由中是否包含products
$list = $routeMapModel->list($map);
$productInfo = $routeMapModel->read(['route'=>'products']);
if($productInfo === false){
$productModel = new Product();
$count = $productModel->formatQuery(['status'=>1])->count();
$v['route'] = 'products';
$this->pageSixList($data,$count,$v,1,15);
}
$newsInfo = $routeMapModel->read(['route'=>'news']);
if($newsInfo === false){
$newsModel = new News();
$count = $newsModel->formatQuery(['status'=>1])->count();
$v['route'] = 'news';
$this->pageSixList($data,$count,$v,1,15);
}
$blogInfo = $routeMapModel->read(['route'=>'blog']);
if($blogInfo === false && ($this->user['is_show_blog'] == 1)){
$blogModel = new Blog();
$count = $blogModel->formatQuery(['status'=>1])->count();
$v['route'] = 'blog';
$this->pageSixList($data,$count,$v,1,15);
}
foreach ($list as $k => $v){
switch ($v['source']){
case RouteMap::SOURCE_PAGE:
if($v['route'] == 'index'){
$data[] = '/';//首页
}else{
$data[] = $v['route'];
}
break;
case RouteMap::SOURCE_PRODUCT_CATE:
//获取当前产品分类关联多少产品
$productModel = new Product();
if($v['route'] == 'products'){
$count = $productModel->formatQuery(['status'=>1])->count();
}else{
$catelists = Cache::get('product_category_trans');
if(!$catelists){
$cateModel = new Category();
$catelists = $cateModel->list(['status'=>1],'id',['id','pid']);
Cache::put('product_category_trans',$catelists,3600 * 24);
}
$ids = $this->getCategoryWithChildrenIds($catelists,$v['source_id']);
if (empty($ids) || !is_array($ids)) {
// 处理空值情况,返回0或者默认值
$count = 0;
} else {
$cateRelateModel = new CategoryRelated();
$count = $cateRelateModel->whereIn('cateid', $ids)->distinct('product_id')->count();
}
}
$this->pageSixList($data,$count,$v,1,15);
break;
case RouteMap::SOURCE_BLOG:
$data[] = 'blogs/'.$v['route'];
break;
case RouteMap::SOURCE_NEWS:
$data[] = 'news/'.$v['route'];
break;
case RouteMap::SOURCE_BLOG_CATE:
$blogModel = new Blog();
if($v['route'] == 'blog'){
$count = $blogModel->formatQuery(['status'=>1])->count();
}else{
$count = $blogModel->formatQuery(['category_id'=>['like','%,'.$v['source_id'].',%'],'status'=>1])->count();
}
$this->pageSixList($data,$count,$v,2,10);
break;
case RouteMap::SOURCE_NEWS_CATE:
$newsModel = new News();
if($v['route'] == 'news'){
$count = $newsModel->formatQuery(['status'=>1])->count();
}else{
$count = $newsModel->formatQuery(['category_id'=>['like','%,'.$v['source_id'].',%'],'status'=>1])->count();
}
$this->pageSixList($data,$count,$v,3,10);
break;
case RouteMap::SOURCE_MODULE:
$customModel = new CustomModuleContent();
$contentInfo = $customModel->read(['id'=>$v['source_id']]);
if(!empty($contentInfo) && !empty($contentInfo['category_id'])){
$categoryIdArr = $contentInfo['category_id'];
$cate_id = (int)array_shift($categoryIdArr);
$routeInfo = $routeMapModel->read(['source'=>RouteMap::SOURCE_MODULE_CATE,'source_id'=>$cate_id]);
$v['path'] = ($routeInfo['route'] ?? '');
if(!empty($v['path'])){
$v['path'] = $v['path'].'/';
}
}
$data[] = $v['path'].$v['route'];
break;
case RouteMap::SOURCE_MODULE_CATE:
$customModel = new CustomModuleContent();
$count = $customModel->formatQuery(['category_id'=>['like','%,'.$v['source_id'].',%']])->count();
$this->pageSixList($data,$count,$v,3,10);
break;
default:
$data[] = $v['route'];
break;
}
}
return $this->success($data);
}
/**
* @remark :5.0列表页路由处理
* @name :pageList
* @author :lyh
* @method :post
* @time :2024/1/17 14:15
*/
public function pageList(&$data,$count,$v,$type = 1,$pageNum = 15){
$page = 1;
if($count != 0){
//获取默认条数
$settingNumModel = new SettingNum();
$numInfo = $settingNumModel->read(['type'=>$type],['num']);
if(!empty($numInfo)){
$pageNum = $numInfo['num'];
}else{
$pageNum = $pageNum;
}
$page = ceil($count / $pageNum);//向上取整
}
if($v['source'] == RouteMap::SOURCE_NEWS_CATE){
if($v['route'] != 'news'){
$v['route'] = RouteMap::PATH_NEWS_CATE.'/'.$v['route'];
}
}elseif($v['source'] == RouteMap::SOURCE_BLOG_CATE){
if($v['route'] != 'blog'){
$v['route'] = RouteMap::PATH_BLOG_CATE.'/'.$v['route'];
}
}
for ($i = 1;$i <= $page;$i++){
if($i == 1){
$data[] = $v['route'];
}else{
$data[] = $v['route'].'/page/'.$i;
}
}
return true;
}
/**
* @remark :6.0列表路由处理
* @name :pageList
* @author :lyh
* @method :post
* @time :2024/1/17 14:15
*/
public function pageSixList(&$data,$count,$v,$type = 1,$pageNum = 15){
$page = 1;
if($count != 0){
//获取默认条数
$settingNumModel = new SettingNum();
$numInfo = $settingNumModel->read(['type'=>$type],['num']);
if(!empty($numInfo)){
$pageNum = $numInfo['num'];
}else{
$pageNum = $pageNum;
}
$page = ceil($count / $pageNum);//向上取整
}
for ($i = 1;$i <= $page;$i++){
if($i == 1){
$data[] = $v['route'];
}else{
$data[] = $v['route'].'/'.$i;
}
}
return true;
}
/**
* 迭代方式获取分类及所有子分类的总数
* @param array $categories 所有分类数据
* @param int $categoryId 分类ID
* @return int 分类总数
*/
public function getCategoryWithChildrenIds($categories, $categoryId) {
$ids = []; // 存储所有ID的数组
$queue = [$categoryId]; // 待处理的ID队列
while (!empty($queue)) {
$currentId = array_shift($queue);
// 将当前ID添加到结果数组
$ids[] = $currentId;
// 查找所有子分类
foreach ($categories as $category) {
if (isset($category['pid']) && $category['pid'] == $currentId) {
$queue[] = $category['id']; // 将子分类ID加入队列
}
}
}
return $ids;
}
}