UserApi.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace app\common\api;
  3. use app\admin\model\User;
  4. use app\admin\model\Enterprise;
  5. use app\admin\model\Person;
  6. use app\admin\model\Role;
  7. const MAX_ERROR_TIMES = 5;
  8. const FREEZETIME = 5;
  9. const FREEZE_NO = 1;
  10. const FREEZE_YES = 2;
  11. /**
  12. * Description of UserApi
  13. *
  14. * @author sgq
  15. */
  16. class UserApi {
  17. protected $username, $password, $usertype, $info;
  18. public function __construct($username, $password, $usertype) {
  19. $this->username = $username;
  20. $this->password = $password;
  21. $this->usertype = $usertype;
  22. switch ($usertype) {
  23. case 2:
  24. //企业
  25. $user = Enterprise::where(['username' => $username])->findOrEmpty();
  26. break;
  27. case 3:
  28. //个人
  29. $user = Person::where('username', $username)->findOrEmpty();
  30. break;
  31. default:
  32. //管理员
  33. $user = User::where(['account' => $username])->findOrEmpty();
  34. break;
  35. }
  36. $this->info = $user;
  37. return $this;
  38. }
  39. public function getRole() {
  40. $role = Role::find($this->info["roleid"]);
  41. return $role->toArray();
  42. }
  43. /**
  44. * 返回用户信息
  45. * @return type
  46. */
  47. public function getUserInfo() {
  48. return $this->info->toArray();
  49. }
  50. /**
  51. * 检查密码
  52. * @return type
  53. */
  54. public function checkPwd() {
  55. switch ($this->usertype) {
  56. case 1:
  57. $salt = hash("md5", $this->info["salt"], true);
  58. $password = simple_hash("md5", $this->password, $salt, 1024);
  59. break;
  60. case 2:
  61. case 3:
  62. $password = hash("md5", $this->password);
  63. break;
  64. }
  65. return $password == $this->info["password"];
  66. }
  67. public function checkState(){
  68. switch ($this->usertype){
  69. case 1:
  70. return false;
  71. break;
  72. case 2:
  73. if($this->info['active'] != 1){
  74. return "账号被冻结, 冻结原因为: {$this->info['activeMsg']}";
  75. }
  76. if($this->info['checkState'] == 1 || $this->info['checkState'] == 4){
  77. return "账号需要后台管理人员审核通过后才能登陆,请耐心等待!";
  78. }
  79. if($this->info['checkState'] == 2){
  80. return "账号审核不通过,原因是:{$this->info['checkMsg']}";
  81. }
  82. return false;
  83. break;
  84. case 3:
  85. return false;
  86. break;
  87. }
  88. }
  89. /**
  90. * 设置冻结与否
  91. * @param type $freezetype
  92. */
  93. public function setFreeze($freezetype = FREEZE_NO) {
  94. $this->info->freeze = $freezetype;
  95. if ($freezetype == FREEZE_NO) {
  96. $this->info->errorCount = null;
  97. $this->info->freezeTime = null;
  98. } else {
  99. $this->info->freezeTime = strtotime(sprintf("+%d minutes", FREEZETIME));
  100. }
  101. $this->info->save();
  102. }
  103. public function setSession() {
  104. $user = $this->getUserInfo();
  105. switch ($this->usertype) {
  106. case 1:
  107. session("user", [
  108. "uid" => $user["id"],
  109. "roleid" => $user["roleid"],
  110. "account" => $user["account"],
  111. "name" => $user["name"],
  112. "avatar" => $user["avatar"],
  113. "sex" => $user["sex"],
  114. "rolename" => $this->getRole()["name"],
  115. "usertype" => $this->usertype
  116. ]);
  117. break;
  118. case 2:
  119. session("user", [
  120. "uid" => $user["id"],
  121. "account" => $user["username"],
  122. "name" => $user["name"],
  123. "avatar" => $user["headPortrait"],
  124. "rolename" => "企业用户",
  125. "usertype" => $this->usertype,
  126. "type" => $user["type"]
  127. ]);
  128. break;
  129. case 3:
  130. session("user", [
  131. "uid" => $user["id"],
  132. "account" => $user["username"],
  133. "name" => $user["name"],
  134. "avatar" => $user["headPortrait"],
  135. "sex" => $user["sex"],
  136. "rolename" => "个人用户",
  137. "usertype" => $this->usertype,
  138. "type" => $user["type"]
  139. ]);
  140. break;
  141. }
  142. }
  143. }