123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2020/6/20
- * Time: 下午1:35
- */
- namespace app\source\service;
- use app\common\library\Redis;
- use app\main\constants\CacheConstants;
- use app\main\constants\ServiceApiConstants;
- use app\main\service\ApiService;
- use app\main\service\BaseService;
- use app\source\model\User;
- use app\source\model\UserUpdate;
- /**
- * 用户信息处理
- * Class UserService
- * @package app\source\service
- */
- class UserService extends BaseService
- {
- /**
- * @var UserService
- */
- protected static $self = NULL;
- /**
- * @var User
- */
- public $user;
- /**
- * @return $this|UserService
- */
- public static function instance()
- {
- if (self::$self == NULL) {
- self::$self = new self();
- self::$self->user = new User();
- }
- return self::$self;
- }
- /**
- * 获取用户信息
- * @param int $user_id
- * @return User
- */
- public function getUserInfo(int $user_id)
- {
- if (!$this->user->id || ($this->user->id != $user_id)) {
- $data = ApiService::instance()->getDataFromApi(ServiceApiConstants::USER_FIND_BY_UID, [$this->user->pk => $user_id])->data;
- $this->user->bind($data);
- }
- return $this->user;
- }
- /**
- * 获取用户
- * @param $channel_id
- * @param $openid
- * @return User
- */
- public function getUserInfoByChannelOpenid($channel_id, $openid)
- {
- if (!$this->user->id || ($this->user->openid != $openid || $this->user->channel_id != $channel_id)) {
- $data = ApiService::instance()->getDataFromApi(ServiceApiConstants::USER_FIND_BY_OPENID, ['openid' => $openid, 'channel_id' => $channel_id])->data;
- $this->user->bind($data);
- }
- return $this->user;
- }
- /**
- * 更新用户
- * @param $userUpdate UserUpdate
- * @return User
- */
- public function updateUser(UserUpdate $userUpdate)
- {
- $data = ApiService::instance()->getDataFromApi(ServiceApiConstants::USER_UPDATE, $userUpdate->toArray())->data;
- $cache = CacheConstants::getUserCacheKey($userUpdate->id);
- Redis::instance()->del($cache);
- return $this->user->bind($data);
- }
- /**
- * 创建用户
- * @param $channel_id
- * @param $openid
- * @return User
- */
- public function createUser($channel_id, $openid)
- {
- $data = ApiService::instance()->getDataFromApi(ServiceApiConstants::USER_ADD, ['openid' => $openid, 'channel_id' => $channel_id])->data;
- return $this->user->bind($data);
- }
- }
|