UserService.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2020/6/20
  6. * Time: 下午1:35
  7. */
  8. namespace app\source\service;
  9. use app\common\library\Redis;
  10. use app\main\constants\CacheConstants;
  11. use app\main\constants\ServiceApiConstants;
  12. use app\main\service\ApiService;
  13. use app\main\service\BaseService;
  14. use app\source\model\User;
  15. use app\source\model\UserUpdate;
  16. /**
  17. * 用户信息处理
  18. * Class UserService
  19. * @package app\source\service
  20. */
  21. class UserService extends BaseService
  22. {
  23. /**
  24. * @var UserService
  25. */
  26. protected static $self = NULL;
  27. /**
  28. * @var User
  29. */
  30. public $user;
  31. /**
  32. * @return $this|UserService
  33. */
  34. public static function instance()
  35. {
  36. if (self::$self == NULL) {
  37. self::$self = new self();
  38. self::$self->user = new User();
  39. }
  40. return self::$self;
  41. }
  42. /**
  43. * 获取用户信息
  44. * @param int $user_id
  45. * @return User
  46. */
  47. public function getUserInfo(int $user_id)
  48. {
  49. if (!$this->user->id || ($this->user->id != $user_id)) {
  50. $data = ApiService::instance()->getDataFromApi(ServiceApiConstants::USER_FIND_BY_UID, [$this->user->pk => $user_id])->data;
  51. $this->user->bind($data);
  52. }
  53. return $this->user;
  54. }
  55. /**
  56. * 获取用户
  57. * @param $channel_id
  58. * @param $openid
  59. * @return User
  60. */
  61. public function getUserInfoByChannelOpenid($channel_id, $openid)
  62. {
  63. if (!$this->user->id || ($this->user->openid != $openid || $this->user->channel_id != $channel_id)) {
  64. $data = ApiService::instance()->getDataFromApi(ServiceApiConstants::USER_FIND_BY_OPENID, ['openid' => $openid, 'channel_id' => $channel_id])->data;
  65. $this->user->bind($data);
  66. }
  67. return $this->user;
  68. }
  69. /**
  70. * 更新用户
  71. * @param $userUpdate UserUpdate
  72. * @return User
  73. */
  74. public function updateUser(UserUpdate $userUpdate)
  75. {
  76. $data = ApiService::instance()->getDataFromApi(ServiceApiConstants::USER_UPDATE, $userUpdate->toArray())->data;
  77. $cache = CacheConstants::getUserCacheKey($userUpdate->id);
  78. Redis::instance()->del($cache);
  79. return $this->user->bind($data);
  80. }
  81. /**
  82. * 创建用户
  83. * @param $channel_id
  84. * @param $openid
  85. * @return User
  86. */
  87. public function createUser($channel_id, $openid)
  88. {
  89. $data = ApiService::instance()->getDataFromApi(ServiceApiConstants::USER_ADD, ['openid' => $openid, 'channel_id' => $channel_id])->data;
  90. return $this->user->bind($data);
  91. }
  92. }