You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.6 KiB

2 weeks ago
<?php
namespace App\Http\Controllers\Miniapp;
use App\Http\Controllers\Controller;
use App\Models\MiniappUser;
use App\Support\ApiResponse;
use App\Support\Miniapp\MiniappPresenter;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
6 days ago
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
2 weeks ago
class ProfileController extends Controller
{
use ApiResponse;
public function update(Request $request): JsonResponse
{
/** @var MiniappUser $user */
$user = $request->user();
$data = $request->validate([
'nickname' => ['nullable', 'string', 'max:64'],
'avatar_url' => ['nullable', 'string', 'max:512'],
'name' => ['nullable', 'string', 'max:64'],
'mobile' => ['nullable', 'string', 'max:32'],
'company' => ['nullable', 'string', 'max:255'],
6 days ago
'job_title' => ['nullable', 'string', 'max:128'],
'research_direction_ids' => ['nullable', 'array'],
'research_direction_ids.*' => ['integer', 'distinct', Rule::exists('research_directions', 'id')->where('status', 1)],
2 weeks ago
]);
6 days ago
$directionIds = $data['research_direction_ids'] ?? null;
unset($data['research_direction_ids']);
DB::transaction(function () use ($user, $data, $directionIds) {
$user->fill(array_filter($data, fn ($v) => $v !== null));
$user->save();
if (is_array($directionIds)) {
$user->researchDirections()->sync($directionIds);
}
});
2 weeks ago
return $this->ok(MiniappPresenter::userPayload($user->fresh()), '资料已保存');
}
}