UserApi.php 5.3 KB

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