ChannelUserService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App\Services\Channel;
  3. use App\Dao\Channel\ChannelDao;
  4. use App\Dao\Channel\ChannelUserDao;
  5. use App\Libs\Utils;
  6. use App\Consts\ErrorConst;
  7. use App\Exceptions\ApiException;
  8. use Illuminate\Support\Facades\DB;
  9. class ChannelUserService
  10. {
  11. private $salt = 'N&vQA!saRWjR7v#X';
  12. private $channelDao;
  13. private $channelUserDao;
  14. public function __construct(
  15. ChannelDao $channelDao,
  16. ChannelUserDao $channelUserDao
  17. )
  18. {
  19. $this->channelDao = $channelDao;
  20. $this->channelUserDao = $channelUserDao;
  21. }
  22. /**
  23. * @param $account
  24. * @param $password
  25. * @return array
  26. * @throws ApiException
  27. */
  28. public function login($account, $password): array
  29. {
  30. // 密码
  31. $md5Pass = md5($password . $this->salt);
  32. // 获取用户
  33. $user = $this->channelUserDao->getUserByAccountAndPassword($account, $md5Pass);
  34. if (empty($user)) {
  35. Utils::throwError(ErrorConst::USER_IS_NOT_EXIST);
  36. }
  37. // 获取站点列表
  38. $channelUserId = (int)getProp($user, 'id');
  39. $channels = $this->channelDao->getUserChannels($channelUserId);
  40. // 更新登录信息
  41. $uid = getProp($user, 'id');
  42. $nickname = getProp($user, 'nickname');
  43. $token = getProp($user, 'token');
  44. $phone = getProp($user, 'phone');
  45. $updateData = [
  46. 'latest_login_ip' => _getIp(),
  47. 'latest_login_time' => date('Y-m-d H:i:s'),
  48. 'updated_at' => date('Y-m-d H:i:s')
  49. ];
  50. // 初始化登录需要设置新的token
  51. if (empty($token)) {
  52. $token = md5(makeRandStr(32));
  53. $updateData['token'] = $token;
  54. }
  55. $this->channelUserDao->updateUserData(['id' => $uid], $updateData);
  56. $is_master = 0;
  57. $sub_channel_ids = DB::table('channel_user_mappings')->where('master_uid', $uid)->select('sub_channel_id')->get()->pluck('sub_channel_id')->toArray();
  58. if ($sub_channel_ids) $is_master = 1;
  59. return compact('uid', 'nickname', 'token', 'phone', 'channels', 'is_master');
  60. }
  61. /**
  62. * @param $account
  63. * @param $password
  64. * @return bool
  65. * @throws ApiException
  66. */
  67. public function register($account, $password): bool
  68. {
  69. // 获取用户
  70. $user = $this->channelUserDao->getUserByAccount($account);
  71. if ($user) {
  72. Utils::throwError(ErrorConst::USER_IS_EXIST);
  73. }
  74. // 密码
  75. $md5Pass = md5($password . $this->salt);
  76. return $this->channelUserDao->createUser($account, $md5Pass);
  77. }
  78. /**
  79. * 退出登录
  80. *
  81. * @param $uid
  82. * @return bool
  83. */
  84. public function logout($uid): bool
  85. {
  86. if (empty($uid)) {
  87. return true;
  88. }
  89. return $this->channelUserDao->updateUserData(['id' => $uid], ['token' => '', 'updated_at' => date('Y-m-d H:i:s')]);
  90. }
  91. /**
  92. * 修改密码
  93. *
  94. * @param $uid
  95. * @param $newPassword
  96. * @return bool
  97. * @throws ApiException
  98. */
  99. public function resetPassword($uid, $newPassword): bool
  100. {
  101. // 获取用户
  102. $user = $this->channelUserDao->getUserByUid($uid);
  103. if (empty($user)) {
  104. Utils::throwError(ErrorConst::USER_IS_NOT_EXIST);
  105. }
  106. // 更新密码
  107. $newMd5Pass = md5($newPassword . $this->salt);
  108. return $this->channelUserDao->updateUserData(['id' => $uid], [
  109. 'password' => $newMd5Pass,
  110. 'updated_at' => date('Y-m-d H:is')
  111. ]);
  112. }
  113. }