这是我今天早上写的一段代码
一个 API 项目的一部分
作用是更新一条记录的 status 字段的值
class State extends Base
{
// 操作
private $action = [
'keep' => [0, 2], // 0 => 2
'back' => [2, 0], // 2 => 0
'fail' => [1, 4], // 1 => 4
'lose' => [3, 4] // 3 => 4
];
function __construct(ContainerInterface $app)
{
parent::__construct($app);
}
public function __invoke($request, $response, $args)
{
$json = [];
if ( ! array_key_exists($args['action'], $this->action) ) {
return $this->respond(20, [], '非法操作,action 可选的值为:keep、back、fail、lose.');
}
$query = $this->app->db->table('record')
->where(
[
['ruid', $args['ruid']],
['status', $this->action[$args['action']][0]]
]
)
->update(['status' => $this->action[$args['action']][1]]);
if ($query) {
return $this->respond(0, $json);
}
return $this->respond(21, [], '操作失败.');
}
}