AccountService.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using LeaRun.Application.Entity;
  2. using LeaRun.Application.IService;
  3. using LeaRun.Data;
  4. using LeaRun.Data.Repository;
  5. using LeaRun.Util;
  6. using LeaRun.Util.Extension;
  7. using System;
  8. using System.Data.Common;
  9. using System.Linq;
  10. namespace LeaRun.Application.Service
  11. {
  12. /// <summary>
  13. /// 版 本
  14. /// Copyright (c) 2013-2016 上海力软信息技术有限公司
  15. /// 创建人:佘赐雄
  16. /// 日 期:2016.05.11 16:23
  17. /// 描 述:注册账户
  18. /// </summary>
  19. public class AccountService : RepositoryFactory<AccountEntity>, IAccountService
  20. {
  21. /// <summary>
  22. /// 登录验证
  23. /// </summary>
  24. /// <param name="mobileCode">用户名</param>
  25. /// <param name="password">密码</param>
  26. /// <returns></returns>
  27. public AccountEntity CheckLogin(string mobileCode, string password)
  28. {
  29. var expression = LinqExtensions.True<AccountEntity>();
  30. expression = expression.And(t => t.MobileCode == mobileCode);
  31. expression = expression.And(t => t.DeleteMark == 0);
  32. return this.BaseRepository("AccountDb").FindEntity(expression);
  33. }
  34. /// <summary>
  35. /// 获取验证码
  36. /// </summary>
  37. /// <param name="mobileCode">手机号码</param>
  38. /// <returns>返回6位数验证码</returns>
  39. public string GetSecurityCode(string mobileCode)
  40. {
  41. if (this.BaseRepository("AccountDb").IQueryable(t => t.MobileCode == mobileCode).Count() == 0)
  42. {
  43. //验证每个IP 不能获取超过5次
  44. if (this.BaseRepository("AccountDb").IQueryable(t => t.IPAddress == Net.Ip).Count() >= 5)
  45. {
  46. throw new Exception("获取验证码失败。");
  47. }
  48. AccountEntity accountEntity = new AccountEntity();
  49. accountEntity.AccountId = Guid.NewGuid().ToString();
  50. accountEntity.MobileCode = mobileCode;
  51. accountEntity.SecurityCode = CommonHelper.RndNum(6);
  52. accountEntity.IPAddress = Net.Ip;
  53. accountEntity.CreateDate = DateTime.Now;
  54. accountEntity.EnabledMark = -1;
  55. this.BaseRepository("AccountDb").Insert(accountEntity);
  56. return accountEntity.SecurityCode;
  57. }
  58. else
  59. {
  60. throw new Exception("手机号已被注册过了。");
  61. }
  62. }
  63. /// <summary>
  64. /// 注册账户
  65. /// </summary>
  66. /// <param name="accountEntity">实体对象</param>
  67. public void Register(AccountEntity accountEntity)
  68. {
  69. var data = this.BaseRepository("AccountDb").FindEntity(t => t.MobileCode == accountEntity.MobileCode);
  70. if (data == null && data.SecurityCode == accountEntity.SecurityCode)
  71. {
  72. throw new Exception("短信验证码不正确。");
  73. }
  74. if (data.RegisterTime != null)
  75. {
  76. throw new Exception("手机号已被注册过了。");
  77. }
  78. accountEntity.AccountId = data.AccountId;
  79. accountEntity.RegisterTime = DateTime.Now;
  80. accountEntity.ExpireTime = DateTime.Now.AddMonths(1);
  81. accountEntity.DeleteMark = 0;
  82. accountEntity.EnabledMark = 1;
  83. this.BaseRepository("AccountDb").Update(accountEntity);
  84. }
  85. /// <summary>
  86. /// 登录限制
  87. /// </summary>
  88. /// <param name="platform">平台(苹果、安卓、PC浏览器)</param>
  89. /// <param name="account">账户</param>
  90. /// <param name="IPAddress">IP地址</param>
  91. /// <param name="iPAddressName">IP所在城市</param>
  92. public void LoginLimit(string platform, string account, string iPAddress, string iPAddressName)
  93. {
  94. //调用存储过程
  95. DbParameter[] parameter =
  96. {
  97. DbParameters.CreateDbParameter("@IPAddress",iPAddress),
  98. DbParameters.CreateDbParameter("@IPAddressName",iPAddressName),
  99. DbParameters.CreateDbParameter("@Account",account),
  100. DbParameters.CreateDbParameter("@Platform",platform),
  101. };
  102. int IsOk = this.BaseRepository("AccountDb").ExecuteByProc("PROC_verify_IPAddress", parameter);
  103. }
  104. }
  105. }