UserRechargeController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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,$open_id] = $request->postMore([
  42. ['price', 0],
  43. ['rechar_id', 0],
  44. ['type', 0],
  45. ['from', 'weixin'],
  46. ['open_id','']
  47. ], true);
  48. if (!$price || $price <= 0) return app('json')->fail(410122);
  49. if (!in_array($type, [0, 1])) return app('json')->fail(410123);
  50. if (!in_array($from, [PayServices::WEIXIN_PAY, 'weixinh5', 'routine', PayServices::ALIAPY_PAY])) return app('json')->fail(410123);
  51. $storeMinRecharge = sys_config('store_user_min_recharge');
  52. if (!$recharId && $price < $storeMinRecharge) return app('json')->fail(410124, null, ['money' => $storeMinRecharge]);
  53. $uid = (int)$request->uid();
  54. $re = $this->services->recharge($uid, $price, $recharId, $type, $from, true,$open_id);
  55. if ($re) {
  56. $payType = $re['pay_type'] ?? '';
  57. unset($re['pay_type']);
  58. return app('json')->status($payType, 410125, $re);
  59. }
  60. return app('json')->fail(410126);
  61. }
  62. /**
  63. * TODO 小程序充值 弃用
  64. * @param Request $request
  65. * @return mixed
  66. */
  67. public function routine(Request $request)
  68. {
  69. list($price, $recharId, $type) = $request->postMore([['price', 0], ['rechar_id', 0], ['type', 0]], true);
  70. if (!$price || $price <= 0) return app('json')->fail(410122);
  71. $storeMinRecharge = sys_config('store_user_min_recharge');
  72. if ($price < $storeMinRecharge) return app('json')->fail(410124, null, ['money' => $storeMinRecharge]);
  73. $from = 'routine';
  74. $uid = (int)$request->uid();
  75. $re = $this->services->recharge($uid, $price, $recharId, $type, $from);
  76. if ($re) {
  77. unset($re['msg']);
  78. return app('json')->success(410125, $re['data']);
  79. }
  80. return app('json')->fail(410126);
  81. }
  82. /**
  83. * TODO 公众号充值 弃用
  84. * @param Request $request
  85. * @return mixed
  86. */
  87. public function wechat(Request $request)
  88. {
  89. list($price, $recharId, $from, $type) = $request->postMore([['price', 0], ['rechar_id', 0], ['from', 'weixin'], ['type', 0]], true);
  90. if (!$price || $price <= 0) return app('json')->fail(410122);
  91. $storeMinRecharge = sys_config('store_user_min_recharge');
  92. if ($price < $storeMinRecharge) return app('json')->fail(410124, null, ['money' => $storeMinRecharge]);
  93. $uid = (int)$request->uid();
  94. $re = $this->services->recharge($uid, $price, $recharId, $type, $from);
  95. if ($re) {
  96. unset($re['msg']);
  97. return app('json')->success(410125, $re);
  98. }
  99. return app('json')->fail(410126);
  100. }
  101. /**
  102. * 充值额度选择
  103. * @return mixed
  104. */
  105. public function index()
  106. {
  107. $rechargeQuota = sys_data('user_recharge_quota') ?? [];
  108. $data['recharge_quota'] = $rechargeQuota;
  109. $recharge_attention = sys_config('recharge_attention');
  110. $recharge_attention = explode("\n", $recharge_attention);
  111. $data['recharge_attention'] = $recharge_attention;
  112. return app('json')->success($data);
  113. }
  114. }