Startup.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 System.Text;
  15. using Microsoft.IdentityModel.Tokens;
  16. using System.Linq;
  17. using Microsoft.AspNetCore.Mvc.Razor;
  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.AddCors(option => option.AddPolicy("cors", policy => policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().SetIsOriginAllowed(_ => true)));
  37. services.AddMvc(options =>
  38. {
  39. options.EnableEndpointRouting = false;
  40. options.Filters.Add(typeof(GlobalExceptionsFilter));
  41. });
  42. //配置模版视图路径
  43. services.Configure<RazorViewEngineOptions>(options =>
  44. {
  45. options.ViewLocationExpanders.Add(new TemplateViewLocationExpander());
  46. });
  47. services.AddSession(options =>
  48. {
  49. // 设置 Session 过期时间
  50. options.IdleTimeout = TimeSpan.FromHours(1);
  51. options.Cookie.HttpOnly = true;
  52. });
  53. services.Configure<FormOptions>(x =>
  54. {
  55. x.MultipartBodyLengthLimit = 50 * 1024 * 1024;//不到300M
  56. });
  57. //生成密钥
  58. var symmetricKeyAsBase64 = Configuration["Setting:JwtSecret"];
  59. var keyByteArray = Encoding.ASCII.GetBytes(symmetricKeyAsBase64);
  60. var signingKey = new SymmetricSecurityKey(keyByteArray);
  61. //认证参数
  62. services.AddAuthentication("Bearer").AddJwtBearer(o =>
  63. {
  64. o.TokenValidationParameters = new TokenValidationParameters
  65. {
  66. ValidateIssuerSigningKey = true,//是否验证签名,不验证的画可以篡改数据,不安全
  67. IssuerSigningKey = signingKey,//解密的密钥
  68. ValidateIssuer = true,//是否验证发行人,就是验证载荷中的Iss是否对应ValidIssuer参数
  69. // ValidIssuer = Configuration["Setting:JwtIss"],//发行人
  70. IssuerValidator = (m, n, z) =>
  71. {
  72. return n.Issuer;
  73. },
  74. ValidateAudience = true,//是否验证订阅人,就是验证载荷中的Aud是否对应ValidAudience参数
  75. // ValidAudience = Configuration["Setting:JwtAud"],//订阅人
  76. AudienceValidator = (m, n, z) =>
  77. {
  78. string check = RedisDbconn.Instance.Get<string>("utoken:" + n.Issuer);
  79. return m != null && m.FirstOrDefault().Equals(check);
  80. },
  81. ValidateLifetime = true,//是否验证过期时间,过期了就拒绝访问
  82. ClockSkew = TimeSpan.Zero,//这个是缓冲过期时间,也就是说,即使我们配置了过期时间,这里也要考虑进去,过期时间+缓冲,默认好像是7分钟,你可以直接设置为0
  83. RequireExpirationTime = true,
  84. };
  85. });
  86. MySystemLib.SystemPublicFuction.appcheck = "success";
  87. RedisDbconn.csredis = new CSRedis.CSRedisClient(Configuration["Setting:RedisConnStr"]);
  88. }
  89. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  90. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  91. {
  92. if (env.IsDevelopment())
  93. {
  94. app.UseDeveloperExceptionPage();
  95. Library.ConfigurationManager.EnvironmentFlag = 1;
  96. }
  97. else
  98. {
  99. app.UseExceptionHandler("/Home/Error");
  100. app.UseHsts();
  101. Library.ConfigurationManager.EnvironmentFlag = 2;
  102. }
  103. Library.function.WritePage("/", "WebRootPath.txt", env.WebRootPath);
  104. app.UseStaticFiles();
  105. app.UseStaticFiles(new StaticFileOptions
  106. {
  107. FileProvider = new PhysicalFileProvider(AppContext.BaseDirectory + "/static"),
  108. RequestPath = "/static"
  109. });
  110. app.UseStaticFiles(new StaticFileOptions
  111. {
  112. ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary<string, string>
  113. {
  114. { ".apk", "application/vnd.android.package-archive" }
  115. })
  116. });
  117. app.UseCors("cors");
  118. app.UseAuthentication();
  119. app.UseRouting();
  120. app.UseAuthorization();
  121. app.UseSession();
  122. app.UseEndpoints(endpoints =>
  123. {
  124. endpoints.MapControllerRoute(
  125. name: "default",
  126. pattern: "{controller=Home}/{action=Index}/{Id?}");
  127. });
  128. initMainServer();
  129. if (Library.ConfigurationManager.EnvironmentFlag == 1)
  130. {
  131. TestHaoDaService.Instance.Start(); //好哒测试
  132. // MerchantStandardService.Instance.Start();
  133. }
  134. if (Library.ConfigurationManager.EnvironmentFlag == 2)
  135. {
  136. GetStoreNoHelper.Instance.Start(); //获取商户门店号、微信支付宝商户号
  137. MerchantConfirmService.Instance.Start(); //提交商户进件
  138. CheckWeChatSignService.Instance.Start(); //查询商户审核状态
  139. CheckWeChatBindService.Instance.Start(); //执行好哒微信绑定appid
  140. ProfitShareService.Instance.Start(); //分账状态监控队列,分账完成则提交返现
  141. // ProfitShareService.Instance.StartCheck(); //复核头一天的分账
  142. HaoDaExtHelper.Instance.StartWeChat();
  143. HaoDaExtHelper.Instance.StartAlipay();
  144. HaoDaExtQueryHelper.Instance.StartWeChat();
  145. HaoDaExtQueryHelper.Instance.StartAlipay();
  146. HaoDaExtQueryAuthHelper.Instance.StartWeChat();
  147. HaoDaExtQueryAuthHelper.Instance.StartAlipay();
  148. HaoDaAuthQueryHelper.Instance.Start(); //查询实名认证状态
  149. WeChatPayBackService.Instance.Start(); //支付回调
  150. WeChatPayBackService.Instance.StartProfitShare(); //分账队列
  151. WeChatPayBackService.Instance.StartSettleOrder(); //接收分账结算金额
  152. WeChatPayBackService.Instance.StartDivi(); //补分账
  153. ProfitHelper.Instance.StartListenTrade(); //返现队列-支付宝
  154. ProfitHelper.Instance.StartListenWxTrade(); //返现队列-微信
  155. ProfitHelper.Instance.StartSetDivi(); //设置订单当前返现金额
  156. // ProfitHelper.Instance.StartListenProfit(); //每月分润
  157. ProfitBeforeHelper.Instance.StartListenProfit(); //每月分润
  158. ProfitAfterHelper.Instance.StartListenProfit(); //每月分润
  159. ProfitCheckHelper.Instance.Start(); //检查订单是否在队列里
  160. AlipayPayBackFeeService.Instance.Start();
  161. ActiveRewardService.Instance.StartAct();
  162. ActiveRewardService.Instance.StartOpenReward();
  163. ActiveRewardService.Instance.StartLeaderReward();
  164. ActiveRewardService.Instance.StartOperateReward();
  165. MerchantStandardService.Instance.Start(); //商户缴纳服务费次月活动交易额大于等于1W,奖励进件创客50元
  166. MerchantStandardService.Instance.StartThree(); //商户缴纳服务费次月起连续不间断三个月,每月活动交易额大于1W,奖励进件创客100元
  167. MerchantStandardService.Instance.StartActive();
  168. // GetHaoDaFTPInfoService.Instance.Start(); // 获取好哒FTP交易数据
  169. GetTencentAddressInfoService.Instance.Start(); // 获取腾讯地图地址
  170. OrderPushHelper.Instance.Start(); //给商户单独推送订单信息
  171. }
  172. // GetHaoDaFTPInfoService.Instance.GetFTPDataInfoYesterday();
  173. }
  174. //初始化数据结构
  175. private void initMainServer()
  176. {
  177. Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  178. string connstr = Configuration["Setting:SqlConnStr"];
  179. System.Data.DataTable tablecollection = Library.CustomerSqlConn.dtable("select DISTINCT TABLE_NAME from information_schema.columns where table_schema = 'QrCodePlateMainServer'", connstr);
  180. foreach (System.Data.DataRow subtable in tablecollection.Rows)
  181. {
  182. Dictionary<string, string> Columns = new Dictionary<string, string>();
  183. 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() + "'", connstr);
  184. foreach (System.Data.DataRow column in columncollection.Rows)
  185. {
  186. string datatype = column["DATA_TYPE"].ToString();
  187. if (datatype == "decimal")
  188. {
  189. datatype = "numeric";
  190. }
  191. Columns.Add(column["COLUMN_NAME"].ToString(), datatype);
  192. }
  193. tables.Add(subtable["TABLE_NAME"].ToString(), Columns);
  194. }
  195. AppConfig.Base.mainTables = tables;
  196. }
  197. }
  198. }