Startup.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. string appkey = Configuration["Setting:AppKey"];
  84. string appid = Configuration["Setting:AppId"];
  85. string checkurl = Configuration["Setting:CheckUrl"];
  86. string serviceurl = Configuration["Setting:WebServiceUrl"];
  87. string schemeurl = Configuration["Setting:DbSchemeUrl"];
  88. MySystemLib.SystemPublicFuction.appkey = appkey;
  89. MySystemLib.SystemPublicFuction.appid = appid;
  90. MySystemLib.SystemPublicFuction.checkurl = checkurl;
  91. MySystemLib.SystemPublicFuction.appcheck = "success";
  92. string conn = Configuration["Setting:SqlConnStr"];
  93. Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  94. System.Data.DataTable tablecollection = Library.CustomerSqlConn.dtable("select DISTINCT TABLE_NAME from information_schema.columns where table_schema = 'KxsProfitServer'", conn);
  95. foreach (System.Data.DataRow subtable in tablecollection.Rows)
  96. {
  97. Dictionary<string, string> Columns = new Dictionary<string, string>();
  98. System.Data.DataTable columncollection = Library.CustomerSqlConn.dtable("select COLUMN_NAME,DATA_TYPE from information_schema.columns where table_schema = 'KxsProfitServer' and TABLE_NAME='" + subtable["TABLE_NAME"].ToString() + "'", conn);
  99. foreach (System.Data.DataRow column in columncollection.Rows)
  100. {
  101. string datatype = column["DATA_TYPE"].ToString();
  102. if (datatype == "decimal")
  103. {
  104. datatype = "numeric";
  105. }
  106. Columns.Add(column["COLUMN_NAME"].ToString(), datatype);
  107. }
  108. tables.Add(subtable["TABLE_NAME"].ToString(), Columns);
  109. }
  110. MySystemLib.SystemPublicFuction.dbtables = tables;
  111. RedisDbconn.csredis = new CSRedis.CSRedisClient(Configuration["Setting:RedisConnStr"]);
  112. }
  113. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  114. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  115. {
  116. if (env.IsDevelopment())
  117. {
  118. app.UseDeveloperExceptionPage();
  119. // app.UseExceptionHandler("/Home/Error");
  120. Library.ConfigurationManager.EnvironmentFlag = 1;
  121. }
  122. else
  123. {
  124. app.UseExceptionHandler("/Home/Error");
  125. app.UseHsts();
  126. Library.ConfigurationManager.EnvironmentFlag = 2;
  127. }
  128. // Library.ConfigurationManager.EnvironmentFlag = 1;
  129. Library.function.WritePage("/", "WebRootPath.txt", env.WebRootPath);
  130. // app.UseStatusCodePagesWithReExecute("/public/errpage/pc/{0}.html");
  131. // RequestDelegate handler = async context =>
  132. // {
  133. // var response = context.Response;
  134. // if (response.StatusCode < 500)
  135. // {
  136. // response.("/public/errpage/pc/{0}.html");
  137. // }
  138. // };
  139. // app.UseStatusCodePages(builder => builder.Run(handler));
  140. app.UseStaticFiles();
  141. // app.UseStaticFiles(new StaticFileOptions
  142. // {
  143. // FileProvider = new PhysicalFileProvider(AppContext.BaseDirectory + "/static"),
  144. // RequestPath = "/static"
  145. // });
  146. // app.UseStaticFiles(new StaticFileOptions
  147. // {
  148. // FileProvider = new PhysicalFileProvider(AppContext.BaseDirectory + "/" + Configuration["Setting:Database"]),
  149. // RequestPath = "/" + Configuration["Setting:Database"]
  150. // });
  151. app.UseStaticFiles(new StaticFileOptions
  152. {
  153. ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary<string, string>
  154. {
  155. { ".apk", "application/vnd.android.package-archive" }
  156. })
  157. });
  158. app.UseCors("cors");
  159. app.UseAuthentication();
  160. app.UseRouting();
  161. app.UseAuthorization();
  162. app.UseSession();
  163. app.UseEndpoints(endpoints =>
  164. {
  165. endpoints.MapControllerRoute(
  166. name: "default",
  167. pattern: "{controller=Home}/{action=Index}/{Id?}");
  168. });
  169. //必须打开的
  170. if(Library.ConfigurationManager.EnvironmentFlag == 1)
  171. {
  172. }
  173. if(Library.ConfigurationManager.EnvironmentFlag == 2)
  174. {
  175. SycnSpBindService.Instance.Start(); //同步SP绑定数据
  176. SycnSpMerchantService.Instance.Start(); //同步SP商户数据
  177. SycnSpActiveService.Instance.Start(); //同步SP激活数据
  178. SycnSpTradeService.Instance.Start(); //同步SP交易数据
  179. SycnSpChangeBindService.Instance.Start(); //同步SP换绑数据
  180. SycnSpUnBindService.Instance.Start(); //同步SP解绑数据
  181. SycnSpMerchantRecordService.Instance.Start(); //同步SP商户记录数据
  182. StatService.Instance.StartActiveReward(); //实时处理激活奖励
  183. StatService.Instance.StartActiveReward1(); //实时处理0押激活奖励
  184. StatService.Instance.StartActiveReward2(); //实时处理盒易付0押激活奖励
  185. StatService.Instance.StartOpenReward(); //实时获取开机奖励
  186. StatService.Instance.ListenFluxRecord(); //实时获取流量费分佣
  187. LeaderPrizeService.Instance.Start(); //大盟主奖励发奖
  188. OperatePrizeService.Instance.Start(); //运营中心奖励发奖
  189. StatService.Instance.StatUserLevel(); //升级
  190. ProfitHelperV2.Instance.StatProfit(); //创客分润
  191. RedPackageV2Helper.Instance.Start(); //每天生成红包
  192. RedPackageV2Helper.Instance.StartStatTop10(); //红包活动统计排行
  193. RedPackageV2Helper.Instance.StartSendPrize(); //红包活动发奖
  194. ActRewardService.Instance.Start(); //发放激活奖励
  195. HelpProfitPreMerchantHelper.Instance.Start(); //助利宝每天增加指定数量商机
  196. AddActService.Instance.Start(); //划拨后检查机具激活状态,并自动补录
  197. PrePosRingService.Instance.Start(); //预发未申请提醒
  198. PrePosWithholdService.Instance.Start(); //预发机过期未申请提交到预扣款
  199. PrePosWithholdService.Instance.StartPre(); //预发机申请成功,处理预扣款
  200. SycnMerchantTradeService.Instance.Start(); //同步商户交易额
  201. MessageCenterService.Instance.Start(); // 消息队列
  202. SetFeeFlagService.Instance.Start(); //118天提前通知创客费率调升消息
  203. LeaderTimeoutSendMessageService.Instance.Start(); //盟主过期消息提醒
  204. SetDepositService.Instance.Start(); //调整费率(通知、标记)
  205. AlipayPayBack2Service.Instance.Start(); //支付宝回调处理
  206. BalancePayBackService.Instance.Start(); //余额支付队列
  207. ReservePayBackService.Instance.Start(); //储备金支付队列
  208. OrderRefundService.Instance.Start(); //商城订单退款
  209. StatService.Instance.Start(); //每日重置交易额
  210. StoreApplyHelper.Instance.Start(); // 每月1号重置仓库额度
  211. DepositReturnStatService.Instance.Start(); //每月1号统计达标商户(退押需要的)
  212. DepositReturnStatService.Instance.StartEverTime(); //统计单个商户达标数据(退押需要的)
  213. AutoOpOrderService.Instance.StartOrderCancel(); //自动取消超时订单(15分钟)
  214. OperateStockService.Instance.Start(); //运营中心库存实时更新
  215. ResetSmallStoreHelper.Instance.Start(); //每月重置小分仓额度
  216. ResetSmallStoreHelper.Instance.Listen(); //监听每月1号重置小分仓额度
  217. OperateService.Instance.Start(); //运营中心每天统计一次发货量、库存
  218. TimeOutPosChargeService.Instance.StartDoChargeAmount(); //实时监听待扣款记录,并扣费
  219. InstallmentDeductionService.Instance.Start(); //分期扣款(每月20号执行)
  220. ChangePosTimer.Instance.Start(); //售后换新执行机具数据转移
  221. RecommendActStatService.Instance.Start(); //推荐王奖励数据统计
  222. StoreApplyHelper.Instance.StartEverTime(); //分仓临时额度变更
  223. PreStoreApplyHelper.Instance.StartEverTime(); //小分仓临时额度变更
  224. SetDepositPostService.Instance.Start(); //提交支付公司设置费率接口
  225. SetDepositPostService.Instance.StartKdb(); //监控开店宝费率设置结果
  226. OperateAmountService.Instance.Start(); //运营中心额度变更
  227. LeaderApplyCouponsHelper.Instance.Start(); //盟主储蓄金申请机具券打标记
  228. DepositReturnService.Instance.Start(); //退押金到支付宝余额
  229. StoreApplyHelper.Instance.ResetStoreReserve(); //重置分仓额度
  230. }
  231. //必须打开的
  232. // UserMonthFeeHelper.Instance.Start(); //每月创客服务费
  233. // UserMonthFeeHelper.Instance.Start2(); //临时扣创客服务费
  234. // TimeOutPosSendMessageService.Instance.Start(); //过期机具提醒
  235. // TimeOutPosChargeService.Instance.Start(); //过期机具计算扣费,并添加到待扣款记录
  236. // TimeOutPosChargeService.Instance.ListenChargeAmount(); //实时等待过期机具扣款指令,并扣费
  237. // StatService.Instance.StartPosActNum(); //实时统计激活数
  238. // StatService.Instance.StartNewUserNum(); //实时统计新增创客数
  239. // StatService.Instance.StatProfit(); //实时统计创客收益
  240. // StatServiceTmp.Instance.Start();
  241. // StatService.Instance.StartEverDay("");
  242. // StatService.Instance.StartEverDayV2();
  243. // RedPackageHelper.Instance.Start();
  244. // ProfitHelperV2.Instance.StatProfit(); //统计分润
  245. // StatService.Instance.StartEverDay2();
  246. // TestHelper.Instance.Start();
  247. // TestService.Instance.Start();
  248. // StatService.Instance.StartEverDay();
  249. // 备用,暂时不放开的
  250. // StatStoreDataService.Instance.Start();
  251. // RabbitMQClient.Instance.StartReceive("SycnTableData");
  252. // PayHelper.Instance.Start();
  253. // OrderHelper.Instance.Start();
  254. // OrderRefundHelper.Instance.Start();
  255. // ProductCommentHelper.Instance.Start();
  256. // TimedTaskHelper.Instance.Start();
  257. // TaskFlowHelper.Instance.Start();
  258. }
  259. }
  260. }