| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- namespace Modules\ManageApi\Controllers;
- use App\Constants\CommonConstant;
- use App\Constants\RedisKeyConstant;
- use App\Models\MerchantModel;
- use App\Models\MerchantPaymentTypeModel;
- use App\Repositories\Service\CommonService;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- class MerchantController extends BaseController
- {
- public function lists()
- {
- $inputData = $this->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([]);
- }
- }
|