UserRechargeController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\api\controller\v1\user;
  12. use app\Request;
  13. use app\services\pay\PayServices;
  14. use app\services\user\UserRechargeServices;
  15. /**
  16. * 充值类
  17. * Class UserRechargeController
  18. * @package app\api\controller\user
  19. */
  20. class UserRechargeController
  21. {
  22. protected $services = NUll;
  23. /**
  24. * UserRechargeController constructor.
  25. * @param UserRechargeServices $services
  26. */
  27. public function __construct(UserRechargeServices $services)
  28. {
  29. $this->services = $services;
  30. }
  31. /**
  32. * 用户充值
  33. * @param Request $request
  34. * @return mixed
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\DbException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. */
  39. public function recharge(Request $request)
  40. {
  41. [$price, $recharId, $type, $from] = $request->postMore([
  42. ['price', 0],
  43. ['rechar_id', 0],
  44. ['type', 0],
  45. ['from', 'weixin']
  46. ], true);
  47. if (!$price || $price <= 0) return app('json')->fail(410122);
  48. if (!in_array($type, [0, 1])) return app('json')->fail(410123);
  49. if (!in_array($from, [PayServices::WEIXIN_PAY, 'weixinh5', 'routine', PayServices::ALIAPY_PAY])) return app('json')->fail(410123);
  50. $storeMinRecharge = sys_config('store_user_min_recharge');
  51. if (!$recharId && $price < $storeMinRecharge) return app('json')->fail(410124, null, ['money' => $storeMinRecharge]);
  52. $uid = (int)$request->uid();
  53. $re = $this->services->recharge($uid, $price, $recharId, $type, $from, true);
  54. if ($re) {
  55. $payType = $re['pay_type'] ?? '';
  56. unset($re['pay_type']);
  57. return app('json')->status($payType, 410125, $re);
  58. }
  59. return app('json')->fail(410126);
  60. }
  61. /**
  62. * TODO 小程序充值 弃用
  63. * @param Request $request
  64. * @return mixed
  65. */
  66. public function routine(Request $request)
  67. {
  68. list($price, $recharId, $type) = $request->postMore([['price', 0], ['rechar_id', 0], ['type', 0]], true);
  69. if (!$price || $price <= 0) return app('json')->fail(410122);
  70. $storeMinRecharge = sys_config('store_user_min_recharge');
  71. if ($price < $storeMinRecharge) return app('json')->fail(410124, null, ['money' => $storeMinRecharge]);
  72. $from = 'routine';
  73. $uid = (int)$request->uid();
  74. $re = $this->services->recharge($uid, $price, $recharId, $type, $from);
  75. if ($re) {
  76. unset($re['msg']);
  77. return app('json')->success(410125, $re['data']);
  78. }
  79. return app('json')->fail(410126);
  80. }
  81. /**
  82. * TODO 公众号充值 弃用
  83. * @param Request $request
  84. * @return mixed
  85. */
  86. public function wechat(Request $request)
  87. {
  88. list($price, $recharId, $from, $type) = $request->postMore([['price', 0], ['rechar_id', 0], ['from', 'weixin'], ['type', 0]], true);
  89. if (!$price || $price <= 0) return app('json')->fail(410122);
  90. $storeMinRecharge = sys_config('store_user_min_recharge');
  91. if ($price < $storeMinRecharge) return app('json')->fail(410124, null, ['money' => $storeMinRecharge]);
  92. $uid = (int)$request->uid();
  93. $re = $this->services->recharge($uid, $price, $recharId, $type, $from);
  94. if ($re) {
  95. unset($re['msg']);
  96. return app('json')->success(410125, $re);
  97. }
  98. return app('json')->fail(410126);
  99. }
  100. /**
  101. * 充值额度选择
  102. * @return mixed
  103. */
  104. public function index()
  105. {
  106. $rechargeQuota = sys_data('user_recharge_quota') ?? [];
  107. $data['recharge_quota'] = $rechargeQuota;
  108. $recharge_attention = sys_config('recharge_attention');
  109. $recharge_attention = explode("\n", $recharge_attention);
  110. $data['recharge_attention'] = $recharge_attention;
  111. return app('json')->success($data);
  112. }
  113. }