CustomRechargeController.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\custom\classes\CustomClass;
  4. use bizGhs\custom\classes\CustomRechargeClass;
  5. use common\components\util;
  6. use Yii;
  7. class CustomRechargeController extends BaseController
  8. {
  9. public $guestAccess = [];
  10. //充值变动明细
  11. public function actionList()
  12. {
  13. $get = Yii::$app->request->get();
  14. $customId = $get['customId'] ?? 0;
  15. $custom = CustomClass::getById($customId);
  16. CustomClass::valid($custom, $this->shopId);
  17. $where = [];
  18. $where['customId'] = $customId;
  19. $list = CustomRechargeClass::getRechargeList($where);
  20. util::success($list);
  21. }
  22. /**
  23. * 充值退款:财务权限;线上原路退,线下扣减余额并记变动明细。
  24. */
  25. public function actionRefund()
  26. {
  27. $staff = $this->shopAdmin;
  28. if (!isset($staff->finance) || intval($staff->finance) === 0) {
  29. util::fail('请有财务权限的人操作');
  30. }
  31. $post = Yii::$app->request->post();
  32. $id = intval($post['id'] ?? 0);
  33. if ($id <= 0) {
  34. util::fail('参数错误');
  35. }
  36. $cacheKey = 'ghs_custom_recharge_refund_' . $id;
  37. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  38. if (!empty($has)) {
  39. util::fail('请5秒之后再提交');
  40. }
  41. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  42. $connection = Yii::$app->db;
  43. $transaction = $connection->beginTransaction();
  44. try {
  45. CustomRechargeClass::refundPaidRecharge($id, $this->shop, $staff);
  46. $transaction->commit();
  47. util::complete('退款成功');
  48. } catch (\Exception $e) {
  49. $transaction->rollBack();
  50. Yii::info('充值退款失败:' . $e->getMessage());
  51. util::fail($e->getMessage() ?: '退款失败');
  52. }
  53. }
  54. }