UserApi.php 5.2 KB

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