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()
|
|
|
|
|
{
|
|
|
|
|
$params = $this->request->params(['live_stream_id', 'gift_id', 'master_id']);
|
2023-10-26 14:17:47 +08:00
|
|
|
|
$user = $this->request->userInfo();
|
|
|
|
|
if (empty($params['live_stream_id']) || empty($params['master_id']) || empty($params['gift_id'])) {
|
|
|
|
|
return app('json')->fail('live_stream_id, master_id, gift_id 参数不能为空');
|
2023-10-26 12:01:13 +08:00
|
|
|
|
}
|
2023-10-26 14:17:47 +08:00
|
|
|
|
$token = request()->header('x-token');
|
2023-10-26 12:01:13 +08:00
|
|
|
|
//查询直播间及礼物接口,验证直播间信息及礼物信息
|
2023-10-26 14:17:47 +08:00
|
|
|
|
$checkUrl = 'https://ceshi-zhibo.lihaink.cn/api/zhibo/checkGift';
|
|
|
|
|
$client = new Client();
|
|
|
|
|
$response = $client->request('POST', $checkUrl, [
|
|
|
|
|
'verify' => false,
|
|
|
|
|
'headers' => ['X-Token' => $token],
|
|
|
|
|
'json' => [
|
|
|
|
|
"live_stream_id" => $params['live_stream_id'],
|
|
|
|
|
"master_id" => $params['master_id'],
|
|
|
|
|
"gift_id" => $params['gift_id']
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
$responseData = json_decode($response->getBody()->getContents(), true);
|
|
|
|
|
if (empty($responseData['code'])) {
|
|
|
|
|
$failMsg = $responseData['msg'];
|
|
|
|
|
return app('json')->fail($failMsg);
|
|
|
|
|
}
|
|
|
|
|
$giftData = $responseData['data'];
|
|
|
|
|
$params['live_name'] = $giftData['live_name'];
|
|
|
|
|
$params['gift_name'] = $giftData['gift_name'];
|
|
|
|
|
$params['amount'] = $giftData['gift_price'];
|
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'],
|
|
|
|
|
'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,
|
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();
|
|
|
|
|
//主播账单
|
|
|
|
|
$userBillRepository->incBill($master['uid'], 'now_money', 'zhibo_reward', [
|
|
|
|
|
'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 14:17:47 +08:00
|
|
|
|
$message = "{$user['nickname']}送出了{$params['gift_name']}";
|
|
|
|
|
//发送礼物消息
|
|
|
|
|
$giftUrl = 'https://ceshi-zhibo.lihaink.cn/api/zhibo/sendGiftMessage';
|
|
|
|
|
$client = new Client();
|
|
|
|
|
$client->request('POST', $giftUrl, [
|
|
|
|
|
'verify' => false,
|
|
|
|
|
'headers' => ['X-Token' => $token],
|
|
|
|
|
'json' => [
|
|
|
|
|
"live_stream_id" => $params['live_stream_id'],
|
|
|
|
|
"app_name" => 'shop',
|
|
|
|
|
"message" => $message
|
|
|
|
|
]
|
|
|
|
|
]);
|
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('类型不能为空');
|
|
|
|
|
}
|
|
|
|
|
$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();
|
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'
|
|
|
|
|
])->fetchSql(false)->select();
|
|
|
|
|
return app('json')->success(compact('count', 'list'));
|
2023-10-26 12:01:13 +08:00
|
|
|
|
}
|
|
|
|
|
}
|