UserService.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Services\Goodnight;
  3. use App\Models\Goodnight\SubscribeModel;
  4. use App\Models\Goodnight\UserModel;
  5. use App\Services\Service;
  6. /**
  7. * Class UserService
  8. * @package App\Services\Goodnight
  9. */
  10. class UserService extends Service
  11. {
  12. /**
  13. * 获取用户信息
  14. * @param \App\Models\User\UserModel $user
  15. */
  16. public function getUser(&$user)
  17. {
  18. $guser = UserModel::firstOrCreate(['uid' => $user->uid]);
  19. UserModel::where('uid', $guser->uid)->update(['login_at' => time()]);
  20. $user->like_sex = $guser->like_sex ?? 0;
  21. $user->cover = $guser->cover ?? "";
  22. $user->gnight_coin = $guser->gnight_coin ?? 0;
  23. $user->like_tone_1 = $guser->like_tone_1 ?? "";
  24. $user->like_tone_2 = $guser->like_tone_2 ?? "";
  25. $user->like_tone_3 = $guser->like_tone_3 ?? "";
  26. $user->subscribed_at = $guser->subscribed_at ?? 0;
  27. $user->get_like = $guser->get_like ?? 0;
  28. $user->voice = $guser->voice ?? "";
  29. $user->get_like_headimgurl = json_decode(
  30. empty($guser->get_like_headimgurl) ? json_encode([]) : $guser->get_like_headimgurl,
  31. true
  32. );
  33. $user->today_fish_cnt = 10 - SubscribeModel::where([
  34. ['uid', $user->uid],
  35. ['type', 2],
  36. ])->whereBetween('created_at', [mktime(0, 0, 0), mktime(0, 0, 0) + 86400])->count();
  37. }
  38. /**
  39. * 更新用户配置
  40. * @param $user
  41. * @param array $data
  42. * @return bool
  43. */
  44. public function updateUser(&$user, array $data)
  45. {
  46. $guser = UserModel::firstOrCreate(['uid' => $user->uid]);
  47. isset($data['cover']) && $guser->cover = $data['cover'];
  48. isset($data['like_sex']) && $guser->like_sex = $data['like_sex'];
  49. isset($data['like_tone_1']) && $guser->like_tone_1 = $data['like_tone_1'];
  50. isset($data['like_tone_2']) && $guser->like_tone_2 = $data['like_tone_2'];
  51. isset($data['like_tone_3']) && $guser->like_tone_3 = $data['like_tone_3'];
  52. isset($data['voice']) && $guser->voice = $data['voice'];
  53. if ($guser->save()) {
  54. $user->like_sex = $guser->like_sex;
  55. $user->cover = $guser->cover;
  56. $user->like_tone_1 = $guser->like_tone_1;
  57. $user->like_tone_2 = $guser->like_tone_2;
  58. $user->like_tone_3 = $guser->like_tone_3;
  59. $user->subscribed_at = $guser->subscribed_at;
  60. $user->voice = $guser->voice;
  61. return true;
  62. } else {
  63. return false;
  64. }
  65. }
  66. }