/** * 获取 */ public function get($id) { $returnStruct = array('status' => 0, 'code' => 501, 'msg' => _('Not Implemented'), 'content' => array()); try { // 是否调用本地服务 $useLocalService = TRUE; //$useLocalService = FALSE; //* 初始化返回数据 */ $returnStatus = 1; $returnCode = 200; $returnMessage = ''; $returnData = array(); //* 收集请求数据 ==根据业务逻辑定制== */ $requestData = $this->input->get(); !empty($id) && !isset($requestData['id']) && ($requestData['id'] = $id); //* 实现功能后屏蔽此异常抛出 */ //throw new MyRuntimeException(_('Not Implemented'),501); //* 权限验证,数据验证,逻辑验证 ==根据业务逻辑定制== */ //if(util::isAccess(array(Logon::$MGR_ROLE_LABEL_SYS_ADMIN,), array(Logon::$USER_ROLE_LABEL_DENIED,Logon::$USER_ROLE_LABEL_GUEST), $this->getUserRoleLabel())==FALSE){ // throw new MyRuntimeException(_('Access Denied'),403); //} if (util::isAccess('*', array(Logon::$USER_ROLE_LABEL_DENIED), $this->getUserRoleLabel()) == FALSE) { throw new MyRuntimeException(_('Access Denied'), 403); } //* 权限验证 ==根据业务逻辑定制== */ //* 数据验证 ==根据业务逻辑定制== */ if (!isset($requestData['id']) || empty($requestData['id']) || !is_numeric($requestData['id'])) { throw new MyRuntimeException(_('Bad Request,id required'), 400); } //* 逻辑验证 ==根据业务逻辑定制== */ // 调用底层服务 !isset($servRouteInstance) && ($servRouteInstance = ServRouteInstance::getInstance(ServRouteConfig::getInstance())); // 执行业务逻辑 if ($useLocalService == TRUE) { !isset($attachmentService) && ($attachmentService = Attachment_Service::getInstance($servRouteInstance)); } else { require_once Lemon::find_file('vendor', 'phprpc/phprpc_client', TRUE); !isset($attachmentService) && ($attachmentService = new PHPRPC_Client(Lemon::config('phprpc.remote.Attachment.host'))); !isset($phprpcApiKey) && ($phprpcApiKey = Lemon::config('phprpc.remote.Attachment.apiKey')); } if ($useLocalService == TRUE) { $attachmentData = $attachmentService->getAttachmentDataById($requestData['id']); } else { $args = array($requestData['id']); $sign = md5(json_encode($args) . $phprpcApiKey); $attachmentData = $attachmentService->phprpc_getAttachmentDataById($requestData['id'], $sign); } if (empty($attachmentData)) { throw new MyRuntimeException(_('Attachment Not Found'), 404); } $attachmentAllowView = in_array($attachmentData['filePostfix'], Lemon::config('mimemap.allowViewTypes')); $isImgType = in_array($attachmentData['filePostfix'], Lemon::config('mimemap.isImgType')); $returnData = $attachmentData; //* 补充&修改返回结构体 */ $returnStruct['status'] = $returnStatus; $returnStruct['code'] = $returnCode; $returnStruct['msg'] = $returnMessage; $returnStruct['content'] = $returnData; // 资源更新时间戳 $resourceUpdateTimestamp = $attachmentData['modifyTimestamp']; // 资源缓存时间间隔 $resourceCacheTimeInterval = Lemon::config('attach.httpCacheTimeDefault'); // 当前应用配置数据覆盖全局设置 // 发送检测http lastModified头 page::httpLastModified($resourceUpdateTimestamp); // 发送http过期头 page::httpExpiresInterval($resourceCacheTimeInterval); //* 请求类型 */ if ($this->isAjaxRequest()) { // ajax 请求 // json 输出 $this->template->content = $returnStruct; // 资源更新时间戳 $this->template->resourceUpdateTimestamp = $resourceUpdateTimestamp; // 资源缓存时间间隔 $this->template->resourceCacheTimeInterval = $resourceCacheTimeInterval; } else { if (!isset($requestData['view']) && !isset($requestData['download'])) { // html 输出 //* 模板输出 */ //$this->template->return_struct = $return_struct; $content = new View($this->packageName . '/' . $this->className . '/' . __FUNCTION__); //* 变量绑定 */ $this->template->title = html::specialchars(strip_tags($attachmentData['fileName'])) . ' - ' . Lemon::config('site.name'); $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->requestData = $requestData; //* 返回结构体绑定 */ $this->template->content->returnStruct = $returnStruct; //:: 当前应用专用数据 $this->template->content->title = $attachmentData['fileName']; $this->template->content->attachmentAllowView = $attachmentAllowView; $this->template->content->isImgType = $isImgType; } else { //查看或者是下载 if ($useLocalService == TRUE) { $storeData = $attachmentService->getStoreDataByStoreId($attachmentData['storeId']); } else { $args = array($attachmentData['storeId']); $sign = md5(json_encode($args) . $phprpcApiKey); $storeData = $attachmentService->phprpc_getStoreDataByStoreId($attachmentData['storeId'], $sign); } if (isset($requestData['view']) && $attachmentAllowView) { // 查看视图 $this->template = new View($this->packageName . '/' . $this->className . '/' . __FUNCTION__ . 'DefaultView'); // 是否为图片类型 $this->template->isImgType = $isImgType; } else { // 下载视图 $this->template = new View($this->packageName . '/' . $this->className . '/' . __FUNCTION__ . 'DefaultDownload'); } // 附件信息 $this->template->attachmentData = $attachmentData; // 存储数据实体 $this->template->storeData = $storeData; } // 资源更新时间戳 isset($resourceUpdateTimestamp) && ($this->template->resourceUpdateTimestamp = $resourceUpdateTimestamp); // 资源缓存时间间隔 isset($resourceCacheTimeInterval) && ($this->template->resourceCacheTimeInterval = $resourceCacheTimeInterval); } // end of request type determine } catch (MyRuntimeException $ex) { $returnStruct['status'] = 0; $returnStruct['code'] = $ex->getCode(); $returnStruct['msg'] = $ex->getMessage(); //TODO 异常处理 //throw $ex; if ($this->isAjaxRequest()) { $this->template->content = $returnStruct; } else { $this->template->returnStruct = $returnStruct; $content = new View('info'); $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->requestData = $requestData; //* 返回结构体绑定 */ $this->template->content->returnStruct = $returnStruct; } } }