|
1
|
-<?php
|
|
|
|
2
|
-
|
|
|
|
3
|
-namespace App\Http\Controllers\Bside\Setting;
|
|
|
|
4
|
-
|
|
|
|
5
|
-use App\Enums\Common\Code;
|
|
|
|
6
|
-use App\Helper\Translate;
|
|
|
|
7
|
-use App\Http\Controllers\Bside\BaseController;
|
|
|
|
8
|
-use App\Models\WebSetting\Proofreading;
|
|
|
|
9
|
-use App\Models\WebSetting\WebLanguage;
|
|
|
|
10
|
-use Illuminate\Support\Facades\DB;
|
|
|
|
11
|
-
|
|
|
|
12
|
-class ProofreadingController extends BaseController
|
|
|
|
13
|
-{
|
|
|
|
14
|
- const LANGUAGE_ID = 1;//默认语言英语
|
|
|
|
15
|
- const TYPE_IMAGE = 2;//校队图片
|
|
|
|
16
|
- /**
|
|
|
|
17
|
- * @name :lists
|
|
|
|
18
|
- * @author :lyh
|
|
|
|
19
|
- * @method :post
|
|
|
|
20
|
- * @time :2023/6/12 10:52
|
|
|
|
21
|
- */
|
|
|
|
22
|
- public function lists(){
|
|
|
|
23
|
- //获取语种信息
|
|
|
|
24
|
- $languageModel = new WebLanguage();
|
|
|
|
25
|
- $languageInfo = $languageModel->read(['id'=>$this->param['language_id']]);
|
|
|
|
26
|
- //获取当前链接和语种的校队列表
|
|
|
|
27
|
- $proofreadingModel = new Proofreading();
|
|
|
|
28
|
- $list = $proofreadingModel->list(['url'=>$this->param['url'],'language_id'=>$this->param['language_id'],'type'=>1],'created_at',['text','translate']);
|
|
|
|
29
|
- //获取当前URl的所有文本内容
|
|
|
|
30
|
- $new_list = $this->getUrlRead($this->param['url']);
|
|
|
|
31
|
- if(empty($list)){
|
|
|
|
32
|
- $data = [];
|
|
|
|
33
|
- $translate_list = Translate::tran($new_list, $languageInfo['short']);
|
|
|
|
34
|
- foreach ($new_list as $k=>$v){
|
|
|
|
35
|
- $data[] = [
|
|
|
|
36
|
- 'text'=>trim($v),
|
|
|
|
37
|
- 'translate'=>$translate_list[$k],
|
|
|
|
38
|
- ];
|
|
|
|
39
|
- }
|
|
|
|
40
|
- return $this->response('success',Code::SUCCESS,$data);
|
|
|
|
41
|
- }
|
|
|
|
42
|
- $data = [];//返回数据
|
|
|
|
43
|
- $old_list = [];
|
|
|
|
44
|
- foreach ($list as $v){
|
|
|
|
45
|
- $old_list[] = $v['text'];
|
|
|
|
46
|
- $data[] = [
|
|
|
|
47
|
- 'text'=>$v['text'],
|
|
|
|
48
|
- 'translate'=>$v['translate'],
|
|
|
|
49
|
- ];
|
|
|
|
50
|
- }
|
|
|
|
51
|
- $arr2 = array_values(array_diff($new_list, $old_list));
|
|
|
|
52
|
- if(!empty($arr2)){
|
|
|
|
53
|
- $translate_list = Translate::tran($arr2, $languageInfo['short']);
|
|
|
|
54
|
- foreach ($arr2 as $k1=>$v1){
|
|
|
|
55
|
- $data[] = [
|
|
|
|
56
|
- 'text'=>$v1,
|
|
|
|
57
|
- 'translate'=>$translate_list[$k1]
|
|
|
|
58
|
- ];
|
|
|
|
59
|
- }
|
|
|
|
60
|
- }
|
|
|
|
61
|
- $this->response('success',Code::SUCCESS,$data);
|
|
|
|
62
|
- }
|
|
|
|
63
|
-
|
|
|
|
64
|
- /**
|
|
|
|
65
|
- * @remark :获取图片列表
|
|
|
|
66
|
- * @name :imageList
|
|
|
|
67
|
- * @author :lyh
|
|
|
|
68
|
- * @method :post
|
|
|
|
69
|
- * @time :2023/11/23 17:29
|
|
|
|
70
|
- */
|
|
|
|
71
|
- public function imageList(){
|
|
|
|
72
|
- $proofreadingModel = new Proofreading();
|
|
|
|
73
|
- $list = $proofreadingModel->list(['url'=>$this->param['url'],'language_id'=>$this->param['language_id'],'type'=>2],'created_at',['text','translate']);
|
|
|
|
74
|
- if(empty($list)){
|
|
|
|
75
|
- $new_list = $this->getUrlImageRead($this->param['url']);
|
|
|
|
76
|
- foreach ($new_list as $k=>$v){
|
|
|
|
77
|
- $data[] = [
|
|
|
|
78
|
- 'text'=>$v,
|
|
|
|
79
|
- 'translate'=>$v,
|
|
|
|
80
|
- ];
|
|
|
|
81
|
- }
|
|
|
|
82
|
- return $this->response('success',Code::SUCCESS,$data);
|
|
|
|
83
|
- }
|
|
|
|
84
|
- $new_list = $this->getUrlImageRead($this->param['url']);
|
|
|
|
85
|
- $data = [];//返回数据
|
|
|
|
86
|
- $old_list = [];
|
|
|
|
87
|
- foreach ($list as $v){
|
|
|
|
88
|
- $old_list[] = $v['text'];
|
|
|
|
89
|
- $data[] = [
|
|
|
|
90
|
- 'text'=>$v['text'],
|
|
|
|
91
|
- 'translate'=>$v['translate'],
|
|
|
|
92
|
- ];
|
|
|
|
93
|
- }
|
|
|
|
94
|
- $arr2 = array_values(array_diff($new_list, $old_list));
|
|
|
|
95
|
- if(!empty($arr2)){
|
|
|
|
96
|
- foreach ($arr2 as $v1){
|
|
|
|
97
|
- $data[] = [
|
|
|
|
98
|
- 'text'=>$v1,
|
|
|
|
99
|
- 'translate'=>$v1
|
|
|
|
100
|
- ];
|
|
|
|
101
|
- }
|
|
|
|
102
|
- }
|
|
|
|
103
|
- $this->response('success',Code::SUCCESS,$data);
|
|
|
|
104
|
- }
|
|
|
|
105
|
-
|
|
|
|
106
|
- /**
|
|
|
|
107
|
- * @name :(新增/更新多语言)save
|
|
|
|
108
|
- * @author :lyh
|
|
|
|
109
|
- * @method :post
|
|
|
|
110
|
- * @time :2023/6/12 10:52
|
|
|
|
111
|
- */
|
|
|
|
112
|
- public function save(){
|
|
|
|
113
|
- //清除以前的翻译校队数据,重新添加
|
|
|
|
114
|
- $param = [
|
|
|
|
115
|
- 'type'=>1,
|
|
|
|
116
|
- 'project_id'=>$this->user['project_id'],
|
|
|
|
117
|
- 'url'=>$this->param['url'],
|
|
|
|
118
|
- 'language_id'=>$this->param['language_id'],
|
|
|
|
119
|
- 'alias'=>$this->param['alias'],
|
|
|
|
120
|
- 'created_at'=>date('Y-m-d H:i:s'),
|
|
|
|
121
|
- 'updated_at'=>date('Y-m-d H:i:s')
|
|
|
|
122
|
- ];
|
|
|
|
123
|
- $proofreadingModel = new Proofreading();
|
|
|
|
124
|
- DB::beginTransaction();
|
|
|
|
125
|
- try {
|
|
|
|
126
|
- $proofreadingModel->del(['language_id'=>$this->param['language_id'],'url'=>$this->param['url'],'type'=>1]);
|
|
|
|
127
|
- //删除成功后,重新添加
|
|
|
|
128
|
- $save_data = [];
|
|
|
|
129
|
- foreach ($this->param['data'] as $k => $v){
|
|
|
|
130
|
- $param['text'] = $v['text'];
|
|
|
|
131
|
- $param['translate'] = $v['translate'];
|
|
|
|
132
|
- $save_data[] = $param;
|
|
|
|
133
|
- }
|
|
|
|
134
|
- $proofreadingModel->insert($save_data);
|
|
|
|
135
|
- DB::commit();
|
|
|
|
136
|
- }catch (\Exception $e){
|
|
|
|
137
|
- DB::rollBack();
|
|
|
|
138
|
- $this->fail('系统错误请联系管理员');
|
|
|
|
139
|
- }
|
|
|
|
140
|
- $this->response('success');
|
|
|
|
141
|
- }
|
|
|
|
142
|
-
|
|
|
|
143
|
- /**
|
|
|
|
144
|
- * @name :(新增/更新多语言)save
|
|
|
|
145
|
- * @author :lyh
|
|
|
|
146
|
- * @method :post
|
|
|
|
147
|
- * @time :2023/6/12 10:52
|
|
|
|
148
|
- */
|
|
|
|
149
|
- public function saveImage(){
|
|
|
|
150
|
- //清除以前的翻译校队数据,重新添加
|
|
|
|
151
|
- $param = [
|
|
|
|
152
|
- 'type'=>2,
|
|
|
|
153
|
- 'project_id'=>$this->user['project_id'],
|
|
|
|
154
|
- 'url'=>$this->param['url'],
|
|
|
|
155
|
- 'language_id'=>$this->param['language_id'],
|
|
|
|
156
|
- 'alias'=>$this->param['alias'],
|
|
|
|
157
|
- 'created_at'=>date('Y-m-d H:i:s'),
|
|
|
|
158
|
- 'updated_at'=>date('Y-m-d H:i:s')
|
|
|
|
159
|
- ];
|
|
|
|
160
|
- $proofreadingModel = new Proofreading();
|
|
|
|
161
|
- DB::beginTransaction();
|
|
|
|
162
|
- try {
|
|
|
|
163
|
- $proofreadingModel->del(['language_id'=>$this->param['language_id'],'url'=>$this->param['url'],'type'=>2]);
|
|
|
|
164
|
- //删除成功后,重新添加
|
|
|
|
165
|
- $save_data = [];
|
|
|
|
166
|
- foreach ($this->param['data'] as $k => $v){
|
|
|
|
167
|
- $param['text'] = $v['text'];
|
|
|
|
168
|
- $param['translate'] = $v['translate'];
|
|
|
|
169
|
- $save_data[] = $param;
|
|
|
|
170
|
- }
|
|
|
|
171
|
- $proofreadingModel->insert($save_data);
|
|
|
|
172
|
- DB::commit();
|
|
|
|
173
|
- }catch (\Exception $e){
|
|
|
|
174
|
- DB::rollBack();
|
|
|
|
175
|
- $this->fail('系统错误请联系管理员');
|
|
|
|
176
|
- }
|
|
|
|
177
|
- $this->response('success');
|
|
|
|
178
|
- }
|
|
|
|
179
|
-
|
|
|
|
180
|
- /**
|
|
|
|
181
|
- * @remark :获取Url内容
|
|
|
|
182
|
- * @name :getUrlRead
|
|
|
|
183
|
- * @author :lyh
|
|
|
|
184
|
- * @method :post
|
|
|
|
185
|
- * @time :2023/11/22 10:02
|
|
|
|
186
|
- */
|
|
|
|
187
|
- public function getUrlRead($url){
|
|
|
|
188
|
- $contextOptions = [
|
|
|
|
189
|
- 'ssl' => [
|
|
|
|
190
|
- 'verify_peer' => false,
|
|
|
|
191
|
- 'verify_peer_name' => false,
|
|
|
|
192
|
- ],
|
|
|
|
193
|
- ];
|
|
|
|
194
|
- $context = stream_context_create($contextOptions);
|
|
|
|
195
|
- $sourceCode = file_get_contents($url, false, $context);
|
|
|
|
196
|
- $pattern = '/<style\b[^>]*>(.*?)<\/style>/s'; // 定义匹配`<style>`标签及其内容的正则表达式
|
|
|
|
197
|
- $strippedContent = preg_replace($pattern, '', $sourceCode); // 删除`<style>`标签及其内容
|
|
|
|
198
|
- $pattern = '/<script\b[^>]*>(.*?)<\/script>/s'; // 定义匹配`<script>`标签及其内容的正则表达式
|
|
|
|
199
|
- $strippedContent = preg_replace($pattern, '', $strippedContent); // 删除`<script>`标签及其内容
|
|
|
|
200
|
- $pattern = '/<link\b[^>]*>/'; // 定义匹配 `<link>` 标签的正则表达式
|
|
|
|
201
|
- $strippedContent = preg_replace($pattern, '', $strippedContent); // 删除 `<link>` 标签
|
|
|
|
202
|
- $pattern = '/>([^<]+)</'; // 定义匹配中间内容不是标签的正则表达式
|
|
|
|
203
|
- $matches = array();
|
|
|
|
204
|
- preg_match_all($pattern, $strippedContent, $matches);
|
|
|
|
205
|
- $textContentArray = array_filter($matches[1], function($item) {
|
|
|
|
206
|
- return !empty(trim($item));
|
|
|
|
207
|
- });
|
|
|
|
208
|
- $data = [];
|
|
|
|
209
|
- foreach ($textContentArray as $v){
|
|
|
|
210
|
- $content = trim($v);
|
|
|
|
211
|
- $trimmedString = preg_replace('/\s+/', ' ', $content);
|
|
|
|
212
|
- $data[] = $trimmedString;
|
|
|
|
213
|
- }
|
|
|
|
214
|
- $data = array_values($data);
|
|
|
|
215
|
-// $uniqueArray = array_unique($data);
|
|
|
|
216
|
-// $data = array_values($uniqueArray);
|
|
|
|
217
|
- return $data;
|
|
|
|
218
|
- }
|
|
|
|
219
|
-
|
|
|
|
220
|
- /**
|
|
|
|
221
|
- * @remark :获取Url内容
|
|
|
|
222
|
- * @name :getUrlRead
|
|
|
|
223
|
- * @author :lyh
|
|
|
|
224
|
- * @method :post
|
|
|
|
225
|
- * @time :2023/11/22 10:02
|
|
|
|
226
|
- */
|
|
|
|
227
|
- public function getUrlImageRead($url){
|
|
|
|
228
|
- $contextOptions = [
|
|
|
|
229
|
- 'ssl' => [
|
|
|
|
230
|
- 'verify_peer' => false,
|
|
|
|
231
|
- 'verify_peer_name' => false,
|
|
|
|
232
|
- ],
|
|
|
|
233
|
- ];
|
|
|
|
234
|
- $pattern = '/<img.*?src="(.*?)".*?>/i';
|
|
|
|
235
|
- $matches = array();
|
|
|
|
236
|
- $context = stream_context_create($contextOptions);
|
|
|
|
237
|
- $sourceCode = file_get_contents($url, false, $context);
|
|
|
|
238
|
- preg_match_all($pattern, $sourceCode, $matches);
|
|
|
|
239
|
- $textContentArray = $matches[1];
|
|
|
|
240
|
- $data = [];
|
|
|
|
241
|
- foreach ($textContentArray as $v){
|
|
|
|
242
|
- if(!empty($v)){
|
|
|
|
243
|
- $data[] = $v;
|
|
|
|
244
|
- }
|
|
|
|
245
|
- }
|
|
|
|
246
|
- $uniqueArray = array_unique($data);
|
|
|
|
247
|
- $data = array_values($uniqueArray);
|
|
|
|
248
|
- return $data;
|
|
|
|
249
|
- }
|
|
|
|
250
|
-} |
|
|