52 lines
1.5 KiB
PHP
52 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Endpoint\Api\Http\Controllers;
|
|
|
|
use App\Exceptions\BizException;
|
|
use App\Models\UserCid;
|
|
use App\Services\Push\MallUnipushService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class PushController extends Controller
|
|
{
|
|
/**
|
|
* 绑定cid
|
|
*
|
|
* @param Request $request
|
|
* @return void
|
|
*/
|
|
public function bindUniCid(Request $request, MallUnipushService $mallUnipushService)
|
|
{
|
|
if (!$request->user()) {
|
|
throw new BizException('绑定失败,请稍后再试');
|
|
}
|
|
|
|
$input = $request->validate([
|
|
'cid' => ['bail', 'required', 'string', 'max:64'],
|
|
'type' => ['bail', 'string', 'max:1'],
|
|
]);
|
|
try {
|
|
DB::beginTransaction();
|
|
$filed = Arr::get($input, 'type', 'u').'_cid';
|
|
//查询目前有没有人已绑定这个cid, 有就解绑
|
|
UserCid::where($filed, $input['cid'])->where('user_id', '<>', $request->user()->id)->update([
|
|
$filed => '',
|
|
]);
|
|
UserCid::updateOrCreate([
|
|
'user_id' => $request->user()->id,
|
|
], [
|
|
$filed => $input['cid'],
|
|
]);
|
|
DB::commit();
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
throw new BizException('绑定失败,请稍后再试');
|
|
}
|
|
return response()->noContent();
|
|
}
|
|
}
|