From ef55b5a77b29a2123953f87b7c89d3fe6ffc04a5 Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Wed, 27 May 2020 15:37:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=89=93=E5=8D=B0=E3=80=82?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E6=9C=8D=E5=8A=A1=E6=97=B6=E5=80=99=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0url=E5=8F=82=E6=95=B0uid=E8=A1=A8=E7=A4=BA=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E7=94=A8=E6=88=B7=E7=BC=96=E5=8F=B7=EF=BC=8Cun?= =?UTF-8?q?=E4=BB=A3=E8=A1=A8=E7=94=A8=E6=88=B7=E5=90=8D=EF=BC=8CreportNam?= =?UTF-8?q?e=E4=BB=A3=E8=A1=A8=E8=A6=81=E5=B1=95=E7=A4=BA=E7=9A=84?= =?UTF-8?q?=E6=8A=A5=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ReportService/Controllers/HomeController.cs | 8 +- .../Controllers/api/ReportApiController.cs | 41 ++++-- ReportService/ReportService.csproj | 3 + ReportService/Views/Home/Index.cshtml | 4 + ReportService/Views/Shared/_Layout.cshtml | 12 +- ReportService/wwwroot/js/site.js | 24 +++- .../lib/jquery.cookie/jquery.cookie.js | 117 ++++++++++++++++++ .../lib/jquery.cookie/jquery.cookie.min.js | 2 + .../{rp => wwwroot/report}/报表1.html | 25 +++- ReportService/wwwroot/report/报表1.rpt.html | 64 ++++++++++ .../{rp => wwwroot/report}/报表2.html | 0 11 files changed, 285 insertions(+), 15 deletions(-) create mode 100644 ReportService/wwwroot/lib/jquery.cookie/jquery.cookie.js create mode 100644 ReportService/wwwroot/lib/jquery.cookie/jquery.cookie.min.js rename ReportService/{rp => wwwroot/report}/报表1.html (54%) create mode 100644 ReportService/wwwroot/report/报表1.rpt.html rename ReportService/{rp => wwwroot/report}/报表2.html (100%) diff --git a/ReportService/Controllers/HomeController.cs b/ReportService/Controllers/HomeController.cs index 87df8ff..7cd1a85 100644 --- a/ReportService/Controllers/HomeController.cs +++ b/ReportService/Controllers/HomeController.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging; using ReportService.Models; using System.IO; using Microsoft.AspNetCore.Hosting; +using System.Text.Json.Serialization; namespace ReportService.Controllers { @@ -20,7 +21,12 @@ namespace ReportService.Controllers _logger = logger; } - public IActionResult Index() { + public IActionResult Index(string uid,string un,string reportName) { + uid ??= ""; + un ??= ""; + reportName ??= ""; + Response.Cookies.Append("_userCK",$"{{\"uid\":\"{uid}\",\"un\":\"{un}\"}}"); + Response.Cookies.Append("_initReport",reportName); return View(); } diff --git a/ReportService/Controllers/api/ReportApiController.cs b/ReportService/Controllers/api/ReportApiController.cs index 088f0f3..e6c305b 100644 --- a/ReportService/Controllers/api/ReportApiController.cs +++ b/ReportService/Controllers/api/ReportApiController.cs @@ -1,12 +1,8 @@ -using System; -using System.Collections.Generic; +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; -using Microsoft.Net.Http.Headers; using ReportService.Database; namespace ReportService.Controllers.api @@ -20,6 +16,15 @@ namespace ReportService.Controllers.api public IWebHostEnvironment Env { get; private set; } public RSDbContext Db { get; set; } + /// + /// 报表存放位置 + /// + public string ReportPath { + get { + return Path.Combine(this.Env.ContentRootPath,"wwwroot/report"); + } + } + public ReportApiController(ILogger logger,IWebHostEnvironment env,RSDbContext db) { this._logger = logger; this.Env = env; @@ -31,11 +36,13 @@ namespace ReportService.Controllers.api /// /// 名称列表 public List GetReportFiles() { - var basePath = Path.Combine(Env.ContentRootPath,"rp"); - var files = Directory.GetFiles(basePath,"*.html"); + var files = Directory.GetFiles(this.ReportPath,"*.html"); var result = new List(); foreach(var f in files) { var file = new System.IO.FileInfo(f); + if(file.Name.ToLower().EndsWith(".rpt.html")) { + continue; + } result.Add(file.Name.Substring(0,file.Name.IndexOf('.'))); } return result; @@ -46,15 +53,28 @@ namespace ReportService.Controllers.api /// 模板文件名 /// 模板内容 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)) { + var htmlFile = Path.Combine(this.ReportPath,fileName + ".html"); + using(var fr = System.IO.File.OpenRead(htmlFile)) { using(var sr = new StreamReader(fr)) { return Content(sr.ReadToEnd()); } } } + /// + /// 获取模板内容显示模板页面 + /// + /// 模板文件名 + /// 模板内容 + public IActionResult GetPrint(string fileName) { + var htmlFile = Path.Combine(this.ReportPath,fileName + ".rpt.html"); + using(var fr = System.IO.File.OpenRead(htmlFile)) { + using(var sr = new StreamReader(fr)) { + return Content(sr.ReadToEnd(),"text/html; charset=utf-8"); + } + } + } + /// /// 调用database执行sql语句,将结果封装为json对象返回 /// @@ -64,5 +84,6 @@ namespace ReportService.Controllers.api var result = this.Db.SqlJsonQuery(sql); return Content(result,"application/json; charset=utf-8"); } + } } diff --git a/ReportService/ReportService.csproj b/ReportService/ReportService.csproj index 5bfbc7d..5105598 100644 --- a/ReportService/ReportService.csproj +++ b/ReportService/ReportService.csproj @@ -7,6 +7,9 @@ + + + diff --git a/ReportService/Views/Home/Index.cshtml b/ReportService/Views/Home/Index.cshtml index c43ad96..5bc2d96 100644 --- a/ReportService/Views/Home/Index.cshtml +++ b/ReportService/Views/Home/Index.cshtml @@ -23,6 +23,10 @@ created() { $.get(this.url, function (d) { vm.reportList = d; + var initReport = $.cookie("_initReport"); + if (vm.reportList.indexOf(initReport) >= 0) { + vm.cli(initReport); + } }); }, methods: { diff --git a/ReportService/Views/Shared/_Layout.cshtml b/ReportService/Views/Shared/_Layout.cshtml index d7adc6b..eba27a9 100644 --- a/ReportService/Views/Shared/_Layout.cshtml +++ b/ReportService/Views/Shared/_Layout.cshtml @@ -8,6 +8,7 @@ + @@ -38,9 +39,18 @@
- © 2020 - ReportService - Privacy +
+ @RenderSection("Scripts",required: false) diff --git a/ReportService/wwwroot/js/site.js b/ReportService/wwwroot/js/site.js index 6f54bf5..b99ae78 100644 --- a/ReportService/wwwroot/js/site.js +++ b/ReportService/wwwroot/js/site.js @@ -3,7 +3,8 @@ var myjs = { urls: { files: "/api/ReportApi/getreportfiles", fileContentUrl: "/api/ReportApi/GetHtml", - getresult: "api/ReportApi/GetResult", + reportUrl: "/api/ReportApi/GetPrint", + getresult: "/api/ReportApi/GetResult", }, //定义从对象获取请求参数的方法 getParaStr(data) { @@ -24,6 +25,8 @@ var myjs = { var v = e.val(); if (n == "ProcedureName") { pn = v; + } else if (n == "reportName" || n == "sqlStr") { + } else { paras = paras + " @" + n + "='" + v + "',"; } @@ -56,4 +59,23 @@ var myjs = { console.log(msg); alert(msg); }, + getUId() { + return JSON.parse($.cookie("_userCK")).uid; + }, + getUName() { + return JSON.parse($.cookie("_userCK")).un; + }, + print() { + var pName = $("input[name='reportName']").val(); + var sqlStr = $("input[name='sqlStr']").val(); + $.cookie("_reportName", pName); + $.cookie("_SqlString", sqlStr); + window.open(this.urls.reportUrl + "?fileName=" + pName); + }, + getReportName() { + return $.cookie("_reportName"); + }, + getSqlStr() { + return $.cookie("_SqlString"); + } }; \ No newline at end of file diff --git a/ReportService/wwwroot/lib/jquery.cookie/jquery.cookie.js b/ReportService/wwwroot/lib/jquery.cookie/jquery.cookie.js new file mode 100644 index 0000000..c7f3a59 --- /dev/null +++ b/ReportService/wwwroot/lib/jquery.cookie/jquery.cookie.js @@ -0,0 +1,117 @@ +/*! + * jQuery Cookie Plugin v1.4.1 + * https://github.com/carhartl/jquery-cookie + * + * Copyright 2013 Klaus Hartl + * Released under the MIT license + */ +(function (factory) { + if (typeof define === 'function' && define.amd) { + // AMD + define(['jquery'], factory); + } else if (typeof exports === 'object') { + // CommonJS + factory(require('jquery')); + } else { + // Browser globals + factory(jQuery); + } +}(function ($) { + + var pluses = /\+/g; + + function encode(s) { + return config.raw ? s : encodeURIComponent(s); + } + + function decode(s) { + return config.raw ? s : decodeURIComponent(s); + } + + function stringifyCookieValue(value) { + return encode(config.json ? JSON.stringify(value) : String(value)); + } + + function parseCookieValue(s) { + if (s.indexOf('"') === 0) { + // This is a quoted cookie as according to RFC2068, unescape... + s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\'); + } + + try { + // Replace server-side written pluses with spaces. + // If we can't decode the cookie, ignore it, it's unusable. + // If we can't parse the cookie, ignore it, it's unusable. + s = decodeURIComponent(s.replace(pluses, ' ')); + return config.json ? JSON.parse(s) : s; + } catch(e) {} + } + + function read(s, converter) { + var value = config.raw ? s : parseCookieValue(s); + return $.isFunction(converter) ? converter(value) : value; + } + + var config = $.cookie = function (key, value, options) { + + // Write + + if (value !== undefined && !$.isFunction(value)) { + options = $.extend({}, config.defaults, options); + + if (typeof options.expires === 'number') { + var days = options.expires, t = options.expires = new Date(); + t.setTime(+t + days * 864e+5); + } + + return (document.cookie = [ + encode(key), '=', stringifyCookieValue(value), + options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE + options.path ? '; path=' + options.path : '', + options.domain ? '; domain=' + options.domain : '', + options.secure ? '; secure' : '' + ].join('')); + } + + // Read + + var result = key ? undefined : {}; + + // To prevent the for loop in the first place assign an empty array + // in case there are no cookies at all. Also prevents odd result when + // calling $.cookie(). + var cookies = document.cookie ? document.cookie.split('; ') : []; + + for (var i = 0, l = cookies.length; i < l; i++) { + var parts = cookies[i].split('='); + var name = decode(parts.shift()); + var cookie = parts.join('='); + + if (key && key === name) { + // If second argument (value) is a function it's a converter... + result = read(cookie, value); + break; + } + + // Prevent storing a cookie that we couldn't decode. + if (!key && (cookie = read(cookie)) !== undefined) { + result[name] = cookie; + } + } + + return result; + }; + + config.defaults = {}; + + $.removeCookie = function (key, options) { + if ($.cookie(key) === undefined) { + return false; + } + + // Must not alter options, thus extending a fresh object... + $.cookie(key, '', $.extend({}, options, { expires: -1 })); + return !$.cookie(key); + }; + +})); diff --git a/ReportService/wwwroot/lib/jquery.cookie/jquery.cookie.min.js b/ReportService/wwwroot/lib/jquery.cookie/jquery.cookie.min.js new file mode 100644 index 0000000..c0f19d8 --- /dev/null +++ b/ReportService/wwwroot/lib/jquery.cookie/jquery.cookie.min.js @@ -0,0 +1,2 @@ +/*! jquery.cookie v1.4.1 | MIT */ +!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):"object"==typeof exports?a(require("jquery")):a(jQuery)}(function(a){function b(a){return h.raw?a:encodeURIComponent(a)}function c(a){return h.raw?a:decodeURIComponent(a)}function d(a){return b(h.json?JSON.stringify(a):String(a))}function e(a){0===a.indexOf('"')&&(a=a.slice(1,-1).replace(/\\"/g,'"').replace(/\\\\/g,"\\"));try{return a=decodeURIComponent(a.replace(g," ")),h.json?JSON.parse(a):a}catch(b){}}function f(b,c){var d=h.raw?b:e(b);return a.isFunction(c)?c(d):d}var g=/\+/g,h=a.cookie=function(e,g,i){if(void 0!==g&&!a.isFunction(g)){if(i=a.extend({},h.defaults,i),"number"==typeof i.expires){var j=i.expires,k=i.expires=new Date;k.setTime(+k+864e5*j)}return document.cookie=[b(e),"=",d(g),i.expires?"; expires="+i.expires.toUTCString():"",i.path?"; path="+i.path:"",i.domain?"; domain="+i.domain:"",i.secure?"; secure":""].join("")}for(var l=e?void 0:{},m=document.cookie?document.cookie.split("; "):[],n=0,o=m.length;o>n;n++){var p=m[n].split("="),q=c(p.shift()),r=p.join("=");if(e&&e===q){l=f(r,g);break}e||void 0===(r=f(r))||(l[q]=r)}return l};h.defaults={},a.removeCookie=function(b,c){return void 0===a.cookie(b)?!1:(a.cookie(b,"",a.extend({},c,{expires:-1})),!a.cookie(b))}}); \ No newline at end of file diff --git a/ReportService/rp/报表1.html b/ReportService/wwwroot/report/报表1.html similarity index 54% rename from ReportService/rp/报表1.html rename to ReportService/wwwroot/report/报表1.html index b22b1f5..91a5bc5 100644 --- a/ReportService/rp/报表1.html +++ b/ReportService/wwwroot/report/报表1.html @@ -1,9 +1,16 @@ 
+ + + + + - + + +
@@ -12,6 +19,8 @@ + + @@ -19,6 +28,8 @@ + +
姓名 年龄 性别登录用户编号登录用户名
{{i.name}} {{i.age}} {{i.sex}}{{i.uid}}{{i.Uname}}
@@ -31,17 +42,27 @@ data: { url: myjs.urls.getresult, result: [], + uid: "", + uName: "", }, created() { + this.uid = myjs.getUId(); + this.uName = myjs.getUName(); }, methods: { find() { - var sql =myjs.createSql("#para"); + var sql = myjs.createSql("#para"); + $("input[name='sqlStr']").val(sql); console.log(sql); myjs.post(this.url, { sql: sql }, function (d) { findPerson.result = d; }); }, + print() { + var sql = myjs.createSql("#para"); + $("input[name='sqlStr']").val(sql); + myjs.print(); + }, } }); diff --git a/ReportService/wwwroot/report/报表1.rpt.html b/ReportService/wwwroot/report/报表1.rpt.html new file mode 100644 index 0000000..e71eb0e --- /dev/null +++ b/ReportService/wwwroot/report/报表1.rpt.html @@ -0,0 +1,64 @@ + + + + 报表1 + + + + + + + + + + + +
+
+ + + + + + + + + + + + + + + + + + + +
姓名年龄性别登录用户编号登录用户名
{{i.name}}{{i.age}}{{i.sex}}{{i.uid}}{{i.Uname}}
+
+
+ + + + diff --git a/ReportService/rp/报表2.html b/ReportService/wwwroot/report/报表2.html similarity index 100% rename from ReportService/rp/报表2.html rename to ReportService/wwwroot/report/报表2.html