diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs b/Yi.Framework/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs index fc4438b9..4c67064c 100644 --- a/Yi.Framework/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs +++ b/Yi.Framework/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs @@ -28,7 +28,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers /// - /// 登录方法,要返回data:{user,token} token先写123456789,不要有导航属性 + /// 登录方法,要返回data:{user,token} token /// /// /// diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/Controllers/UserController.cs b/Yi.Framework/Yi.Framework.ApiMicroservice/Controllers/UserController.cs index 7dfeda30..d56a98e3 100644 --- a/Yi.Framework/Yi.Framework.ApiMicroservice/Controllers/UserController.cs +++ b/Yi.Framework/Yi.Framework.ApiMicroservice/Controllers/UserController.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/Log4net.config b/Yi.Framework/Yi.Framework.ApiMicroservice/Log4net.config new file mode 100644 index 00000000..958c7e78 --- /dev/null +++ b/Yi.Framework/Yi.Framework.ApiMicroservice/Log4net.config @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/Program.cs b/Yi.Framework/Yi.Framework.ApiMicroservice/Program.cs index 12bd5248..cbe8c6a4 100644 --- a/Yi.Framework/Yi.Framework.ApiMicroservice/Program.cs +++ b/Yi.Framework/Yi.Framework.ApiMicroservice/Program.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Yi.Framework.WebCore.BuilderExtend; namespace Yi.Framework.ApiMicroservice { @@ -19,9 +20,25 @@ namespace Yi.Framework.ApiMicroservice public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) + + .ConfigureAppConfiguration((hostBuilderContext, configurationBuilder) => + { + configurationBuilder.AddCommandLine(args); + configurationBuilder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false); + #region + //Apollo + #endregion + //configurationBuilder.AddApolloService("Yi"); + }) + .ConfigureLogging(loggingBuilder => + { + loggingBuilder.AddFilter("System", Microsoft.Extensions.Logging.LogLevel.Warning); + loggingBuilder.AddFilter("Microsoft", Microsoft.Extensions.Logging.LogLevel.Warning); + loggingBuilder.AddLog4Net(); + }) .ConfigureWebHostDefaults(webBuilder => { - webBuilder.UseStartup(); + webBuilder.UseStartup().UseUrls("https://*:44329"); }).UseServiceProviderFactory(new AutofacServiceProviderFactory()); } } diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/Startup.cs b/Yi.Framework/Yi.Framework.ApiMicroservice/Startup.cs index 2936745f..653f3d23 100644 --- a/Yi.Framework/Yi.Framework.ApiMicroservice/Startup.cs +++ b/Yi.Framework/Yi.Framework.ApiMicroservice/Startup.cs @@ -13,13 +13,14 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Yi.Framework.ApiMicroservice.Utility; using Yi.Framework.Common.IOCOptions; using Yi.Framework.Interface; using Yi.Framework.Model; using Yi.Framework.Service; using Yi.Framework.WebCore; +using Yi.Framework.WebCore.FilterExtend; using Yi.Framework.WebCore.MiddlewareExtend; +using Yi.Framework.WebCore.Utility; namespace Yi.Framework.ApiMicroservice { @@ -43,7 +44,9 @@ namespace Yi.Framework.ApiMicroservice #region //+ #endregion - services.AddControllers(); + services.AddControllers(optios=> { + //optios.Filters.Add(typeof(CustomExceptionFilterAttribute)); + }); #region //Swagger @@ -55,6 +58,11 @@ namespace Yi.Framework.ApiMicroservice #endregion services.AddCorsService(); + #region + //JwtȨ + #endregion + services.AddJwtService(); + #region //Sqlite #endregion @@ -98,6 +106,10 @@ namespace Yi.Framework.ApiMicroservice #endregion app.UseSwaggerService(); } + #region + //ץȡע + #endregion + app.UseErrorHandlingService(); #region //HttpsRedirectionע diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/SwaggerDoc.xml b/Yi.Framework/Yi.Framework.ApiMicroservice/SwaggerDoc.xml index b83daa02..89ebe304 100644 --- a/Yi.Framework/Yi.Framework.ApiMicroservice/SwaggerDoc.xml +++ b/Yi.Framework/Yi.Framework.ApiMicroservice/SwaggerDoc.xml @@ -6,7 +6,7 @@ - 登录方法,要返回data:{user,token} token先写123456789,不要有导航属性 + 登录方法,要返回data:{user,token} token @@ -162,13 +162,5 @@ - - - 必须实现IHostingStartup接口 - 必须标记HostingStartup特性 - - 就像木马一样 - - diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/YIDB.db b/Yi.Framework/Yi.Framework.ApiMicroservice/YIDB.db index 819a767c..2d0b5178 100644 Binary files a/Yi.Framework/Yi.Framework.ApiMicroservice/YIDB.db and b/Yi.Framework/Yi.Framework.ApiMicroservice/YIDB.db differ diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/Yi.Framework.ApiMicroservice.csproj b/Yi.Framework/Yi.Framework.ApiMicroservice/Yi.Framework.ApiMicroservice.csproj index 94766da2..492c6c65 100644 --- a/Yi.Framework/Yi.Framework.ApiMicroservice/Yi.Framework.ApiMicroservice.csproj +++ b/Yi.Framework/Yi.Framework.ApiMicroservice/Yi.Framework.ApiMicroservice.csproj @@ -9,17 +9,6 @@ 1701;1702;CS1591 - - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/appsettings.json b/Yi.Framework/Yi.Framework.ApiMicroservice/appsettings.json index 49492726..933191fa 100644 --- a/Yi.Framework/Yi.Framework.ApiMicroservice/appsettings.json +++ b/Yi.Framework/Yi.Framework.ApiMicroservice/appsettings.json @@ -6,8 +6,49 @@ "Microsoft.Hosting.Lifetime": "Information" } }, + "AllowedHosts": "*", + "Apollo": { + "AppId": "Yi-ApiMicroservice", + "Env": "DEV", + "MetaServer": "http://192.168.2.168:8080", + "ConfigServer": [ "http://192.168.2.168:8080" ] + }, "SqliteConn": { "Url": "Filename=YIDB.db" }, - "AllowedHosts": "*" + "MysqlConn": { + "Url": "server=192.168.2.128;port=3306;database=ECDB;user id=root;password=123456" + }, + + "JWTTokenOptions": { + "Audience": "http://localhost:7000", + "Issuer": "http://localhost:7000", + "SecurityKey": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDI2a2EJ7m872v0afyoSDJT2o1+SitIeJSWtLJU8/Wz2m7gStexajkeD+Lka6DSTy8gt9UwfgVQo6uKjVLG5Ex7PiGOODVqAEghBuS7JzIYU5RvI543nNDAPfnJsas96mSA7L/mD7RTE2drj6hf3oZjJpMPZUQI/B1Qjb5H3K3PNwIDAQAB" + }, + "RedisConn": { + "Host": "192.168.2.128", + "Prot": 6379, + "DB": 0, + "Password": "123456" + }, + "KafkaOptions": { + "BrokerList": "192.168.3.230:9092", + "TopicName": "kafkalog" + }, + "ConsulClientOption": { + "IP": "192.168.2.128", + "Port": "8500", + "Datacenter": "dc1" + }, + "ConsulRegisterOption": { + "IP": "192.168.1.104", + "Port": "7001", + "GroupName": "ApiMicroservice", + "HealthCheckUrl": "/Health", + "Interval": 10, + "Timeout": 5, + "DeregisterCriticalServiceAfter": 60, + "Tag": "13" + }, + "IPLibraryServiceUrl": "http://gRPCIPLibraryService" } diff --git a/Yi.Framework/Yi.Framework.Common/Models/Result.cs b/Yi.Framework/Yi.Framework.Common/Models/Result.cs index 6b7f1e2f..890b01f7 100644 --- a/Yi.Framework/Yi.Framework.Common/Models/Result.cs +++ b/Yi.Framework/Yi.Framework.Common/Models/Result.cs @@ -26,6 +26,11 @@ namespace Yi.Framework.Common.Models { return new Result() { status = true, code = 200, msg = msg }; } + public static Result UnAuthorize(string msg = "unAuthorize") + { + return new Result() { status = false, code = 401, msg = msg }; + } + public Result SetData(object obj) { this.data = obj; @@ -56,6 +61,12 @@ namespace Yi.Framework.Common.Models { return new Result { status = true, code = 200, msg = msg }; } + + public static Result UnAuthorize(string msg = "unAuthorize") + { + return new Result{ status = false, code = 401, msg = msg }; + } + public Result SetData(T TValue) { this.data = TValue; diff --git a/Yi.Framework/Yi.Framework.Model/Yi.Framework.Model.csproj b/Yi.Framework/Yi.Framework.Model/Yi.Framework.Model.csproj index 92106451..d73d9b8a 100644 --- a/Yi.Framework/Yi.Framework.Model/Yi.Framework.Model.csproj +++ b/Yi.Framework/Yi.Framework.Model/Yi.Framework.Model.csproj @@ -10,6 +10,13 @@ + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/Yi.Framework/Yi.Framework.WebCore/BuilderExtend/ApolloExtension.cs b/Yi.Framework/Yi.Framework.WebCore/BuilderExtend/ApolloExtension.cs new file mode 100644 index 00000000..5055c222 --- /dev/null +++ b/Yi.Framework/Yi.Framework.WebCore/BuilderExtend/ApolloExtension.cs @@ -0,0 +1,63 @@ +using Com.Ctrip.Framework.Apollo; +using Microsoft.Extensions.Configuration; +using System; +using System.Collections.Generic; +using System.Linq; +using Com.Ctrip.Framework.Apollo.Enums; +using Com.Ctrip.Framework.Apollo.Logging; +using Microsoft.Extensions.Primitives; +using System.Reflection; + +namespace Yi.Framework.WebCore.BuilderExtend +{ + public static class ApolloExtension + { + /// + /// 接入Apollo + /// + /// + /// apollo配置文件路径 如果写入appsettings.json中 则jsonPath传null即可 + public static void AddApolloService(this IConfigurationBuilder builder, params string[] NameSpace) + { + //阿波罗的日志级别调整 + LogManager.UseConsoleLogging(LogLevel.Warn); + var root = builder.Build(); + var apolloBuilder = builder.AddApollo(root.GetSection("apollo")).AddDefault(); + + + + foreach (var item in NameSpace) + { + apolloBuilder.AddNamespace(item, ConfigFileFormat.Json); + } + //监听apollo配置 + Monitor(builder.Build()); + + + } + #region private + /// + /// 监听配置 + /// + private static void Monitor(IConfigurationRoot root) + { + //TODO 需要根据改变执行特定的操作 如 mq redis 等其他跟配置相关的中间件 + //TODO 初步思路:将需要执行特定的操作key和value放入内存字典中,在赋值操作时通过标准事件来执行特定的操作。 + + //要重新Build 此时才将Apollo provider加入到ConfigurationBuilder中 + ChangeToken.OnChange(() => root.GetReloadToken(), () => + { + foreach (var apolloProvider in root.Providers.Where(p => p is ApolloConfigurationProvider)) + { + var property = apolloProvider.GetType().BaseType.GetProperty("Data", BindingFlags.Instance | BindingFlags.NonPublic); + var data = property.GetValue(apolloProvider) as IDictionary; + foreach (var item in data) + { + Console.WriteLine($"key {item.Key} value {item.Value}"); + } + } + }); + } + #endregion + } +} \ No newline at end of file diff --git a/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/ErrorHandExtension.cs b/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/ErrorHandExtension.cs index 20ca07c2..eed13c4c 100644 --- a/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/ErrorHandExtension.cs +++ b/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/ErrorHandExtension.cs @@ -43,12 +43,10 @@ namespace Yi.Framework.WebCore.MiddlewareExtend switch (statusCode) { - case 401: msg = "未授权";break; case 403: msg = "未授权"; break; case 404: msg = "未找到服务"; break; case 502: msg = "请求错误"; break; - } if (!string.IsNullOrWhiteSpace(msg)) { @@ -59,7 +57,16 @@ namespace Yi.Framework.WebCore.MiddlewareExtend //异常错误信息捕获,将错误信息用Json方式返回 private static Task HandleExceptionAsync(HttpContext context, int statusCode, string msg) { - var result = JsonConvert.SerializeObject( Result.Error(msg).SetCode(statusCode)); + Result resp; + if (statusCode == 401) + { + resp = Result.UnAuthorize(msg); + } + else + { + resp = Result.Error(msg); + } + var result = JsonConvert.SerializeObject(resp); context.Response.ContentType = "application/json;charset=utf-8"; return context.Response.WriteAsync(result); } @@ -67,7 +74,7 @@ namespace Yi.Framework.WebCore.MiddlewareExtend //扩展方法 public static class ErrorHandlingExtensions { - public static IApplicationBuilder UseErrorHandling(this IApplicationBuilder builder) + public static IApplicationBuilder UseErrorHandlingService(this IApplicationBuilder builder) { return builder.UseMiddleware(); } diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/Utility/CustomAutofacAop.cs b/Yi.Framework/Yi.Framework.WebCore/Utility/CustomAutofacAop.cs similarity index 92% rename from Yi.Framework/Yi.Framework.ApiMicroservice/Utility/CustomAutofacAop.cs rename to Yi.Framework/Yi.Framework.WebCore/Utility/CustomAutofacAop.cs index 99fd8cc4..85b0c251 100644 --- a/Yi.Framework/Yi.Framework.ApiMicroservice/Utility/CustomAutofacAop.cs +++ b/Yi.Framework/Yi.Framework.WebCore/Utility/CustomAutofacAop.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -namespace Yi.Framework.ApiMicroservice.Utility +namespace Yi.Framework.WebCore.Utility { public class CustomAutofacAop : IInterceptor { diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/Utility/CustomAutofacModule.cs b/Yi.Framework/Yi.Framework.WebCore/Utility/CustomAutofacModule.cs similarity index 91% rename from Yi.Framework/Yi.Framework.ApiMicroservice/Utility/CustomAutofacModule.cs rename to Yi.Framework/Yi.Framework.WebCore/Utility/CustomAutofacModule.cs index 502f06a0..c989edba 100644 --- a/Yi.Framework/Yi.Framework.ApiMicroservice/Utility/CustomAutofacModule.cs +++ b/Yi.Framework/Yi.Framework.WebCore/Utility/CustomAutofacModule.cs @@ -10,13 +10,10 @@ using System.IO; using System.Linq; using System.Reflection; using System.Threading.Tasks; -using Yi.Framework.ApiMicroservice.Utility; -using Yi.Framework.Interface; -using Yi.Framework.Model; -using Yi.Framework.Service; +using Yi.Framework.WebCore.Utility; using Module = Autofac.Module; -namespace Yi.Framework.ApiMicroservice.Utility +namespace Yi.Framework.WebCore.Utility { public class CustomAutofacModule : Module { diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/Utility/CustomHostingStartup.cs b/Yi.Framework/Yi.Framework.WebCore/Utility/CustomHostingStartup.cs similarity index 92% rename from Yi.Framework/Yi.Framework.ApiMicroservice/Utility/CustomHostingStartup.cs rename to Yi.Framework/Yi.Framework.WebCore/Utility/CustomHostingStartup.cs index fa8fa924..58fcabb7 100644 --- a/Yi.Framework/Yi.Framework.ApiMicroservice/Utility/CustomHostingStartup.cs +++ b/Yi.Framework/Yi.Framework.WebCore/Utility/CustomHostingStartup.cs @@ -7,8 +7,8 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -[assembly: HostingStartup(typeof(Yi.Framework.ApiMicroservice.Utility.CustomHostingStartup))] -namespace Yi.Framework.ApiMicroservice.Utility +[assembly: HostingStartup(typeof(Yi.Framework.WebCore.Utility.CustomHostingStartup))] +namespace Yi.Framework.WebCore.Utility { /// /// 必须实现IHostingStartup接口 diff --git a/Yi.Framework/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj b/Yi.Framework/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj index 7ec33ca6..ce702729 100644 --- a/Yi.Framework/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj +++ b/Yi.Framework/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj @@ -5,11 +5,16 @@ + + + + + diff --git a/Yi.Vue/src/layouts/default/AppBar.vue b/Yi.Vue/src/layouts/default/AppBar.vue index 26aa754c..ab818026 100644 --- a/Yi.Vue/src/layouts/default/AppBar.vue +++ b/Yi.Vue/src/layouts/default/AppBar.vue @@ -25,6 +25,7 @@ + diff --git a/Yi.Vue/src/layouts/default/View.vue b/Yi.Vue/src/layouts/default/View.vue index bd3f2c0c..dfc65be7 100644 --- a/Yi.Vue/src/layouts/default/View.vue +++ b/Yi.Vue/src/layouts/default/View.vue @@ -1,6 +1,13 @@ + diff --git a/Yi.Vue/src/main.js b/Yi.Vue/src/main.js index ea413e95..4443c42a 100644 --- a/Yi.Vue/src/main.js +++ b/Yi.Vue/src/main.js @@ -5,6 +5,7 @@ import vuetify from './plugins/vuetify' import VuetifyDialog from 'vuetify-dialog' import 'vuetify-dialog/dist/vuetify-dialog.css' import './plugins' +import "./permission" import store from './store/index' Vue.config.productionTip = false @@ -13,9 +14,11 @@ Vue.use(VuetifyDialog, { vuetify } }); -new Vue({ +let vm = new Vue({ router, store, vuetify, render: function(h) { return h(App) } -}).$mount('#app') \ No newline at end of file +}).$mount('#app') + +export default vm; \ No newline at end of file diff --git a/Yi.Vue/src/permission.js b/Yi.Vue/src/permission.js new file mode 100644 index 00000000..df11defa --- /dev/null +++ b/Yi.Vue/src/permission.js @@ -0,0 +1,19 @@ +import router from './router/index' +import store from './store/index' +// import accountApi from '@/api/accountApi' + + +router.beforeEach((to, from, next) => { + const user = store.state.user.user; //获取是有user + console.log(user) + if (!user) { //如果没有登入 + if (to.path == '/login/' || to.path == '/register/' || to.path == '/reset_password/' || to.path == '/qq/') { + next(); + } else { + next({ path: '/login/' }); + } + } else { + next(); + } + +}) \ No newline at end of file diff --git a/Yi.Vue/src/util/myaxios.js b/Yi.Vue/src/util/myaxios.js index 26928d07..6e3751a6 100644 --- a/Yi.Vue/src/util/myaxios.js +++ b/Yi.Vue/src/util/myaxios.js @@ -1,5 +1,6 @@ import axios from 'axios' import store from '../store/index' +import vm from '../main' // import VuetifyDialogPlugin from 'vuetify-dialog/nuxt/index'; const myaxios = axios.create({ // baseURL:'/'// @@ -12,25 +13,44 @@ const myaxios = axios.create({ // 请求拦截器 myaxios.interceptors.request.use(function(config) { config.headers.Authorization = 'Bearer ' + store.state.user.token; - // store.dispatch("openLoad"); + store.dispatch("openLoad"); return config; }, function(error) { return Promise.reject(error); }); // 响应拦截器 -myaxios.interceptors.response.use(function(response) { +myaxios.interceptors.response.use(async function(response) { const resp = response.data - // store.dispatch("closeLoad"); + if (resp.code == undefined && resp.msg == undefined) { + vm.$dialog.notify.error("错误代码:无,原因:与服务器失去连接", { + position: "top-right", + timeout: 5000, + }); + } else if (resp.code == 401) { + const res = await vm.$dialog.error({ + text: `错误代码:${resp.code},原因:${resp.msg}是否重新进行登录?`, + title: '错误', + actions: { + 'false': '取消', + 'true': '跳转' + } + }); + if (res) { + vm.$router.push({ path: "/login" }); + } + + } else if (resp.code !== 200) { + vm.$dialog.notify.error(`错误代码:${resp.code},原因:${resp.msg}`, { + position: "top-right", + timeout: 5000, + }); + } + + store.dispatch("closeLoad"); return resp; }, function(error) { - const resp = error.response.data - // if (resp.code == undefined && resp.msg == undefined) { - // alert(`错误代码:无,原因:与服务器失去连接`) - // } else if (resp.code != 200) { - // alert(`错误代码:${resp.code},原因:${resp.msg}`) - // } - // store.dispatch("closeLoad"); + store.dispatch("closeLoad"); return Promise.reject(error); }); export default myaxios \ No newline at end of file diff --git a/Yi.Vue/src/util/routes.js b/Yi.Vue/src/util/routes.js index 00a2e3a1..eecc16bd 100644 --- a/Yi.Vue/src/util/routes.js +++ b/Yi.Vue/src/util/routes.js @@ -55,8 +55,7 @@ export function redirect( export function route(name, component, path = '') { component = Object(component) === component ? - component : - { default: name.replace(' ', '') } + component : { default: name.replace(' ', '') } const components = {} diff --git a/Yi.Vue/src/views/Index.vue b/Yi.Vue/src/views/Index.vue index 8f8d5206..cadecbbd 100644 --- a/Yi.Vue/src/views/Index.vue +++ b/Yi.Vue/src/views/Index.vue @@ -1,14 +1,25 @@ - - - - - - - - 还有什么呢? - - - - - \ No newline at end of file + + + + + + 还有什么呢? + + + + \ No newline at end of file