UserApi.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. $where = [];
  24. switch ($usertype) {
  25. case 2:
  26. //企业
  27. $name1 = str_replace(["(", ")"], ["(", ")"], $username);
  28. $name2 = str_replace(["(", ")"], ["(", ")"], $username);
  29. $where[] = ["username", "in", [$name1, $name2]];
  30. $where[] = ["delete", "=", 0];
  31. $user = Enterprise::where($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. if ($this->info['active'] == 2) {
  132. return "账号被冻结, 冻结原因为: {$this->info['activeMsg']}";
  133. }
  134. return false;
  135. }
  136. }
  137. /**
  138. * 设置冻结与否
  139. * @param type $freezetype
  140. */
  141. public function setFreeze($freezetype = FREEZE_NO) {
  142. $this->info->freeze = $freezetype;
  143. if ($freezetype == FREEZE_NO) {
  144. $this->info->errorCount = null;
  145. $this->info->freezeTime = null;
  146. } else {
  147. $this->info->freezeTime = strtotime(sprintf("+%d minutes", FREEZETIME));
  148. }
  149. $this->info->save();
  150. }
  151. public function setSession() {
  152. session('temp', null);
  153. session("isCaptcha", null);
  154. session('login_fail', null);
  155. $user = $this->getUserInfo();
  156. switch ($this->usertype) {
  157. case 1:
  158. session("user", [
  159. "uid" => $user["id"],
  160. "roleid" => $user["roleid"],
  161. "companyId" => $user["companyId"],
  162. "companyName" => $this->getCompany()["name"],
  163. "account" => $user["account"],
  164. "name" => $user["name"],
  165. "avatar" => $user["avatar"],
  166. "sex" => $user["sex"],
  167. "rolename" => $this->getRole()["name"],
  168. "usertype" => $this->usertype,
  169. "type" => $user['type']
  170. ]);
  171. $loginData = [];
  172. $loginData["logname"] = "登录日志";
  173. $loginData["userid"] = $user["id"];
  174. $loginData["createtime"] = date("Y-m-d H:i:s");
  175. $loginData["succeed"] = "成功";
  176. $loginData["ip"] = get_client_ip();
  177. \think\facade\Db::table("sys_login_log")->insert($loginData);
  178. break;
  179. case 2:
  180. session("user", [
  181. "uid" => $user["id"],
  182. "account" => $user["username"],
  183. "name" => $user["name"],
  184. "avatar" => $user["headPortrait"],
  185. "rolename" => "企业用户",
  186. "usertype" => $this->usertype,
  187. "type" => $user["type"],
  188. "isGeneral" => $user["isGeneral"],
  189. "medicalCommunityId" => $user["medicalCommunityId"]
  190. ]);
  191. break;
  192. case 3:
  193. session("user", [
  194. "uid" => $user["id"],
  195. "account" => $user["username"],
  196. "name" => $user["name"],
  197. "avatar" => $user["headPortrait"],
  198. "sex" => $user["sex"],
  199. "rolename" => "个人用户",
  200. "usertype" => $this->usertype,
  201. "type" => $user["type"],
  202. "idCard" => $user["idCard"],
  203. "phone" => $user["phone"],
  204. "address" => $user["address"],
  205. "email" => $user["email"]
  206. ]);
  207. break;
  208. }
  209. }
  210. }