Browse Source

提交评价接口

TODO LIST:
1、用户、市场、服务人员是否存在的校验
2、保存失败的异常处理
master
weigang 5 years ago
parent
commit
fc288e1c49
  1. 5
      app/Constants/ErrorCode.php
  2. 15
      app/Controller/ServiceEvaluateController.php
  3. 11
      app/Exception/Handler/ValidationExceptionHandler.php
  4. 22
      app/Model/ServiceEvaluate.php
  5. 23
      app/Request/EvaluateRequest.php
  6. 18
      app/Service/ServiceEvaluateService.php
  7. 2
      app/Service/ServiceEvaluateServiceInterface.php

5
app/Constants/ErrorCode.php

@ -28,4 +28,9 @@ class ErrorCode extends AbstractConstants
* @Message("Params Invalid!")
*/
const PARAMS_INVALID = 900;
/**
* @Message("Save failure!");
*/
const SAVE_FAILURE = 100;
}

15
app/Controller/ServiceEvaluateController.php

@ -2,6 +2,8 @@
namespace App\Controller;
use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Request\EvaluateRequest;
use App\Service\ServiceEvaluateServiceInterface;
use Hyperf\Di\Annotation\Inject;
@ -16,10 +18,21 @@ class ServiceEvaluateController extends BaseController
/**
* 提交评价
*
* 评价内容:
* 服务态度评分、服务质量评分、满意度评分、评价内容
* 关联信息:
* 服务专员ID、市场ID、评价的用户ID
* 查询信息:
* 用户注册/创建时间
*
*/
public function evaluate(EvaluateRequest $validator)
{
return $this->success($this->evaluateService->evaluate($this->request));
// TODO 数据保存失败的返回处理
$ret = $this->evaluateService->evaluate();
return $this->success($ret);
}
}

11
app/Exception/Handler/ValidationExceptionHandler.php

@ -7,17 +7,10 @@ use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\Validation\ValidationException;
use Psr\Http\Message\ResponseInterface;
// use Hyperf\Di\Annotation\Inject;
// use Hyperf\HttpServer\Contract\ResponseInterface as Response;
use Throwable;
class ValidationExceptionHandler extends ExceptionHandler
{
// /**
// * @Inject
// * @var Response
// */
// private $reponse;
public function handle(Throwable $throwable, ResponseInterface $response)
{
@ -30,7 +23,9 @@ class ValidationExceptionHandler extends ExceptionHandler
"message" => $throwable->validator->errors()->first()
]);
return $response->withStatus($throwable->status)->withBody(new SwooleStream($content));
return $response->withHeader('Content-Type', 'application/json')
->withStatus($throwable->status)
->withBody(new SwooleStream($content));
}
public function isValid(Throwable $throwable): bool

22
app/Model/ServiceEvaluate.php

@ -0,0 +1,22 @@
<?php
namespace App\Model;
class ServiceEvaluate extends Model
{
/**
* @var string table name
*/
protected $table = 'lanzu_service_evaluate';
/**
* @var array 允许插入的属性
*/
protected $fillable = ['user_id', 'user_created_at', 'service_personnel_id', 'market_id', 'c_attitude', 'c_service', 'c_quality', 'content'];
/**
* 默认值
*/
protected $attributes = [];
}

23
app/Request/EvaluateRequest.php

@ -22,15 +22,26 @@ class EvaluateRequest extends FormRequest
public function rules(): array
{
return [
'user_id' => 'required|nonempty',
'c_attitude' => 'required|nonempty|integer',
'c_service' => 'required|nonempty|integer',
'c_quality' => 'required|nonempty|integer',
'content' => 'required|nonempty|between:15,150',
'user_id' => 'required|nonempty|integer',
'service_personnel_id' => 'required|nonempty|integer',
'market_id' => 'required|nonempty|integer',
];
}
public function messages(): array
{
return [
'user_id.required' => ':attribute参数缺失',
'user_id.nonempty' => ':attribute信息异常',
'user_id.*' => ':attribute信息不正确',
'service_personnel_id.*' => ':attribute信息不正确',
'market_id.*' => ':attribute信息不正确',
'c_attitude.*' => ':attribute信息不正确',
'c_service.*' => ':attribute信息不正确',
'c_quality.*' => ':attribute信息不正确',
'content.*' => ':attribute信息不正确',
];
}
@ -38,6 +49,12 @@ class EvaluateRequest extends FormRequest
{
return [
'user_id' => '用户',
'service_personnel_id' => '服务专员',
'market_id' => '服务专员市场',
'c_attitude' => '服务态度评分',
'c_service' => '服务值评分',
'c_quality' => '服务质量评分',
'content' => '服务评价',
];
}
}

18
app/Service/ServiceEvaluateService.php

@ -2,13 +2,27 @@
namespace App\Service;
use App\Model\ServiceEvaluate;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Di\Annotation\Inject;
class ServiceEvaluateService implements ServiceEvaluateServiceInterface
{
public function evaluate(RequestInterface $request)
/**
* @Inject
* @var RequestInterface
*/
private $request;
public function evaluate()
{
return $request->all();
$data = $this->request->all();
$data['user_created_at'] = Db::table('ims_cjdc_user')
->where(['id' => $data['user_id']])
->value('join_time');
return ServiceEvaluate::create($data);
}
}

2
app/Service/ServiceEvaluateServiceInterface.php

@ -7,6 +7,6 @@ use Hyperf\HttpServer\Contract\RequestInterface;
interface ServiceEvaluateServiceInterface
{
public function evaluate(RequestInterface $request);
public function evaluate();
}
Loading…
Cancel
Save