diff --git a/app/controller/api/user/Zhibo.php b/app/controller/api/user/Zhibo.php new file mode 100644 index 00000000..2d7a3299 --- /dev/null +++ b/app/controller/api/user/Zhibo.php @@ -0,0 +1,92 @@ + +// +---------------------------------------------------------------------- + +namespace app\controller\api\user; + +use crmeb\basic\BaseController; +use app\common\repositories\user\UserBillRepository; +use think\App; +use think\exception\HttpResponseException; +use think\exception\ValidateException; +use think\facade\Db; +use think\response\Json; + +/** + * Class Auth + * @package app\controller\api + * @author xaboy + * @day 2020-05-06 + */ +class Zhibo extends BaseController +{ + public function reward() + { + $params = $this->request->params(['live_stream_id', 'gift_id', 'master_id']); + $user = $this->request->userInfo(); + $params['live_name'] = '直播间名称'; + $params['gift_name'] = '礼物名称'; + $params['amount'] = 10; + if ($params['amount'] > $user['now_money']) { + return app('json')->fail('用户余额不足'); + } + //查询直播间及礼物接口,验证直播间信息及礼物信息 + + try { + Db::transaction(function () use ($user, $params) { + //打赏订单 + $orderId = Db::name('user_zhibo_order')->insertGetId([ + 'live_stream_id' => $params['live_stream_id'], + 'live_name' => $params['live_name'], + 'gift_id' => $params['gift_id'], + 'gift_name' => $params['gift_name'], + 'uid' => $user['uid'], + 'master_id' => $params['master_id'], + 'order_sn' => 'zb' . date('YmdHis') . mt_rand(1000, 9999), + 'amount' => $params['amount'], + 'pay_status' => 1, + 'pay_time' => date('Y-m-d H:i:s'), + 'create_time' => date('Y-m-d H:i:s'), + 'update_time' => date('Y-m-d H:i:s'), + ]); + //打赏人扣钱 + $user->now_money = bcsub($user['now_money'], $params['amount']); + $user->save(); + $userBillRepository = app()->make(UserBillRepository::class); + //打赏人账单 + $userBillRepository->decBill($user['uid'], 'now_money', 'zhibo_reward', [ + 'link_id' => $orderId, + 'status' => 1, + 'title' => '直播打赏支出', + 'number' => $params['amount'], + 'mark' => '余额打赏', + 'balance' => $user->now_money + ]); + //主播加钱 + Db::name('user')->where('uid', $params['master_id'])->inc('now_money', $params['amount'])->update(); + $master = Db::name('user')->where('uid', $params['master_id'])->find(); + //主播账单 + $userBillRepository->incBill($master['uid'], 'now_money', 'zhibo_reward', [ + 'link_id' => $orderId, + 'status' => 1, + 'title' => '直播打赏收入', + 'number' => $params['amount'], + 'mark' => '直播打赏收入', + 'balance' => $master['now_money'] + ]); + }); + } catch (Exception $e) { + return app('json')->fail($e->getMessage()); + } + + return app('json')->success($params); + } +} diff --git a/route/api.php b/route/api.php index ff9c030b..6cd8411a 100644 --- a/route/api.php +++ b/route/api.php @@ -77,6 +77,9 @@ Route::group('api/', function () { Route::post('user/margin', 'api.Auth/doMargin'); Route::get('user/margin/list', 'api.Auth/marginList'); + //绑定推荐人 + Route::post('zhibo/reward', 'api.user.Zhibo/reward'); + //优惠券 Route::group('coupon', function () { Route::post('receive/:id', 'api.store.product.StoreCoupon/receiveCoupon');