UserApi.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. public function checkPwd() {
  58. if (!$this->_checkPwd() && $this->usertype == 2 && in_array($this->info["source"], [1, 2])) {
  59. //source3是新系统注册,企业号登录密码不正确时,验证一下是不是聚财账号
  60. if ($this->info["source"] == 1) {
  61. if (!$this->info["jUsername"]) {
  62. return false;
  63. }
  64. }
  65. try {
  66. $res = JucaiApi::login($this->username, $this->password, 1);
  67. $resObj = json_decode($res);
  68. if ($resObj->state == 1) {
  69. return true;
  70. }
  71. return false;
  72. } catch (think\exception $e) {
  73. return false;
  74. }
  75. }
  76. }
  77. /**
  78. * 检查密码
  79. * @return type
  80. */
  81. public function _checkPwd() {
  82. switch ($this->usertype) {
  83. case 1:
  84. $salt = hash("md5", $this->info["salt"], true);
  85. $password = simple_hash("md5", $this->password, $salt, 1024);
  86. break;
  87. case 2:
  88. case 3:
  89. $password = hash("md5", $this->password);
  90. break;
  91. }
  92. return $password == $this->info["password"];
  93. }
  94. public function checkState() {
  95. switch ($this->usertype) {
  96. case 1:
  97. return false;
  98. break;
  99. case 2:
  100. if ($this->info['active'] != 1) {
  101. return "账号被冻结, 冻结原因为: {$this->info['activeMsg']}";
  102. }
  103. if ($this->info['checkState'] == 1 || $this->info['checkState'] == 4) {
  104. return "账号需要后台管理人员审核通过后才能登陆,请耐心等待!";
  105. }
  106. if ($this->info['checkState'] == 2) {
  107. $temp = [];
  108. $temp['uid'] = $this->info['id'];
  109. $temp['msg'] = "账号审核不通过,原因是:{$this->info['checkMsg']}";
  110. session('temp', $temp);
  111. return "账号审核不通过,原因是:{$this->info['checkMsg']}";
  112. }
  113. return false;
  114. break;
  115. case 3:
  116. return false;
  117. break;
  118. }
  119. }
  120. /**
  121. * 设置冻结与否
  122. * @param type $freezetype
  123. */
  124. public function setFreeze($freezetype = FREEZE_NO) {
  125. $this->info->freeze = $freezetype;
  126. if ($freezetype == FREEZE_NO) {
  127. $this->info->errorCount = null;
  128. $this->info->freezeTime = null;
  129. } else {
  130. $this->info->freezeTime = strtotime(sprintf("+%d minutes", FREEZETIME));
  131. }
  132. $this->info->save();
  133. }
  134. public function setSession() {
  135. session('temp', null);
  136. session("isCaptcha", null);
  137. session('login_fail', null);
  138. $user = $this->getUserInfo();
  139. switch ($this->usertype) {
  140. case 1:
  141. session("user", [
  142. "uid" => $user["id"],
  143. "roleid" => $user["roleid"],
  144. "companyId" => $user["companyId"],
  145. "companyName" => $this->getCompany()["name"],
  146. "account" => $user["account"],
  147. "name" => $user["name"],
  148. "avatar" => $user["avatar"],
  149. "sex" => $user["sex"],
  150. "rolename" => $this->getRole()["name"],
  151. "usertype" => $this->usertype,
  152. "type" => $user['type']
  153. ]);
  154. $loginData = [];
  155. $loginData["logname"] = "登录日志";
  156. $loginData["userid"] = $user["id"];
  157. $loginData["createtime"] = date("Y-m-d H:i:s");
  158. $loginData["succeed"] = "成功";
  159. $loginData["ip"] = get_client_ip();
  160. \think\facade\Db::table("sys_login_log")->insert($loginData);
  161. break;
  162. case 2:
  163. session("user", [
  164. "uid" => $user["id"],
  165. "account" => $user["username"],
  166. "name" => $user["name"],
  167. "avatar" => $user["headPortrait"],
  168. "rolename" => "企业用户",
  169. "usertype" => $this->usertype,
  170. "type" => $user["type"]
  171. ]);
  172. break;
  173. case 3:
  174. session("user", [
  175. "uid" => $user["id"],
  176. "account" => $user["username"],
  177. "name" => $user["name"],
  178. "avatar" => $user["headPortrait"],
  179. "sex" => $user["sex"],
  180. "rolename" => "个人用户",
  181. "usertype" => $this->usertype,
  182. "type" => $user["type"]
  183. ]);
  184. break;
  185. }
  186. }
  187. }