| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- namespace app\api\controller\user;
- use app\models\system\SystemGroupData;
- use app\models\user\User;
- use app\models\user\UserBill;
- use app\models\user\UserRecharge;
- use app\Request;
- use crmeb\services\GroupDataService;
- use crmeb\services\SystemConfigService;
- use crmeb\services\UtilService;
- /**
- * 充值类
- * Class UserRechargeController
- * @package app\api\controller\user
- */
- class UserRechargeController
- {
- /**
- * 小程序充值
- *
- * @param Request $request
- * @return mixed
- */
- public function routine(Request $request)
- {
- list($price, $recharId, $type) = UtilService::postMore([
- [['price', 'f'], 0],
- [['rechar_id', 'd'], 0],
- ['type', 0]
- ], $request, true);
- if (!$price || $price <= 0) return app('json')->fail('参数错误');
- $storeMinRecharge = sys_config('store_user_min_recharge');
- if ($price < $storeMinRecharge) return app('json')->fail('充值金额不能低于' . $storeMinRecharge);
- switch ((int)$type) {
- case 0: //支付充值余额
- $paid_price = 0;
- if ($recharId) {
- $data = SystemGroupData::getDateValue($recharId);
- if ($data === false) {
- return app('json')->fail('您选择的充值方式已下架!');
- } else {
- $paid_price = $data['give_money'] ?? 0;
- }
- }
- $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'routine', $paid_price);
- if (!$rechargeOrder) return app('json')->fail('充值订单生成失败!');
- try {
- return app('json')->successful(UserRecharge::jsPay($rechargeOrder));
- } catch (\Exception $e) {
- return app('json')->fail($e->getMessage());
- }
- break;
- case 1: //佣金转入余额
- if (UserRecharge::importNowMoney($request->uid(), $price))
- return app('json')->successful('转入余额成功');
- else
- return app('json')->fail(UserRecharge::getErrorInfo());
- break;
- default:
- return app('json')->fail('缺少参数');
- break;
- }
- }
- /**
- * 公众号充值
- *
- * @param Request $request
- * @return mixed
- */
- public function wechat(Request $request)
- {
- list($price, $recharId, $from, $type) = UtilService::postMore([
- [['price', 'f'], 0],
- [['rechar_id', 'd'], 0],
- ['from', 'weixin'],
- ['type', 0]
- ], $request, true);
- if (!$price || $price <= 0) return app('json')->fail('参数错误');
- $storeMinRecharge = sys_config('store_user_min_recharge');
- if ($price < $storeMinRecharge) return app('json')->fail('充值金额不能低于' . $storeMinRecharge);
- switch ((int)$type) {
- case 0: //支付充值余额
- $paid_price = 0;
- if ($recharId) {
- $data = SystemGroupData::getDateValue($recharId);
- if ($data === false) {
- return app('json')->fail('您选择的充值方式已下架!');
- } else {
- $paid_price = $data['give_money'] ?? 0;
- }
- }
- $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'weixin', $paid_price);
- if (!$rechargeOrder) return app('json')->fail('充值订单生成失败!');
- try {
- if ($from == 'weixinh5') {
- $recharge = UserRecharge::wxH5Pay($rechargeOrder);
- } else {
- $recharge = UserRecharge::wxPay($rechargeOrder);
- }
- } catch (\Exception $e) {
- return app('json')->fail($e->getMessage());
- }
- return app('json')->successful(['type' => $from, 'data' => $recharge]);
- break;
- case 1: //佣金转入余额
- if (UserRecharge::importNowMoney($request->uid(), $price))
- return app('json')->successful('转入余额成功');
- else
- return app('json')->fail(UserRecharge::getErrorInfo());
- break;
- default:
- return app('json')->fail('缺少参数');
- break;
- }
- }
- public function trade_money(Request $request)
- {
- $uid = $request->post('to_uid', 0);
- $num = $request->post('num', 0);
- $type = $request->post('type', 0);
- if ($num < 100) return app('json')->fail('至少100余额才可以转账');
- //$uid = User::where('phone', $uid)->value('uid');
- if (!$uid || !$trader = User::where('uid|phone',$uid)->find()) return app('json')->fail('接收用户不存在');
- $user = User::get($request->uid());
- $field = "brokerage_price";
- $title = '佣金'; $bill_type = "brokerage";$bill_type_ex = "extract";
- if($type == 1)
- {
- $field = "now_money";
- $bill_type ="recharge";
- $bill_type_ex ="pay_money";
- $title = '余额';
- if ($user['now_money'] < $num) return app('json')->fail('余额不足');
- }elseif($type==0)
- {
- if ($user['brokerage_price'] < $num) return app('json')->fail('余额不足');
- }
- User::beginTrans();
- try {
- $real_get = bcsub($num, bcmul(bcdiv(sys_config('trade_commission', 0), 100, 4), $num, 2), 2);
- UserBill::expend('转账'.$title, $request->uid(), 'now_money', $bill_type_ex, $num, 0, $user[$field] - $num, '转账给' . $trader['phone'] . '|' . $uid . $num );
- UserBill::income('转账'.$title, $trader['uid'], 'now_money', $bill_type, $real_get, 0, $trader[$field] + (float)$real_get, '转账自' . $user['phone'] . '|' . $user['uid'] . $num . '元,扣除手续费后实际到账' . $real_get );
- User::bcDec($request->uid(), $field, $num, 'uid');
- User::bcInc($trader['uid'], $field, $real_get, 'uid');
- User::commitTrans();
- return app('json')->success('转账成功');
- } catch (\Exception $e) {
- User::rollbackTrans();
- return app('json')->fail($e->getMessage());
- }
- }
- /**
- * 充值额度选择
- * @return mixed
- */
- public function index()
- {
- $rechargeQuota = sys_data('user_recharge_quota') ?? [];
- $data['recharge_quota'] = $rechargeQuota;
- $recharge_attention = sys_config('recharge_attention');
- $recharge_attention = explode("\n", $recharge_attention);
- $data['recharge_attention'] = $recharge_attention;
- return app('json')->successful($data);
- }
- }
|