Browse Source

公共翻译与公告

lizhen_gitee 1 year ago
parent
commit
1e06287c96
2 changed files with 74 additions and 4 deletions
  1. 44 0
      application/api/controller/Message.php
  2. 30 4
      application/common/controller/Api.php

+ 44 - 0
application/api/controller/Message.php

@@ -0,0 +1,44 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+/**
+ * 系统公告
+ */
+class Message extends Api
+{
+    protected $noNeedLogin = ['*'];
+    protected $noNeedRight = ['*'];
+
+
+    /*
+     * 列表
+     */
+    public function lists() {
+        $list = Db::name('message_sys')->autopage()->order('id desc')->select();
+        $list = $this->list_lang($list,['title','info']);
+
+        $this->success(1,$list);
+    }
+
+    public function info(){
+        $id = input('id',0);
+        $list = Db::name('message_sys')->where('id',$id)->find();
+        $list = $this->info_lang($list,['title','info']);
+
+        $this->success(1,$list);
+    }
+
+
+
+
+
+
+
+
+
+
+
+}

+ 30 - 4
application/common/controller/Api.php

@@ -64,8 +64,7 @@ class Api
      */
     protected $responseType = 'json';
 
-    public $page = 1;
-    public $listrow = 10;
+    protected $lang = 'CN';
 
     /**
      * 构造方法
@@ -75,8 +74,7 @@ class Api
     public function __construct(Request $request = null)
     {
         $this->request = is_null($request) ? Request::instance() : $request;
-        $this->page = input('page',1);
-        $this->listrow= input('listrow',10);
+        $this->lang = input('lang','CN');
 
         // 控制器初始化
         $this->_initialize();
@@ -166,6 +164,34 @@ class Api
         Lang::load(APP_PATH . $this->request->module() . '/lang/' . $lang . '/' . str_replace('.', '/', $name) . '.php');
     }
 
+    //结果集信息里,多个字段需要翻译
+    protected function list_lang($list,$field){
+        if(!$list || empty($list)){
+            return $list;
+        }
+        foreach($list as $vo => $info){
+            $list[$vo] = $this->info_lang($info,$field);
+        }
+        return $list;
+    }
+    //单条信息里,多个字段需要翻译
+    protected function info_lang($data,$field){
+        if(!$data || empty($data)){
+            return $data;
+        }
+        foreach($data as $key => $val){
+            if(in_array($key,$field)){
+                if($this->lang == 'EN'){
+                    $data[$key] = $data[$key.'_en'];
+                    unset($data[$key.'_en']);
+                }else{
+                    unset($data[$key.'_en']);
+                }
+            }
+        }
+        return $data;
+    }
+
     /**
      * 操作成功返回的数据
      * @param string $msg    提示信息