UserApi.php 4.8 KB

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