| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 |
- <?php
- namespace Modules\ManageApi\Controllers;
- use App\Constants\CommonConstant;
- use App\Constants\RedisKeyConstant;
- use App\Models\PaymentAttrModel;
- use App\Models\PaymentModel;
- use App\Models\PaymentTypeModel;
- use App\Repositories\Service\CommonService;
- use Illuminate\Support\Facades\Cache;
- use Modules\ManageApi\Services\MerchantServices;
- class PayController extends BaseController
- {
- /**
- * 支付方式列表
- *
- * @return \Illuminate\Http\JsonResponse|mixed
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function paymentList()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'page' => 'nullable|integer',
- 'page_size' => 'nullable|integer'
- ];
- $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'] ?? 1);
- $model = PaymentModel::query();
- if (!empty($inputData['name'])) {
- $model = $model->where('name', 'like', '%' . $inputData['name'] . '%');
- }
- if (!empty($inputData['status'])) {
- $model = $model->where('status', $inputData['status']);
- }
- $res = $model->orderByDesc('created_at')->paginate( $pageSize, ["*"], 'page', $page);
- return json_success($res);
- }
- /**
- * 支付方式添加
- *
- * @return \Illuminate\Http\JsonResponse|mixed
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function paymentAdd()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'name' => 'required|string',
- 'pay_code' => 'required|string',
- 'partner_id' => 'required|string',
- ];
- $validator = app('validator')->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $setarr = [
- 'name' => $inputData['name'],
- 'pay_code' => $inputData['pay_code'],
- 'partner_id' => $inputData['partner_id'],
- ];
- $res = PaymentModel::query()->create($setarr);
- if ($res) {
- return json_success([], '添加成功');
- } else {
- return json_fail('添加失败');
- }
- }
- /**
- * 支付方式修改
- *
- * @return \Illuminate\Http\JsonResponse|mixed
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function paymentEdit()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'name' => 'nullable|string',
- 'pay_code' => 'nullable|string',
- 'partner_id' => 'nullable|string',
- 'status' => 'nullable|in:1,2',
- 'uid' => 'required|string',
- ];
- $validator = app('validator')->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $info = PaymentModel::query()->where('uid', $inputData['uid'])->first();
- if (!$info) {
- return json_fail('操作的支付方式不存在');
- }
- if (!empty($inputData['name'])) {
- $info->name = $inputData['name'];
- }
- if (!empty($inputData['pay_code'])) {
- $info->pay_code = $inputData['pay_code'];
- }
- if (!empty($inputData['partner_id'])) {
- $info->partner_id = $inputData['partner_id'];
- }
- if (!empty($inputData['status'])) {
- $info->status = $inputData['status'];
- }
- if ($info->save()) {
- return json_success([], '修改成功');
- } else {
- return json_fail('修改失败');
- }
- }
- /**
- * 支付通道列表
- *
- * @return \Illuminate\Http\JsonResponse|mixed
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function paymentAttrList()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'page' => 'required|integer',
- 'page_size' => 'required|integer',
- 'name' => 'nullable|string',
- 'pay_uid' => 'nullable',
- 'status' => 'nullable|in:1,2',
- ];
- $validator = app('validator')->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $model = PaymentAttrModel::query();
- if (!empty($inputData['pay_uid'])) {
- $model = $model->where('pay_uid', $inputData['pay_uid']);
- }
- if (!empty($inputData['status'])) {
- $model = $model->where('status', $inputData['status']);
- }
- if (!empty($inputData['name'])) {
- $model = $model->where('name', 'like', '%' . $inputData['name'] . '%');
- }
- $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
- if ($pageSize > 500) {
- $pageSize = CommonConstant::PAGE_SIZE;
- }
- $page = format_page($inputData['page'] ?? 0);
- $res = $model->orderByDesc('created_at')->paginate( $pageSize, ["*"], 'page', $page);
- return json_success($res);
- }
- /**
- * 支付通道添加
- *
- * @return \Illuminate\Http\JsonResponse|mixed
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function paymentAttrAdd()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'pay_uid' => 'required|string',
- 'code' => 'required|string',
- 'name' => 'required|string',
- ];
- $validator = app('validator')->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $setarr = [
- 'name' => $inputData['name'],
- 'pay_uid' => $inputData['pay_uid'],
- 'code' => $inputData['code'],
- ];
- $res = PaymentAttrModel::query()->create($setarr);
- if ($res) {
- return json_success([], '添加成功');
- } else {
- return json_fail('添加失败');
- }
- }
- /**
- * 支付通道修改
- *
- * @return \Illuminate\Http\JsonResponse|mixed
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function paymentAttrEdit()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'pay_uid' => 'nullable|string',
- 'code' => 'nullable|string',
- 'name' => 'nullable|string',
- 'uid' => 'required|string',
- 'status' => 'nullable|in:1,2'
- ];
- $validator = app('validator')->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $info = PaymentAttrModel::query()->where('uid', $inputData['uid'])->first();
- if (!$info) {
- return json_fail('操作的支付通道不存在');
- }
- if (!empty($inputData['name'])) {
- $info->name = $inputData['name'];
- }
- if (!empty($inputData['pay_uid'])) {
- $info->pay_uid = $inputData['pay_uid'];
- }
- if (!empty($inputData['code'])) {
- $info->code = $inputData['code'];
- }
- if (!empty($inputData['status'])) {
- $info->status = $inputData['status'];
- }
- if ($info->save()) {
- return json_success([], '修改成功');
- } else {
- return json_fail('修改失败');
- }
- }
- /**
- * 支付类型列表
- *
- * @return \Illuminate\Http\JsonResponse|mixed
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function paymentTypeList()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'page' => 'nullable|integer',
- 'page_size' => 'nullable|integer',
- 'payment_uid' => 'nullable|string',
- 'status' => 'nullable|in:1,2',
- 'pay_type' => 'nullable|in:1,2,3,4,5',
- 'belong_device' => 'nullable|in:A,I',
- ];
- $validator = app('validator')->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $model = PaymentTypeModel::query();
- if (!empty($inputData['payment_uid'])) {
- $model->payment_uid = $model->where('payment_uid', $inputData['payment_uid']);
- }
- if (!empty($inputData['status'])) {
- $model = $model->where('status', $inputData['status']);
- }
- if (!empty($inputData['pay_type'])) {
- $model = $model->where('pay_type', $inputData['pay_type']);
- }
- $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
- if ($pageSize > 500) {
- $pageSize = CommonConstant::PAGE_SIZE;
- }
- $page = format_page($inputData['page'] ?? 1);
- $res = $model->orderByDesc('weights')->paginate( $pageSize, ["*"], 'page', $page);
- foreach ($res->items() as &$val) {
- $val->payment_attr_code = $val->paymentAttr->code ?? '';
- unset($val->paymentAttr);
- }
- return json_success($res);
- }
- /**
- * 支付类型添加
- *
- * @return \Illuminate\Http\JsonResponse|mixed
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function paymentTypeAdd()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'name' => 'required|string',
- 'payment_uid' => 'required|string',
- 'payment_attr_uid' => 'required|string',
- 'pay_type' => 'required|in:1,2,3,4,5',
- 'limit_type' => 'required|in:1,2',
- 'limit' => 'required|string',
- 'weights' => 'required|integer',
- 'remark' => 'nullable|string',
- 'status' => 'required|in:1,2',
- 'belong_device' => 'nullable|in:I,A',
- ];
- $validator = app('validator')->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $setarr = [
- 'name' => $inputData['name'],
- 'payment_uid' => $inputData['payment_uid'],
- 'payment_attr_uid' => $inputData['payment_attr_uid'],
- 'pay_type' => $inputData['pay_type'],
- 'limit_type' => $inputData['limit_type'],
- 'limit' => $inputData['limit'],
- 'weights' => $inputData['weights'],
- 'remark' => $inputData['remark'],
- 'status' => $inputData['status'],
- 'rate' => $inputData['rate'],
- 'belong_device' => $inputData['belong_device'],
- ];
- $res = PaymentTypeModel::query()->create($setarr);
- if ($res) {
- return json_success([], '添加成功');
- } else {
- return json_fail('添加失败');
- }
- }
- /**
- * 支付类型修改
- *
- * @return \Illuminate\Http\JsonResponse|mixed
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function paymentTypeEdit()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'name' => 'nullable|string',
- 'payment_uid' => 'nullable|string',
- 'payment_attr_uid' => 'nullable|string',
- 'pay_type' => 'nullable|in:1,2,3,4,5',
- 'limit_type' => 'nullable|in:1,2',
- 'limit' => 'nullable|string',
- 'weights' => 'nullable|integer',
- 'remark' => 'nullable|string',
- 'status' => 'nullable|in:1,2',
- 'uid' => 'required|string',
- 'belong_device' => 'nullable|in:I,A',
- ];
- $validator = app('validator')->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $info = PaymentTypeModel::query()->where('uid', $inputData['uid'])->first();
- if (!$info) {
- return json_fail('操作的支付类型不存在');
- }
- if (!empty($inputData['name'])) {
- $info->name = $inputData['name'];
- }
- if (!empty($inputData['payment_uid'])) {
- $info->payment_uid = $inputData['payment_uid'];
- }
- if (!empty($inputData['payment_attr_uid'])) {
- $info->payment_attr_uid = $inputData['payment_attr_uid'];
- }
- if (!empty($inputData['pay_type'])) {
- $info->pay_type = $inputData['pay_type'];
- }
- if (!empty($inputData['limit_type'])) {
- $info->limit_type = $inputData['limit_type'];
- }
- if (!empty($inputData['belong_device'])) {
- $info->belong_device = $inputData['belong_device'];
- }
- if (!empty($inputData['limit'])) {
- $info->limit = $inputData['limit'];
- }
- if (!empty($inputData['weights'])) {
- $info->weights = $inputData['weights'];
- }
- if (!empty($inputData['status'])) {
- $info->status = $inputData['status'];
- }
- if (!empty($inputData['rate'])) {
- $info->rate = $inputData['rate'];
- }
- $info->remark = $inputData['remark'];
- if ($info->save()) {
- Cache::forget(RedisKeyConstant::PAYMENTTYPE);
- app(MerchantServices::class)->clearCache();
- app(CommonService::class)->clearAllMerchantCache();
- return json_success([], '修改成功');
- } else {
- return json_fail('修改失败');
- }
- }
- /**
- * 支付类型删除
- *
- * @return \Illuminate\Http\JsonResponse|mixed
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function paymentTypeDel()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'uid' => 'required|string',
- ];
- $validator = app('validator')->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $res = PaymentTypeModel::query()->where('uid', $inputData['uid'])->delete();
- if ($res) {
- Cache::forget(RedisKeyConstant::PAYMENTTYPE);
- app(CommonService::class)->clearAllMerchantCache();
- return json_success([], '删除成功');
- } else {
- return json_fail('删除失败');
- }
- }
- /**
- * 下拉
- *
- * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
- */
- public function dropDown()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'down_type' => 'required|in:1,2,3',
- 'pay_uid' => 'nullable|string'
- ];
- $validator = app('validator')->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- if ($inputData['down_type'] == 1) {
- $res = PaymentModel::query()->get(['uid', 'name'])->keyBy('uid');
- } elseif ($inputData['down_type'] == 2) {
- $model = PaymentAttrModel::query();
- if (!empty($inputData['pay_uid'])) {
- $model = $model->where('pay_uid', $inputData['pay_uid']);
- }
- $res = $model->get(['uid', 'name', 'pay_uid'])->keyBy('uid');
- } else {
- $res = PaymentTypeModel::query()->where('status', 1)->get(['uid', 'name', 'payment_uid'])->keyBy('uid');
- if ($res->isNotEmpty()) {
- foreach ($res as &$val) {
- $val->name = $val->payment->name . '-' . $val->name;
- unset($val->payment, $val->payment_uid);
- }
- }
- }
- return $res;
- }
- }
|