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()); } } }