| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 | <?phpnamespace App\MicroApi\Services;use App\MicroApi\Exceptions\RpcException;use App\MicroApi\Facades\HttpClient;use App\MicroApi\Items\TokenItem;use App\MicroApi\Items\UserItem;use Illuminate\Support\Facades\Log;class UserService{    use DataHandler;    protected $servicePrefix = '/user/userService';    /**     * @param $data     * @return UserItem     * @throws RpcException     */    public function create($data)    {        $path = $this->servicePrefix . '/create';        $user = new UserItem();        if (!empty($data['name'])) {            $user->name = $data['name'];        }        if (!empty($data['email'])) {            $user->email = $data['email'];        }        if (!empty($data['password'])) {            $user->password = $data['password'];        }        $options = ['json' => $user];        try {            $response = HttpClient::post($path, $options);        } catch (\Exception $exception) {            Log::error("MicroApi.UserService.create Call Failed: " . $exception->getMessage());            throw new RpcException("调用远程服务失败");        }        $result = $this->decode($response->getBody()->getContents());        return $result->user;    }    public function getAll()    {        $path = $this->servicePrefix . '/getAll';        try {            $response = HttpClient::get($path);        } catch (\Exception $exception) {            Log::error("MicroApi.UserService.getAll Call Failed: " . $exception->getMessage());            throw new RpcException("调用远程服务失败");        }        $result = $this->decode($response->getBody()->getContents());        return $result->users;    }    public function getById($id)    {        $path = $this->servicePrefix . '/get';        $user = new UserItem();        $user->id = $id;        $options = ['json' => $user];        try {            $response = HttpClient::post($path, $options);        } catch (\Exception $exception) {            Log::error("MicroApi.UserService.getByCondition Call Failed: " . $exception->getMessage());            throw new RpcException("调用远程服务失败");        }        $result = $this->decode($response->getBody()->getContents());        return $result->user;    }    public function auth($credentials)    {        $path = $this->servicePrefix . '/auth';        $user = new UserItem();        if (!empty($credentials['email'])) {            $user->email = $credentials['email'];        }        if (!empty($credentials['password'])) {            $user->password = $credentials['password'];        }        $options = ['json' => $user];        try {            $response = HttpClient::post($path, $options);        } catch (\Exception $exception) {            Log::error("MicroApi.UserService.auth Call Failed: " . $exception->getMessage());            throw new RpcException("调用远程服务失败");        }        $result = $this->decode($response->getBody()->getContents());        return $result->token;    }    public function isAuth($token)    {        $path = $this->servicePrefix . '/validateToken';        $item = new TokenItem();        $item->token = $token;        $options = ['json' => $item];        try {            $response = HttpClient::post($path, $options);        } catch (\Exception $exception) {            Log::error("MicroApi.UserService.auth Call Failed: " . $exception->getMessage());            throw new RpcException("调用远程服务失败");        }        $result = $this->decode($response->getBody()->getContents());        return  $result->valid;    }    public function getByEmail($email)    {        $path = $this->servicePrefix . '/get';        $user = new UserItem();        $user->email = $email;        $options = ['json' => $user];        try {            $response = HttpClient::post($path, $options);        } catch (\Exception $exception) {            Log::error("MicroApi.UserService.getByEmail Call Failed: " . $exception->getMessage());            throw new RpcException("调用远程服务失败");        }        $result = $this->decode($response->getBody()->getContents());        return isset($result->user) ? $result->user : null;    }}
 |