name = null; $this->type = null; $this->mask = null; $this->value = null; $this->default = null; $this->options = null; $this->script = null; $this->returnfield = null; $this->FromString($String); } public function FromArray($Array) { if (is_null($Array)) return false; if (is_object($Array)) $Array = (array)$Array; if (!is_array($Array)) { $this->Name = strval($Array); return true; } $Reflect = new ReflectionClass($this); foreach($Array as $Key => $Value) { //if (!$Reflect->HasProperty($Key)) // continue; if ($Property = $Reflect->GetProperty($Key)) { //if ($Property->IsPublic()) $Property->SetValue($this, $Value); } } return true; } public function FromString($String) { if (!$this->FromArray(JsonStringToJsonObject( strtr($String, array("\r" => ' ', "\n" => ' ', ))))) $this->name = empty($String) ? '' : $String; return true; } } class report extends BaseMethod { private $sql_table = 'ct_report_list'; /** * @param $String * @param int $SplitLength * @return array|bool */ function utf8_str_split($String, $SplitLength = 1) { if (!preg_match('/^[0-9]+$/', $SplitLength) || $SplitLength < 1) return false; $Length = mb_strlen($String, 'UTF-8'); if ($Length <= $SplitLength) return array($String); preg_match_all('/.{' . $SplitLength . '}|[^x00]{1,' . $SplitLength . '}$/us', $String, $Result); return $Result[0]; } /** * Convert a string to an array * @param string $String * @param integer $SplitLength * @param string $Charset * @return array of char */ function mb_str_split($String, $SplitLength = 1 ,$Charset = USEDCHARSET) { if (strcasecmp($Charset, 'utf-8') == 0) return $this->utf8_str_split($String, $SplitLength); elseif (func_num_args() == 1) return preg_split('/(?\r\n"; /// $CurrentIndex = 0; $StartIndex = 0; //$SqlCommandArray = str_split($SqlCommand); $SqlCommandArray = $this->mb_str_split($SqlCommand, 1, USEDCHARSET); //$SqlCommandArray = $this->utf8_str_split($SqlCommand); while ($CurrentIndex < $CommandLength) { $c = $SqlCommandArray[$CurrentIndex]; //$c = mb_substr($SqlCommand, $CurrentIndex, 1, USEDCHARSET); /// 先过滤掉不需要判断的字符,一直碰到参数的前导字符或者是引号 while ($CurrentIndex < $CommandLength) { $c = $SqlCommandArray[$CurrentIndex]; //$c = mb_substr($SqlCommand, $CurrentIndex, 1, USEDCHARSET); if (mb_strstr($StringDelimiter, $c, false, USEDCHARSET)) break; else $CurrentIndex++; } switch ($c) { /// 字符串结束 case "\0": case '': break; /// 引号表示字符串 case '`': case '\'': case '"': /// 记录下这个标记,表示现在是字符串开始;再碰到一个同样的标记表示字符串结束。 $cLiteral = $c; do { /// 快速跳过字符串内容 $CurrentIndex++; $c = $SqlCommandArray[$CurrentIndex]; //$c = mb_substr($SqlCommand, $CurrentIndex, 1, USEDCHARSET); if ($cLiteral == $c) break; } while ($CurrentIndex < $CommandLength); $CurrentIndex++; break; default: $CurrentString = mb_substr($SqlCommand, $CurrentIndex, 2, USEDCHARSET); if (0 == strcasecmp($CurrentString, '--')) /// 单行注释 { $CurrentIndex += 2; $c = $SqlCommandArray[$CurrentIndex]; //$c = mb_substr($SqlCommand, $CurrentIndex, 1, USEDCHARSET); /// 迅速跳过注释行 while ($CurrentIndex < $CommandLength) { if ("\r" == $c || "\n" == $c) { while ("\r" == $c || "\n" == $c) { $CurrentIndex++; $c = $SqlCommandArray[$CurrentIndex]; //$c = mb_substr($SqlCommand, $CurrentIndex, 1, USEDCHARSET); } break; } else { $CurrentIndex++; $c = $SqlCommandArray[$CurrentIndex]; //$c = mb_substr($SqlCommand, $CurrentIndex, 1, USEDCHARSET); } } break; } else if (0 == strcasecmp($CurrentString, '/*')) /// 多行注释 { $CurrentIndex += 2; /// 迅速跳过注释内容 while ($CurrentIndex < $CommandLength) { $CurrentString = mb_substr($SqlCommand, $CurrentIndex, 2, USEDCHARSET); if (0 == strcasecmp($CurrentString, '*/')) { $CurrentIndex += 2; break; } else { $CurrentIndex++; } } break; } else if (mb_strstr($ForwadChars, $c, false, USEDCHARSET)) { $CurrentIndex++; $c = $SqlCommandArray[$CurrentIndex]; //$c = mb_substr($SqlCommand, $CurrentIndex, 1, USEDCHARSET); if (mb_strstr($ForwadChars, $c, false, USEDCHARSET)) { $CurrentIndex++; $c = $SqlCommandArray[$CurrentIndex]; //$c = mb_substr($SqlCommand, $CurrentIndex, 1, USEDCHARSET); } $Result .= mb_substr($SqlCommand, $StartIndex, $CurrentIndex - $StartIndex - 1) . $ReplaceIdentifier; if ('{' == $c) /// 处理json字符串表示的参数 { $ParameterIndex = $CurrentIndex; $JsonFlagCount = 0; while ($CurrentIndex < $CommandLength) { $c = $SqlCommandArray[$CurrentIndex]; //$c = mb_substr($SqlCommand, $CurrentIndex, 1, USEDCHARSET); $CurrentIndex++; switch ($c) { case '{': $JsonFlagCount++; break; case '}': $JsonFlagCount--; break; } if (0 == $JsonFlagCount) break; } } else /// 非json表示的参数 { $cLiteral = ''; if (mb_strstr('`\'"', $c, false, USEDCHARSET)) { $cLiteral = $c; $CurrentIndex++; } $ParameterIndex = $CurrentIndex; while ($CurrentIndex < $CommandLength) { $c = $SqlCommandArray[$CurrentIndex]; //$c = mb_substr($SqlCommand, $CurrentIndex, 1, USEDCHARSET); if (($cLiteral == $c) || ('' == $cLiteral && mb_strstr($NameDelimiter, $c, false, USEDCHARSET))) break; $CurrentIndex++; } } $Parameter = mb_substr($SqlCommand, $ParameterIndex, $CurrentIndex - $ParameterIndex, USEDCHARSET); array_push($SqlParameters, new ReportParameter($Parameter)); $StartIndex = $CurrentIndex; } break; } } $Result .= mb_substr($SqlCommand, $StartIndex, $CurrentIndex - $StartIndex); $SqlCommand = $Result; return true; } /** * 获取菜单 * @param RequestParameter $request * @param ReturnParameter $return * @return bool */ public function getMenus($request, &$return) { try { $biz_content = $request->biz_content; //查询用户 $admin_id = isset($biz_content['admin_id']) ? $biz_content['admin_id'] : false; // 管理员id if(!$admin_id){ $return->SetErrors(ERRORCODE_INPARAMERROR, ERRORINFO_INPARAMERROR); return false; } $admin = $this->PDO_Request(/** @lang text */' SELECT `level` FROM ct_manager_info m WHERE `login_id` = ? AND `is_enabled` = ?;', $admin_id, 1); if (!$this->pdo_isdone()) { $return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo()); return false; } if (!$admin) { $return->SetErrors(ERRORCODE_NOPOWERERROR, ERRORINFO_NOPOWERERROR); return false; } $cmd = $this->NewMasterCommand(); // 获取所有报表脚本信息 $items = $this->PDO_Request(/** @lang text */" select a.id as cate_id, a.name, a.icon, b.report_id, b.report_name, b.command_line, b.url, b.desc from ct_report_cate as a left join {$this->sql_table} as b on a.`id` = b.`cate_id` where a.`status` = 1 and b.`is_enabled` = 1 group by cate_id, report_id"); $menu = array(); foreach($items as $key=>$val) { // 主菜单部分 $menu[$val['cate_id']]['id'] = $val['cate_id']; $menu[$val['cate_id']]['name'] = $val['name']; $menu[$val['cate_id']]['icon'] = $val['icon']; $this->ParseSqlCommand($val['command_line'], $SqlParameters); //$SqlParameters = json_decode( json_encode( $SqlParameters),true); foreach ($SqlParameters as $sk => $sv) { // 脚本处理 if (strcasecmp($sv->type, 'sql') == 0) { $replace_admin_id = $cmd->getidentifiers($admin_id, true); // 处理sql引号 $script = str_replace('[%adminId%]', $replace_admin_id, $sv->script); $result = $this->PDO_Request($script); if (!$this->pdo_isdone()) { $return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo()); return false; } $SqlParameters[$sk]->options = $result; unset($SqlParameters[$sk]->script); } } // 子菜单部分 $menu[$val['cate_id']]['child'][] = [ 'report_id' => $val['report_id'], 'report_name' => $val['report_name'], 'url' => $val['url'], 'desc' => json_decode($val['desc']), 'values' => $SqlParameters, ]; } // 5、数据返回部分 $return->biz_content = $menu; return true; } catch (Exception $e) { $return->SetErrors($e->getCode(), $e->getMessage()); return false; } } /** * 处理查询 * @param RequestParameter $request * @param ReturnParameter $return * @return bool */ public function getReportTest($request, &$return) { try { // 获取具体id $biz_content = $request->biz_content; $report_id = isset($biz_content['report_id']) ? $biz_content['report_id'] : false; // 报表id $admin_id = isset($biz_content['admin_id']) ? $biz_content['admin_id'] : false; // 管理员id $channel = isset($biz_content['channel']) ? $biz_content['channel'] : false; // 指定渠道key if(!$report_id || !$admin_id){ $return->SetErrors(ERRORCODE_INPARAMERROR, ERRORINFO_INPARAMERROR); return false; } //查询用户 $admin = $this->PDO_Request(/** @lang text */' SELECT `level` FROM ct_manager_info m WHERE `login_id` = ? AND `is_enabled` = ?;', $admin_id, 1); if (!$this->pdo_isdone()) { $return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo()); return false; } if (!$admin) { $return->SetErrors(ERRORCODE_NOPOWERERROR, ERRORINFO_NOPOWERERROR); return false; } $item = $this->PDO_Request(/** @lang text */"select report_id, report_name, command_line, url from {$this->sql_table} where report_id = ?;", $report_id); if (!$this->pdo_isdone()) { $return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo()); return false; } $SqlCommand = $item[0]['command_line']; # 分页信息 $page_index = empty($biz_content['page_index']) ? 1 : intval($biz_content['page_index']); // 当前页 $page_size = empty($biz_content['page_size']) ? 20: intval($biz_content['page_size']); // 单页容量 $start = ($page_index - 1) * $page_size; if (!empty($page_index)) $strPage = " LIMIT {$start},{$page_size} "; $SqlCommand = str_replace("[%strPage%]", $strPage, $SqlCommand); $cmd=$this->NewMasterCommand(); $replace_admin_id = $cmd->getidentifiers($admin_id, true); // 处理sql引号 $SqlCommand = str_replace('[%adminId%]', $replace_admin_id, $SqlCommand); $this->ParseSqlCommand($SqlCommand, $SqlParameters); $SqlParameters = json_decode( json_encode( $SqlParameters),true); $params = []; // 查询参数 foreach ($SqlParameters as $key => $value) { if($value['type'] === 'sql') { // 脚本处理 $result = $this->PDO_Request(/** @lang text */ $value['script']); if (!$this->pdo_isdone()) { $return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo()); return false; } if(DEBUG_MODE){ //按指定渠道查询 if($channel){ if(!strstr($result[0]['value'], $channel)){ $return->SetErrors(ERRORCODE_NOPOWERERROR, ERRORINFO_NOPOWERERROR); return false; } }else{ $value['default'] = $result[0]['value']; } } } $params[] = isset($biz_content[$value['returnfield']]) ? $biz_content[$value['returnfield']] : $value['default']; } var_dump($SqlCommand); var_dump($params);die; // 最终查询 $items = $this->PDO_Request(/** @lang text */ $SqlCommand, $params); if (!$this->pdo_isdone()) { $return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo()); return false; } if(is_array($items) && isset($items[0])) { $data['title'] = array_keys($items[0]); foreach ($items as $k => $v) { foreach ($v as $q) { $data['data'][$k][] = $q; } } }else{ $data = []; } // 5、数据返回部分 $return->biz_content = $data; return true; } catch (Exception $e) { $return->SetErrors($e->getCode(), $e->getMessage()); return false; } } /** * 处理查询 * @param RequestParameter $request * @param ReturnParameter $return * @return bool */ public function getReport($request, &$return) { /// saved max execute time count. $MaxExecuteTime = ini_get('max_execution_time'); try { // 获取具体id $biz_content = $request->biz_content; $report_id = isset($biz_content['report_id']) ? $biz_content['report_id'] : false; // 报表id $admin_id = isset($biz_content['admin_id']) ? $biz_content['admin_id'] : false; // 管理员id $channel = isset($biz_content['channel']) ? $biz_content['channel'] : false; // 指定渠道key if(!$report_id || !$admin_id){ $return->SetErrors(ERRORCODE_INPARAMERROR, ERRORINFO_INPARAMERROR); return false; } //查询用户 $admin = $this->PDO_Request(/** @lang text */' SELECT `level` FROM ct_manager_info m WHERE `login_id` = ? AND `is_enabled` = ?;', $admin_id, 1); if (!$this->pdo_isdone()) { $return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo()); return false; } if (!$admin) { $return->SetErrors(ERRORCODE_NOPOWERERROR, ERRORINFO_NOPOWERERROR); return false; } $item = $this->PDO_Request(/** @lang text */' SELECT report_id, report_name, command_line, url FROM '.$this->sql_table.' WHERE report_id = ?; ', $report_id); if (!$this->pdo_isdone()) { $return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo()); return false; } $SqlCommand = $item[0]['command_line']; # 分页信息 $page_index = empty($biz_content['page_index']) ? 1 : intval($biz_content['page_index']); // 当前页 $page_size = empty($biz_content['page_size']) ? 20: intval($biz_content['page_size']); // 单页容量 $start = ($page_index - 1) * $page_size; if (!empty($page_index)) $strPage = " LIMIT {$start},{$page_size} "; $SqlCommand = str_replace("[%strPage%]", $strPage, $SqlCommand); $cmd=$this->NewMasterCommand(); $replace_admin_id = $cmd->getidentifiers($admin_id, true); // 处理sql引号 $SqlCommand = str_replace('[%adminId%]', $replace_admin_id, $SqlCommand); $this->ParseSqlCommand($SqlCommand, $SqlParameters); $SqlParameters = json_decode( json_encode( $SqlParameters),true); $params = []; // 查询参数 foreach ($SqlParameters as $key => $value) { if($value['type'] === 'sql') { // 脚本处理 $result = $this->PDO_Request(/** @lang text */ $value['script']); if (!$this->pdo_isdone()) { $return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo()); return false; } if(DEBUG_MODE){ //按指定渠道查询 if($channel){ if(!strstr($result[0]['value'], $channel)){ $return->SetErrors(ERRORCODE_NOPOWERERROR, ERRORINFO_NOPOWERERROR); return false; } }else{ $value['default'] = $result[0]['value']; } } } $params[] = isset($biz_content[$value['returnfield']]) ? $biz_content[$value['returnfield']] : $value['default']; } /// disable timeout limit. set_time_limit(0); // 最终查询 $items = $this->PDO_Request(/** @lang text */$SqlCommand, $params); if (!$this->pdo_isdone()) { $return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo()); return false; } /// restore max execute time count. set_time_limit($MaxExecuteTime); if(is_array($items) && isset($items[0])) { $data['title'] = array_keys($items[0]); foreach ($items as $k => $v) { foreach ($v as $q) { $data['data'][$k][] = $q; } } }else{ $data = []; } // 5、数据返回部分 $return->biz_content = $data; return true; } catch (Exception $e) { $return->SetErrors($e->getCode(), $e->getMessage()); return false; } } /** * 导出报表 * @param RequestParameter $request * @param ReturnParameter $return * @return bool */ public function export_report_test($request, &$return) { // 获取具体id $biz_content = $request->biz_content; $report_id = isset($biz_content['report_id']) ? $biz_content['report_id'] : false; // 报表id $admin_id = isset($biz_content['admin_id']) ? $biz_content['admin_id'] : false; // 管理员id $channel = isset($biz_content['channel']) ? $biz_content['channel'] : false; // 指定渠道key if(!$report_id || !$admin_id){ $return->SetErrors(ERRORCODE_INPARAMERROR, ERRORINFO_INPARAMERROR); return false; } //查询用户 $admin = $this->PDO_Request(/** @lang text */' SELECT `level` FROM ct_manager_info m WHERE `login_id` = ? AND `is_enabled` = ?;', $admin_id, 1); if (!$this->pdo_isdone()) { $return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo()); return false; } if (!$admin) { $return->SetErrors(ERRORCODE_NOPOWERERROR, ERRORINFO_NOPOWERERROR); return false; } $item = $this->PDO_Request(/** @lang text */' SELECT report_id, report_name, command_line, url FROM '.$this->sql_table.' WHERE report_id = ?; ', $report_id); if (!$this->pdo_isdone()) { $return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo()); return false; } $SqlCommand = $item[0]['command_line']; $SqlCommand = str_replace("[%strPage%]", '', $SqlCommand); // 去掉分页 $cmd=$this->NewMasterCommand(); $replace_admin_id = $cmd->getidentifiers($admin_id, true); // 处理sql引号 $SqlCommand = str_replace('[%adminId%]', $replace_admin_id, $SqlCommand); $this->ParseSqlCommand($SqlCommand, $SqlParameters); $SqlParameters = json_decode( json_encode( $SqlParameters),true); $params = []; // 查询参数 foreach ($SqlParameters as $key => $value) { if($value['type'] === 'sql') { // 脚本处理 $result = $this->PDO_Request(/** @lang text */ $value['script']); if (!$this->pdo_isdone()) { $return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo()); return false; } if(DEBUG_MODE){ //按指定渠道查询 if($channel){ if(!strstr($result[0]['value'], $channel)){ $return->SetErrors(ERRORCODE_NOPOWERERROR, ERRORINFO_NOPOWERERROR); return false; } }else{ $value['default'] = $result[0]['value']; } } } $params[] = isset($biz_content[$value['returnfield']]) ? $biz_content[$value['returnfield']] : $value['default']; } // 最终查询 $items = $this->PDO_Request(/** @lang text */ $SqlCommand, $params); if (!$this->pdo_isdone()) { $return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo()); return false; } if(is_array($items) && isset($items[0])) { $title = array_keys($items[0]); }else{ $title = false; } $config = [ // 'body'=>[ // 'use_time' => '核销时间', // 'checker_name'=> '核销员', // 'discount_no' => '券号', // 'id' => '活动编号', // 'title' => '活动名称', // 'price' => '卡券面额', // 'user_name' => '兑换人', // ], 'body'=>$title, 'data'=>$items, ]; if (!$items) { return json(['code'=>'-1', 'data'=>null, 'msg'=>'数据准备失败']); } $this->exportData($config); } /** * 导出报表 * @param RequestParameter $request * @param ReturnParameter $return * @return bool */ public function exportReport($request, &$return) { // 获取具体id $biz_content = $request->biz_content; $report_id = isset($biz_content['report_id']) ? $biz_content['report_id'] : false; // 报表id $admin_id = isset($biz_content['admin_id']) ? $biz_content['admin_id'] : false; // 管理员id $channel = isset($biz_content['channel']) ? $biz_content['channel'] : false; // 指定渠道key if(!$report_id || !$admin_id){ $return->SetErrors(ERRORCODE_INPARAMERROR, ERRORINFO_INPARAMERROR); return false; } //查询用户 $admin = $this->PDO_Request(/** @lang text */' SELECT `level` FROM ct_manager_info m WHERE `login_id` = ? AND `is_enabled` = ?;', $admin_id, 1); if (!$this->pdo_isdone()) { $return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo()); return false; } if (!$admin) { $return->SetErrors(ERRORCODE_NOPOWERERROR, ERRORINFO_NOPOWERERROR); return false; } $item = $this->PDO_Request(/** @lang text */' SELECT report_id, report_name, command_line, url FROM '.$this->sql_table.' WHERE report_id = ?; ', $report_id); if (!$this->pdo_isdone()) { $return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo()); return false; } $SqlCommand = $item[0]['command_line']; $SqlCommand = str_replace("[%strPage%]", '', $SqlCommand); // 去掉分页 $cmd=$this->NewMasterCommand(); $replace_admin_id = $cmd->getidentifiers($admin_id, true); // 处理sql引号 $SqlCommand = str_replace('[%adminId%]', $replace_admin_id, $SqlCommand); $this->ParseSqlCommand($SqlCommand, $SqlParameters); $SqlParameters = json_decode( json_encode( $SqlParameters),true); $params = []; // 查询参数 foreach ($SqlParameters as $key => $value) { if($value['type'] === 'sql') { // 脚本处理 $result = $this->PDO_Request(/** @lang text */ $value['script']); if (!$this->pdo_isdone()) { $return->SetErrors($this->GetErrorCode(), $this->GetErrorInfo()); return false; } if(DEBUG_MODE){ //按指定渠道查询 if($channel){ if(!strstr($result[0]['value'], $channel)){ $return->SetErrors(ERRORCODE_NOPOWERERROR, ERRORINFO_NOPOWERERROR); return false; } }else{ $value['default'] = $result[0]['value']; } } } $params[] = isset($biz_content[$value['returnfield']]) ? $biz_content[$value['returnfield']] : $value['default']; } $DatabaseHelper = $this->GetMasterDatabase(); // $DatabaseHelper->ExportToFile('test', $SqlCommand, $params); $DatabaseHelper->ExportToBrowser($SqlCommand, $params); die; } /** * 导出报表工具方法 * @param RequestParameter $request * @param ReturnParameter $return * @return bool */ public function exportData($confg) { $name = isset($confg['name']) ? $confg['name'] : time(); // 导出文件名 $body = isset($confg['body'])&&is_array($confg['body']) ? $confg['body'] : false; // 表格结构 $data = isset($confg['data'])&&is_array($confg['data']) ? $confg['data'] : false; // 表数据 $type = isset($confg['type']) ? $confg['type'] : 'xls'; // 导出拓展名 if (!$body || !$data) { return false; } $table = ''; $table .= '
| {$v} | "; } $table .= '
| {$v[$bk]} | "; } $table .= "