69 lines
2.4 KiB
C#
69 lines
2.4 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Threading.Tasks;
|
||
using Microsoft.AspNetCore.Hosting;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.Extensions.Logging;
|
||
|
||
namespace ReportService.Controllers.api
|
||
{
|
||
[Area("api")]
|
||
[Route("api/[Controller]/[Action]")]
|
||
public class ReportApiController:Controller
|
||
{
|
||
private readonly ILogger<HomeController> _logger;
|
||
|
||
public IWebHostEnvironment Env { get; private set; }
|
||
|
||
public ReportApiController(ILogger<HomeController> logger,IWebHostEnvironment env) {
|
||
this._logger = logger;
|
||
this.Env = env;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取页面模板列表
|
||
/// </summary>
|
||
/// <returns>名称列表</returns>
|
||
public List<string> GetReportFiles() {
|
||
var basePath = Path.Combine(Env.ContentRootPath,"rp");
|
||
var files = Directory.GetFiles(basePath,"*.html");
|
||
var result = new List<string>();
|
||
foreach(var f in files) {
|
||
var file = new System.IO.FileInfo(f);
|
||
result.Add(file.Name.Substring(0,file.Name.IndexOf('.')));
|
||
}
|
||
return result;
|
||
}
|
||
/// <summary>
|
||
/// 获取模板内容显示模板页面
|
||
/// </summary>
|
||
/// <param name="fileName">模板文件名</param>
|
||
/// <returns>模板内容</returns>
|
||
public IActionResult GetHtml(string fileName) {
|
||
var basePath = Path.Combine(this.Env.ContentRootPath,"rp",fileName + ".html");
|
||
var f = new FileInfo(basePath);
|
||
using(var fr = System.IO.File.OpenRead(basePath)) {
|
||
using(var sr = new StreamReader(fr)) {
|
||
return Content(sr.ReadToEnd());
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 调用database执行sql语句,将结果封装为json对象返回
|
||
/// </summary>
|
||
/// <param name="sql">要执行的sql语句</param>
|
||
/// <returns>json对象</returns>
|
||
public object GetResult(string sql) {
|
||
var result = new List<dynamic> {
|
||
new {name="n1",age=20,sex=1 },
|
||
new {name="n2",age=30,sex=2 },
|
||
new {name="n3",age=40,sex=1 },
|
||
};
|
||
return result;
|
||
}
|
||
}
|
||
}
|