success('', $data); } /** * 用户协议 * @return void */ public function agreenment(){ $type = input('type'); $data = \app\common\model\config\System::getConfig('agreement'); if(!isset($data[$type])){ $this->error('未获取到相关内容'); } $this->success('', $data[$type]); } /** * 上传文件 * @ApiMethod (POST) * @param File $file 文件流 */ public function upload() { Config::set('default_return_type', 'json'); //必须设定cdnurl为空,否则cdnurl函数计算错误 if(\app\common\library\Platform::getSystemType() == 'single'){ Config::set('upload.cdnurl', ''); } $chunkid = $this->request->post("chunkid"); if ($chunkid) { if (!Config::get('upload.chunking')) { $this->error(__('Chunk file disabled')); } $action = $this->request->post("action"); $chunkindex = $this->request->post("chunkindex/d"); $chunkcount = $this->request->post("chunkcount/d"); $filename = $this->request->post("filename"); $method = $this->request->method(true); if ($action == 'merge') { $attachment = null; //合并分片文件 try { $upload = new Upload(); $attachment = $upload->merge($chunkid, $chunkcount, $filename); } catch (UploadException $e) { $this->error($e->getMessage()); } $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'full_url' => cdnurl($attachment->url, true)]); } elseif ($method == 'clean') { //删除冗余的分片文件 try { $upload = new Upload(); $upload->clean($chunkid); } catch (UploadException $e) { $this->error($e->getMessage()); } $this->success(); } else { //上传分片文件 //默认普通上传文件 $file = $this->request->file('file'); try { $upload = new Upload($file); $upload->chunk($chunkid, $chunkindex, $chunkcount); } catch (UploadException $e) { $this->error($e->getMessage()); } $this->success(); } } else { $attachment = null; //默认普通上传文件 $file = $this->request->file('file'); try { $upload = new Upload($file); $attachment = $upload->upload(); } catch (UploadException $e) { $this->error($e->getMessage()); } global $_W; if(\app\common\library\Platform::getSystemType() == 'single'){ return $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'full_url' => cdnurl($attachment->url,true)]); }else{ return $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'full_url' => cdnurl($attachment->url)]); } } } /** * 加载语言包 */ public function lang() { $header = ['Content-Type' => 'application/javascript']; $header = []; if (!config('app_debug')) { $offset = 30 * 60 * 60 * 24; // 缓存一个月 $header['Cache-Control'] = 'public'; $header['Pragma'] = 'cache'; $header['Expires'] = gmdate("D, d M Y H:i:s", time() + $offset) . " GMT"; } $controllername = input("controllername"); //默认只加载了控制器对应的语言名,你还根据控制器名来加载额外的语言包 $this->loadlang($controllername); $allLangFile = $this->getAllLangFile(APP_PATH . $this->request->module() . '/lang/zh-cn/'); foreach ($allLangFile as $file){ $this->loadlang($file); } return $this->success("获取成功",Lang::get()); } /** * 加载语言文件 * @param string $name */ protected function loadlang($name) { $name = Loader::parseName($name); $name = preg_match("/^([a-zA-Z0-9_\.\/]+)\$/i", $name) ? $name : 'index'; $lang = $this->request->langset(); $lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn'; Lang::load(APP_PATH . $this->request->module() . '/lang/' . $lang . '/' . str_replace('.', '/', $name) . '.php'); } /** * 获取全部语言文件名称 * @param $path * @param $dir * @return array */ public function getAllLangFile($path,$dir=[]){ $files=[]; $data = scandir($path); foreach ($data as $value){ $sub_path = $path .'/'.$value; if($value == '.'|| $value == '..'){ continue; } $mulu = $dir; if(is_dir($sub_path)){ $mulu[] = $value; $childFiles = $this->getAllLangFile($sub_path,$mulu); if(!empty($childFiles)){ $files = array_merge($files,$childFiles); } }else{ $value = str_replace(".php","",$value); $mulu = $dir; if($mulu){ $mulu[] = $value; $files[] = implode(".",$mulu); }else{ $files[] = $value; } } } return $files; } }