chengkun
2025-08-06 b54e02d98e42ae73071e3c01e59f12671d13d06a
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
<?php
 
namespace app\admin\controller;
 
use think\Exception;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use app\admin\model\LanguagePag as LanguagePagModel;
use app\admin\model\Language;
 
class LanguagePag extends Common {
    private $tableName = 'language_pag';
    private $viewPath = 'languagePag';
    
    /**
     * 货运属性列表
     * @return string
     */
    public function index() {
        ////////
        if (!Request::isPost()) {
            $other['title'] = '多语言包管理';
            View::assign('other', $other);
            View::assign('viewPath', $this->viewPath);
            return View::fetch();
        } else {
            try {
                $platform_type = input('platform_type');
                $lang = input('lang');
                $type = input('type');
                $name = input('name');
                $value = input('value');
                $condition = [];
                if (!empty($platform_type)) {
                    $condition[] = ['platform_type', '=', $platform_type];
                }
                if (!empty($lang)) {
                    $condition[] = ['lang', '=', $lang];
                }
                if (!empty($type)) {
                    $condition[] = ['type', '=', $type];
                }
                if (!empty($name)) {
                    $condition[] = ['name', 'like', "%$name%"];
                }
                if (!empty($value)) {
                    $condition[] = ['value', 'like', "%$value%"];
                }
                
                $p = input('p', 1);
                $other['page_size'] = input('page_size', 20);
                $order = 'id desc';
                $count = LanguagePagModel::where($condition)->count();
                $list = LanguagePagModel::field("id,lang,type,name,value,platform_type")->append(['from_value'])->where($condition)->order($order)->page($p, $other['page_size'])->select()->toArray();
                $other['count'] = $count;
                if (!$list) {
                    $list = [];
                }
                $result['other'] = $other;
                $result['list'] = $list;
                return $this->successResponse($result);
            } catch (Exception $exc) {
                return $this->errorResponse($exc->getMessage());
            }
        }
    }
    
