update user manage

This commit is contained in:
Tokumeikoi 2020-02-28 16:38:44 +08:00
parent 53710f2e01
commit dd924d95c6

View File

@ -59,31 +59,31 @@ class UserController extends Controller
public function update(UserUpdate $request) public function update(UserUpdate $request)
{ {
$updateData = $request->only(array_keys(UserUpdate::RULES)); $params = $request->only(array_keys(UserUpdate::RULES));
$user = User::find($request->input('id')); $user = User::find($request->input('id'));
if (!$user) { if (!$user) {
abort(500, '用户不存在'); abort(500, '用户不存在');
} }
if (User::where('email', $updateData['email'])->first() && $user->email !== $updateData['email']) { if (User::where('email', $params['email'])->first() && $user->email !== $params['email']) {
abort(500, '邮箱已被使用'); abort(500, '邮箱已被使用');
} }
if (isset($updateData['password'])) { if (isset($params['password'])) {
$updateData['password'] = password_hash($updateData['password'], PASSWORD_DEFAULT); $params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
} else { } else {
unset($updateData['password']); unset($params['password']);
} }
if (isset($updateData['plan_id'])) { if (isset($params['plan_id'])) {
$plan = Plan::find($updateData['plan_id']); $plan = Plan::find($params['plan_id']);
if (!$plan) { if (!$plan) {
abort(500, '订阅计划不存在'); abort(500, '订阅计划不存在');
} }
$updateData['group_id'] = $plan->group_id; $params['group_id'] = $plan->group_id;
// plan type is onetime, set expired time 0 // plan type is onetime, set expired time 0
if ($plan->type === 1) { if ($plan->type === 1) {
$user->expired_at = 0; $user->expired_at = 0;
} }
} }
if (!$user->update($updateData)) { if (!$user->update($params)) {
abort(500, '保存失败'); abort(500, '保存失败');
} }
return response([ return response([