Startup.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. MySystemLib.SystemPublicFuction.appcheck = "success";
  84. string conn = Configuration["Setting:SqlConnStr"];
  85. Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  86. System.Data.DataTable tablecollection = Library.CustomerSqlConn.dtable("select DISTINCT TABLE_NAME from information_schema.columns where table_schema = 'QrCodePlateMainServer'", conn);
  87. foreach (System.Data.DataRow subtable in tablecollection.Rows)
  88. {
  89. Dictionary<string, string> Columns = new Dictionary<string, string>();
  90. System.Data.DataTable columncollection = Library.CustomerSqlConn.dtable("select COLUMN_NAME,DATA_TYPE from information_schema.columns where table_schema = 'QrCodePlateMainServer' and TABLE_NAME='" + subtable["TABLE_NAME"].ToString() + "'", conn);
  91. foreach (System.Data.DataRow column in columncollection.Rows)
  92. {
  93. string datatype = column["DATA_TYPE"].ToString();
  94. if (datatype == "decimal")
  95. {
  96. datatype = "numeric";
  97. }
  98. Columns.Add(column["COLUMN_NAME"].ToString(), datatype);
  99. }
  100. tables.Add(subtable["TABLE_NAME"].ToString(), Columns);
  101. }
  102. MySystemLib.SystemPublicFuction.dbtables = tables;
  103. RedisDbconn.csredis = new CSRedis.CSRedisClient(Configuration["Setting:RedisConnStr"]);
  104. }
  105. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  106. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  107. {
  108. if (env.IsDevelopment())
  109. {
  110. app.UseDeveloperExceptionPage();
  111. // app.UseExceptionHandler("/Home/Error");
  112. Library.ConfigurationManager.EnvironmentFlag = 1;
  113. }
  114. else
  115. {
  116. app.UseExceptionHandler("/Home/Error");
  117. app.UseHsts();
  118. Library.ConfigurationManager.EnvironmentFlag = 2;
  119. }
  120. Library.function.WritePage("/", "WebRootPath.txt", env.WebRootPath);
  121. // app.UseStatusCodePagesWithReExecute("/public/errpage/pc/{0}.html");
  122. // RequestDelegate handler = async context =>
  123. // {
  124. // var response = context.Response;
  125. // if (response.StatusCode < 500)
  126. // {
  127. // response.("/public/errpage/pc/{0}.html");
  128. // }
  129. // };
  130. // app.UseStatusCodePages(builder => builder.Run(handler));
  131. app.UseStaticFiles();
  132. app.UseStaticFiles(new StaticFileOptions
  133. {
  134. FileProvider = new PhysicalFileProvider(AppContext.BaseDirectory + "/static"),
  135. RequestPath = "/static"
  136. });
  137. // app.UseStaticFiles(new StaticFileOptions
  138. // {
  139. // FileProvider = new PhysicalFileProvider(AppContext.BaseDirectory + "/" + Configuration["Setting:Database"]),
  140. // RequestPath = "/" + Configuration["Setting:Database"]
  141. // });
  142. app.UseStaticFiles(new StaticFileOptions
  143. {
  144. ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary<string, string>
  145. {
  146. { ".apk", "application/vnd.android.package-archive" }
  147. })
  148. });
  149. app.UseCors("cors");
  150. app.UseAuthentication();
  151. app.UseRouting();
  152. app.UseAuthorization();
  153. app.UseSession();
  154. app.UseEndpoints(endpoints =>
  155. {
  156. endpoints.MapControllerRoute(
  157. name: "default",
  158. pattern: "{controller=Home}/{action=Index}/{Id?}");
  159. });
  160. //必须打开的
  161. if(Library.ConfigurationManager.EnvironmentFlag == 1)
  162. {
  163. // MerchantStandardService.Instance.Start();
  164. // MerchantStandardService.Instance.StartThree();
  165. // MerchantStandardService.Instance.StartActive();
  166. // ProfitHelper.Instance.ReturnStat("157186", 2);
  167. // ProfitHelper.Instance.ReturnStat("157187", 2);
  168. // ProfitHelper.Instance.ReturnStat("157190", 2);
  169. // ProfitHelper.Instance.ReturnStat("157191", 2);
  170. // ProfitHelper.Instance.ReturnStat("157192", 2);
  171. // ProfitHelper.Instance.ReturnStat("157193", 2);
  172. // ProfitHelper.Instance.ReturnStat("157194", 2);
  173. // ProfitHelper.Instance.ReturnStat("157195", 2);
  174. // ProfitHelper.Instance.ReturnStat("157196", 2);
  175. // ProfitHelper.Instance.ReturnStat("157197", 2);
  176. // ProfitHelper.Instance.ReturnStat("157198", 2);
  177. }
  178. if(Library.ConfigurationManager.EnvironmentFlag == 2)
  179. {
  180. GetTencentAddressInfoService.Instance.Start(); // 获取腾讯地图地址
  181. UpdateSignUrlService.Instance.Start(); //更新签约码
  182. MerchantConfirmService.Instance.Start(); //特约商户进件队列
  183. CheckAlipaySignService.Instance.Start(); //特约商户签约队列(支付宝)
  184. CheckWeChatSignService.Instance.Start(); //特约商户签约队列(微信)
  185. AlipayPayBackService.Instance.Start(); //支付宝支付回调队列
  186. AlipayPayBackService.Instance.StartProfitShare(); //监听已支付订单,超过1分钟的订单执行分账
  187. WeChatPayBackService.Instance.Start(); //微信支付回调队列
  188. WeChatPayBackService.Instance.StartProfitShare(); //监听已支付订单,超过1分钟的订单执行分账
  189. // ProfitHelper.Instance.StartActive(); //发放达标奖励队列
  190. ProfitHelper.Instance.StartListenTrade(); //支付宝返现队列
  191. ProfitHelper.Instance.StartListenWxTrade(); //微信返现队列
  192. ProfitHelper.Instance.StartSetDivi(); //设置订单当前返现金额
  193. ProfitShareService.Instance.Start(); //分账状态监控队列,分账完成则提交返现(微信)
  194. AlipayShareService.Instance.Start(); //分账状态监控队列,分账完成则提交返现(支付宝)
  195. WeChatPayBackService.Instance.StartDivi(); //补分账
  196. AlipayPayBackService.Instance.StartDivi(); //补分账
  197. ProfitHelper.Instance.StartListenProfit(); //每月分润
  198. AlipayPayBackFeeService.Instance.Start();
  199. ActiveRewardService.Instance.StartAct();
  200. ActiveRewardService.Instance.StartOpenReward();
  201. ActiveRewardService.Instance.StartLeaderReward();
  202. ActiveRewardService.Instance.StartOperateReward();
  203. MerchantStandardService.Instance.Start(); //商户缴纳服务费次月活动交易额大于等于1W,奖励进件创客50元
  204. MerchantStandardService.Instance.StartThree(); //商户缴纳服务费次月起连续不间断三个月,每月活动交易额大于1W,奖励进件创客100元
  205. MerchantStandardService.Instance.StartActive();
  206. }
  207. // HaoDaExtHelper.Instance.StartWeChat();
  208. // HaoDaExtHelper.Instance.StartAlipay();
  209. // HaoDaExtQueryHelper.Instance.StartWeChat();
  210. // HaoDaExtQueryHelper.Instance.StartAlipay();
  211. //必须打开的
  212. }
  213. }
  214. }