MerchantController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace Modules\ManageApi\Controllers;
  3. use App\Constants\CommonConstant;
  4. use App\Constants\RedisKeyConstant;
  5. use App\Models\MerchantModel;
  6. use App\Models\MerchantPaymentTypeModel;
  7. use App\Repositories\Service\CommonService;
  8. use Illuminate\Support\Facades\Cache;
  9. use Illuminate\Support\Facades\DB;
  10. class MerchantController extends BaseController
  11. {
  12. public function lists()
  13. {
  14. $inputData = $this->getJsonData();
  15. $rules = [
  16. 'page' => 'required|integer',
  17. 'page_size' => 'required|integer',
  18. 'username' => 'nullable|string',
  19. 'status' => 'nullable|in:1,2'
  20. ];
  21. $validator = app('validator')->make($inputData, $rules);
  22. if ($validator->fails()) {
  23. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  24. return json_fail($validator->errors()->first());
  25. }
  26. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  27. if ($pageSize > 500) {
  28. $pageSize = CommonConstant::PAGE_SIZE;
  29. }
  30. $page = format_page($inputData['page'] ?? 0);
  31. $model = MerchantModel::query();
  32. if (!empty($inputData['username'])) {
  33. $model = $model->where('username', 'like', '%' . $inputData['username'] . '%');
  34. }
  35. if (!empty($inputData['status'])) {
  36. $model = $model->where('status', $inputData['status']);
  37. }
  38. $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);
  39. foreach ($res->items() as &$val) {
  40. $payUid = $val->merchantPaymentType;
  41. $uid = '';
  42. foreach ($payUid as $uidV) {
  43. $uid .= $uidV->payment_type_uid . ',';
  44. }
  45. $val->pay_uid = substr($uid, 0, -1);
  46. unset($val->merchantPaymentType);
  47. }
  48. return json_success($res);
  49. }
  50. /**
  51. * 创建商户
  52. *
  53. * @return \Illuminate\Http\JsonResponse|mixed
  54. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  55. */
  56. public function add()
  57. {
  58. $inputData = $this->getJsonData();
  59. $rules = [
  60. 'username' => 'required|string',
  61. 'merchant_code' => 'required|string',
  62. 'account' => 'required|string',
  63. 'password' => 'required|string',
  64. 'cache_notice_url' => 'required|string',
  65. 'pay_uid' => 'required|string',
  66. 'merchant_mark' => 'required|string',
  67. ];
  68. $validator = app('validator')->make($inputData, $rules);
  69. if ($validator->fails()) {
  70. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  71. return json_fail($validator->errors()->first());
  72. }
  73. $data = [
  74. 'username' => $inputData['username'],
  75. 'merchant_code' => $inputData['merchant_code'],
  76. 'merchant_mark' => $inputData['merchant_mark'],
  77. 'account' => $inputData['account'],
  78. 'cache_notice_url' => $inputData['cache_notice_url'],
  79. 'password' => generatePassword($inputData['password'], 1),
  80. 'key' => createKey()
  81. ];
  82. $res = MerchantModel::query()->create($data);
  83. if ($res) {
  84. $payUid = explode(',', $inputData['pay_uid']);
  85. $mercPayData = [];
  86. foreach ($payUid as $val) {
  87. $mercPayData[] = [
  88. 'merchant_uid' => $res['uid'],
  89. 'payment_type_uid' => $val,
  90. 'created_at' => date('Y-m-d H:i:s'),
  91. 'updated_at' => date('Y-m-d H:i:s')
  92. ];
  93. }
  94. $payRes = MerchantPaymentTypeModel::query()->insert($mercPayData);
  95. if ($payRes) {
  96. return json_success([], '创建成功');
  97. } else {
  98. return json_fail('创建失败');
  99. }
  100. } else {
  101. return json_fail('创建失败');
  102. }
  103. }
  104. /**
  105. * 修改商户
  106. *
  107. * @return \Illuminate\Http\JsonResponse|mixed
  108. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  109. */
  110. public function edit()
  111. {
  112. $inputData = $this->getJsonData();
  113. $rules = [
  114. 'username' => 'nullable|string',
  115. 'merchant_code' => 'nullable|string',
  116. 'merchant_mark' => 'nullable|string',
  117. 'password' => 'nullable|string',
  118. 'cache_notice_url' => 'nullable|string',
  119. 'pay_uid' => 'nullable|string',
  120. 'uid' => 'nullable|string',
  121. 'status' => 'nullable|in:1,2',
  122. ];
  123. $validator = app('validator')->make($inputData, $rules);
  124. if ($validator->fails()) {
  125. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  126. return json_fail($validator->errors()->first());
  127. }
  128. $merchant = MerchantModel::query()->where('uid', $inputData['uid'])->first();
  129. if (!$merchant) {
  130. return json_fail('该商户不存在');
  131. }
  132. if (!empty($inputData['username'])) {
  133. $merchant->username = $inputData['username'];
  134. }
  135. if (!empty($inputData['status'])) {
  136. $merchant->status = $inputData['status'];
  137. }
  138. if (!empty($inputData['merchant_code'])) {
  139. $merchant->merchant_code = $inputData['merchant_code'];
  140. }
  141. if (!empty($inputData['merchant_mark'])) {
  142. $merchant->merchant_mark = $inputData['merchant_mark'];
  143. }
  144. if (!empty($inputData['cache_notice_url'])) {
  145. $merchant->cache_notice_url = $inputData['cache_notice_url'];
  146. }
  147. if (!empty($inputData['password'])) {
  148. $merchant->password = generatePassword($inputData['password'], 1);
  149. }
  150. if (!empty($inputData['pay_uid'])) {
  151. $payUid = explode(',', $inputData['pay_uid']);
  152. // 先删除 后插入
  153. DB::beginTransaction();
  154. MerchantPaymentTypeModel::query()->where('merchant_uid', $inputData['uid'])->delete();
  155. $mercPayData = [];
  156. foreach ($payUid as $val) {
  157. $mercPayData[] = [
  158. 'merchant_uid' => $inputData['uid'],
  159. 'payment_type_uid' => $val,
  160. 'created_at' => date('Y-m-d H:i:s'),
  161. 'updated_at' => date('Y-m-d H:i:s')
  162. ];
  163. }
  164. MerchantPaymentTypeModel::query()->insert($mercPayData);
  165. DB::commit();
  166. }
  167. $res = $merchant->save();
  168. if ($res) {
  169. // 清除商户通道缓存
  170. Cache::forget(RedisKeyConstant::PAYMENT_TYPE . $inputData['uid']);
  171. // 清除商户缓存
  172. app(CommonService::class)->clearMerchantCache($merchant['key'], $merchant['cache_notice_url']);
  173. return json_success([], '修改成功');
  174. } else {
  175. return json_fail('修改失败');
  176. }
  177. }
  178. /**
  179. * 商户下拉
  180. *
  181. * @return mixed
  182. */
  183. public function dow()
  184. {
  185. $res = MerchantModel::query()->get(['uid', 'username'])->keyBy('uid');
  186. return json_success($res);
  187. }
  188. /**
  189. * 清除商户缓存
  190. *
  191. * @return \Illuminate\Http\JsonResponse|mixed
  192. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  193. */
  194. public function clearMerchantCache()
  195. {
  196. $inputData = $this->getJsonData();
  197. $rules = [
  198. 'uid' => 'required|string',
  199. ];
  200. $validator = app('validator')->make($inputData, $rules);
  201. if ($validator->fails()) {
  202. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  203. return json_fail($validator->errors()->first());
  204. }
  205. $merchant = MerchantModel::query()->where('uid', $inputData['uid'])->first();
  206. if (!$merchant) {
  207. return json_fail('该商户不存在');
  208. }
  209. app(CommonService::class)->clearMerchantCache($merchant['key'], $merchant['cache_notice_url']);
  210. return json_success([]);
  211. }
  212. }