/** * 设置返回结果 * @author 欧远宁 * @param array $data 返回的数据 * @param string $type 返回的类型,默认为json。其他还有:tpl,jsonp,amf * @param any $para 对应的参数 * @example * //返回json<br/> * $this->_ret(<br/> * array(<br/> * 'msg'=>'hi',<br/> * 'data'=>array('aa','bb');<br/> * )<br/> * );<br/><br/> * * //根据模版返回<br/> * $this->_ret(array('name'=>'aaaa'), 'tpl', 'index');<br/> * * //直接下载 * $this->_ret(array( * 'name'=>'毕业证号导入模板.xls', * 'path'=>OP.'static/excel/import_grad_num.xls', * 'type'=>'application/vnd.ms-excel;charset=gb2312' * ),'down'); */ protected final function _ret($data = array(), $type = 'json', $para = null) { inject::cmd_after($this->method, $data, $type, $para); if ($type == 'json') { view::json($data); } else { if ($type == 'jsonp') { view::jsonp($para, $data); } else { if ($type == 'tpl') { view::tpl($para, $data); } else { if ($type == 'amf') { view::amf($data); } else { if ($type == 'excel') { view::excel($para['name'], $para['head'], $data); } else { if ($type == 'down') { if (isset($data['type'])) { view::down($data['name'], $data['path'], $data['type']); } else { view::down($data['name'], $data['path']); } } else { if ($type == 'redirect') { view::redirect($data); } } } } } } } }
/** * 根据参数,执行业务逻辑,返回结果 * @author 欧远宁 */ public static function execute() { $data = null; try { self::inc(); $data = self::get_data(); $len = count($data['cmd']); if ($len < 2) { throw new err('cmd error'); } elseif ($len == 2) { $data['cmd'][2] = 'index'; } $mdl = $data['cmd'][0]; $cls_name = $data['cmd'][1]; $method = $data['cmd'][2]; //得到session $cfg_sess = $GLOBALS['cfg']['cfg']['session']; session::$secret = $cfg_sess['secret']; session::$to_cookie = $cfg_sess['to_cookie']; if (isset($data['para']['_s'])) { session::get_session($data['para']['_s']); unset($data['para']['_s']); } else { if (session::$to_cookie) { //保存到cookie中 session::get_session($data['para']); } } $cls_path = 'cmd\\' . $mdl . '\\' . $cls_name; $method_path = $mdl . '.' . $cls_name . '.' . $method; $cls = new $cls_path($method_path); inject::cmd_before($method_path, $data['para']); unset($data['para']['_nowsid']); call_user_func(array($cls, $method), $data['para']); self::commit(); } catch (err $e) { //优先捕获自定义异常 self::rollback(); if (isset($data['para']['r'])) { } else { $ret = array('_c' => $e->getCode(), '_m' => $e->getMessage(), 'rows' => array(), 'total' => 0); view::json($ret); } } catch (Exception $ex) { //捕获未预估的异常 self::rollback(); if (isset($data['para']['r'])) { } else { $ret = array('_c' => $ex->getCode(), '_m' => $ex->getMessage(), 'rows' => array(), 'total' => 0); view::json($ret); } } }