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.
67 lines
2.1 KiB
67 lines
2.1 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\WechatUser;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class H5ProfileController extends Controller
|
|
{
|
|
public function show(Request $request): JsonResponse
|
|
{
|
|
$user = $request->user();
|
|
if (! $user instanceof WechatUser) {
|
|
abort(403, '仅微信用户可访问');
|
|
}
|
|
|
|
return response()->json([
|
|
'real_name' => $user->real_name,
|
|
'phone' => $user->phone,
|
|
'id_card' => $user->id_card,
|
|
'avatar_url' => $user->avatar_url,
|
|
'nickname' => $user->nickname,
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request): JsonResponse
|
|
{
|
|
$user = $request->user();
|
|
if (! $user instanceof WechatUser) {
|
|
abort(403, '仅微信用户可访问');
|
|
}
|
|
|
|
$data = $request->validate([
|
|
'real_name' => ['required', 'string', 'max:80'],
|
|
'phone' => ['required', 'regex:/^1\d{10}$/'],
|
|
'id_card' => ['nullable', 'string', 'max:18'],
|
|
'avatar_url' => ['nullable', 'string', 'max:500'],
|
|
]);
|
|
|
|
if (! empty($data['id_card']) && ! preg_match('/^\d{17}[\dXx]$/', $data['id_card'])) {
|
|
throw ValidationException::withMessages(['id_card' => ['身份证号格式不正确']]);
|
|
}
|
|
|
|
$user->real_name = $data['real_name'];
|
|
$user->phone = $data['phone'];
|
|
$user->id_card = ($data['id_card'] ?? '') !== '' ? $data['id_card'] : null;
|
|
if (array_key_exists('avatar_url', $data) && $data['avatar_url'] !== null && $data['avatar_url'] !== '') {
|
|
$user->avatar_url = $data['avatar_url'];
|
|
}
|
|
$user->save();
|
|
|
|
return response()->json([
|
|
'message' => '保存成功',
|
|
'profile' => [
|
|
'real_name' => $user->real_name,
|
|
'phone' => $user->phone,
|
|
'id_card' => $user->id_card,
|
|
'avatar_url' => $user->avatar_url,
|
|
'nickname' => $user->nickname,
|
|
],
|
|
]);
|
|
}
|
|
}
|