133 lines
5.6 KiB
PHP
Raw Normal View History

2023-10-26 12:01:13 +08:00
<?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 think\App;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\facade\Db;
use think\response\Json;
2023-10-26 14:17:47 +08:00
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
2023-10-26 12:01:13 +08:00
/**
* Class Auth
* @package app\controller\api
* @author xaboy
* @day 2020-05-06
*/
class Zhibo extends BaseController
{
2023-10-26 14:17:47 +08:00
//用户直播送礼
2023-10-26 12:01:13 +08:00
public function reward()
{
2023-10-26 14:17:47 +08:00
$user = $this->request->userInfo();
2023-10-27 09:56:45 +08:00
$params = $this->request->params(['live_stream_id', 'master_id', 'live_name', 'gift_id', 'gift_name', 'gift_num', 'amount']);
2023-10-26 16:43:58 +08:00
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'])) {
2023-10-27 09:56:45 +08:00
return app('json')->fail('live_stream_id, master_id, live_name, gift_id, gift_name, gift_num, amount 参数不能为空');
2023-10-26 14:17:47 +08:00
}
2023-10-26 15:56:14 +08:00
if ($params['amount'] > $user['now_money']) {
return app('json')->fail('余额不足');
}
2023-10-26 12:01:13 +08:00
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'],
2023-10-27 09:56:45 +08:00
'gift_num' => $params['gift_num'],
2023-10-26 12:01:13 +08:00
'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);
//打赏人账单
2023-10-28 09:38:06 +08:00
$userBillRepository->decBill($user['uid'], 'now_money', 'zhibo_reward_dec', [
2023-10-26 12:01:13 +08:00
'link_id' => $orderId,
'status' => 1,
2023-10-26 12:56:33 +08:00
'title' => '直播送礼支出',
2023-10-26 12:01:13 +08:00
'number' => $params['amount'],
2023-10-26 12:56:33 +08:00
'mark' => '余额送礼',
2023-10-26 12:01:13 +08:00
'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();
//主播账单
2023-10-28 09:38:06 +08:00
$userBillRepository->incBill($master['uid'], 'now_money', 'zhibo_reward_inc', [
2023-10-26 12:01:13 +08:00
'link_id' => $orderId,
'status' => 1,
2023-10-26 12:56:33 +08:00
'title' => '直播送礼收入',
2023-10-26 12:01:13 +08:00
'number' => $params['amount'],
2023-10-26 12:56:33 +08:00
'mark' => '直播送礼收入',
2023-10-26 12:01:13 +08:00
'balance' => $master['now_money']
]);
});
} catch (Exception $e) {
return app('json')->fail($e->getMessage());
}
2023-10-26 12:56:33 +08:00
return app('json')->success('送礼成功');
}
2023-10-26 14:17:47 +08:00
//用户送礼收礼记录
2023-10-26 12:56:33 +08:00
public function rewardList()
{
[$page, $limit] = $this->getPage();
2023-10-26 14:17:47 +08:00
$token = request()->header('x-token');
2023-10-26 12:56:33 +08:00
$params = $this->request->params(['type']);
if (empty($params['type'])) {
return app('json')->fail('类型不能为空');
}
2023-10-26 15:39:42 +08:00
$user = $this->request->userInfo();
2023-10-26 12:56:33 +08:00
$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();
2023-10-26 14:17:47 +08:00
$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([
2023-10-26 12:56:33 +08:00
'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'
2023-10-26 15:39:42 +08:00
])->select();
2023-10-26 12:56:33 +08:00
return app('json')->success(compact('count', 'list'));
2023-10-26 12:01:13 +08:00
}
}