LoginPasswordLogic.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace app\api\logic;
  3. use app\common\basics\Logic;
  4. use app\common\model\Client_;
  5. use app\common\model\user\User;
  6. class LoginPasswordLogic extends Logic
  7. {
  8. /**
  9. * Notes: 忘记密码(找回密码)
  10. * @param $post
  11. * @author 段誉(2021/6/23 17:01)
  12. * @return array|bool
  13. */
  14. public static function forget($post)
  15. {
  16. try {
  17. $client = self::getClient($post);
  18. $account = User::where(['mobile' => $post['mobile'], 'del' => 0])
  19. ->find();
  20. if (!$account) {
  21. throw new \Exception('账号不存在');
  22. }
  23. //更新密码
  24. $password = create_password($post['password'], $account['salt']);//生成密码
  25. if ($account['password'] == $password) {
  26. throw new \Exception('密码未改动');
  27. }
  28. $data = [
  29. 'password' => $password,
  30. 'update_time' => time(),
  31. ];
  32. User::where(['id' => $account['id'], 'del' => 0])
  33. ->update($data);
  34. $token = LoginLogic::createSession($account['id'], $client);
  35. return ['token' => $token];
  36. } catch (\Exception $e) {
  37. self::$error = $e->getMessage();
  38. return false;
  39. }
  40. }
  41. /***
  42. * @param $post
  43. * @return int
  44. */
  45. public static function getClient($post)
  46. {
  47. $client = $post['client'] ?? Client_::mnp;
  48. $client_arr = array_keys(Client_::getClient(true));
  49. if (in_array($client, $client_arr)) {
  50. return $client;
  51. }
  52. return Client_::mnp;
  53. }
  54. }