├── .gitattributes ├── .gitignore ├── JDGuardian.sln ├── JDGuardian ├── .config │ └── dotnet-tools.json ├── Controllers │ ├── AreaController.cs │ ├── CheckController.cs │ ├── MonitorController .cs │ └── WareBusinessController.cs ├── Enums │ └── MonitorType.cs ├── Helpers │ ├── HttpHelper.cs │ ├── ThreadHelper.cs │ └── TimeHelper.cs ├── Interfaces │ └── IMonitor.cs ├── JDGuardian.csproj ├── Models │ ├── Area.cs │ ├── CheckResult.cs │ ├── CreateMonitorItem.cs │ ├── DeliveryInfo.cs │ ├── MailSetting.cs │ ├── MiaoshaInfo.cs │ ├── MonitorItem.cs │ ├── PlusTag.cs │ ├── Price.cs │ ├── StockInfo.cs │ ├── StockInfoSub.cs │ ├── StockMonitor.cs │ ├── WareBusiness.cs │ ├── WareInfo.cs │ └── YuyueInfo.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── Services │ ├── MailService.cs │ └── StockMonitorService.cs ├── appsettings.Development.json └── appsettings.json └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Oo]ut/ 33 | [Ll]og/ 34 | [Ll]ogs/ 35 | 36 | # Visual Studio 2015/2017 cache/options directory 37 | .vs/ 38 | # Uncomment if you have tasks that create the project's static files in wwwroot 39 | #wwwroot/ 40 | 41 | # Visual Studio 2017 auto generated files 42 | Generated\ Files/ 43 | 44 | # MSTest test Results 45 | [Tt]est[Rr]esult*/ 46 | [Bb]uild[Ll]og.* 47 | 48 | # NUnit 49 | *.VisualState.xml 50 | TestResult.xml 51 | nunit-*.xml 52 | 53 | # Build Results of an ATL Project 54 | [Dd]ebugPS/ 55 | [Rr]eleasePS/ 56 | dlldata.c 57 | 58 | # Benchmark Results 59 | BenchmarkDotNet.Artifacts/ 60 | 61 | # .NET Core 62 | project.lock.json 63 | project.fragment.lock.json 64 | artifacts/ 65 | 66 | # ASP.NET Scaffolding 67 | ScaffoldingReadMe.txt 68 | 69 | # StyleCop 70 | StyleCopReport.xml 71 | 72 | # Files built by Visual Studio 73 | *_i.c 74 | *_p.c 75 | *_h.h 76 | *.ilk 77 | *.meta 78 | *.obj 79 | *.iobj 80 | *.pch 81 | *.pdb 82 | *.ipdb 83 | *.pgc 84 | *.pgd 85 | *.rsp 86 | *.sbr 87 | *.tlb 88 | *.tli 89 | *.tlh 90 | *.tmp 91 | *.tmp_proj 92 | *_wpftmp.csproj 93 | *.log 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio LightSwitch build output 298 | **/*.HTMLClient/GeneratedArtifacts 299 | **/*.DesktopClient/GeneratedArtifacts 300 | **/*.DesktopClient/ModelManifest.xml 301 | **/*.Server/GeneratedArtifacts 302 | **/*.Server/ModelManifest.xml 303 | _Pvt_Extensions 304 | 305 | # Paket dependency manager 306 | .paket/paket.exe 307 | paket-files/ 308 | 309 | # FAKE - F# Make 310 | .fake/ 311 | 312 | # CodeRush personal settings 313 | .cr/personal 314 | 315 | # Python Tools for Visual Studio (PTVS) 316 | __pycache__/ 317 | *.pyc 318 | 319 | # Cake - Uncomment if you are using it 320 | # tools/** 321 | # !tools/packages.config 322 | 323 | # Tabs Studio 324 | *.tss 325 | 326 | # Telerik's JustMock configuration file 327 | *.jmconfig 328 | 329 | # BizTalk build output 330 | *.btp.cs 331 | *.btm.cs 332 | *.odx.cs 333 | *.xsd.cs 334 | 335 | # OpenCover UI analysis results 336 | OpenCover/ 337 | 338 | # Azure Stream Analytics local run output 339 | ASALocalRun/ 340 | 341 | # MSBuild Binary and Structured Log 342 | *.binlog 343 | 344 | # NVidia Nsight GPU debugger configuration file 345 | *.nvuser 346 | 347 | # MFractors (Xamarin productivity tool) working folder 348 | .mfractor/ 349 | 350 | # Local History for Visual Studio 351 | .localhistory/ 352 | 353 | # BeatPulse healthcheck temp database 354 | healthchecksdb 355 | 356 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 357 | MigrationBackup/ 358 | 359 | # Ionide (cross platform F# VS Code tools) working folder 360 | .ionide/ 361 | 362 | # Fody - auto-generated XML schema 363 | FodyWeavers.xsd -------------------------------------------------------------------------------- /JDGuardian.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.1.32421.90 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JDGuardian", "JDGuardian\JDGuardian.csproj", "{DB91F746-C032-4810-870E-6C45E47A67A4}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {DB91F746-C032-4810-870E-6C45E47A67A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {DB91F746-C032-4810-870E-6C45E47A67A4}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {DB91F746-C032-4810-870E-6C45E47A67A4}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {DB91F746-C032-4810-870E-6C45E47A67A4}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {C51CE2D6-F191-4508-A6F4-3352D460B6A8} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /JDGuardian/.config/dotnet-tools.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 1, 3 | "isRoot": true, 4 | "tools": { 5 | "dotnet-ef": { 6 | "version": "6.0.9", 7 | "commands": [ 8 | "dotnet-ef" 9 | ] 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /JDGuardian/Controllers/AreaController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Cors; 2 | using Microsoft.AspNetCore.Http; 3 | using Microsoft.AspNetCore.Mvc; 4 | using Newtonsoft.Json; 5 | 6 | namespace JDGuardian.Controllers 7 | { 8 | /// 9 | /// 区域控制器 10 | /// 11 | [ApiController] 12 | [Route("area")] 13 | [EnableCors("AllowCors")] 14 | public class AreaController : Controller 15 | { 16 | /// 17 | /// 获取指定区域的子区域 18 | /// 19 | /// 指定区域id,默认-1表示获取国家及地区 20 | /// 21 | [HttpGet] 22 | public async Task> Get(int fid = -1) 23 | { 24 | string result = await Helpers.HttpHelper.GetAsync($"https://fts.jd.com/area/get?fid={fid}"); 25 | return JsonConvert.DeserializeObject>(result); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /JDGuardian/Controllers/CheckController.cs: -------------------------------------------------------------------------------- 1 | using JDGuardian.Models; 2 | using Microsoft.AspNetCore.Cors; 3 | using Microsoft.AspNetCore.Http; 4 | using Microsoft.AspNetCore.Mvc; 5 | using Newtonsoft.Json; 6 | 7 | namespace JDGuardian.Controllers 8 | { 9 | /// 10 | /// 区域控制器 11 | /// 12 | [ApiController] 13 | [Route("check")] 14 | [EnableCors("AllowCors")] 15 | public class CheckController : Controller 16 | { 17 | /// 18 | /// 是否有库存 19 | /// 20 | /// 物品id 21 | /// 地区id组合,格式示例:19_1601_50258_51885 22 | /// 23 | [Route("isStock")] 24 | [HttpGet] 25 | public async Task IsStock(long skuId, string area) 26 | { 27 | var ware = await Models.WareBusiness.CreatAsync(skuId, area); 28 | if (ware == null) 29 | { 30 | return CheckResult.CreateErro("未知物品"); 31 | } 32 | else 33 | { 34 | return new CheckResult() 35 | { 36 | Is = ware.IsStock(), 37 | WareBusiness = ware 38 | }; 39 | } 40 | } 41 | /// 42 | /// 是否在秒杀 43 | /// 不包括即将开始秒杀 44 | /// 45 | /// 物品id 46 | /// 地区id组合,格式示例:19_1601_50258_51885 47 | /// 48 | [Route("isMiaoshaing")] 49 | [HttpGet] 50 | public async Task IsMiaoshaing(long skuId, string area) 51 | { 52 | var ware = await Models.WareBusiness.CreatAsync(skuId, area); 53 | if (ware == null) 54 | { 55 | return CheckResult.CreateErro("未知物品"); 56 | } 57 | else 58 | { 59 | return new CheckResult() 60 | { 61 | Is = ware.IsMiaoshaing(), 62 | WareBusiness = ware 63 | }; 64 | } 65 | } 66 | /// 67 | /// 是否即将秒杀 68 | /// 不包括在秒杀中 69 | /// 70 | /// 物品id 71 | /// 地区id组合,格式示例:19_1601_50258_51885 72 | /// 73 | [Route("isWillMiaosha")] 74 | [HttpGet] 75 | public async Task IsWillMiaosha(long skuId, string area) 76 | { 77 | var ware = await Models.WareBusiness.CreatAsync(skuId, area); 78 | if (ware == null) 79 | { 80 | return CheckResult.CreateErro("未知物品"); 81 | } 82 | else 83 | { 84 | return new CheckResult() 85 | { 86 | Is = ware.IsWillMiaoshaing(), 87 | WareBusiness = ware 88 | }; 89 | } 90 | } 91 | /// 92 | /// 是否在预约 93 | /// 94 | /// 物品id 95 | /// 地区id组合,格式示例:19_1601_50258_51885 96 | /// 97 | [Route("isYuyueing")] 98 | [HttpGet] 99 | public async Task IsYuyueing(long skuId, string area) 100 | { 101 | var ware = await Models.WareBusiness.CreatAsync(skuId, area); 102 | if (ware == null) 103 | { 104 | return CheckResult.CreateErro("未知物品"); 105 | } 106 | else 107 | { 108 | return new CheckResult() 109 | { 110 | Is = ware.IsYuyueing(), 111 | WareBusiness = ware 112 | }; 113 | } 114 | } 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /JDGuardian/Controllers/MonitorController .cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Cors; 2 | using Microsoft.AspNetCore.Http; 3 | using Microsoft.AspNetCore.Mvc; 4 | using Newtonsoft.Json; 5 | 6 | namespace JDGuardian.Controllers 7 | { 8 | /// 9 | /// 监控控制器 10 | /// 11 | [ApiController] 12 | [Route("monitor")] 13 | [EnableCors("AllowCors")] 14 | public class MonitorController : Controller 15 | { 16 | /// 17 | /// 添加库存监控 18 | /// 19 | /// 订阅数据 20 | /// 操作索引id 21 | /// 22 | /// Post示例: 23 | /// 24 | /// { 25 | /// "mail": "qq@qq.com", 26 | /// "span": 60, 27 | /// "skuId": 100016148888, 28 | /// "area": "19_1601_50258_51885" 29 | /// } 30 | /// 31 | /// 32 | [Route("addStock")] 33 | [HttpPost] 34 | [Produces("application/json")] 35 | public string Post([FromBody] Models.CreateMonitorItem item) 36 | { 37 | string id = Services.StockMonitorService.AddStockMonitor(item.Mail, item.Span, item.SkuId, item.Area); 38 | if(!string.IsNullOrEmpty(id)) 39 | { 40 | Services.MailService.SendMail(item.Mail, "JDGuardian库存监控", $"已添加 https://item.jd.com/{item.SkuId}.html 到库存监控列表"); 41 | } 42 | return id; 43 | } 44 | 45 | /// 46 | /// 删除监控的物品 47 | /// 48 | /// 添加监控时返回的id 49 | /// 50 | [Route("deleteStock")] 51 | [HttpDelete] 52 | public bool Del(string id) 53 | { 54 | return Services.StockMonitorService.TryDelMonitor(id); 55 | } 56 | 57 | /// 58 | /// 获取所有库存监控项 59 | /// 60 | /// 61 | [Route("stockMonitorList")] 62 | [HttpGet] 63 | public Dictionary GetList() 64 | { 65 | return Services.StockMonitorService.Monitors; 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /JDGuardian/Controllers/WareBusinessController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Cors; 2 | using Microsoft.AspNetCore.Http; 3 | using Microsoft.AspNetCore.Mvc; 4 | using Newtonsoft.Json; 5 | 6 | namespace JDGuardian.Controllers 7 | { 8 | /// 9 | /// 物品控制器 10 | /// 11 | [ApiController] 12 | [Route("getWareBusiness")] 13 | [EnableCors("AllowCors")] 14 | public class WareBusinessController : Controller 15 | { 16 | /// 17 | /// 获取物品价格信息 18 | /// 19 | /// 物品id 20 | /// 地区id组合,格式示例:19_1601_50258_51885 21 | /// 22 | [HttpGet] 23 | public async Task Get(long skuId,string area) 24 | { 25 | return await Models.WareBusiness.CreatAsync(skuId,area); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /JDGuardian/Enums/MonitorType.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel; 2 | 3 | namespace JDGuardian.Enums 4 | { 5 | public enum MonitorType 6 | { 7 | [Description("库存监控")] 8 | Stock, 9 | [Description("价格监控")] 10 | Pirce 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /JDGuardian/Helpers/HttpHelper.cs: -------------------------------------------------------------------------------- 1 | namespace JDGuardian.Helpers 2 | { 3 | public static class HttpHelper 4 | { 5 | /// 6 | /// 使用post方法异步请求 7 | /// 8 | /// 目标链接 9 | /// 发送的参数json 10 | /// 返回的字符串 11 | public static async Task PostJsonAsync(string url, string data, Dictionary header = null) 12 | { 13 | HttpClient client = new HttpClient(new HttpClientHandler() { UseCookies = false }); 14 | HttpContent content = new StringContent(data); 15 | if (header != null) 16 | { 17 | client.DefaultRequestHeaders.Clear(); 18 | foreach (var item in header) 19 | { 20 | client.DefaultRequestHeaders.Add(item.Key, item.Value); 21 | } 22 | } 23 | try 24 | { 25 | HttpResponseMessage response = await client.PostAsync(url, content); 26 | if (response.IsSuccessStatusCode) 27 | { 28 | return await response.Content.ReadAsStringAsync(); 29 | } 30 | else 31 | { 32 | return null; 33 | } 34 | } 35 | catch (Exception) 36 | { 37 | return null; 38 | } 39 | } 40 | 41 | 42 | public static async Task PostAsync(string url, Dictionary form) 43 | { 44 | HttpClient client = new HttpClient(new HttpClientHandler() { UseCookies = false }); 45 | HttpContent content = new FormUrlEncodedContent(form); 46 | try 47 | { 48 | HttpResponseMessage response = await client.PostAsync(url, content); 49 | string responseBody = await response.Content.ReadAsStringAsync(); 50 | return responseBody; 51 | } 52 | catch (Exception) 53 | { 54 | return null; 55 | } 56 | } 57 | 58 | /// 59 | /// 使用get方法异步请求 60 | /// 61 | /// 目标链接 62 | /// 返回的字符串 63 | public static async Task GetAsync(string url, Dictionary header = null) 64 | { 65 | HttpClient client = new HttpClient(new HttpClientHandler() { UseCookies = false }); 66 | if (header != null) 67 | { 68 | client.DefaultRequestHeaders.Clear(); 69 | foreach (var item in header) 70 | { 71 | client.DefaultRequestHeaders.Add(item.Key, item.Value); 72 | } 73 | } 74 | client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Mobile Safari/537.36"); 75 | try 76 | { 77 | HttpResponseMessage response = await client.GetAsync(url); 78 | return await response.Content.ReadAsStringAsync(); 79 | } 80 | catch (Exception) 81 | { 82 | return null; 83 | } 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /JDGuardian/Helpers/ThreadHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace JDGuardian.Helpers 9 | { 10 | static class ThreadHelper 11 | { 12 | /// 13 | /// 并行处理集合中的每一项 14 | /// 15 | /// 集合类型 16 | /// 需要并行处理的集合 17 | /// 处理集合元素的委托 18 | /// 异步任务 19 | public static Task RunAsync(IEnumerable source, Action action) => RunAsync(source, Environment.ProcessorCount, action); 20 | 21 | /// 22 | /// 并行处理集合中的每一项 23 | /// 24 | /// 集合类型 25 | /// 需要并行处理的集合 26 | /// 最大线程数限制 27 | /// 处理集合元素的委托 28 | /// 异步任务 29 | public static Task RunAsync(IEnumerable source, int threadLimit, Action action) 30 | { 31 | ConcurrentQueue datas = new ConcurrentQueue(source); 32 | var tasks = Enumerable.Range(0, threadLimit).Select(t => Task.Run(() => 33 | { 34 | while (datas.TryDequeue(out var data)) 35 | { 36 | action(data); 37 | } 38 | })); 39 | return Task.WhenAll(tasks); 40 | } 41 | 42 | /// 43 | /// 并行处理集合中的每一项 44 | /// 45 | /// 集合类型 46 | /// 需要并行处理的集合 47 | /// 处理集合元素的委托 48 | /// 异步任务 49 | public static Task RunAsync(IEnumerable source, Func action) => RunAsync(source, Environment.ProcessorCount, action); 50 | 51 | /// 52 | /// 并行处理集合中的每一项 53 | /// 54 | /// 集合类型 55 | /// 需要并行处理的集合 56 | /// 最大线程数限制 57 | /// 处理集合元素的委托 58 | /// 异步任务 59 | public static Task RunAsync(IEnumerable source, int threadLimit, Func action) 60 | { 61 | ConcurrentQueue datas = new ConcurrentQueue(source); 62 | var tasks = Enumerable.Range(0, threadLimit).Select(t => Task.Run(async () => 63 | { 64 | while (datas.TryDequeue(out var data)) 65 | { 66 | await action(data); 67 | } 68 | })); 69 | return Task.WhenAll(tasks); 70 | } 71 | 72 | /// 73 | /// 并行处理集合中的每一项,并将返回每一项的处理结果集合 74 | /// 75 | /// 集合类型 76 | /// 需要并行处理的集合 77 | /// 最大线程数限制 78 | /// 处理集合元素的委托 79 | /// 处理结果集合的异步任务 80 | public static async Task> RunAsync(IEnumerable source, Func action) => await RunAsync(source, Environment.ProcessorCount, action); 81 | 82 | /// 83 | /// 并行处理集合中的每一项,并将返回每一项的处理结果集合 84 | /// 85 | /// 集合类型 86 | /// 需要并行处理的集合 87 | /// 最大线程数限制 88 | /// 处理集合元素的委托 89 | /// 处理结果集合的异步任务 90 | public static async Task> RunAsync(IEnumerable source, int threadLimit, Func action) 91 | { 92 | ConcurrentQueue result = new ConcurrentQueue(); 93 | ConcurrentQueue datas = new ConcurrentQueue(source); 94 | var tasks = Enumerable.Range(0, threadLimit).Select(t => Task.Run(() => 95 | { 96 | while (datas.TryDequeue(out var data)) 97 | { 98 | result.Enqueue(action(data)); 99 | } 100 | })); 101 | await Task.WhenAll(tasks); 102 | return result.ToList(); 103 | } 104 | 105 | public static IReadOnlyList Run(IEnumerable source, Func action) => Run(source, Environment.ProcessorCount, action); 106 | 107 | public static IReadOnlyList Run(IEnumerable source, int threadLimit, Func action) 108 | { 109 | ConcurrentQueue result = new ConcurrentQueue(); 110 | ConcurrentQueue datas = new ConcurrentQueue(source); 111 | var tasks = Enumerable.Range(0, threadLimit).Select(t => Task.Run(() => 112 | { 113 | while (datas.TryDequeue(out var data)) 114 | { 115 | result.Enqueue(action(data)); 116 | } 117 | })); 118 | Task.WaitAll(tasks.ToArray()); 119 | return result.ToList(); 120 | } 121 | 122 | public static void Run(IEnumerable source, Action action)=> Run(source, Environment.ProcessorCount, action); 123 | 124 | /// 125 | /// 并行处理集合中的每一项 126 | /// 127 | /// 集合类型 128 | /// 需要并行处理的集合 129 | /// 最大线程数限制 130 | /// 处理集合元素的委托 131 | /// 异步任务 132 | public static void Run(IEnumerable source, int threadLimit, Action action) 133 | { 134 | ConcurrentQueue datas = new ConcurrentQueue(source); 135 | var tasks = Enumerable.Range(0, threadLimit).Select(t => Task.Run(() => 136 | { 137 | while (datas.TryDequeue(out var data)) 138 | { 139 | action(data); 140 | } 141 | })); 142 | Task.WaitAll(tasks.ToArray()); 143 | return; 144 | } 145 | 146 | /// 147 | /// 并行处理集合中的每一项,并将返回每一项的处理结果集合 148 | /// 149 | /// 集合类型 150 | /// 需要并行处理的集合 151 | /// 最大线程数限制 152 | /// 处理集合元素的委托 153 | /// 处理结果集合的异步任务 154 | public static async Task> RunAsync(IEnumerable source, Func> action) => await RunAsync(source, Environment.ProcessorCount, action); 155 | 156 | /// 157 | /// 并行处理集合中的每一项,并将返回每一项的处理结果集合 158 | /// 159 | /// 集合类型 160 | /// 需要并行处理的集合 161 | /// 最大线程数限制 162 | /// 处理集合元素的委托 163 | /// 处理结果集合的异步任务 164 | public static async Task> RunAsync(IEnumerable source, int threadLimit, Func> action) 165 | { 166 | ConcurrentQueue result = new ConcurrentQueue(); 167 | ConcurrentQueue datas = new ConcurrentQueue(source); 168 | var tasks = Enumerable.Range(0, threadLimit).Select(t => Task.Run(async () => 169 | { 170 | while (datas.TryDequeue(out var data)) 171 | { 172 | result.Enqueue(await action(data)); 173 | } 174 | })); 175 | await Task.WhenAll(tasks); 176 | return result.ToList(); 177 | } 178 | } 179 | } 180 | -------------------------------------------------------------------------------- /JDGuardian/Helpers/TimeHelper.cs: -------------------------------------------------------------------------------- 1 | namespace JDGuardian.Helpers 2 | { 3 | public static class TimeHelper 4 | { 5 | /// 6 | /// Unix时间戳转DateTime 7 | /// 8 | /// 时间戳 9 | /// 10 | public static DateTime UnixToDateTime(long timestamp) 11 | { 12 | DateTime time = DateTime.MinValue; 13 | DateTime startTime = new DateTime(1970, 1, 1); 14 | if (timestamp >= 1000000000000) //精确到毫秒 15 | { 16 | time = startTime.AddMilliseconds(timestamp); 17 | } 18 | else if (timestamp >= 1000000000) //精确到秒 19 | { 20 | time = startTime.AddSeconds(timestamp); 21 | } 22 | return time; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /JDGuardian/Interfaces/IMonitor.cs: -------------------------------------------------------------------------------- 1 | namespace JDGuardian.Interfaces 2 | { 3 | public interface IMonitor 4 | { 5 | /// 6 | /// 执行检查 7 | /// 8 | void Check(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /JDGuardian/JDGuardian.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | disable 6 | enable 7 | True 8 | 9 | 10 | 11 | 1701;1702;1591 12 | 13 | 14 | 15 | 1701;1702;1591 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /JDGuardian/Models/Area.cs: -------------------------------------------------------------------------------- 1 | namespace JDGuardian.Models 2 | { 3 | /// 4 | /// 送货区域 5 | /// 6 | public class Area 7 | { 8 | /// 9 | /// 未知作用 10 | /// 11 | public string AreaCode { get; set; } 12 | public string Id { get; set; } 13 | public string Name { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /JDGuardian/Models/CheckResult.cs: -------------------------------------------------------------------------------- 1 | namespace JDGuardian.Models 2 | { 3 | public class CheckResult 4 | { 5 | public bool Is { get; set; } 6 | public string Message { get; set; } 7 | public WareBusiness WareBusiness { get; set; } 8 | 9 | public static CheckResult CreateErro(string msg) 10 | { 11 | return new CheckResult() { Is = false, Message = msg }; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /JDGuardian/Models/CreateMonitorItem.cs: -------------------------------------------------------------------------------- 1 | namespace JDGuardian.Models 2 | { 3 | public class CreateMonitorItem 4 | { 5 | /// 6 | /// 通知邮箱 7 | /// 8 | public string Mail { get; set; } 9 | /// 10 | /// 间隔 11 | /// 12 | public int Span { get; set; } 13 | /// 14 | /// 商品id 15 | /// 16 | public long SkuId { get; set; } 17 | /// 18 | /// 区域id组合 19 | /// 20 | public string Area { get; set; } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /JDGuardian/Models/DeliveryInfo.cs: -------------------------------------------------------------------------------- 1 | namespace JDGuardian.Models 2 | { 3 | /// 4 | /// 配送 5 | /// 6 | public class DeliveryInfo 7 | { 8 | public string State { get; set; } 9 | public string Support { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /JDGuardian/Models/MailSetting.cs: -------------------------------------------------------------------------------- 1 | namespace JDGuardian.Models 2 | { 3 | /// 4 | /// 发送通知邮件的发件人邮箱设置 5 | /// 6 | public class MailSetting 7 | { 8 | public string Address { get; set; } 9 | public string DisplayName { get; set; } = "JDGuardian"; 10 | public string Host { get; set; } 11 | public int Port { get; set; } 12 | public bool EnableSsl { get; set; } = true; 13 | public string UserName { get; set; } 14 | public string Password { get; set; } 15 | public bool IsVaild() 16 | { 17 | return !string.IsNullOrEmpty(Address) && !string.IsNullOrEmpty(Host) && Port != 0 && !string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /JDGuardian/Models/MiaoshaInfo.cs: -------------------------------------------------------------------------------- 1 | namespace JDGuardian.Models 2 | { 3 | /// 4 | /// 秒杀 5 | /// 6 | public class MiaoshaInfo 7 | { 8 | /// 9 | /// 正在秒杀时才有 10 | /// 图片url 11 | /// 12 | public string BannerUrl { get; set; } 13 | /// 14 | /// 即将开始秒杀时才有,表示什么时候开始 15 | /// 如:9月15日 00:00 16 | /// 17 | public string BenifitText { get; set; } 18 | public long EndTime { get; set; } 19 | public bool Miaosha { get; set; } 20 | /// 21 | /// 如果已经开始,则为秒杀结束剩余秒数 22 | /// 如果未开始,则为秒杀开始剩余秒数 23 | /// 24 | public int MiaoshaRemainTime { get; set; } 25 | /// 26 | /// 距离开始还剩 27 | /// 距离结束 28 | /// 29 | public string MsTrailer { get; set; } 30 | /// 31 | /// 原价 32 | /// 33 | public double OriginPrice { get; set; } 34 | /// 35 | /// 秒杀价 36 | /// 37 | public double PromoPrice { get; set; } 38 | /// 39 | /// 未知 40 | /// 秒杀中、即将秒杀都是1 41 | /// 42 | public int SeckillType { get; set; } 43 | public long StartTime { get; set; } 44 | /// 45 | /// 1 即将秒杀 46 | /// 2 秒杀中 47 | /// 48 | public int State { get; set; } 49 | /// 50 | /// 秒杀预告 51 | /// 京东秒杀 52 | /// 53 | public string Title { get; set; } 54 | 55 | /// 56 | /// 扩展字段 57 | /// 将StartTime以DateTime表示 58 | /// 59 | public DateTime StartTime2 60 | { 61 | get => GetStartTime(); 62 | } 63 | /// 64 | /// 扩展字段 65 | /// 将EndTime以DateTime表示 66 | /// 67 | public DateTime EndTime2 68 | { 69 | get => GetEndTime(); 70 | } 71 | /// 72 | /// 扩展字段 73 | /// 将MiaoshaRemainTime以字符串表示 74 | /// 如:1天1小时1分、1小时1分、1分1秒、1秒 75 | /// 76 | public string MiaoshaRemainTime2 77 | { 78 | get => GetRemainTime(); 79 | } 80 | 81 | public DateTime GetStartTime() 82 | { 83 | return Helpers.TimeHelper.UnixToDateTime(StartTime); 84 | } 85 | 86 | public DateTime GetEndTime() 87 | { 88 | return Helpers.TimeHelper.UnixToDateTime(EndTime); 89 | } 90 | /// 91 | /// 获取剩余时间字符串表达式 92 | /// 如:1天1小时1分、1小时1分、1分1秒、1秒 93 | /// 94 | /// 95 | public string GetRemainTime() 96 | { 97 | var timeSpan = TimeSpan.FromSeconds(MiaoshaRemainTime); 98 | if(timeSpan.Days > 0) 99 | { 100 | return $"{timeSpan.Days}天{timeSpan.Hours}小时{timeSpan.Minutes}分"; 101 | } 102 | else if(timeSpan.Hours > 0) 103 | { 104 | return $"{timeSpan.Hours}小时{timeSpan.Minutes}分"; 105 | } 106 | else if(timeSpan.Minutes > 0) 107 | { 108 | return $"{timeSpan.Minutes}分{timeSpan.Seconds}秒"; 109 | } 110 | else 111 | { 112 | return $"{timeSpan.Seconds}秒"; 113 | } 114 | } 115 | 116 | /// 117 | /// 是否秒杀中 118 | /// 119 | /// 120 | public bool IsMiaoshaing() 121 | { 122 | return State == 2; 123 | } 124 | /// 125 | /// 是否即将秒杀 126 | /// 127 | /// 128 | public bool IsWillMiaosha() 129 | { 130 | return State == 1; 131 | } 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /JDGuardian/Models/MonitorItem.cs: -------------------------------------------------------------------------------- 1 | namespace JDGuardian.Models 2 | { 3 | public abstract class MonitorItem 4 | { 5 | public MonitorItem(string mail, int span, long skuid,string area) 6 | { 7 | Mail = mail; 8 | Span = span; 9 | SkuId = skuid; 10 | Area = area; 11 | CreateTime = DateTime.Now; 12 | LastTime = DateTime.Now; 13 | Id = Guid.NewGuid().ToString(); 14 | Span = span; 15 | RemainTime = span; 16 | } 17 | public string Mail { get; set; } 18 | /// 19 | /// 索引id 20 | /// 21 | public string Id { get; set; } 22 | /// 23 | /// 商品id 24 | /// 25 | public long SkuId { get; set; } 26 | /// 27 | /// 区域id组合 28 | /// 29 | public string Area { get; set; } 30 | public DateTime CreateTime { get; set; } 31 | /// 32 | /// 间隔时间 33 | /// 单位:秒 34 | /// 35 | public int Span { get; set; } 36 | /// 37 | /// 上一次执行时间 38 | /// 39 | public DateTime LastTime { get; set; } 40 | /// 41 | /// 距离下一次执行时间 42 | /// 单位:秒 43 | /// 44 | public int RemainTime { get; set; } 45 | /// 46 | /// 监控类型 47 | /// 48 | public Enums.MonitorType MonitorType { get; set; } 49 | /// 50 | /// 执行检查 51 | /// 52 | public virtual void Check() 53 | { 54 | LastTime = DateTime.Now; 55 | RemainTime = Span; 56 | } 57 | /// 58 | /// 更新RemainTime 59 | /// 60 | public void Update() 61 | { 62 | RemainTime = Span - (int)(DateTime.Now - LastTime).TotalSeconds; 63 | } 64 | /// 65 | /// 是否需要执行检查 66 | /// 67 | /// 68 | public bool IsTime() 69 | { 70 | return RemainTime <= 0; 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /JDGuardian/Models/PlusTag.cs: -------------------------------------------------------------------------------- 1 | namespace JDGuardian.Models 2 | { 3 | public class PlusTag 4 | { 5 | public string Limit { get; set; } 6 | public string Min { get; set; } 7 | public string Max { get; set; } 8 | public string Overlying { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /JDGuardian/Models/Price.cs: -------------------------------------------------------------------------------- 1 | namespace JDGuardian.Models 2 | { 3 | public class Price 4 | { 5 | public string Epp { get; set; } 6 | public bool HagglePromotion { get; set; } 7 | public long Id { get; set; } 8 | public double M { get; set; } 9 | public string Nup { get; set; } 10 | /// 11 | /// 原始价格 12 | /// 13 | public double Op { get; set; } 14 | /// 15 | /// 当前价格 16 | /// -1时表示不可购买,如无货、下架 17 | /// 18 | public double P { get; set; } 19 | /// 20 | /// 额外信息 21 | /// 22 | public PlusTag PlusTag { get; set; } 23 | public string Pp { get; set; } 24 | public string Sdp { get; set; } 25 | /// 26 | /// 粉丝价格 27 | /// 28 | public string Sfp { get; set; } 29 | public string Sp { get; set; } 30 | public string Tkp { get; set; } 31 | public string Tpp { get; set; } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /JDGuardian/Models/StockInfo.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace JDGuardian.Models 4 | { 5 | /// 6 | /// 库存 7 | /// 8 | public class StockInfo 9 | { 10 | public DeliveryInfo DeliveryInfo { get; set; } 11 | public string IsPlus { get; set; } 12 | public string Code { get; set; } 13 | /// 14 | /// 具体的配送时间 15 | /// 如:23:10前下单,预计明天(09月14日)送达 16 | /// 17 | public string PromiseResult { get; set; } 18 | /// 19 | /// 配送、售后之类的服务信息 20 | /// 如:由京东发货,微软京东自营官方旗舰店提供售后服务 21 | /// 22 | public string ServiceInfo { get; set; } 23 | /// 24 | /// 库存描述 25 | /// 如:有货 26 | /// 27 | public string StockDesc { get; set; } 28 | public string WeightValue { get; set; } 29 | public string FxgCode { get; set; } 30 | public string Is7ToReturn { get; set; } 31 | /// 32 | /// 库存状态码 33 | /// 33:有货 34:无货 34 | /// 35 | public int StockState { get; set; } 36 | [JsonProperty(PropertyName = "stockInfo")] 37 | public StockInfoSub StockInfoSub { get; set; } 38 | public string SupportHKMOShip { get; set; } 39 | public string IsStock { get; set; } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /JDGuardian/Models/StockInfoSub.cs: -------------------------------------------------------------------------------- 1 | namespace JDGuardian.Models 2 | { 3 | /// 4 | /// 库存 5 | /// 6 | public class StockInfoSub 7 | { 8 | public string Ae { get; set; } 9 | public string Ah { get; set; } 10 | public string AvailableNum { get; set; } 11 | public string BusinessType { get; set; } 12 | public string Ca { get; set; } 13 | public string Date { get; set; } 14 | public string DcId { get; set; } 15 | public string DcIdDely { get; set; } 16 | public string Ef { get; set; } 17 | public string Eh { get; set; } 18 | public string Ej { get; set; } 19 | public string Ek { get; set; } 20 | public string FreshEdi { get; set; } 21 | public string PopPatType { get; set; } 22 | public string PreStore { get; set; } 23 | public string RailModel { get; set; } 24 | public string ReservationType { get; set; } 25 | public string Rid { get; set; } 26 | public string SidDely { get; set; } 27 | public string SiteId4Dada { get; set; } 28 | public string StDely { get; set; } 29 | public string StockM { get; set; } 30 | public string StockState { get; set; } 31 | public string StockV { get; set; } 32 | public string StoreId { get; set; } 33 | public string StoreId4Dada { get; set; } 34 | public string StoreState4Dada { get; set; } 35 | public string StoreType { get; set; } 36 | public string UseStockNum { get; set; } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /JDGuardian/Models/StockMonitor.cs: -------------------------------------------------------------------------------- 1 | namespace JDGuardian.Models 2 | { 3 | public class StockMonitor : MonitorItem 4 | { 5 | public StockMonitor(string mail, int span, long skuid, string area) : base(mail,span, skuid, area) 6 | { 7 | 8 | } 9 | 10 | public override void Check() 11 | { 12 | base.Check(); 13 | var ware = Models.WareBusiness.Creat(SkuId, Area); 14 | if (ware != null) 15 | { 16 | if(ware.IsStock()) 17 | { 18 | string content = $"{DateTime.Now}:https://item.jd.com/{SkuId} ({Id})有货"; 19 | Console.WriteLine(content); 20 | if(Services.MailService.SendMail(Mail,"库存提醒",content)) 21 | { 22 | Console.WriteLine($"已发送邮件到{Mail}"); 23 | } 24 | else 25 | { 26 | Console.WriteLine("发送邮件失败"); 27 | } 28 | } 29 | else 30 | { 31 | string content = $"{DateTime.Now}:{SkuId}({Id})无货"; 32 | Console.WriteLine(content); 33 | } 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /JDGuardian/Models/WareBusiness.cs: -------------------------------------------------------------------------------- 1 | namespace JDGuardian.Models 2 | { 3 | /// 4 | /// 物品信息 5 | /// 6 | public class WareBusiness 7 | { 8 | /// 9 | /// 物品基本信息 10 | /// 11 | public WareInfo WareInfo { get; set; } 12 | /// 13 | /// 价格 14 | /// 15 | public Price Price { get; set; } 16 | /// 17 | /// 秒杀 18 | /// 19 | public MiaoshaInfo MiaoshaInfo { get; set; } 20 | /// 21 | /// 预约 22 | /// 23 | public YuyueInfo YuyueInfo { get; set; } 24 | /// 25 | /// 库存 26 | /// 27 | public StockInfo StockInfo { get; set; } 28 | 29 | public static async Task CreatAsync(long skuId, string area) 30 | { 31 | string result = await Helpers.HttpHelper.GetAsync($"https://item-soa.jd.com/getWareBusiness?skuId={skuId}&area={area}"); 32 | return Newtonsoft.Json.JsonConvert.DeserializeObject(result); 33 | } 34 | 35 | public static WareBusiness Creat(long skuId, string area) 36 | { 37 | string result = Helpers.HttpHelper.GetAsync($"https://item-soa.jd.com/getWareBusiness?skuId={skuId}&area={area}").Result; 38 | return Newtonsoft.Json.JsonConvert.DeserializeObject(result); 39 | } 40 | 41 | /// 42 | /// 是否有货可买 43 | /// 44 | /// 45 | public bool IsStock() 46 | { 47 | //可考虑配合https://cd.jd.com/stocks?type=getstocks&skuIds=100016148864&area=19_1601_50258_51885使用 48 | return Price != null && Price.P != -1 && StockInfo != null && StockInfo.StockState == 33; 49 | } 50 | 51 | /// 52 | /// 是否正处于秒杀 53 | /// 54 | /// 55 | public bool IsMiaoshaing() 56 | { 57 | return MiaoshaInfo != null && MiaoshaInfo.IsMiaoshaing(); 58 | } 59 | 60 | /// 61 | /// 是否即将秒杀 62 | /// 63 | /// 64 | public bool IsWillMiaoshaing() 65 | { 66 | return MiaoshaInfo != null && MiaoshaInfo.IsWillMiaosha(); 67 | } 68 | 69 | /// 70 | /// 是否处于预约 71 | /// 72 | /// 73 | public bool IsYuyueing() 74 | { 75 | return YuyueInfo != null; 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /JDGuardian/Models/WareInfo.cs: -------------------------------------------------------------------------------- 1 | namespace JDGuardian.Models 2 | { 3 | public class WareInfo 4 | { 5 | public string Wname { get; set; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /JDGuardian/Models/YuyueInfo.cs: -------------------------------------------------------------------------------- 1 | namespace JDGuardian.Models 2 | { 3 | /// 4 | /// 预约 5 | /// 6 | public class YuyueInfo 7 | { 8 | public string BannerUrl { get; set; } 9 | public string BtnText { get; set; } 10 | public string BuyTime { get; set; } 11 | public string CdPrefix { get; set; } 12 | public int Countdown { get; set; } 13 | public int HidePrice { get; set; } 14 | public string HideText { get; set; } 15 | public int Num { get; set; } 16 | public string PlusText { get; set; } 17 | public int PlusType { get; set; } 18 | public string SellWhilePresell { get; set; } 19 | public string ShowDraw { get; set; } 20 | public string ShowPlusTime { get; set; } 21 | public int ShowPromoPrice { get; set; } 22 | public int State { get; set; } 23 | public int SupportOther { get; set; } 24 | public string Type { get; set; } 25 | public string Url { get; set; } 26 | public string UserType { get; set; } 27 | public string Yuyue { get; set; } 28 | public List YuyueRuleText { get; set; } 29 | public string YuyueTime { get; set; } 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /JDGuardian/Program.cs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qedsd/JDGuardian/3773669330345da8cb6ed1da08a840e2c52ff5ff/JDGuardian/Program.cs -------------------------------------------------------------------------------- /JDGuardian/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/launchsettings.json", 3 | "iisSettings": { 4 | "windowsAuthentication": false, 5 | "anonymousAuthentication": true, 6 | "iisExpress": { 7 | "applicationUrl": "http://localhost:56072", 8 | "sslPort": 44384 9 | } 10 | }, 11 | "profiles": { 12 | "JDGuardian": { 13 | "commandName": "Project", 14 | "dotnetRunMessages": true, 15 | "launchBrowser": true, 16 | "launchUrl": "swagger", 17 | "applicationUrl": "https://localhost:7255;http://localhost:5255", 18 | "environmentVariables": { 19 | "ASPNETCORE_ENVIRONMENT": "Development" 20 | } 21 | }, 22 | "IIS Express": { 23 | "commandName": "IISExpress", 24 | "launchBrowser": true, 25 | "launchUrl": "swagger", 26 | "environmentVariables": { 27 | "ASPNETCORE_ENVIRONMENT": "Development" 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /JDGuardian/Services/MailService.cs: -------------------------------------------------------------------------------- 1 | using JDGuardian.Models; 2 | using System.Net; 3 | using System.Net.Mail; 4 | using System.Text; 5 | 6 | namespace JDGuardian.Services 7 | { 8 | public static class MailService 9 | { 10 | public static MailSetting MailSetting { get; set; } 11 | private static SmtpClient smtpClient; 12 | private static SmtpClient SmtpClient 13 | { 14 | get 15 | { 16 | if(smtpClient == null) 17 | { 18 | //指定smtp服务地址(根据发件人邮箱指定对应SMTP服务器地址) 19 | SmtpClient client = new SmtpClient();//格式:smtp.126.com smtp.164.com 20 | smtpClient = client; 21 | client.Host = MailSetting.Host; 22 | //要用587端口 23 | client.Port = 587;//端口 24 | //加密 25 | client.EnableSsl = true; 26 | //通过用户名和密码验证发件人身份 27 | client.Credentials = new NetworkCredential(MailSetting.UserName, MailSetting.Password); 28 | } 29 | return smtpClient; 30 | } 31 | } 32 | private static object locker = new object(); 33 | public static bool SendMail(string to, string subject,string str) 34 | { 35 | lock (locker) 36 | { 37 | if (MailSetting != null) 38 | { 39 | MailMessage mailMsg = new MailMessage();//实例化对象 40 | mailMsg.From = new MailAddress(MailSetting.Address, MailSetting.DisplayName);//源邮件地址和发件人 41 | mailMsg.To.Add(new MailAddress(to));//收件人地址 42 | mailMsg.Subject = subject;//发送邮件的标题 43 | mailMsg.Body = str;//发送邮件的内容 44 | try 45 | { 46 | SmtpClient.Send(mailMsg); 47 | return true; 48 | } 49 | catch (SmtpException ex) 50 | { 51 | Console.WriteLine(ex); 52 | return false; 53 | } 54 | } 55 | else 56 | { 57 | Console.WriteLine("未配置邮箱"); 58 | } 59 | } 60 | return false; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /JDGuardian/Services/StockMonitorService.cs: -------------------------------------------------------------------------------- 1 | namespace JDGuardian.Services 2 | { 3 | public static class StockMonitorService 4 | { 5 | public static Dictionary Monitors = new Dictionary(); 6 | public static string AddStockMonitor(string mail, int span, long skuid, string area) 7 | { 8 | Models.StockMonitor stockMonitor = new Models.StockMonitor(mail,span, skuid,area); 9 | Monitors.Add(stockMonitor.Id, stockMonitor); 10 | TryStartService(); 11 | return stockMonitor.Id; 12 | } 13 | public static bool TryDelMonitor(string id) 14 | { 15 | return Monitors.Remove(id); 16 | } 17 | private static Timer MonitorTimer; 18 | public static void TryStartService() 19 | { 20 | if (MonitorTimer == null) 21 | { 22 | StartService(); 23 | } 24 | } 25 | public static void StartService() 26 | { 27 | MonitorTimer = new Timer(TimerCallback,null,0,1000); 28 | } 29 | public static void TimerCallback(object state) 30 | { 31 | var items = Monitors.Values.Where(p => p.IsTime()).ToList(); 32 | if(items.Any()) 33 | { 34 | Helpers.ThreadHelper.Run(items, (p) => p.Check()); 35 | } 36 | foreach(var p in Monitors.Values) 37 | { 38 | p.Update(); 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /JDGuardian/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /JDGuardian/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | }, 8 | "AllowedHosts": "*", 9 | "MailSetting": { 10 | "Address": "Address", 11 | "DisplayName": "JDGuardian", 12 | "Host": "", 13 | "Port": 0, 14 | "EnableSsl": true, 15 | "UserName": "UserName", 16 | "Password": "Password" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JDGuardian 2 | 京东商品库存、价格监控 3 | 4 | --- 5 | 6 | # 注意!!! 7 | 仅作相关邮件通知,**没有**自动下单功能 8 | 9 | # 概述 10 | 将京东的接口简单二次封装,组合成需要的价格、库存、预约、秒杀监控功能,并将监控情况发送邮件通知。 11 | 12 | 13 | # 已支持功能 14 | - 查看商品信息 15 | - 检查商品是否有库存、处于预约、秒杀中 16 | - 监控商品是否有货,自动发送有货通知邮件 17 | 18 | # 待开发功能 19 | 20 | - 商品开始预约、秒杀监控 21 | - 商品价格变化监控 22 | 23 | 实现方式与监控商品是否有货类似 24 | 25 | --- 26 | 27 | 28 | # 使用 29 | 30 | 项目本质上就是个WebApi,一切操作都是基于swagger来操作。 31 | 32 | ## 环境要求 33 | 34 | 这是一个ASP.NET 6 WebApi项目,仅需要安装.Net 6运行环境,Linux、Windows均支持。 35 | 36 | [.NET 6.0](https://dotnet.microsoft.com/zh-cn/download/dotnet/6.0) 37 | 38 | 下载ASP.NET Core 运行时即可。 39 | 40 | ## 配置发送通知的邮箱 41 | 42 | 发送监控情况需要指定发件人邮箱,在appsettings.json 配置文件中MailSetting节点配置。 43 | 44 | - Address 发件人邮箱地址 45 | - DisplayName 发件人名称 46 | - Host 发件人Smtp服务地址 47 | - Port Smtp端口 48 | - EnableSsl 是否使用SSL通讯 49 | - UserName 发件人账号名(一般为邮箱地址) 50 | - Password 发件人账号密码(或邮箱授权码) 51 | 52 | appsettings.json示例 53 | ``` 54 | { 55 | "Logging": { 56 | "LogLevel": { 57 | "Default": "Information", 58 | "Microsoft.AspNetCore": "Warning" 59 | } 60 | }, 61 | "AllowedHosts": "*", 62 | "MailSetting": { 63 | "Address": "xxx@qq.com", 64 | "DisplayName": "JDGuardian", 65 | "Host": "smtp.qq.com", 66 | "Port": 587, 67 | "EnableSsl": true, 68 | "UserName": "xxx@qq.com", 69 | "Password": "qwertyuiop" 70 | } 71 | } 72 | 73 | 74 | ``` 75 | 76 | ## 运行 77 | 执行JDGuardian即可,然后按以下地址进入swagger 78 | 79 | - https://localhost:5003/swagger/index.html 80 | - http://localhost:5002/swagger/index.html 81 | 82 | swagger接口均已写明注释,不再重述。仅挑几个特别说明一下 83 | 84 | ### area 地区 85 | 京东查看商品价格、库存等信息都必须带上收货地址,这个area是一个地区id组合字符串,表示省、市、区等组合起来的地址,如19_1601_50258_51885表示了广东_广州_白云区_城区这个地址。 86 | 87 | 从接口area接口可以逐步获取到每个地区的下属地区,一直到没有下属地区为止,然后把前面每一步拿到的地区id拼起来就是一个有效的地区参数,但不用带上国家(至少中国不需要)。 88 | 89 | ### skuId 商品id 90 | 商品在京东内部的唯一id 91 | 92 | ### 添加库存监控参数 93 | 此时post过去的数据中,mail表示如果有货时将把通知邮件发到这个地址,span表示轮询间隔(检查是否有货实际上是不停地调用库存接口check/isStock),单位为秒。 94 | 95 | ``` 96 | { 97 | "mail": "qq@qq.com", 98 | "span": 60, 99 | "skuId": 100016148888, 100 | "area": "19_1601_50258_51885" 101 | } 102 | ``` 103 | 104 | 如上参数即表示商品100016148888如果在19_1601_50258_51885这个地区有货,将把通知邮件发送到邮箱qq@qq.com,每次间隔60s判断一次是否有货。 105 | 106 | 发送请求后,如果返回一个GUID字符串,则为添加成功,此时就会开始按指定间隔监控是否有货。 107 | 108 | --- 109 | 110 | # 一些我用到的商品skuId 111 | - XSX 100011513445 112 | - XSX & 黑色游戏耳机套装 100016346817 113 | - Xbox Series 手柄 磨砂黑 100016148888 114 | - Xbox Series 手柄 磨砂黑 USB-C线缆 100016148810 115 | - Xbox Series 手柄 磨砂黑 无线适配器 100016148908 116 | - Xbox Series 手柄 冰雪白 100016148864 117 | - Xbox Series 手柄 波动蓝 100009153053 118 | - Xbox Series 手柄 极光蓝 100013915485 119 | - Xbox Series 手柄 电光黄 100021197380 120 | - Xbox Series 手柄 锦鲤红 100017833496 121 | - Xbox 无线适配器 5028827 122 | 123 | 来源于[WX-JDCheck](https://github.com/Gavin914/WX-JDCheck) 124 | 125 | 巨硬你能不能硬一回?! 126 | --------------------------------------------------------------------------------