UserApi.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. $where = [];
  27. $name1 = str_replace(["(", ")"], ["(", ")"], $username);
  28. $name2 = str_replace(["(", ")"], ["(", ")"], $username);
  29. $where[] = ["username", "=", $name1];
  30. $where[] = ["username", "=", $name2];
  31. $user = Enterprise::whereOr($where)->findOrEmpty();
  32. break;
  33. case 3:
  34. //个人
  35. $user = Person::where('username', $username)->findOrEmpty();
  36. break;
  37. default:
  38. //管理员
  39. $where[] = ["account", "=", $username];
  40. $where[] = ["status", "<>", 3];
  41. $user = User::where($where)->findOrEmpty();
  42. break;
  43. }
  44. $this->info = $user;
  45. return $this;
  46. }
  47. public function getRole() {
  48. $role = Role::findOrEmpty($this->info["roleid"]);
  49. return $role->toArray();
  50. }
  51. public function getCompany() {
  52. $company = \app\common\model\Company::findOrEmpty($this->info["companyId"])->toArray();
  53. return $company;
  54. }
  55. /**
  56. * 返回用户信息
  57. * @return type
  58. */
  59. public function getUserInfo() {
  60. return $this->info->toArray();
  61. }
  62. public function checkPwd() {
  63. if ($this->_checkPwd()) {
  64. return true;
  65. } else {
  66. if ($this->usertype == 2 && in_array($this->info["source"], [1, 2])) {
  67. //source3是新系统注册,企业号登录密码不正确时,验证一下是不是聚财账号
  68. if ($this->info["source"] == 1) {
  69. if (!$this->info["jUsername"]) {
  70. return false;
  71. }
  72. }
  73. try {
  74. $res = JucaiApi::login($this->username, $this->password, 1);
  75. $resObj = json_decode($res);
  76. if ($resObj->state == 1) {
  77. return true;
  78. }
  79. return false;
  80. } catch (think\exception $e) {
  81. return false;
  82. }
  83. }
  84. return false;
  85. }
  86. }
  87. /**
  88. * 检查密码
  89. * @return type
  90. */
  91. public function _checkPwd() {
  92. switch ($this->usertype) {
  93. case 1:
  94. $salt = hash("md5", $this->info["salt"], true);
  95. $password = simple_hash("md5", $this->password, $salt, 1024);
  96. break;
  97. case 2:
  98. case 3:
  99. $password = hash("md5", $this->password);
  100. break;
  101. }
  102. return $password == $this->info["password"];
  103. }
  104. public function checkState() {
  105. switch ($this->usertype) {
  106. case 1:
  107. return false;
  108. break;
  109. case 2:
  110. if ($this->info['active'] != 1) {
  111. return "账号被冻结, 冻结原因为: {$this->info['activeMsg']}";
  112. }
  113. if ($this->info["checkState"] == 3) {
  114. return false;
  115. } else if ($this->info["checkState"] == 6) {
  116. //6是初审状态,待复审
  117. return "您的账号正在审核中,请耐心等待!";
  118. } else if (in_array($this->info['checkState'], [1, 4])) {
  119. return "账号需要后台管理人员审核通过后才能登陆,请耐心等待!";
  120. } else if (in_array($this->info['checkState'], [2, 5])) {
  121. $temp = [];
  122. $temp['uid'] = $this->info['id'];
  123. $temp['msg'] = "账号审核不通过,原因是:{$this->info['checkMsg']}";
  124. session('temp', $temp);
  125. return "账号审核不通过,原因是:{$this->info['checkMsg']}";
  126. } else {
  127. return "账户状态未知,登录失败";
  128. }
  129. break;
  130. case 3:
  131. return false;
  132. break;
  133. }
  134. }
  135. /**
  136. * 设置冻结与否
  137. * @param type $freezetype
  138. */
  139. public function setFreeze($freezetype = FREEZE_NO) {
  140. $this->info->freeze = $freezetype;
  141. if ($freezetype == FREEZE_NO) {
  142. $this->info->errorCount = null;
  143. $this->info->freezeTime = null;
  144. } else {
  145. $this->info->freezeTime = strtotime(sprintf("+%d minutes", FREEZETIME));
  146. }
  147. $this->info->save();
  148. }
  149. public function setSession() {
  150. session('temp', null);
  151. session("isCaptcha", null);
  152. session('login_fail', null);
  153. $user = $this->getUserInfo();
  154. switch ($this->usertype) {
  155. case 1:
  156. session("user", [
  157. "uid" => $user["id"],
  158. "roleid" => $user["roleid"],
  159. "companyId" => $user["companyId"],
  160. "companyName" => $this->getCompany()["name"],
  161. "account" => $user["account"],
  162. "name" => $user["name"],
  163. "avatar" => $user["avatar"],
  164. "sex" => $user["sex"],
  165. "rolename" => $this->getRole()["name"],
  166. "usertype" => $this->usertype,
  167. "type" => $user['type']
  168. ]);
  169. $loginData = [];
  170. $loginData["logname"] = "登录日志";
  171. $loginData["userid"] = $user["id"];
  172. $loginData["createtime"] = date("Y-m-d H:i:s");
  173. $loginData["succeed"] = "成功";
  174. $loginData["ip"] = get_client_ip();
  175. \think\facade\Db::table("sys_login_log")->insert($loginData);
  176. break;
  177. case 2:
  178. session("user", [
  179. "uid" => $user["id"],
  180. "account" => $user["username"],
  181. "name" => $user["name"],
  182. "avatar" => $user["headPortrait"],
  183. "rolename" => "企业用户",
  184. "usertype" => $this->usertype,
  185. "type" => $user["type"]
  186. ]);
  187. break;
  188. case 3:
  189. session("user", [
  190. "uid" => $user["id"],
  191. "account" => $user["username"],
  192. "name" => $user["name"],
  193. "avatar" => $user["headPortrait"],
  194. "sex" => $user["sex"],
  195. "rolename" => "个人用户",
  196. "usertype" => $this->usertype,
  197. "type" => $user["type"]
  198. ]);
  199. break;
  200. }
  201. }
  202. }