getJsonData(); $rules = [ 'page' => 'required|integer', 'page_size' => 'required|integer', 'username' => 'nullable|string', 'status' => 'nullable|in:1,2' ]; $validator = app('validator')->make($inputData, $rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE; if ($pageSize > 500) { $pageSize = CommonConstant::PAGE_SIZE; } $page = format_page($inputData['page'] ?? 0); $model = MerchantModel::query(); if (!empty($inputData['username'])) { $model = $model->where('username', 'like', '%' . $inputData['username'] . '%'); } if (!empty($inputData['status'])) { $model = $model->where('status', $inputData['status']); } $res = $model->orderByDesc('created_at')->paginate($pageSize, ['uid', 'username', 'account', 'status', 'merchant_code', 'key', 'cache_notice_url', 'created_at', 'updated_at', 'merchant_mark'], 'page', $page); foreach ($res->items() as &$val) { $payUid = $val->merchantPaymentType; $uid = ''; foreach ($payUid as $uidV) { $uid .= $uidV->payment_type_uid . ','; } $val->pay_uid = substr($uid, 0, -1); unset($val->merchantPaymentType); } return json_success($res); } /** * 创建商户 * * @return \Illuminate\Http\JsonResponse|mixed * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function add() { $inputData = $this->getJsonData(); $rules = [ 'username' => 'required|string', 'merchant_code' => 'required|string', 'account' => 'required|string', 'password' => 'required|string', 'cache_notice_url' => 'required|string', 'pay_uid' => 'required|string', 'merchant_mark' => 'required|string', ]; $validator = app('validator')->make($inputData, $rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $data = [ 'username' => $inputData['username'], 'merchant_code' => $inputData['merchant_code'], 'merchant_mark' => $inputData['merchant_mark'], 'account' => $inputData['account'], 'cache_notice_url' => $inputData['cache_notice_url'], 'password' => generatePassword($inputData['password'], 1), 'key' => createKey() ]; $res = MerchantModel::query()->create($data); if ($res) { $payUid = explode(',', $inputData['pay_uid']); $mercPayData = []; foreach ($payUid as $val) { $mercPayData[] = [ 'merchant_uid' => $res['uid'], 'payment_type_uid' => $val, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s') ]; } $payRes = MerchantPaymentTypeModel::query()->insert($mercPayData); if ($payRes) { return json_success([], '创建成功'); } else { return json_fail('创建失败'); } } else { return json_fail('创建失败'); } } /** * 修改商户 * * @return \Illuminate\Http\JsonResponse|mixed * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function edit() { $inputData = $this->getJsonData(); $rules = [ 'username' => 'nullable|string', 'merchant_code' => 'nullable|string', 'merchant_mark' => 'nullable|string', 'password' => 'nullable|string', 'cache_notice_url' => 'nullable|string', 'pay_uid' => 'nullable|string', 'uid' => 'nullable|string', 'status' => 'nullable|in:1,2', ]; $validator = app('validator')->make($inputData, $rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $merchant = MerchantModel::query()->where('uid', $inputData['uid'])->first(); if (!$merchant) { return json_fail('该商户不存在'); } if (!empty($inputData['username'])) { $merchant->username = $inputData['username']; } if (!empty($inputData['status'])) { $merchant->status = $inputData['status']; } if (!empty($inputData['merchant_code'])) { $merchant->merchant_code = $inputData['merchant_code']; } if (!empty($inputData['merchant_mark'])) { $merchant->merchant_mark = $inputData['merchant_mark']; } if (!empty($inputData['cache_notice_url'])) { $merchant->cache_notice_url = $inputData['cache_notice_url']; } if (!empty($inputData['password'])) { $merchant->password = generatePassword($inputData['password'], 1); } if (!empty($inputData['pay_uid'])) { $payUid = explode(',', $inputData['pay_uid']); // 先删除 后插入 DB::beginTransaction(); MerchantPaymentTypeModel::query()->where('merchant_uid', $inputData['uid'])->delete(); $mercPayData = []; foreach ($payUid as $val) { $mercPayData[] = [ 'merchant_uid' => $inputData['uid'], 'payment_type_uid' => $val, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s') ]; } MerchantPaymentTypeModel::query()->insert($mercPayData); DB::commit(); } $res = $merchant->save(); if ($res) { // 清除商户通道缓存 Cache::forget(RedisKeyConstant::PAYMENT_TYPE . $inputData['uid']); // 清除商户缓存 app(CommonService::class)->clearMerchantCache($merchant['key'], $merchant['cache_notice_url']); return json_success([], '修改成功'); } else { return json_fail('修改失败'); } } /** * 商户下拉 * * @return mixed */ public function dow() { $res = MerchantModel::query()->get(['uid', 'username'])->keyBy('uid'); return json_success($res); } /** * 清除商户缓存 * * @return \Illuminate\Http\JsonResponse|mixed * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function clearMerchantCache() { $inputData = $this->getJsonData(); $rules = [ 'uid' => 'required|string', ]; $validator = app('validator')->make($inputData, $rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $merchant = MerchantModel::query()->where('uid', $inputData['uid'])->first(); if (!$merchant) { return json_fail('该商户不存在'); } app(CommonService::class)->clearMerchantCache($merchant['key'], $merchant['cache_notice_url']); return json_success([]); } }