173 lines
7.3 KiB
PHP
Executable File
173 lines
7.3 KiB
PHP
Executable File
<?php
|
||
|
||
// +----------------------------------------------------------------------
|
||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||
// +----------------------------------------------------------------------
|
||
// | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
|
||
// +----------------------------------------------------------------------
|
||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||
// +----------------------------------------------------------------------
|
||
// | Author: CRMEB Team <admin@crmeb.com>
|
||
// +----------------------------------------------------------------------
|
||
|
||
namespace app\controller\api\user;
|
||
|
||
use crmeb\basic\BaseController;
|
||
use app\common\repositories\user\UserBillRepository;
|
||
use app\common\repositories\system\RelevanceRepository;
|
||
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('余额不足', ['balance_status' => -1]);
|
||
}
|
||
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']);
|
||
$user = $this->request->userInfo();
|
||
$where = [];
|
||
$queryBuilder = Db::name('user_bill')->alias('ub')->leftJoin('user_zhibo_order uzo','uzo.order_id = ub.link_id')->leftJoin('user u','u.uid = uzo.uid');
|
||
if ($params['type'] == 1) {
|
||
// 送出的礼物
|
||
$where['ub.pm'] = 0;
|
||
$where['ub.type'] = 'zhibo_reward_dec';
|
||
$where['uzo.uid'] = $user['uid'];
|
||
$queryBuilder = $queryBuilder->where($where);
|
||
} elseif ($params['type'] == 2){
|
||
// 收到的礼物
|
||
$where['ub.pm'] = 1;
|
||
$where['ub.type'] = 'zhibo_reward_inc';
|
||
$where['uzo.master_id'] = $user['uid'];
|
||
$queryBuilder = $queryBuilder->where($where);
|
||
} else {
|
||
$whereUser['ub.pm'] = 0;
|
||
$whereUser['ub.type'] = 'zhibo_reward_dec';
|
||
$whereUser['uzo.uid'] = $user['uid'];
|
||
$whereMaster['ub.pm'] = 1;
|
||
$whereMaster['ub.type'] = 'zhibo_reward_inc';
|
||
$whereMaster['uzo.master_id'] = $user['uid'];
|
||
|
||
$queryBuilder = $queryBuilder->where(function ($query) use($whereUser) {
|
||
$query->where($whereUser);
|
||
});
|
||
$queryBuilder = $queryBuilder->whereOr(function ($query) use($whereMaster) {
|
||
$query->where($whereMaster);
|
||
});
|
||
}
|
||
$count = $queryBuilder->fetchSql(false)->count();
|
||
$list = $queryBuilder->limit(($page-1) * $limit, $limit)->setOption('field',[])->field([
|
||
'uzo.uid',
|
||
'uzo.master_id',
|
||
'u.nickname',
|
||
'u.avatar',
|
||
'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'));
|
||
}
|
||
|
||
//用户送礼收礼汇总
|
||
public function rewardAmount()
|
||
{
|
||
$liveStreamId = $this->request->param('live_stream_id', 0);
|
||
$gift_num = Db::name('user_zhibo_order')->where('live_stream_id', $liveStreamId)->sum('gift_num');
|
||
$gift_amount = Db::name('user_zhibo_order')->where('live_stream_id', $liveStreamId)->sum('amount');
|
||
return app('json')->success(compact('gift_num', 'gift_amount'));
|
||
}
|
||
|
||
// 获取主播粉丝关注数
|
||
public function fansNum()
|
||
{
|
||
$params = $this->request->params(['master_id']);
|
||
if (empty($params['master_id'])) {
|
||
return app('json')->fail('主播用户id不能为空');
|
||
}
|
||
$data['fan_num'] = app()->make(RelevanceRepository::class)->getUserFans(intval($params['master_id']), 1, 1, 1);
|
||
$data['focus_num'] = app()->make(RelevanceRepository::class)->getUserFocus(intval($params['master_id']), 1, 1, 1);
|
||
return app('json')->success($data);
|
||
}
|
||
|
||
}
|