User.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\model\ConfigModel;
  5. use app\model\SettingModel;
  6. use app\model\TokenModel;
  7. use app\model\UserModel;
  8. use think\facade\Cache;
  9. use think\facade\Log;
  10. use think\facade\View;
  11. class User extends BaseController
  12. {
  13. protected $qq_bind_mode = false;
  14. public function login(): \think\response\Json
  15. {
  16. $user = $this->request->post('username', '0');
  17. $pass = $this->request->post('password', '0');
  18. $user = trim($user);
  19. $pass = trim($pass);
  20. $info = UserModel::where('mail', $user)->find();
  21. if (Cache::get('login.' . $user)) {
  22. return $this->error('账号已被安全锁定,您可以修改密码然后登录');
  23. }
  24. if (!$info) {
  25. return $this->error('账号不存在');
  26. }
  27. if ($info['login_fail_count'] == 10) {
  28. Cache::set('login.' . $user, 'lock', 7200);
  29. $info->login_fail_count = 0;
  30. $info->save();
  31. return $this->error('账号已被锁定2小时');
  32. }
  33. if ($info['password'] != md5($pass)) {
  34. $info->login_fail_count += 1;
  35. $info->save();
  36. return $this->error('账号不存在或密码错误');
  37. }
  38. if ($info['status'] === 1) {
  39. return $this->error('账号已被冻结');
  40. }
  41. $auth = $this->refreshToken($info);
  42. $info->login_ip = getRealIp();
  43. $info->login_time = date('Y-m-d H:i:s');
  44. $info->login_fail_count = 0;//登陆成功将失败次数归零
  45. $info->save();
  46. return $this->success('登录成功', $auth);
  47. }
  48. private function refreshToken($info): array
  49. {
  50. $token = renderToken($info['id']);
  51. $agent = $this->request->header('User-Agent');
  52. $agent = mb_substr($agent, 0, 250);
  53. $auth = ['user_id' => $info['id'], 'token' => $token, 'create_time' => time(), 'ip' => getRealIp(), 'user_agent' => $agent];
  54. if (isset($info['access_token'])) {
  55. $auth['access_token'] = $info['access_token'];
  56. }
  57. TokenModel::insert($auth);
  58. unset($auth['user_agent']);
  59. unset($auth['access_token']);
  60. unset($auth['ip']);
  61. return $auth;
  62. }
  63. function register(): \think\response\Json
  64. {
  65. $user = $this->request->post('username', false);
  66. $pass = $this->request->post('password', false);
  67. $code = $this->request->post('code', '0000');
  68. if ($user && $pass) {
  69. $user = trim($user);
  70. $pass = trim($pass);
  71. if (!validateEmail($user)) {
  72. return $this->error('邮箱格式错误');
  73. }
  74. if (strlen($pass) < 6) {
  75. return $this->error('密码过短');
  76. }
  77. $cacheCode = Cache::get('code' . $user);
  78. if (!$cacheCode || $cacheCode != $code) {
  79. return $this->error('验证码错误');
  80. }
  81. if (UserModel::where('mail', $user)->field('id,mail')->find()) {
  82. return $this->error('账号已存在');
  83. }
  84. $add = UserModel::insert(['mail' => $user, 'password' => md5($pass), 'create_time' => date('Y-m-d H:i:s'), 'register_ip' => getRealIp()]);
  85. if ($add) {
  86. Cache::delete('code' . $user);
  87. return $this->success('ok');
  88. }
  89. }
  90. return $this->error('注册失败');
  91. }
  92. public function forgetPass(): \think\response\Json
  93. {
  94. $user = $this->request->post('username', false);
  95. $pass = $this->request->post('password', false);
  96. $code = $this->request->post('code', '0000');
  97. if ($user && $pass) {
  98. $user = trim($user);
  99. $pass = trim($pass);
  100. if (!validateEmail($user)) {
  101. return $this->error('邮箱格式错误');
  102. }
  103. if (strlen($pass) < 6) {
  104. return $this->error('密码过短');
  105. }
  106. $info = UserModel::where('mail', $user)->field('id,mail')->find();
  107. if (!$info) {
  108. return $this->error('账号不存在');
  109. }
  110. $cacheCode = Cache::get('code' . $user);
  111. if ($cacheCode && $cacheCode == $code) {
  112. $info->password = md5($pass);
  113. $add = $info->save();
  114. if ($add) {
  115. TokenModel::where('user_id', $info['id'])->delete(); //删除所有登录记录
  116. Cache::delete('login.' . $user);
  117. return $this->success('ok');
  118. }
  119. } else {
  120. return $this->error('验证码错误');
  121. }
  122. }
  123. return $this->error('修改失败');
  124. }
  125. function newMail(): \think\response\Json
  126. {
  127. $userinfo = $this->getUser(true);
  128. $user = $this->request->post('mail', false);
  129. $code = $this->request->post('code', false);
  130. if ($user && $code) {
  131. $user = trim($user);
  132. if (!validateEmail($user)) {
  133. return $this->error('邮箱格式错误');
  134. }
  135. $cacheCode = Cache::get('code' . $user);
  136. if ($cacheCode && $cacheCode == $code) {
  137. $info = UserModel::where('mail', $user)->field('id,mail')->find();
  138. if ($info) {
  139. return $this->error('该邮箱已被使用!');
  140. }
  141. $info = UserModel::where('id', $userinfo['user_id'])->field('id,mail')->find();
  142. $info->mail = $user;
  143. $info->save();
  144. Cache::delete('code' . $user);
  145. return $this->success('修改成功');
  146. } else {
  147. return $this->error('验证码错误');
  148. }
  149. }
  150. return $this->error('请认真填写表单');
  151. }
  152. function loginOut(): \think\response\Json
  153. {
  154. $user = $this->getUser();
  155. if ($user) {
  156. TokenModel::where('user_id', $user['user_id'])->where('token', $user['token'])->delete();
  157. }
  158. return $this->success('ok');
  159. }
  160. public function get(): \think\response\Json
  161. {
  162. $info = $this->getUser(true);
  163. if ($info) {
  164. $info = UserModel::field('id,mail,manager,nickname,avatar,qq_open_id')->find($info['user_id']);
  165. if ($info['qq_open_id']) {
  166. $info['qqBind'] = true;
  167. unset($info['qq_open_id']);
  168. }
  169. return $this->success('ok', $info);
  170. }
  171. return $this->error('获取失败');
  172. }
  173. public function unbindQQ(): \think\response\Json
  174. {
  175. $info = $this->getUser(true);
  176. if ($info) {
  177. $info = UserModel::field('id,mail,manager,nickname,avatar,qq_open_id')->find($info['user_id']);
  178. if (empty($info->mail)) {
  179. return $this->error("请先绑定邮箱后再解绑");
  180. }
  181. $info->qq_open_id = "";
  182. $info->save();
  183. }
  184. return $this->success('解绑成功', $info);
  185. }
  186. public function updateInfo(): \think\response\Json
  187. {
  188. $info = $this->getUser(true);
  189. $field = $this->request->post('field', false);
  190. $value = $this->request->post('value', false);
  191. //允许修改的字段
  192. $allow = ['nickname', 'avatar'];
  193. if ($info && $field && $value && in_array($field, $allow)) {
  194. UserModel::where('id', $info['user_id'])->update([$field => $value]);
  195. }
  196. return $this->success('修改成功');
  197. }
  198. function qLogin(): \think\response\Redirect
  199. {
  200. $appId = SettingModel::Config('qq_login_appid', false);
  201. $callback = 'https://' . $this->request->host() . '/qq_login';
  202. $type = $this->request->get('type', '');
  203. $query = [
  204. 'redirect_uri' => $callback,
  205. 'state' => md5(uniqid()),
  206. 'response_type' => 'code',
  207. 'scope' => 'get_user_info,list_album,upload_pic',
  208. 'client_id' => $appId
  209. ];
  210. if ($type === 'bind') {
  211. $query['state'] = $query['state'] . 'bind';
  212. }
  213. $http = http_build_query($query);
  214. return redirect('https://graph.qq.com/oauth2.0/authorize?' . $http);
  215. }
  216. function qq_login(): string
  217. {
  218. $appId = SettingModel::Config('qq_login_appid', false);
  219. $code = $this->request->get('code', false);
  220. $state = $this->request->get('state');
  221. if (strpos($state, 'bind')) {
  222. //绑定模式
  223. $this->qq_bind_mode = true;
  224. }
  225. $callback = 'https://' . $this->request->host() . '/qq_login';
  226. $result = \Axios::http()->get('https://graph.qq.com/oauth2.0/token', [
  227. 'query' => [
  228. 'grant_type' => 'authorization_code',
  229. 'client_id' => $appId,
  230. 'client_secret' => SettingModel::Config('qq_login_appkey', false),
  231. 'code' => $code,
  232. 'redirect_uri' => $callback,
  233. 'fmt' => 'json'
  234. ]
  235. ]);
  236. if ($result->getStatusCode() === 200) {
  237. $content = $result->getBody()->getContents();
  238. $js = \Axios::toJson($content);
  239. if (isset($js['access_token'])) {
  240. $access_token = $js['access_token'];
  241. return $this->getOpenId($access_token);
  242. }
  243. }
  244. return View::fetch('/qq_login_error');
  245. }
  246. //此方法禁止网络访问
  247. private function getOpenId($access_token): string
  248. {
  249. $result = \Axios::http()->get('https://graph.qq.com/oauth2.0/me', [
  250. 'query' => [
  251. 'access_token' => $access_token,
  252. 'fmt' => 'json'
  253. ]
  254. ]);
  255. if ($result->getStatusCode() === 200) {
  256. $content = $result->getBody()->getContents();
  257. $js = \Axios::toJson($content);
  258. if (isset($js['openid'])) {
  259. $openid = $js['openid'];
  260. if ($this->qq_bind_mode) {
  261. //绑定模式
  262. if (UserModel::where('qq_open_id', $openid)->field('id,qq_open_id')->find()) {
  263. return View::fetch('/qq_login_error');
  264. }
  265. //如果openid数据库不存在说明QQ没有被绑定过,可以绑定
  266. $this->BindQQ($openid);//绑定后需要替换Token,不然之前的QQ登录会失效
  267. }
  268. $info = UserModel::where('qq_open_id', $openid)->find();
  269. if (!$info) {//不存在就创建一个新用户,如果上一个步骤绑定成功的话,是不可能进入此步骤的
  270. UserModel::insert(['mail' => '', 'password' => md5(time()), 'create_time' => date('Y-m-d H:i:s'), 'register_ip' => getRealIp(), 'qq_open_id' => $openid]);
  271. $info = UserModel::where('qq_open_id', $openid)->find();
  272. $this->getUserOpenInfo($access_token, $openid);//获取一些用户的默认信息
  273. }
  274. if ($info) {//如果用户存在
  275. $info->login_ip = getRealIp();
  276. $info->login_time = date('Y-m-d H:i:s');
  277. $info->login_fail_count = 0;//登陆成功将失败次数归零
  278. $info->save();
  279. $info['access_token'] = $access_token;
  280. $auth = $this->refreshToken($info);
  281. if ($info['status'] === 1) {
  282. return View::fetch('/qq_login_error');
  283. }
  284. return View::fetch('/qq_login', ['info' => $auth]);
  285. }
  286. }
  287. }
  288. return View::fetch('/qq_login_error');
  289. }
  290. private function BindQQ($qq_open_id)
  291. {
  292. $user = $this->getUser();
  293. if ($user) {
  294. $info = UserModel::where('id', $user['user_id'])->field('id,mail,qq_open_id,password,login_fail_count,login_ip,login_time')->find();
  295. if ($info) {
  296. $info->qq_open_id = $qq_open_id;
  297. $info->save();
  298. }
  299. }
  300. }
  301. private function getUserOpenInfo($access_token, $openid)
  302. {
  303. $result = \Axios::http()->get('https://graph.qq.com/user/get_user_info', [
  304. 'query' => [
  305. 'openid' => $openid,
  306. 'oauth_consumer_key' => SettingModel::Config('qq_login_appid', false),
  307. 'access_token' => $access_token
  308. ]
  309. ]);
  310. if ($result->getStatusCode() === 200) {
  311. $content = $result->getBody()->getContents();
  312. $js = \Axios::toJson($content);
  313. if ($js['ret'] === 0) {
  314. UserModel::where('qq_open_id', $openid)->update(['nickname' => $js['nickname'], 'avatar' => $js['figureurl_qq_1']]);
  315. }
  316. }
  317. }
  318. }