// +---------------------------------------------------------------------- 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; use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Client; /** * Class Auth * @package app\controller\api * @author xaboy * @day 2020-05-06 */ class Zhibo extends BaseController { //用户直播送礼 public function reward() { $user = $this->request->userInfo(); $params = $this->request->params(['live_stream_id', 'master_id', 'live_name', 'gift_id', 'gift_name', 'gift_num', 'amount']); if (empty($params['live_stream_id']) || empty($params['master_id']) || empty($params['gift_id']) || empty($params['live_name']) || empty($params['gift_name']) || empty($params['amount'])) { return app('json')->fail('live_stream_id, master_id, live_name, gift_id, gift_name, gift_num, amount 参数不能为空'); } 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'], 'gift_num' => $params['gift_num'], '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_dec', [ '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_inc', [ '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('送礼成功'); } //用户送礼收礼记录 public function rewardList() { [$page, $limit] = $this->getPage(); $token = request()->header('x-token'); $params = $this->request->params(['type']); if (empty($params['type'])) { return app('json')->fail('类型不能为空'); } $user = $this->request->userInfo(); $where = []; if ($params['type'] == 1) { // 送出的礼物 $where['ub.pm'] = 0; $where['ub.type'] = 'zhibo_reward'; $where['uzo.uid'] = $user['uid']; } elseif ($params['type'] == 2){ // 收到的礼物 $where['ub.pm'] = 1; $where['ub.type'] = 'zhibo_reward'; $where['uzo.master_id'] = $user['uid']; } else { return app('json')->fail('类型错误'); } $count = Db::name('user_bill')->alias('ub')->leftJoin('user_zhibo_order uzo','uzo.order_id = ub.link_id')->where($where)->count(); $list = Db::name('user_bill')->alias('ub')->leftJoin('user_zhibo_order uzo','uzo.order_id = ub.link_id')->where($where)->limit(($page-1) * $limit, $limit)->field([ 'uzo.order_id', 'uzo.live_stream_id', 'uzo.live_name', 'uzo.gift_id', 'uzo.gift_name', 'uzo.order_sn', 'uzo.amount', 'ub.title', 'uzo.create_time' ])->select(); return app('json')->success(compact('count', 'list')); } }