Startup.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ServiceModel;
  4. using Microsoft.AspNetCore.Builder;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Http.Features;
  8. using Microsoft.AspNetCore.Rewrite;
  9. using Microsoft.AspNetCore.StaticFiles;
  10. using Microsoft.Extensions.Configuration;
  11. using Microsoft.Extensions.DependencyInjection;
  12. using Microsoft.Extensions.FileProviders;
  13. using Microsoft.Extensions.Hosting;
  14. using MySystem.PublicClass.GraphQL;
  15. using System.Text;
  16. using Microsoft.IdentityModel.Tokens;
  17. using System.Linq;
  18. namespace MySystem
  19. {
  20. public class Startup
  21. {
  22. public Startup(IConfiguration configuration)
  23. {
  24. Configuration = configuration;
  25. }
  26. public IConfiguration Configuration { get; }
  27. // This method gets called by the runtime. Use this method to add services to the container.
  28. public void ConfigureServices(IServiceCollection services)
  29. {
  30. services.AddControllersWithViews();
  31. services.AddRouting(options =>
  32. {
  33. options.LowercaseUrls = true;
  34. });
  35. services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  36. services.Configure<Setting>(Configuration.GetSection("Setting"));
  37. services.AddCors(option => option.AddPolicy("cors", policy => policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().SetIsOriginAllowed(_ => true)));
  38. services.AddMvc(options =>
  39. {
  40. options.EnableEndpointRouting = false;
  41. options.Filters.Add(typeof(GlobalExceptions));
  42. });
  43. services.AddSession(options =>
  44. {
  45. // 设置 Session 过期时间
  46. options.IdleTimeout = TimeSpan.FromHours(1);
  47. options.Cookie.HttpOnly = true;
  48. });
  49. services.AddSingleton<IRepository, Repository>();
  50. services.Configure<FormOptions>(x =>
  51. {
  52. x.MultipartBodyLengthLimit = 50 * 1024 * 1024;//不到300M
  53. });
  54. //生成密钥
  55. var symmetricKeyAsBase64 = Configuration["Setting:JwtSecret"];
  56. var keyByteArray = Encoding.ASCII.GetBytes(symmetricKeyAsBase64);
  57. var signingKey = new SymmetricSecurityKey(keyByteArray);
  58. //认证参数
  59. services.AddAuthentication("Bearer").AddJwtBearer(o =>
  60. {
  61. o.TokenValidationParameters = new TokenValidationParameters
  62. {
  63. ValidateIssuerSigningKey = true,//是否验证签名,不验证的画可以篡改数据,不安全
  64. IssuerSigningKey = signingKey,//解密的密钥
  65. ValidateIssuer = true,//是否验证发行人,就是验证载荷中的Iss是否对应ValidIssuer参数
  66. // ValidIssuer = Configuration["Setting:JwtIss"],//发行人
  67. IssuerValidator = (m, n, z) =>
  68. {
  69. return n.Issuer;
  70. },
  71. ValidateAudience = true,//是否验证订阅人,就是验证载荷中的Aud是否对应ValidAudience参数
  72. // ValidAudience = Configuration["Setting:JwtAud"],//订阅人
  73. AudienceValidator = (m, n, z) =>
  74. {
  75. string check = RedisDbconn.Instance.Get<string>("utoken:" + n.Issuer);
  76. return m != null && m.FirstOrDefault().Equals(check);
  77. },
  78. ValidateLifetime = true,//是否验证过期时间,过期了就拒绝访问
  79. ClockSkew = TimeSpan.Zero,//这个是缓冲过期时间,也就是说,即使我们配置了过期时间,这里也要考虑进去,过期时间+缓冲,默认好像是7分钟,你可以直接设置为0
  80. RequireExpirationTime = true,
  81. };
  82. });
  83. // services.AddHostedService<TradeService>();
  84. // services.AddHostedService<TeamTopService>();
  85. // services.AddHostedService<PopService>();
  86. // services.AddHostedService<SycnSpTimer>(); //同步SP数据
  87. // services.AddHostedService<TimerStatTimer>(); //实时统计创客、激活商户数
  88. // services.AddHostedService<PosTradeStatTimer>(); // 统计头天的交易额、商户型创客、激活奖励、开机奖励
  89. // services.AddHostedService<AlipayPayBack2Timer>(); //支付宝回调处理
  90. //services.AddHttpContextAccessor();
  91. string appkey = Configuration["Setting:AppKey"];
  92. string appid = Configuration["Setting:AppId"];
  93. string checkurl = Configuration["Setting:CheckUrl"];
  94. string serviceurl = Configuration["Setting:WebServiceUrl"];
  95. string schemeurl = Configuration["Setting:DbSchemeUrl"];
  96. MySystemLib.SystemPublicFuction.appkey = appkey;
  97. MySystemLib.SystemPublicFuction.appid = appid;
  98. MySystemLib.SystemPublicFuction.checkurl = checkurl;
  99. MySystemLib.SystemPublicFuction.appcheck = "success";
  100. // var binding = new BasicHttpBinding();
  101. // binding.MaxReceivedMessageSize = 2147483647;
  102. // var endpoint = new EndpointAddress(Configuration["Setting:WebServiceUrl"]);
  103. // var factory = new ChannelFactory<GetDataBaseSoap>(binding, endpoint);
  104. // var callClient = factory.CreateChannel();
  105. // DataBaseRequestBody body = new DataBaseRequestBody();
  106. // body.appid = appid;
  107. // body.appkey = appkey;
  108. // var request = new DataBaseRequest(body);
  109. // var wait = callClient.DataBaseAsync(request);
  110. // wait.Wait();
  111. // tempuri.org.CustomerTables[] servicetable = wait.Result.Body.DataBaseResult;
  112. // Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  113. // foreach (tempuri.org.CustomerTables subtable in servicetable)
  114. // {
  115. // Dictionary<string, string> Columns = new Dictionary<string, string>();
  116. // foreach (tempuri.org.CustomerColumns subcolumn in subtable.Columns)
  117. // {
  118. // Columns.Add(subcolumn.FieldName, subcolumn.FieldType);
  119. // }
  120. // tables.Add(subtable.TableName, Columns);
  121. // }
  122. // servicetable = null;
  123. // string database = Library.function.GetWebRequest(serviceurl + "?appid=" + appid + "&appkey=" + appkey);
  124. // Dictionary<string, Dictionary<string, string>> tables = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(database);
  125. // MySystemLib.SystemPublicFuction.dbtables = tables;
  126. // string schemeString = Library.function.GetWebRequest(schemeurl + "?appid=" + appid + "&appkey=" + appkey);
  127. // RedisDbconn.Instance.Set("dbscheme", Newtonsoft.Json.JsonConvert.DeserializeObject<List<MySystem.DbScheme>>(schemeString));
  128. Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  129. System.Data.DataTable tablecollection = Library.dbconn.dtable("select DISTINCT TABLE_NAME from information_schema.columns where table_schema = 'KxsMainServer'");
  130. foreach (System.Data.DataRow subtable in tablecollection.Rows)
  131. {
  132. Dictionary<string, string> Columns = new Dictionary<string, string>();
  133. System.Data.DataTable columncollection = Library.dbconn.dtable("select COLUMN_NAME,DATA_TYPE from information_schema.columns where table_schema = 'KxsMainServer' and TABLE_NAME='" + subtable["TABLE_NAME"].ToString() + "'");
  134. foreach (System.Data.DataRow column in columncollection.Rows)
  135. {
  136. string datatype = column["DATA_TYPE"].ToString();
  137. if (datatype == "decimal")
  138. {
  139. datatype = "numeric";
  140. }
  141. Columns.Add(column["COLUMN_NAME"].ToString(), datatype);
  142. }
  143. tables.Add(subtable["TABLE_NAME"].ToString(), Columns);
  144. }
  145. MySystemLib.SystemPublicFuction.dbtables = tables;
  146. RedisDbconn.csredis = new CSRedis.CSRedisClient(Configuration["Setting:RedisConnStr"]);
  147. TendisDbconn.csredis = new CSRedis.CSRedisClient(Configuration["Setting:TendisConnStr"]);
  148. }
  149. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  150. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  151. {
  152. if (env.IsDevelopment())
  153. {
  154. app.UseDeveloperExceptionPage();
  155. // app.UseExceptionHandler("/Home/Error");
  156. }
  157. else
  158. {
  159. app.UseExceptionHandler("/Home/Error");
  160. app.UseHsts();
  161. }
  162. Library.function.WritePage("/", "WebRootPath.txt", env.WebRootPath);
  163. // app.UseStatusCodePagesWithReExecute("/public/errpage/pc/{0}.html");
  164. // RequestDelegate handler = async context =>
  165. // {
  166. // var response = context.Response;
  167. // if (response.StatusCode < 500)
  168. // {
  169. // response.("/public/errpage/pc/{0}.html");
  170. // }
  171. // };
  172. // app.UseStatusCodePages(builder => builder.Run(handler));
  173. app.UseStaticFiles();
  174. // app.UseStaticFiles(new StaticFileOptions
  175. // {
  176. // FileProvider = new PhysicalFileProvider(AppContext.BaseDirectory + "/static"),
  177. // RequestPath = "/static"
  178. // });
  179. // app.UseStaticFiles(new StaticFileOptions
  180. // {
  181. // FileProvider = new PhysicalFileProvider(AppContext.BaseDirectory + "/" + Configuration["Setting:Database"]),
  182. // RequestPath = "/" + Configuration["Setting:Database"]
  183. // });
  184. app.UseStaticFiles(new StaticFileOptions
  185. {
  186. ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary<string, string>
  187. {
  188. { ".apk", "application/vnd.android.package-archive" }
  189. })
  190. });
  191. app.UseCors("cors");
  192. app.UseAuthentication();
  193. app.UseRouting();
  194. app.UseAuthorization();
  195. app.UseSession();
  196. app.UseEndpoints(endpoints =>
  197. {
  198. endpoints.MapControllerRoute(
  199. name: "default",
  200. pattern: "{controller=Home}/{action=Index}/{Id?}");
  201. });
  202. // app.UseMiddleware<GraphQLMiddleware>();
  203. // TradeStatTimer2.Instance.Start(); //交易统计
  204. //必须打开的
  205. // ActiveRewardTimer.Instance.Start(); //实时处理激活记录
  206. // TradeStatTimer.Instance.Start(); //交易统计
  207. // ChangePosTimer.Instance.Start(); //售后换新执行机具数据转移
  208. // MakeReferenceQrCodeService.Instance.StartListen(); //生成创客邀请二维码
  209. // SycnSpBindService.Instance.Start(); //同步SP绑定数据
  210. // SycnSpMerchantService.Instance.Start(); //同步SP商户数据
  211. // SycnSpActiveService.Instance.Start(); //同步SP激活数据
  212. // SycnSpTradeService.Instance.Start(); //同步SP交易数据
  213. //必须打开的
  214. // StatService.Instance.StatUserLevel();
  215. // StatService.Instance.StartEverDay("2022-05-11");
  216. ProfitHelper.Instance.StatProfit("202204"); //统计分润
  217. // StatService.Instance.ListenFluxRecord();
  218. // StatService.Instance.StartEverDay2();
  219. // TestHelper.Instance.Start();
  220. // StatService.Instance.StartEverDay();
  221. // RabbitMQClient.Instance.StartReceive("MerchantConfirmList");
  222. // RabbitMQClient.Instance.StartReceive("CheckWeChatSign");
  223. // RabbitMQClient.Instance.StartReceive("CheckAlipaySign");
  224. // RabbitMQClient.Instance.StartReceive("DeleteMySqlData");
  225. // RabbitMQClient.Instance.StartReceive("WeChatPayBack");
  226. // RabbitMQClient.Instance.StartReceive("AlipayPayBack");
  227. // RabbitMQClient.Instance.StartReceive("ConsumerOrdersReturnDo");
  228. // RabbitMQClient.Instance.StartReceive("ConsumerOrdersReturnStat");
  229. // RabbitMQClient.Instance.StartReceive("ConsumerOrdersStat");
  230. // RabbitMQClient.Instance.StartReceive("PublicMainServer");
  231. // 备用,暂时不放开的
  232. // StatStoreDataService.Instance.Start();
  233. // RabbitMQClient.Instance.StartReceive("SycnTableData");
  234. // PayHelper.Instance.Start();
  235. // OrderHelper.Instance.Start();
  236. // OrderRefundHelper.Instance.Start();
  237. // ProductCommentHelper.Instance.Start();
  238. // TimedTaskHelper.Instance.Start();
  239. // TaskFlowHelper.Instance.Start();
  240. }
  241. }
  242. }