    /*
     * 同步语言包
     */
    public function sync() {
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $data = Request::post();
            if (!isset($data['from_id']) || !is_numeric($data['from_id']) || $data['from_id'] < 1) {
                throw new Exception('源语言ID错误');
            }
            $from_id = $data['from_id'];
            if (!isset($data['to_id']) || !is_numeric($data['to_id']) || $data['to_id'] < 1) {
                throw new Exception('目标语言ID错误');
            }
            $to_id = $data['to_id'];
            //获取源语言
            $from = Language::find($from_id);
            if ($from->isEmpty()) {
                throw new Exception('源语言不存在');
            }
            $platform_type = $data['platform_type'];//平台类型
            
            //获取目标语言
            $to = Language::find($to_id);
            if ($to->isEmpty()) {
                throw new Exception('目标语言不存在');
            }
            //获取源语言
            $pags = LanguagePagModel::where(['platform_type' => $platform_type, 'lang' => $from['symbol']])
                ->order(['type' => 'asc', 'name' => 'asc'])
                ->select()
                ->toArray();
            
            foreach ($pags as &$pag) {
                //查询是否存在
                $to_pag = LanguagePagModel::where(['platform_type' => $platform_type, 'lang' => $to['symbol'], 'type' => $pag['type'], 'name' => $pag['name']])->findOrEmpty();
                if ($to_pag->isEmpty()) {
                    LanguagePagModel::create([
                        'platform_type' => $platform_type,
                        'lang'          => $to['symbol'],
                        'type'          => $pag['type'],
                        'name'          => $pag['name'],
                    ]);
                }
            }
            Db::commit();
            return $this->successResponse('同步成功!');
        } catch (Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /*
     * 翻译语言包
     */
    public function trans() {
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $data = Request::post();
            if (!isset($data['from_id']) || !is_numeric($data['from_id']) || $data['from_id'] < 1) {
                throw new Exception('源语言ID错误');
            }
            $from_id = $data['from_id'];
            if (!isset($data['to_id']) || !is_numeric($data['to_id']) || $data['to_id'] < 1) {
                throw new Exception('目标语言ID错误');
            }
            $to_id = $data['to_id'];
            //获取源语言
            $from = Language::find($from_id);
            if ($from->isEmpty()) {
                throw new Exception('源语言不存在');
            }
            //获取目标语言
            $to = Language::find($to_id);
            if ($to->isEmpty()) {
                throw new Exception('目标语言不存在');
            }
            $platform_type = $data['platform_type'];//平台类型
            //获取待翻译目标语言
            $pags = LanguagePagModel::where(['platform_type' => $platform_type, 'lang' => $to['symbol']])
                ->where(" value IS NULL OR value='' ")
                ->order(['type' => 'asc', 'name' => 'asc'])
                ->select()
                ->toArray();
            
            $qArray = array();
            
            $idArray = array();
            foreach ($pags as &$pag) {
                //查询源语言
                $from_pag = LanguagePagModel::where(['platform_type' => $platform_type, 'lang' => $from['symbol'], 'type' => $pag['type'], 'name' => $pag['name']])->where(" value IS NOT NULL  OR value='' ")->findOrEmpty();
                if ($from_pag->isEmpty()) {
                    continue;
                } else {
                    array_push($qArray, $from_pag['value']);
                    array_push($idArray, $pag['id']);
                }
            }
            if (count($qArray) == 0) throw new Exception('不存在待翻译的内容');
            $YDObj = new  \app\common\service\YouDaoTrans();
            $ret = $YDObj->do_translate($qArray, $from['trans_symbol'], $to['trans_symbol']);
            $ret = json_decode($ret, TRUE);
            if ($ret['errorCode'] != 0) {
                throw new Exception('错误代码:' . $ret['errorCode'] . ',请核对错误代码列表');
            }
            foreach ($idArray as $key => $id) {
                LanguagePagModel::update([
                    'id'    => $id,
                    'value' => $ret['translateResults'][$key]['translation'],
                ]);
            }
            Db::commit();
            return $this->successResponse('翻译成功!');
        } catch (Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    public function getLanguageList() {
        ////////
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $condition['is_show'] = 1;
            $order = 'order_id asc,id asc';
            $list = Db::name('language')->field("*")->where($condition)->order($order)->select()->toArray();
            if (!$list) {
                $list = [];
            }
            $result['list'] = $list;
            return $this->successResponse($result);
        } catch (Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
        
    }
    
    /**
     *保存
     */
    public function save() {
        //开始事务
        Db::startTrans();
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $data = Request::post();
            $id = input('post.id');
            if (!is_numeric($id) || $id < 1) {
                unset($data['id']);
            }
            $validate = new \app\admin\validate\LanguagePag();
            $check_result = $validate->check($data);
            if (!$check_result) {
                throw new Exception($validate->getError());
            }
            
            if (is_numeric($id) && $id > 0) {
                //更新数据
                $update_result = LanguagePagModel::where('id', $id)->withoutField('id')->update($data);
                if ($update_result === FALSE) {
                    throw new Exception(lang('editing_failed'));
                }
                //提交事务
                Db::commit();
                return $this->successResponse('修改成功!');
            } else {
                //添加数据
                $add_result = LanguagePagModel::insertGetId($data);
                if (!$add_result) {
                    throw new Exception(lang('add_failed'));
                }
                //提交事务
                Db::commit();
                return $this->successResponse('添加成功!');
            }
        } catch (Exception $exc) {
            // 回滚事务
            Db::rollback();
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /**
     * 删除
     * @return array|int[]
     */
    public function delete() {
        if (!Request::isPost()) {
            return $this->errorResponse(lang('request_method_incorrect'));
        }
        //开始事务
        Db::startTrans();
        try {
            $id = input('id');
            if (!is_numeric($id)) {
                return $this->errorResponse(lang('parameter_error'));
            }
            $condition['id'] = $id;
            $info = Db::name($this->tableName)->field('id')->where($condition)->find();
            if (!$info) {
                return $this->errorResponse(lang('parameter_error'));
            }
            Db::name($this->tableName)->where('id', $id)->delete();
            //提交事务
            Db::commit();
            return $this->successResponse('删除成功!');
        } catch (Exception $exc) {
            // 回滚事务
            Db::rollback();
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /*
     * 清除值
     */
    public function clearValue() {
        if (!Request::isPost()) {
            return $this->errorResponse(lang('request_method_incorrect'));
        }
        //开始事务
        Db::startTrans();
        try {
            $id = input('id');
            if (!is_numeric($id)) {
                return $this->errorResponse(lang('parameter_error'));
            }
            $condition['id'] = $id;
            $info = Db::name($this->tableName)->field('id')->where($condition)->find();
            if (!$info) {
                return $this->errorResponse(lang('parameter_error'));
            }
            Db::name($this->tableName)->where('id', $id)->update(['value'=>'']);
            //提交事务
            Db::commit();
            return $this->successResponse('值已清空!');
        } catch (Exception $exc) {
            // 回滚事务
            Db::rollback();
            return $this->errorResponse($exc->getMessage());
        }
    }
    /*
     * 生成json包
     */
    public function createJson() {
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $list = Language::order('order_id asc,id asc')->select()->toArray();
            foreach ($list as $item) {
                $paglist = LanguagePagModel::field("platform_type,lang,type,name,value")
                    ->where(['lang' => $item['symbol']])
                    ->order(['type' => 'asc', 'name' => 'asc', 'value' => 'asc'])
                    ->select()
                    ->toArray();
                if (!$paglist) {
                    continue;
                }
                foreach ($paglist as $pag){
                    $pag_result[$pag['platform_type']][$pag['lang']][$pag['type']][$pag['name']] = $pag['value'];
                }
                //生成json包
                foreach ($pag_result as $pltype => $platformtype){
                    //平台类型
                    foreach ($platformtype as $lang => $paglang){
                        $json = json_encode($paglang, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
                        // 保存文件
                        g_mkdir("./static/lang/$pltype");
                        $filePath="./static/lang/$pltype/$lang" . '.json';
                        file_put_contents($filePath, $json);
                    }
                }
            }
            $result='生成成功!';
            return $this->successResponse($result);
        } catch (Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    
}