82 lines
2.2 KiB
PHP
82 lines
2.2 KiB
PHP
<?php
|
|
|
|
|
|
namespace app\services\base;
|
|
|
|
use app\services\Auth;
|
|
|
|
class UserRequestBase
|
|
{
|
|
protected $data;
|
|
|
|
protected function isEmpty($key, $message)
|
|
{
|
|
if (empty($this->data->{$key})) $this->sendError($key, $message);
|
|
}
|
|
|
|
protected function isNotEmail($key, $message)
|
|
{
|
|
if (!filter_var($this->data->{$key}, FILTER_VALIDATE_EMAIL)) $this->sendError($key, $message);
|
|
}
|
|
|
|
protected function isExists($key, $classModel, $message)
|
|
{
|
|
$exists = $classModel::find()->where([$key => $this->data->{$key}])->one();
|
|
if ($exists) $this->sendError($key, $message);
|
|
}
|
|
|
|
protected function isNotSecurePassword($key, $message)
|
|
{
|
|
$value = $this->data->{$key};
|
|
if (mb_strlen($value) < 6)
|
|
$this->sendError($key, $message);
|
|
}
|
|
|
|
protected function wrongOldPassword($key, $message)
|
|
{
|
|
$value = $this->data->{$key};
|
|
$user = Auth::getUser();
|
|
if (!password_verify($value, $user->password_hash))
|
|
$this->sendError($key, $message);
|
|
}
|
|
|
|
protected function isNotMatch($key, $matchKey, $message)
|
|
{
|
|
if ($this->data->{$key} != $this->data->{$matchKey})
|
|
$this->sendError($key, $message);
|
|
}
|
|
|
|
protected function isNotChecked($key, $message)
|
|
{
|
|
if ($this->data->{$key} != true)
|
|
$this->sendError($key, $message);
|
|
}
|
|
|
|
public function call($method)
|
|
{
|
|
if (method_exists($this, $method)) {
|
|
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
|
$this->data = json_decode(\Yii::$app->request->getRawBody());
|
|
$result = $this->{$method}();
|
|
$result = $result ?? (object)[];
|
|
echo json_encode(['success' => 1, 'data' => $result]);
|
|
exit;
|
|
}
|
|
}
|
|
}
|
|
|
|
private function sendError($key, $message)
|
|
{
|
|
header('Content-type: application/json');
|
|
echo json_encode(['error' => ['key' => $key, 'message' => $message]]);
|
|
exit;
|
|
}
|
|
|
|
public function sendSuccess($message)
|
|
{
|
|
header('Content-type: application/json');
|
|
echo json_encode(['success' => ['message' => $message]]);
|
|
exit;
|
|
}
|
|
}
|