UserApi.php 6.6 KB

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