93 lines
3.8 KiB
PHP
93 lines
3.8 KiB
PHP
<?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;
|
||
|
||
/**
|
||
* 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);
|
||
}
|
||
}
|