作者 lyh
@@ -83,7 +83,7 @@ class HtmlCollect extends Command @@ -83,7 +83,7 @@ class HtmlCollect extends Command
83 //采集html页面,下载资源到本地并替换 83 //采集html页面,下载资源到本地并替换
84 try { 84 try {
85 $html = curl_c('https://' . $collect_info->domain . $collect_info->route, false); 85 $html = curl_c('https://' . $collect_info->domain . $collect_info->route, false);
86 - if ($html == '0' || strpos($html,'404 Not Found') !== false) { 86 + if ($html == '0' || strpos($html, '404 Not Found') !== false) {
87 $collect_info->status = CollectTask::STATUS_FAIL; 87 $collect_info->status = CollectTask::STATUS_FAIL;
88 $collect_info->save(); 88 $collect_info->save();
89 89
@@ -138,7 +138,7 @@ class HtmlCollect extends Command @@ -138,7 +138,7 @@ class HtmlCollect extends Command
138 } 138 }
139 139
140 140
141 - $update_log = UpdateLog::whereNotIn('project_id', [555, 626])->where('status', UpdateLog::STATUS_COM)->where('collect_status', UpdateLog::COLLECT_STATUS_UN)->orderBy('project_id', 'asc')->first(); 141 + $update_log = UpdateLog::where('status', UpdateLog::STATUS_COM)->where('collect_status', UpdateLog::COLLECT_STATUS_UN)->orderBy('project_id', 'asc')->first();
142 if (!$update_log) { 142 if (!$update_log) {
143 return false; 143 return false;
144 } 144 }
@@ -286,7 +286,7 @@ class HtmlCollect extends Command @@ -286,7 +286,7 @@ class HtmlCollect extends Command
286 return [ 286 return [
287 'download' => true, 287 'download' => true,
288 'url' => $url, 288 'url' => $url,
289 - 'url_complete' => ($scheme ?: 'https') . '://' . ($host ?: $domain) . $path . ($query ? '?' . $query : '') 289 + 'url_complete' => ($scheme ?: 'https') . '://' . $domain . $path . ($query ? '?' . $query : '')
290 ]; 290 ];
291 } else { 291 } else {
292 return [ 292 return [
@@ -302,7 +302,7 @@ class HtmlLanguageCollect extends Command @@ -302,7 +302,7 @@ class HtmlLanguageCollect extends Command
302 return [ 302 return [
303 'download' => true, 303 'download' => true,
304 'url' => $url, 304 'url' => $url,
305 - 'url_complete' => ($scheme ?: 'https') . '://' . ($host ?: $domain) . $path . ($query ? '?' . $query : '') 305 + 'url_complete' => ($scheme ?: 'https') . '://' . $domain . $path . ($query ? '?' . $query : '')
306 ]; 306 ];
307 } else { 307 } else {
308 return [ 308 return [
1 -<?php  
2 -  
3 -namespace App\Console\Commands\Update;  
4 -  
5 -use App\Models\Collect\CollectSource;  
6 -use App\Models\Collect\CollectTask;  
7 -use App\Models\Com\UpdateLog;  
8 -use App\Models\Com\UpdateOldInfo;  
9 -use App\Models\RouteMap\RouteMap;  
10 -use App\Services\CosService;  
11 -use App\Services\ProjectServer;  
12 -use Illuminate\Console\Command;  
13 -use Illuminate\Support\Facades\Cache;  
14 -use Illuminate\Support\Facades\DB;  
15 -use Illuminate\Support\Facades\Redis;  
16 -  
17 -/**  
18 - * 4.0,5.0升级到6.0,小语种页面采集  
19 - * Class ProjectImport  
20 - * @package App\Console\Commands  
21 - * @author Akun  
22 - * @date 2023/11/20 14:04  
23 - */  
24 -class HtmlLanguageSpecialCollect extends Command  
25 -{  
26 - /**  
27 - * The name and signature of the console command.  
28 - *  
29 - * @var string  
30 - */  
31 - protected $signature = 'project_html_language_special_collect';  
32 -  
33 - /**  
34 - * The console command description.  
35 - *  
36 - * @var string  
37 - */  
38 - protected $description = '执行项目html页面采集';  
39 -  
40 -  
41 - public function handle()  
42 - {  
43 - ini_set('memory_limit', '512M');  
44 - while (true) {  
45 - $this->start_collect();  
46 - }  
47 - }  
48 -  
49 - protected function start_collect()  
50 - {  
51 - $task_id = $this->get_task();  
52 - if ($task_id === false) {  
53 - //所有项目采集完成  
54 - sleep(60);  
55 - return true;  
56 - } elseif ($task_id === 0) {  
57 - //当前项目采集完成  
58 - sleep(2);  
59 - return true;  
60 - }  
61 -  
62 - $task_arr = explode('_', $task_id);  
63 - $project_id = $task_arr[0];  
64 - $collect_id = $task_arr[1];  
65 -  
66 - //设置数据库  
67 - $project = ProjectServer::useProject($project_id);  
68 - if ($project) {  
69 - $collect_info = CollectTask::select(['id', 'domain', 'route', 'language'])->where('id', $collect_id)->where('status', CollectTask::STATUS_UN)->where('language', '=', '')->first();  
70 -  
71 - if (!$collect_info) {  
72 - sleep(2);  
73 - return true;  
74 - }  
75 -  
76 - echo 'date:' . date('Y-m-d H:i:s') . ', project_id: ' . $project_id . ', collect_id: ' . $collect_id . ', collect start' . PHP_EOL;  
77 -  
78 - $collect_info->status = CollectTask::STATUS_ING;  
79 - $collect_info->save();  
80 -  
81 - //获取站点正式和测试域名  
82 - $domain_en = $this->get_domain_en($project_id);  
83 - $old_info = UpdateOldInfo::getOldDomain($project_id, $domain_en);  
84 -  
85 - //采集html页面,下载资源到本地并替换  
86 - try {  
87 - $html = curl_c('https://' . $collect_info->domain . $collect_info->route, false);  
88 - if ($html == '0' || strpos($html,'404 Not Found') !== false) {  
89 - $collect_info->status = CollectTask::STATUS_FAIL;  
90 - $collect_info->save();  
91 -  
92 - $error = $html == '0' ? 'no html' : '404 not found';  
93 - echo 'date:' . date('Y-m-d H:i:s') . ', project_id: ' . $project_id . ', collect_id: ' . $collect_id . ', error: ' . $error . PHP_EOL;  
94 - sleep(2);  
95 - return true;  
96 - }  
97 -  
98 - //如果有base64图片,先替换掉,再进行资源匹配  
99 - $new_html = $html;  
100 - preg_match_all("/data:([^;]*);base64,(.*)?\"/", $new_html, $result_img);  
101 - $img_base64 = $result_img[2] ?? [];  
102 - foreach ($img_base64 as $v64) {  
103 - $new_html = str_replace($v64, '', $new_html);  
104 - }  
105 -  
106 - $source_list = $this->html_preg($new_html, $project_id, $domain_en, $old_info['web_url_domain'], $old_info['home_url']);  
107 -  
108 - if ($source_list) {  
109 - $html = $this->upload_source($html, $source_list, $project_id, $domain_en, $old_info['web_url_domain'], $old_info['home_url']);  
110 - }  
111 - } catch (\Exception $e) {  
112 - $collect_info->status = CollectTask::STATUS_FAIL;  
113 - $collect_info->save();  
114 -  
115 - echo 'date:' . date('Y-m-d H:i:s') . ', project_id: ' . $project_id . ', collect_id: ' . $collect_id . ', error: ' . $e->getMessage() . PHP_EOL;  
116 - sleep(2);  
117 - return true;  
118 - }  
119 -  
120 - $collect_info->html = $html;  
121 - $collect_info->status = CollectTask::STATUS_COM;  
122 - $collect_info->save();  
123 -  
124 - echo 'date:' . date('Y-m-d H:i:s') . ', project_id: ' . $project_id . ', collect_id: ' . $collect_id . ', collect end' . PHP_EOL;  
125 - }  
126 - //关闭数据库  
127 - DB::disconnect('custom_mysql');  
128 -  
129 - sleep(2);  
130 - return true;  
131 - }  
132 -  
133 - //获取任务  
134 - protected function get_task()  
135 - {  
136 - $key = 'console_html_language_special_collect_task';  
137 - $task_id = Redis::rpop($key);  
138 - if ($task_id) {  
139 - return $task_id;  
140 - }  
141 -  
142 -  
143 - $update_log = UpdateLog::whereIn('project_id', [555, 626])->where('status', UpdateLog::STATUS_COM)->where('collect_status', UpdateLog::COLLECT_STATUS_UN)->first();  
144 - if (!$update_log) {  
145 - return false;  
146 - }  
147 -  
148 - switch ($update_log->api_type) {  
149 - case 'page':  
150 - $source = RouteMap::SOURCE_PAGE;  
151 - break;  
152 - case 'news':  
153 - $source = RouteMap::SOURCE_NEWS;  
154 - break;  
155 - case 'blog':  
156 - $source = RouteMap::SOURCE_BLOG;  
157 - break;  
158 - default:  
159 - $source = RouteMap::SOURCE_PRODUCT;  
160 - break;  
161 - }  
162 -  
163 - $complete = false;  
164 - //设置数据库  
165 - $project = ProjectServer::useProject($update_log->project_id);  
166 - if ($project) {  
167 - $collect_list = CollectTask::select(['id', 'project_id'])->where('project_id', $update_log['project_id'])->where('source', $source)->where('language', '=', '')->where('status', CollectTask::STATUS_UN)->orderBy('id', 'asc')->limit(50)->get();  
168 -  
169 - if ($collect_list->count() == 0) {  
170 - $complete = true;  
171 - } else {  
172 - foreach ($collect_list as $collect) {  
173 - Redis::lpush($key, $collect['project_id'] . '_' . $collect['id']);  
174 - }  
175 - }  
176 - }  
177 - //关闭数据库  
178 - DB::disconnect('custom_mysql');  
179 -  
180 - if ($complete) {  
181 - $update_log->collect_status = UpdateLog::COLLECT_STATUS_COM;  
182 - $update_log->save();  
183 - return 0;  
184 - }  
185 -  
186 - $task_id = Redis::rpop($key);  
187 - return $task_id;  
188 - }  
189 -  
190 - //获取英文站域名  
191 - protected function get_domain_en($project_id)  
192 - {  
193 - $key = 'console_html_language_domain_en';  
194 - $domain = Cache::get($key);  
195 - if (!$domain) {  
196 - $domain = CollectTask::where('project_id', $project_id)->where('language', '')->value('domain');  
197 -  
198 - Cache::add($key, $domain, 3600);  
199 - }  
200 -  
201 - return $domain;  
202 - }  
203 -  
204 - //正则匹配html资源  
205 - protected function html_preg($html, $project_id, $domain, $web_url_domain, $home_url)  
206 - {  
207 - $source = [];  
208 -  
209 - if (!$html) {  
210 - return $source;  
211 - }  
212 -  
213 - //image  
214 - preg_match_all('/<img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $html, $result_img);  
215 - $img = $result_img[2] ?? [];  
216 - foreach ($img as $vi) {  
217 - $check_vi = $this->url_check($vi, $project_id, $domain, $web_url_domain, $home_url);  
218 - if ($check_vi && (!in_array($check_vi, $source))) {  
219 - $check_vi && $source[] = $check_vi;  
220 - }  
221 - }  
222 -  
223 - //js  
224 - preg_match_all('/<script\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $html, $result_js);  
225 - $js = $result_js[2] ?? [];  
226 - foreach ($js as $vj) {  
227 - $check_vj = $this->url_check($vj, $project_id, $domain, $web_url_domain, $home_url);  
228 - if ($check_vj && (!in_array($check_vj, $source))) {  
229 - $check_vj && $source[] = $check_vj;  
230 - }  
231 - }  
232 -  
233 - //video  
234 - preg_match_all('/<source\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $html, $result_video);  
235 - $video = $result_video[2] ?? [];  
236 - foreach ($video as $vv) {  
237 - $check_vv = $this->url_check($vv, $project_id, $domain, $web_url_domain, $home_url);  
238 - if ($check_vv && (!in_array($check_vv, $source))) {  
239 - $check_vv && $source[] = $check_vv;  
240 - }  
241 - }  
242 -  
243 - //css  
244 - preg_match_all('/<link\s+[^>]*?href\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $html, $result_css);  
245 - $css = $result_css[2] ?? [];  
246 - foreach ($css as $vc) {  
247 - $check_vc = $this->url_check($vc, $project_id, $domain, $web_url_domain, $home_url);  
248 - if ($check_vc && (!in_array($check_vc, $source))) {  
249 - $check_vc && $source[] = $check_vc;  
250 - }  
251 - }  
252 -  
253 - //css background  
254 - preg_match_all("/url\(['\"]?(\s*[^>]+?)['\"]?\)/i", $html, $result_css_b);  
255 - $css_b = $result_css_b[1] ?? [];  
256 - foreach ($css_b as $vc_b) {  
257 - $check_vc_b = $this->url_check($vc_b, $project_id, $domain, $web_url_domain, $home_url);  
258 - if ($check_vc_b && (!in_array($check_vc_b, $source))) {  
259 - $check_vc_b && $source[] = $check_vc_b;  
260 - }  
261 - }  
262 -  
263 - //a标签下载资源  
264 - preg_match_all('/<a\s+[^>]*?href\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $html, $result_a);  
265 - $down = $result_a[2] ?? [];  
266 - foreach ($down as $vd) {  
267 - $check_vd = $this->url_check($vd, $project_id, $domain, $web_url_domain, $home_url);  
268 - if ($check_vd && (!in_array($check_vd, $source))) {  
269 - $check_vd && $source[] = $check_vd;  
270 - }  
271 - }  
272 -  
273 - return $source;  
274 - }  
275 -  
276 - //判断资源是否需要下载  
277 - protected function url_check($url, $project_id, $domain, $web_url_domain, $home_url)  
278 - {  
279 - if ($url) {  
280 - $url = str_replace('&quot;', '', $url);  
281 - $arr = parse_url($url);  
282 - $scheme = $arr['scheme'] ?? '';  
283 - $host = $arr['host'] ?? '';  
284 - $path = $arr['path'] ?? '';  
285 - $query = $arr['query'] ?? '';  
286 -  
287 - $path_arr = explode('.', $path);  
288 - $path_end = end($path_arr);  
289 - if (  
290 - (empty($scheme) || $scheme == 'https' || $scheme == 'http')  
291 - && (empty($host) || (strpos($web_url_domain, $host) !== false) || (strpos($home_url, $host) !== false))  
292 - && $path  
293 - && (substr($path, 0, 1) == '/')  
294 - && (strpos($path, '.') !== false)  
295 - && (strpos($path_end, 'html') === false)  
296 - && (strpos($path_end, 'php') === false)  
297 - && (strpos($path_end, 'com') === false)  
298 - && (strpos($path_end, 'xml') === false)  
299 - ) {  
300 - $source = CollectSource::where('project_id', $project_id)->where('origin', $url)->first();  
301 - if (!$source) {  
302 - $new_url = str_replace($web_url_domain, $home_url, $url);  
303 - $source_new = CollectSource::where('project_id', $project_id)->where('origin', $new_url)->first();  
304 - if (!$source_new) {  
305 - return [  
306 - 'download' => true,  
307 - 'url' => $url,  
308 - 'url_complete' => ($scheme ?: 'https') . '://' . $home_url . $path . ($query ? '?' . $query : '')  
309 - ];  
310 - } else {  
311 - return [  
312 - 'download' => false,  
313 - 'url' => $url,  
314 - 'url_complete' => $source_new['target']  
315 - ];  
316 - }  
317 - } else {  
318 - return [  
319 - 'download' => false,  
320 - 'url' => $url,  
321 - 'url_complete' => $source['target']  
322 - ];  
323 - }  
324 - } else {  
325 - return false;  
326 - }  
327 - } else {  
328 - return false;  
329 - }  
330 - }  
331 -  
332 - //下载并替换资源  
333 - protected function upload_source($html, $source, $project_id, $domain, $web_url_domain, $home_url)  
334 - {  
335 - foreach ($source as $vs) {  
336 -  
337 - if ($vs['download']) {  
338 - $new_source = CosService::uploadRemote($project_id, 'source', $vs['url_complete']);  
339 - if ($new_source) {  
340 - CollectSource::insert([  
341 - 'project_id' => $project_id,  
342 - 'origin' => $vs['url'],  
343 - 'target' => $new_source,  
344 - 'created_at' => date('Y-m-d H:i:s'),  
345 - 'updated_at' => date('Y-m-d H:i:s'),  
346 - ]);  
347 - $html = str_replace($vs['url'], getImageUrl($new_source), $html);  
348 -  
349 - if (substr($new_source, -3, 3) == 'css' || substr($new_source, -2, 2) == 'js') {  
350 -  
351 - $source_html = curl_c(getImageUrl($new_source), false);  
352 -  
353 - if (substr($new_source, -3, 3) == 'css') {  
354 - preg_match_all("/url\(['\"]?(\s*[^>]+?)['\"]?\)/i", $source_html, $result_source);  
355 - } else {  
356 - preg_match_all("/[large|thumb]+URL:['\"]+(\s*[^>]+?)['\"]+,/i", $source_html, $result_source);  
357 - }  
358 -  
359 - $js_css_source = $result_source[1] ?? [];  
360 - if ($js_css_source) {  
361 - foreach ($js_css_source as $vjs) {  
362 - if (strpos($vjs, 'URL:"') !== false) {  
363 - $vjs = substr($vjs, strpos($vjs, 'URL:"') + 5);  
364 - }  
365 -  
366 - $vjs_down = str_replace('&quot;', '', $vjs);  
367 - if (strpos($vjs_down, 'data:') !== false) {  
368 - //过滤二进制文件  
369 - continue;  
370 - }  
371 - if (strlen($vjs_down) > 255) {  
372 - //过滤太长文件  
373 - continue;  
374 - }  
375 -  
376 - $vjs_down_arr = parse_url($vjs_down);  
377 - $vjs_down_host = $vjs_down_arr['host'] ?? '';  
378 -  
379 - $cos = config('filesystems.disks.cos');  
380 - $cosCdn = $cos['cdn'];  
381 -  
382 - if ($vjs_down_host && $vjs_down_host == $cosCdn) {  
383 - //过滤已经下载的  
384 - continue;  
385 - }  
386 -  
387 - if (empty($vjs_down_host) && substr($vjs_down, 0, 1) != '/') {  
388 - //相对路径  
389 - $url_arr = explode('/', $vs['url']);  
390 - $url_arr[count($url_arr) - 1] = $vjs_down;  
391 - $vjs_down = implode('/', $url_arr);  
392 - }  
393 -  
394 - $vjs_result = $this->url_check($vjs_down, $project_id, $domain, $web_url_domain, $home_url);  
395 - if (!$vjs_result) {  
396 - continue;  
397 - }  
398 -  
399 - if ($vjs_result['download']) {  
400 - $new_vjs = CosService::uploadRemote($project_id, 'source', $vjs_result['url_complete']);  
401 - if ($new_vjs) {  
402 - CollectSource::insert([  
403 - 'project_id' => $project_id,  
404 - 'origin' => $vjs_result['url'],  
405 - 'target' => $new_vjs,  
406 - 'created_at' => date('Y-m-d H:i:s'),  
407 - 'updated_at' => date('Y-m-d H:i:s'),  
408 - ]);  
409 - $source_html = str_replace($vjs, getImageUrl($new_vjs), $source_html);  
410 - }  
411 - } else {  
412 - $source_html = str_replace($vjs, getImageUrl($vjs_result['url_complete']), $source_html);  
413 - }  
414 - }  
415 -  
416 - CosService::uploadRemote($project_id, 'source', $new_source, $new_source, $source_html);  
417 - }  
418 - }  
419 - }  
420 - } else {  
421 - $html = str_replace($vs['url'], getImageUrl($vs['url_complete']), $html);  
422 - }  
423 - }  
424 -  
425 - return $html;  
426 - }  
427 -}  
1 <?php 1 <?php
2 2
3 -namespace App\Http\Controllers\Bside\ProjectAssociation; 3 +namespace App\Http\Controllers\Aside\ProjectAssociation;
4 4
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
6 use App\Exceptions\BsideGlobalException; 6 use App\Exceptions\BsideGlobalException;
7 use App\Http\Controllers\Bside\BaseController; 7 use App\Http\Controllers\Bside\BaseController;
8 -use App\Http\Logic\Aside\ProjectAssociation\ProjectAssociationLogic;  
9 use App\Models\ProjectAssociation\ProjectAssociation; 8 use App\Models\ProjectAssociation\ProjectAssociation;
10 -use Illuminate\Http\Request;  
11 -use Illuminate\Support\Facades\DB;  
12 -use Psr\Container\ContainerExceptionInterface;  
13 -use Psr\Container\NotFoundExceptionInterface; 9 +use App\Services\Aside\ProjectAssociation\ProjectAssociationServices;
14 10
15 class ProjectAssociationController extends BaseController 11 class ProjectAssociationController extends BaseController
16 { 12 {
17 - private $ProjectAssociationLogic;  
18 -  
19 - public function __construct(Request $request)  
20 - {  
21 - $this->ProjectAssociationLogic = new ProjectAssociationLogic();  
22 - parent::__construct($request);  
23 - }  
24 13
25 /** 14 /**
26 * V6与AICC数据关联 15 * V6与AICC数据关联
27 * @return void 16 * @return void
28 * @throws BsideGlobalException 17 * @throws BsideGlobalException
29 */ 18 */
30 - public function saveWeChatData() 19 + public function save()
31 { 20 {
32 $project_id = (int)request()->post('project_id', 0); 21 $project_id = (int)request()->post('project_id', 0);
33 if (empty($project_id)) { 22 if (empty($project_id)) {
34 - $this->fail('请选择项目!', Code::USER_PARAMS_ERROE); 23 + $this->response('请选择项目!', Code::USER_PARAMS_ERROE);
35 } 24 }
36 $status = (bool)request()->post('status', 1); # 1 - 正常, 0 - 禁用 25 $status = (bool)request()->post('status', 1); # 1 - 正常, 0 - 禁用
37 26
38 $user_id = (int)env('AICC_WECHAT_USER_ID') ?? 0; 27 $user_id = (int)env('AICC_WECHAT_USER_ID') ?? 0;
39 if (empty($user_id) && $status) { 28 if (empty($user_id) && $status) {
40 - $this->fail('请选择要绑定的AICC用户!', Code::USER_PARAMS_ERROE); 29 + $this->response('请选择要绑定的AICC用户!', Code::USER_PARAMS_ERROE);
41 } 30 }
42 $friend_id = (int)request()->post('friend_id', 0); 31 $friend_id = (int)request()->post('friend_id', 0);
43 if (empty($friend_id) && $status) { 32 if (empty($friend_id) && $status) {
44 - $this->fail('请选择要绑定的AICC用户列表!', Code::USER_PARAMS_ERROE); 33 + $this->response('请选择要绑定的AICC用户列表!', Code::USER_PARAMS_ERROE);
45 } 34 }
  35 + $app = (int)request()->post('app', ProjectAssociation::ENTERPRISE_WECHAT);
  36 + $app = ProjectAssociation::getApplicationID($app);
46 $nickname = request()->post('nickname', ''); 37 $nickname = request()->post('nickname', '');
47 $user_name = request()->post('user_name', ''); 38 $user_name = request()->post('user_name', '');
48 $image = request()->post('image', ''); 39 $image = request()->post('image', '');
49 - $data = compact('project_id', 'user_id', 'friend_id', 'nickname', 'user_name', 'image');  
50 - $this->ProjectAssociationLogic->saveWeChatData($data); 40 + $data = compact('project_id', 'user_id', 'friend_id', 'nickname', 'user_name', 'image', 'app');
  41 + ProjectAssociationServices::getInstance()->save($data);
51 $this->response('绑定成功'); 42 $this->response('绑定成功');
52 } 43 }
53 44
@@ -58,27 +49,34 @@ class ProjectAssociationController extends BaseController @@ -58,27 +49,34 @@ class ProjectAssociationController extends BaseController
58 public function check() 49 public function check()
59 { 50 {
60 $project_id = (int)request()->input('project_id', 0); 51 $project_id = (int)request()->input('project_id', 0);
61 - $status = request()->input('status'); 52 + if (empty($project_id)) {
  53 + $this->response('请输入项目信息!', Code::SERVER_ERROR);
  54 + }
  55 + // 项目绑定状态
  56 + $app = (int)request()->input('type', ProjectAssociation::ENTERPRISE_WECHAT);
  57 + $app = ProjectAssociation::getApplicationID($app);
  58 + $status = request()->input('status');
62 // 重载redis缓存 59 // 重载redis缓存
63 $cache = request()->input('cache'); 60 $cache = request()->input('cache');
64 if (isset($status)) { 61 if (isset($status)) {
65 $status = (int)$status ? ProjectAssociation::STATUS_NORMAL : ProjectAssociation::STATUS_DISABLED; 62 $status = (int)$status ? ProjectAssociation::STATUS_NORMAL : ProjectAssociation::STATUS_DISABLED;
66 } 63 }
67 - $isRes = $this->ProjectAssociationLogic->normal($project_id); 64 + $isRes = ProjectAssociationServices::getInstance()->normal($project_id, $app);
68 // 当数据不存在时并开启状态,自动添加一条数据 65 // 当数据不存在时并开启状态,自动添加一条数据
69 if (is_null($isRes) && (!is_null($status) && $status)) { 66 if (is_null($isRes) && (!is_null($status) && $status)) {
70 - $isRes = $this->ProjectAssociationLogic->saveProject($project_id, $status);  
71 - if (is_null($isRes)) { 67 + $value = ProjectAssociationServices::getInstance()->saveProject($project_id, $status, $app);
  68 + if (empty($value['bool'])) {
72 // 保存数据失败 69 // 保存数据失败
73 $this->response('绑定AICC数据失败!', Code::SERVER_ERROR); 70 $this->response('绑定AICC数据失败!', Code::SERVER_ERROR);
74 } 71 }
  72 + $isRes = $value['isRes'];
75 } // 关闭状态 73 } // 关闭状态
76 elseif (!is_null($isRes) && (!is_null($status) && empty($status))) { 74 elseif (!is_null($isRes) && (!is_null($status) && empty($status))) {
77 - $bool = $this->ProjectAssociationLogic->closedState($isRes, $status); 75 + $bool = ProjectAssociationServices::getInstance()->closedState($isRes, $status);
78 if ($bool) { 76 if ($bool) {
79 // 关闭aicc绑定成功 77 // 关闭aicc绑定成功
80 $this->response('关闭aicc绑定 - 成功!'); 78 $this->response('关闭aicc绑定 - 成功!');
81 - }else{ 79 + } else {
82 // 关闭aicc绑定失败 80 // 关闭aicc绑定失败
83 $this->response('关闭aicc绑定 - 失败!'); 81 $this->response('关闭aicc绑定 - 失败!');
84 } 82 }
@@ -88,7 +86,7 @@ class ProjectAssociationController extends BaseController @@ -88,7 +86,7 @@ class ProjectAssociationController extends BaseController
88 $this->response('success', Code::SERVER_ERROR); 86 $this->response('success', Code::SERVER_ERROR);
89 } 87 }
90 $cache = isset($cache); 88 $cache = isset($cache);
91 - $result = $this->ProjectAssociationLogic->getAiccWechatLists($isRes, $cache); 89 + $result = ProjectAssociationServices::getInstance()->getAiccWechatLists($isRes, $app, $cache);
92 $this->response('success', Code::SUCCESS, $result); 90 $this->response('success', Code::SUCCESS, $result);
93 } 91 }
94 } 92 }
@@ -5,6 +5,7 @@ namespace App\Models\ProjectAssociation; @@ -5,6 +5,7 @@ namespace App\Models\ProjectAssociation;
5 use Illuminate\Database\Eloquent\Builder; 5 use Illuminate\Database\Eloquent\Builder;
6 use Illuminate\Database\Eloquent\Model; 6 use Illuminate\Database\Eloquent\Model;
7 7
  8 +
8 /** 9 /**
9 * App\Models\ProjectAssociation\ProjectAssociation 10 * App\Models\ProjectAssociation\ProjectAssociation
10 * 11 *
@@ -17,11 +18,13 @@ use Illuminate\Database\Eloquent\Model; @@ -17,11 +18,13 @@ use Illuminate\Database\Eloquent\Model;
17 * @property string|null $image AICC朋友头像 18 * @property string|null $image AICC朋友头像
18 * @property int|null $status 1 - 正常, 0 - 禁用 19 * @property int|null $status 1 - 正常, 0 - 禁用
19 * @property int|null $m_status 统计当月是否生成数据 20 * @property int|null $m_status 统计当月是否生成数据
  21 + * @property int|null $binding_app 1 - 企业微信 2 - 个人微信
20 * @property \Illuminate\Support\Carbon|null $created_at 22 * @property \Illuminate\Support\Carbon|null $created_at
21 * @property \Illuminate\Support\Carbon|null $updated_at 23 * @property \Illuminate\Support\Carbon|null $updated_at
22 * @method Builder|ProjectAssociation newModelQuery() 24 * @method Builder|ProjectAssociation newModelQuery()
23 * @method Builder|ProjectAssociation newQuery() 25 * @method Builder|ProjectAssociation newQuery()
24 * @method static Builder|ProjectAssociation query() 26 * @method static Builder|ProjectAssociation query()
  27 + * @method Builder|ProjectAssociation whereBindingApp($value)
25 * @method Builder|ProjectAssociation whereCreatedAt($value) 28 * @method Builder|ProjectAssociation whereCreatedAt($value)
26 * @method Builder|ProjectAssociation whereFriendId($value) 29 * @method Builder|ProjectAssociation whereFriendId($value)
27 * @method Builder|ProjectAssociation whereId($value) 30 * @method Builder|ProjectAssociation whereId($value)
@@ -46,60 +49,30 @@ class ProjectAssociation extends Model @@ -46,60 +49,30 @@ class ProjectAssociation extends Model
46 /** @var int 数据状态 - 禁用 */ 49 /** @var int 数据状态 - 禁用 */
47 const STATUS_DISABLED = 0; 50 const STATUS_DISABLED = 0;
48 51
49 - /**  
50 - * 保存|修改数据  
51 - * @param array $data  
52 - * @return bool  
53 - */  
54 - public function saveData(array $data): bool  
55 - {  
56 - # V6项目ID  
57 - $project_id = (int)$data['project_id'] ?? 0;  
58 - $isRes = self::query()->where('project_id', $project_id)->first();  
59 - if (!$isRes) {  
60 - $isRes = new self();  
61 - $isRes->project_id = $project_id;  
62 - }  
63 - # AICC朋友ID  
64 - $isRes->friend_id = $data['friend_id'] ?? 0;  
65 - # AICC朋友昵称  
66 - $isRes->nickname = $data['nickname'] ?? 0;  
67 - # AICC用户ID  
68 - $isRes->user_id = $data['user_id'] ?? 0;  
69 - # AICC用户姓名  
70 - $isRes->user_name = $data['user_name'] ?? '';  
71 - # AICC朋友头像  
72 - $isRes->image = $data['image'] ?? '';  
73 - return $isRes->save();  
74 - } 52 + /** @var int 企业微信 */
  53 + const ENTERPRISE_WECHAT = 1;
75 54
76 - /**  
77 - * 检查项目是否存在  
78 - * @param $project_id  
79 - * @return Builder|Model|object|null  
80 - */  
81 - public function check($project_id) 55 + /** @var int 个人微信 */
  56 + const PERSONAL_WECHAT = 2;
  57 +
  58 + public static function getApplicationApiUrl($app)
82 { 59 {
83 - return self::query()->where('project_id', $project_id)->first(); 60 + return [
  61 + 1 => env('AICC_ENTERPRISE_WECHAT_FRIEND_API_URL'),
  62 + 2 => env('AICC_WECHAT_FRIEND_API_URL'),
  63 + ][$app];
84 } 64 }
85 65
86 /** 66 /**
87 - * @param int $page  
88 - * @param int $perPage  
89 - * @return array 67 + * 返回绑定的APP应用类型
  68 + * @param $app
  69 + * @return int
90 */ 70 */
91 - public function allData(int $page = 1, int $perPage = 15) 71 + public static function getApplicationID($app)
92 { 72 {
93 - $status = 1; # 1 - 正常, 0 - 禁用  
94 - $lists = self::query()->where('status', $status)  
95 - ->where('project_id', '!=', 0)  
96 - ->where('friend_id', '!=', 0)  
97 - ->where('user_id', '!=', 0)  
98 - ->paginate($perPage, ['project_id', 'friend_id', 'user_id'], 'page', $page);  
99 - $items = $lists->Items();  
100 - $totalPage = $lists->lastPage();  
101 - $total = $lists->total();  
102 - $currentPage = $lists->currentPage();  
103 - return compact('total', 'items', 'totalPage', 'currentPage'); 73 + return [
  74 + 1 => self::ENTERPRISE_WECHAT,
  75 + 2 => self::PERSONAL_WECHAT,
  76 + ][$app] ?? self::ENTERPRISE_WECHAT;
104 } 77 }
105 } 78 }
1 <?php 1 <?php
2 2
3 -namespace App\Http\Logic\Aside\ProjectAssociation; 3 +namespace App\Services\Aside\ProjectAssociation;
4 4
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
6 -use App\Http\Logic\Logic; 6 +use App\Exceptions\BsideGlobalException;
7 use App\Models\ProjectAssociation\ProjectAssociation; 7 use App\Models\ProjectAssociation\ProjectAssociation;
  8 +use App\Services\BaseService;
8 use Illuminate\Database\Eloquent\Builder; 9 use Illuminate\Database\Eloquent\Builder;
9 use Illuminate\Database\Eloquent\Model; 10 use Illuminate\Database\Eloquent\Model;
10 use Illuminate\Support\Facades\DB; 11 use Illuminate\Support\Facades\DB;
11 12
12 -class ProjectAssociationLogic extends Logic 13 +class ProjectAssociationServices extends BaseService
13 { 14 {
14 - public function saveWeChatData($data) 15 + /**
  16 + * 保存aicc与项目关系数据
  17 + * @param array $data
  18 + * @return bool
  19 + * @throws BsideGlobalException
  20 + */
  21 + public function save(array $data)
15 { 22 {
16 - $wx = new ProjectAssociation(); 23 + $bool = false;
17 DB::beginTransaction(); 24 DB::beginTransaction();
18 try { 25 try {
19 - $status = $wx->saveData($data); 26 + # V6项目ID
  27 + $project_id = (int)$data['project_id'] ?? 0;
  28 + $isRes = ProjectAssociation::query()->where('project_id', $project_id)->first();
  29 + if (!$isRes) {
  30 + $isRes = new ProjectAssociation();
  31 + $isRes->project_id = $project_id;
  32 + }
  33 + # AICC朋友ID
  34 + $isRes->friend_id = $data['friend_id'] ?? 0;
  35 + # AICC朋友昵称
  36 + $isRes->nickname = $data['nickname'] ?? 0;
  37 + # AICC用户ID
  38 + $isRes->user_id = $data['user_id'] ?? 0;
  39 + # AICC用户姓名
  40 + $isRes->user_name = $data['user_name'] ?? '';
  41 + # AICC朋友头像
  42 + $isRes->image = $data['image'] ?? '';
  43 + $isRes->binding_app = $data['app'] ?? 1;
  44 + $bool = $isRes->save();
20 DB::commit(); 45 DB::commit();
21 } catch (\Exception $e) { 46 } catch (\Exception $e) {
22 DB::rollBack(); 47 DB::rollBack();
23 $e->getMessage(); 48 $e->getMessage();
24 - errorLog('V6与AICC关联失败', $wx, $e); 49 + errorLog('V6与AICC关联失败', $data, $e);
25 $this->fail('请检查操作是否正确!', Code::SERVER_MYSQL_ERROR); 50 $this->fail('请检查操作是否正确!', Code::SERVER_MYSQL_ERROR);
26 } 51 }
27 - return $status; 52 + return $bool;
28 } 53 }
29 54
30 /** 55 /**
31 * status - 正常 56 * status - 正常
32 * @param $project_id 57 * @param $project_id
  58 + * @param $app
33 * @return ProjectAssociation|Builder|Model|object|null 59 * @return ProjectAssociation|Builder|Model|object|null
34 */ 60 */
35 - public function normal($project_id) 61 + public function normal($project_id, $app)
36 { 62 {
37 - return ProjectAssociation::query()->whereProjectId($project_id)->whereStatus(ProjectAssociation::STATUS_NORMAL)->first(); 63 + return ProjectAssociation::query()->whereProjectId($project_id)->whereStatus(ProjectAssociation::STATUS_NORMAL)->whereBindingApp($app)->first();
38 } 64 }
39 65
40 -  
41 /** 66 /**
42 * status - 禁用 67 * status - 禁用
43 * @param $project_id 68 * @param $project_id
@@ -50,11 +75,12 @@ class ProjectAssociationLogic extends Logic @@ -50,11 +75,12 @@ class ProjectAssociationLogic extends Logic
50 75
51 /** 76 /**
52 * 初始化数据/修改数据 77 * 初始化数据/修改数据
53 - * @param int $project_id  
54 - * @param int $status  
55 - * @return Builder|Model|object|ProjectAssociation|null 78 + * @param $project_id
  79 + * @param $status
  80 + * @param $app
  81 + * @return array
56 */ 82 */
57 - public function saveProject($project_id, $status) 83 + public function saveProject($project_id, $status, $app)
58 { 84 {
59 $bool = false; 85 $bool = false;
60 DB::beginTransaction(); 86 DB::beginTransaction();
@@ -62,32 +88,58 @@ class ProjectAssociationLogic extends Logic @@ -62,32 +88,58 @@ class ProjectAssociationLogic extends Logic
62 if (is_null($isRes)) { 88 if (is_null($isRes)) {
63 $isRes = new ProjectAssociation(); 89 $isRes = new ProjectAssociation();
64 } 90 }
65 - $isRes->project_id = $project_id;  
66 - $isRes->user_id = (int)env('AICC_WECHAT_USER_ID');  
67 - $isRes->status = $status; 91 + $isRes->project_id = $project_id;
  92 + $isRes->user_id = (int)env('AICC_WECHAT_USER_ID');
  93 + $isRes->status = $status;
  94 + $isRes->binding_app = $app;
  95 + try {
  96 + $bool = $isRes->save();
  97 + DB::commit();
  98 + } catch (\Exception $exception) {
  99 + DB::rollBack();
  100 + }
  101 + return compact('bool', 'isRes');
  102 + }
  103 +
  104 +
  105 + /**
  106 + * 关闭状态
  107 + * @param ProjectAssociation $res
  108 + * @param int $status 1 - 正常, 0 - 禁用
  109 + * @return bool
  110 + */
  111 + public function closedState($res, $status)
  112 + {
  113 + DB::beginTransaction();
  114 + $bool = false;
68 try { 115 try {
69 - $isRes->save(); 116 + $res->status = $status;
  117 + $bool = $res->save();
70 DB::commit(); 118 DB::commit();
71 } catch (\Exception $exception) { 119 } catch (\Exception $exception) {
72 DB::rollBack(); 120 DB::rollBack();
73 } 121 }
74 - return $isRes; 122 + return $bool;
75 } 123 }
76 124
77 /** 125 /**
78 * 获取AICC微信列表数据 126 * 获取AICC微信列表数据
79 * @param ProjectAssociation $res 127 * @param ProjectAssociation $res
80 - * @param bool $cache 128 + * @param int $type 绑定app应用类型
  129 + * @param bool $cache 是否缓存
81 * @return mixed 130 * @return mixed
82 */ 131 */
83 - public function getAiccWechatLists($res, $cache = false) 132 + public function getAiccWechatLists($res, $type = ProjectAssociation::PERSONAL_WECHAT, $cache = false)
84 { 133 {
85 - $redis_key = 'aicc_friend_lists_' . (int)env('AICC_WECHAT_USER_ID'); 134 + $redis_key = 'aicc_friend_lists_' . $type . '_' . (int)env('AICC_WECHAT_USER_ID');
86 $result = $cache ? false : redis_get($redis_key); 135 $result = $cache ? false : redis_get($redis_key);
87 if (empty($result)) { 136 if (empty($result)) {
88 - $url = env('AICC_URL') . env('AICC_WECHAT_FRIEND_API_URL'); 137 + $apiUrl = ProjectAssociation::getApplicationApiUrl($type);
  138 + $url = env('AICC_URL') . $apiUrl;
89 $result = curlGet($url); 139 $result = curlGet($url);
90 - redis_set($redis_key, json_encode($result), 60); 140 + if ($result['data']) {
  141 + redis_set($redis_key, json_encode($result), 60);
  142 + }
91 } else { 143 } else {
92 $result = json_decode($result, true); 144 $result = json_decode($result, true);
93 } 145 }
@@ -99,24 +151,4 @@ class ProjectAssociationLogic extends Logic @@ -99,24 +151,4 @@ class ProjectAssociationLogic extends Logic
99 ]; 151 ];
100 return $result; 152 return $result;
101 } 153 }
102 -  
103 - /**  
104 - * 关闭状态  
105 - * @param ProjectAssociation $res  
106 - * @param int $status 1 - 正常, 0 - 禁用  
107 - * @return bool  
108 - */  
109 - public function closedState($res, $status)  
110 - {  
111 - DB::beginTransaction();  
112 - $bool = false;  
113 - try {  
114 - $res->status = $status;  
115 - $bool = $res->save();  
116 - DB::commit();  
117 - } catch (\Exception $exception) {  
118 - DB::rollBack();  
119 - }  
120 - return $bool;  
121 - }  
122 } 154 }
@@ -8,10 +8,56 @@ namespace App\Services; @@ -8,10 +8,56 @@ namespace App\Services;
8 8
9 use App\Enums\Common\Code; 9 use App\Enums\Common\Code;
10 use App\Exceptions\BsideGlobalException; 10 use App\Exceptions\BsideGlobalException;
  11 +use Mockery;
11 12
12 class BaseService 13 class BaseService
13 { 14 {
14 /** 15 /**
  16 + * 单例模式
  17 + * 特点:三私,两静,一公
  18 + * 三私:三个私有方法,(静态变量、构造函数、克隆函数)
  19 + * 两静:两个静态方法,(一个静态变量,一个静态方法)
  20 + * 一公:单例模式的出口
  21 + *
  22 + * 优点:
  23 + * 1、在内存里只有一个实例,减少了内存的开销,尤其是频繁的创建和销毁实例(比如管理学院首页页面缓存)。
  24 + * 2、避免对资源的多重占用(比如写文件操作)。
  25 + */
  26 +
  27 + protected static $instance;
  28 +
  29 + /**
  30 + * @return static
  31 + */
  32 + public static function getInstance()
  33 + {
  34 + if ((static::$instance[static::class] ?? null) instanceof static) {
  35 + return static::$instance[static::class];
  36 + }
  37 + return static::$instance[static::class] = new static();
  38 + }
  39 +
  40 + /**
  41 + * @return Mockery\Mock
  42 + */
  43 + public static function mockInstance()
  44 + {
  45 + return static::$instance[static::class] = Mockery::mock(static::class)
  46 + ->makePartial()
  47 + ->shouldAllowMockingProtectedMethods();
  48 + }
  49 +
  50 + private function __construct()
  51 + {
  52 +
  53 + }
  54 +
  55 + private function __clone()
  56 + {
  57 + // TODO: Implement __clone() method.
  58 + }
  59 +
  60 + /**
15 * @notes: 手动抛出异常 61 * @notes: 手动抛出异常
16 * @param string $msg 62 * @param string $msg
17 * @param string $code 63 * @param string $code
@@ -4,7 +4,7 @@ @@ -4,7 +4,7 @@
4 */ 4 */
5 5
6 use App\Http\Controllers\Aside; 6 use App\Http\Controllers\Aside;
7 -use App\Http\Controllers\Bside\ProjectAssociation\ProjectAssociationController; 7 +use App\Http\Controllers\Aside\ProjectAssociation\ProjectAssociationController;
8 use Illuminate\Support\Facades\Route; 8 use Illuminate\Support\Facades\Route;
9 9
10 10
@@ -72,7 +72,7 @@ Route::middleware(['aloginauth'])->group(function () { @@ -72,7 +72,7 @@ Route::middleware(['aloginauth'])->group(function () {
72 //发送记录 72 //发送记录
73 Route::any('/log', [Aside\Ai\AiLogController::class, 'lists'])->name('admin.lists'); 73 Route::any('/log', [Aside\Ai\AiLogController::class, 'lists'])->name('admin.lists');
74 // 绑定AICC微信应用 74 // 绑定AICC微信应用
75 - Route::post('/wechat', [ProjectAssociationController::class, 'saveWeChatData'])->name('admin.aicc.wechat'); 75 + Route::post('/binding/save', [ProjectAssociationController::class, 'save'])->name('admin.binding.save');
76 // OA后台开启关闭AICC用户绑定 76 // OA后台开启关闭AICC用户绑定
77 Route::post('/check', [ProjectAssociationController::class, 'check'])->name('admin.aicc.check'); 77 Route::post('/check', [ProjectAssociationController::class, 'check'])->name('admin.aicc.check');
78 }); 78 });