UsdtController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. namespace Modules\Manage\Controllers;
  3. use App\Constants\CommonConstant;
  4. use App\Repositories\Criterias\OrderLimitCriteria;
  5. use App\Repositories\Criterias\UsdtChainLimitCriteria;
  6. use App\Repositories\UsdtChainRepository;
  7. use App\Repositories\UsdtTypeRepository;
  8. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  9. use Modules\Manage\Responses\CommonPagesResponse;
  10. use Modules\Manage\Responses\CommonSuccessPkResponse;
  11. use Modules\Manage\Responses\PaymentSuccessUidResponse;
  12. class UsdtController extends BaseController
  13. {
  14. /**
  15. * usdt类型列表
  16. *
  17. * @param ValidatorFactoryInterface $validatorFactory
  18. * @return mixed|CommonPagesResponse
  19. */
  20. public function lists(ValidatorFactoryInterface $validatorFactory)
  21. {
  22. $inputData = $this->getJsonData();
  23. $rules = [
  24. 'page' => 'required|int',
  25. 'page_size' => 'required|int',
  26. ];
  27. $validator = $validatorFactory->make($inputData, $rules);
  28. if ($validator->fails()) {
  29. return json_fail($validator->errors()->first());
  30. }
  31. $orders = [
  32. ['id', 'DESC'],
  33. ];
  34. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  35. if ($pageSize > 500) {
  36. $pageSize = CommonConstant::PAGE_SIZE;
  37. }
  38. $page = format_page($inputData['page'] ?? 1);
  39. $res = make(UsdtTypeRepository::class)->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
  40. return new CommonPagesResponse($res);
  41. }
  42. /**
  43. * 新增usdt类型
  44. *
  45. * @param ValidatorFactoryInterface $validatorFactory
  46. * @return mixed|CommonSuccessPkResponse
  47. */
  48. public function add(ValidatorFactoryInterface $validatorFactory)
  49. {
  50. $inputData = $this->getJsonData();
  51. $rules = [
  52. 'name' => 'required|string',
  53. 'code' => 'required|string',
  54. 'chain_type' => 'required|string',
  55. ];
  56. $validator = $validatorFactory->make($inputData, $rules);
  57. if ($validator->fails()) {
  58. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  59. return json_fail($validator->errors()->first());
  60. }
  61. $setarr = [
  62. 'name' => $inputData['name'],
  63. 'code' => $inputData['code'],
  64. 'chain_type' => $inputData['chain_type'],
  65. ];
  66. $result = make(UsdtTypeRepository::class)->create($setarr);
  67. if (!$result) {
  68. return json_fail('创建失败');
  69. } else {
  70. $logSubject = '添加一个新的usdt类型:' . $inputData['name'] . '-' . $inputData['code'];
  71. $logSvalue = $result->id;
  72. $logDbData = [
  73. 'after_items' => [
  74. $result->toArray(),
  75. ],
  76. 'before_items' => [],
  77. ];
  78. $this->log($logSubject, $logSvalue, $logDbData);
  79. return new CommonSuccessPkResponse($result->uid);
  80. }
  81. }
  82. /**
  83. * 修改usdt类型
  84. *
  85. * @param ValidatorFactoryInterface $validatorFactory
  86. * @return mixed
  87. */
  88. public function update(ValidatorFactoryInterface $validatorFactory)
  89. {
  90. $inputData = $this->getJsonData();
  91. $rules = [
  92. 'name' => 'nullable|string',
  93. 'code' => 'nullable|string',
  94. 'status' => 'nullable|in:1,2',
  95. 'chain_type' => 'nullable|string',
  96. 'uid' => 'required',
  97. ];
  98. $validator = $validatorFactory->make($inputData, $rules);
  99. if ($validator->fails()) {
  100. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  101. return json_fail($validator->errors()->first());
  102. }
  103. $usdtTypeInfo = make(UsdtTypeRepository::class)->findBy('uid', $inputData['uid']);
  104. if (!$usdtTypeInfo) {
  105. return json_fail('该支付类型不存在!');
  106. }
  107. $oldUsdtTypeInfo = clone $usdtTypeInfo;
  108. if (!empty($inputData['name'])) {
  109. $usdtTypeInfo->name = $inputData['name'];
  110. }
  111. if (!empty($inputData['code'])) {
  112. $usdtTypeInfo->code = $inputData['code'];
  113. }
  114. if (!empty($inputData['status'])) {
  115. $usdtTypeInfo->status = $inputData['status'];
  116. }
  117. if (!empty($inputData['chain_type'])) {
  118. $usdtTypeInfo->chain_type = $inputData['chain_type'];
  119. }
  120. if (!$usdtTypeInfo->save()) {
  121. return json_fail('修改失败!');
  122. } else {
  123. $logSubject = '修改usdt类型:'. $oldUsdtTypeInfo->name;
  124. $logSvalue = $usdtTypeInfo->id;
  125. $logDbData = [
  126. 'after_items' => [
  127. $usdtTypeInfo->toArray(),
  128. ],
  129. 'before_items' => [
  130. $oldUsdtTypeInfo->toArray(),
  131. ],
  132. ];
  133. $this->log($logSubject, $logSvalue, $logDbData);
  134. }
  135. return new CommonSuccessPkResponse($oldUsdtTypeInfo->id);
  136. }
  137. /**
  138. * usdt支付链列表
  139. *
  140. * @param ValidatorFactoryInterface $validatorFactory
  141. * @return mixed|CommonPagesResponse
  142. */
  143. public function chainLists(ValidatorFactoryInterface $validatorFactory)
  144. {
  145. $inputData = $this->getJsonData();
  146. $rules = [
  147. 'page' => 'required|int',
  148. 'page_size' => 'required|int',
  149. 'is_lock' => 'nullable|in:1,2',
  150. 'chain_address' => 'nullable',
  151. 'usdt_type_uid' => 'nullable'
  152. ];
  153. $validator = $validatorFactory->make($inputData, $rules);
  154. if ($validator->fails()) {
  155. return json_fail($validator->errors()->first());
  156. }
  157. $orders = [
  158. ['id', 'DESC'],
  159. ];
  160. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  161. if ($pageSize > 500) {
  162. $pageSize = CommonConstant::PAGE_SIZE;
  163. }
  164. $page = format_page($inputData['page'] ?? 1);
  165. $res = make(UsdtChainRepository::class)->pushCriteria(new UsdtChainLimitCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
  166. return new CommonPagesResponse($res);
  167. }
  168. /**
  169. * usdt支付链添加
  170. *
  171. * @param ValidatorFactoryInterface $validatorFactory
  172. * @return mixed|PaymentSuccessUidResponse
  173. */
  174. public function chainAdd(ValidatorFactoryInterface $validatorFactory)
  175. {
  176. $inputData = $this->getJsonData();
  177. $rules = [
  178. 'usdt_type_uid' => 'required|string',
  179. 'chain_address' => 'required|string',
  180. ];
  181. $validator = $validatorFactory->make($inputData, $rules);
  182. if ($validator->fails()) {
  183. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  184. return json_fail($validator->errors()->first());
  185. }
  186. $setarr = [
  187. 'usdt_type_uid' => $inputData['usdt_type_uid'],
  188. 'chain_address' => $inputData['chain_address']
  189. ];
  190. $result = make(UsdtChainRepository::class)->create($setarr);
  191. if (!$result) {
  192. return json_fail('添加usdt支付链失败');
  193. } else {
  194. $logSubject = '添加了一个usdt支付链:' . $inputData['chain_address'];
  195. $logSvalue = $result->id;
  196. $logDbData = [
  197. 'after_items' => [
  198. $result->toArray(),
  199. ],
  200. 'before_items' => [],
  201. ];
  202. $this->log($logSubject, $logSvalue, $logDbData);
  203. return new PaymentSuccessUidResponse($result->manager_id);
  204. }
  205. }
  206. /**
  207. * usdt支付链修改
  208. *
  209. * @param ValidatorFactoryInterface $validatorFactory
  210. * @return mixed|CommonSuccessPkResponse
  211. */
  212. public function chainUpdate(ValidatorFactoryInterface $validatorFactory)
  213. {
  214. $inputData = $this->getJsonData();
  215. $rules = [
  216. 'uid' => 'required|string',
  217. 'usdt_type_uid' => 'nullable|string',
  218. 'chain_address' => 'nullable|string',
  219. 'is_lock' => 'nullable|in:1,2',
  220. ];
  221. $validator = $validatorFactory->make($inputData, $rules);
  222. if ($validator->fails()) {
  223. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  224. return json_fail($validator->errors()->first());
  225. }
  226. $usdtChainInfo = make(UsdtChainRepository::class)->findBy('uid', $inputData['uid']);
  227. if (!$usdtChainInfo) {
  228. return json_fail('该usdt支付链接不存在!');
  229. }
  230. $oldUsdtChainInfo = $usdtChainInfo;
  231. if (!empty($inputData['usdt_type_uid'])) {
  232. $usdtChainInfo->usdt_type_uid = $inputData['usdt_type_uid'];
  233. }
  234. if (!empty($inputData['chain_address'])) {
  235. $usdtChainInfo->chain_address = $inputData['chain_address'];
  236. }
  237. if (!empty($inputData['is_lock'])) {
  238. $usdtChainInfo->is_lock = $inputData['is_lock'];
  239. }
  240. $result = $usdtChainInfo->save();
  241. if (!$result) {
  242. return json_fail('修改失败');
  243. } else {
  244. $logSubject = '修改usdt链:'. $usdtChainInfo->chain_address;
  245. $logSvalue = $usdtChainInfo->id;
  246. $logDbData = [
  247. 'after_items' => [
  248. $usdtChainInfo->toArray(),
  249. ],
  250. 'before_items' => [
  251. $oldUsdtChainInfo->toArray(),
  252. ],
  253. ];
  254. $this->log($logSubject, $logSvalue, $logDbData);
  255. }
  256. return new CommonSuccessPkResponse($usdtChainInfo->id);
  257. }
  258. /**
  259. * usdt支付链删除
  260. *
  261. * @param ValidatorFactoryInterface $validatorFactory
  262. * @return mixed|CommonSuccessPkResponse
  263. */
  264. public function chainDelete(ValidatorFactoryInterface $validatorFactory)
  265. {
  266. $inputData = $this->getJsonData();
  267. $rules = [
  268. 'uid' => 'required|string',
  269. ];
  270. $validator = $validatorFactory->make($inputData, $rules);
  271. if ($validator->fails()) {
  272. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  273. return json_fail($validator->errors()->first());
  274. }
  275. $usdtChainInfo = make(UsdtChainRepository::class)->find($inputData['id']);
  276. if (!$usdtChainInfo) {
  277. return json_fail('该usdt支付链接不存在!');
  278. }
  279. $oldUsdtChainInfo = $usdtChainInfo;
  280. $result = $usdtChainInfo->delete();
  281. if (!$result) {
  282. return json_fail('删除失败');
  283. } else {
  284. $logSubject = '删除usdt支付链:'. $oldUsdtChainInfo->chain_address;
  285. $logSvalue = $oldUsdtChainInfo->id;
  286. $logDbData = [
  287. 'after_items' => [
  288. ],
  289. 'before_items' => [
  290. $oldUsdtChainInfo->toArray(),
  291. ],
  292. ];
  293. $this->log($logSubject, $logSvalue, $logDbData);
  294. return new CommonSuccessPkResponse($inputData['id']);
  295. }
  296. }
  297. /**
  298. * usdt类型下拉
  299. *
  300. * @return CommonPagesResponse
  301. */
  302. public function usdtTypeDown()
  303. {
  304. $res = make(UsdtTypeRepository::class)->all(['name', 'uid'])->keyBy('uid');
  305. return new CommonPagesResponse($res);
  306. }
  307. }