This commit is contained in:
parent
29ace7e8ec
commit
0a26e53c7c
|
|
@ -0,0 +1,244 @@
|
|||
<?php
|
||||
|
||||
namespace app\czg\app\controller;
|
||||
|
||||
use app\api\model\Orders;
|
||||
use app\api\model\UserMoney;
|
||||
use app\common\controller\BaseController;
|
||||
use app\common\library\DatabaseRoute;
|
||||
use app\czg\app\model\CommonInfo;
|
||||
use app\czg\app\model\TbUser;
|
||||
use app\enums\ErrEnums;
|
||||
use app\exception\SysException;
|
||||
use app\utils\RedisUtils;
|
||||
use ba\Random;
|
||||
use support\Log;
|
||||
use think\facade\Db;
|
||||
|
||||
class OrderController extends BaseController
|
||||
{
|
||||
// protected array $noNeedLogin = ["payOrders"];
|
||||
|
||||
protected array $noNeedPermission = [];
|
||||
|
||||
private function checkWxBind()
|
||||
{
|
||||
$val = (new CommonInfo())->getByCode(938);
|
||||
if ($val && $val['value'] == '1') {
|
||||
$openId = $this->auth->getUserInfo()['wx_open_id'];
|
||||
if (!$openId) {
|
||||
throw new SysException("请先绑定微信", 407);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function insertCourseOrders()
|
||||
{
|
||||
|
||||
$params = request()->get();
|
||||
|
||||
$userInfo = $this->auth->getUser();
|
||||
debounce('insertCourseOrders'.$userInfo['user_id']);
|
||||
Log::info('生成商品订单信息接口入参为: {}', $params);
|
||||
|
||||
self::checkWxBind();
|
||||
|
||||
if (!RedisUtils::checkCanCreateOrder($userInfo['user_id'])) {
|
||||
throw new SysException(ErrEnums::MANY_REQUEST);
|
||||
}
|
||||
|
||||
$val = (new CommonInfo())->getByCodeToInt(934);
|
||||
RedisUtils::setUserCanCreateOrder($userInfo['user_id'], $val);
|
||||
|
||||
TbUser::checkEnable($userInfo);
|
||||
|
||||
|
||||
// 查询用户是否购买整部短剧
|
||||
$courseUserCount = DatabaseRoute::getDb('course_user', $userInfo['user_id'])->where([
|
||||
'course_id' => $params['courseId'],
|
||||
'user_id' => $userInfo['user_id'],
|
||||
'classify' => 1
|
||||
])->count();
|
||||
if ($courseUserCount > 0 ) {
|
||||
$this->success('成功', [
|
||||
'status' => 1
|
||||
]);
|
||||
}
|
||||
|
||||
if (!empty($params['courseDetailsId'])) {
|
||||
// 查询短剧
|
||||
$courseDetail = DatabaseRoute::getDb('course_details', [
|
||||
'course_id' => $params['courseId'],
|
||||
])->where([
|
||||
'course_id' => $params['courseId'],
|
||||
'course_details_id' => $params['courseDetailsId'],
|
||||
])->find();
|
||||
if (!$courseDetail) {
|
||||
throw new SysException("未知短剧");
|
||||
}
|
||||
|
||||
if ($courseDetail['is_price'] == 2) {
|
||||
$this->success('成功', [
|
||||
'status' => 1
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
// 查询是否已购买单集
|
||||
$courseUserCount2 = DatabaseRoute::getDb('course_user', $userInfo['user_id'])->where([
|
||||
'course_id' => $params['courseId'],
|
||||
'user_id' => $userInfo['user_id'],
|
||||
'course_details_id' => $params['courseDetailsId'],
|
||||
'classify' => 2
|
||||
])->count();
|
||||
|
||||
if ($courseUserCount2 > 0 ) {
|
||||
$this->success('成功', [
|
||||
'status' => 1
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//查询短剧信息
|
||||
$course = Db::name('course')->where([
|
||||
'course_id' => $params['courseId']
|
||||
])->find();
|
||||
if (!$course) {
|
||||
throw new SysException("短剧不存在");
|
||||
}
|
||||
|
||||
// 订单编号
|
||||
$ordersNo = uuid();
|
||||
|
||||
// 获取金币与金额比例
|
||||
$commonInfo = (new CommonInfo())->getByCode(914);
|
||||
$ratio = $commonInfo ? floatval($commonInfo['value']) : 1;
|
||||
|
||||
if (!empty($params['courseDetailsId'])) {
|
||||
$courseDetail = DatabaseRoute::getDb('course_details', [
|
||||
'course_id' => $params['courseId']
|
||||
])->where('course_details_id', $params['courseDetailsId'])
|
||||
->find();
|
||||
|
||||
if (!$courseDetail) {
|
||||
throw new SysException('短剧详情不存在');
|
||||
}
|
||||
|
||||
$payMoney = floatval($courseDetail['price']);
|
||||
$payDiamond = bcmul($payMoney, $ratio);
|
||||
} else {
|
||||
if (!isset($course['price']) || floatval($course['price']) <= 0) {
|
||||
throw new SysException("该剧暂不支持整剧购买方式!");
|
||||
}
|
||||
|
||||
$payMoney = floatval($course['price']);
|
||||
$payDiamond = bcmul($payMoney, $ratio);
|
||||
}
|
||||
|
||||
// 订单数据组装
|
||||
$orders = [
|
||||
'orders_id' => Random::generateRandomPrefixedId(18),
|
||||
'orders_no' => $ordersNo,
|
||||
'user_id' => $userInfo['user_id'],
|
||||
'course_id' => $params['courseId'],
|
||||
'course_details_id' => $params['courseDetailsId'] ?? null,
|
||||
'pay_money' => $payMoney,
|
||||
'pay_diamond' => $payDiamond,
|
||||
'status' => 0,
|
||||
'create_time' => date('Y-m-d H:i:s'),
|
||||
'orders_type' => 1,
|
||||
];
|
||||
|
||||
Orders::fillSysUserId($orders, $userInfo);
|
||||
// 插入订单
|
||||
$count = DatabaseRoute::getDb('orders', $userInfo['user_id'], true)->insert($orders);
|
||||
|
||||
$result = [
|
||||
'flag' => 2,
|
||||
'orders' => convertToCamelCase($orders),
|
||||
];
|
||||
|
||||
if ($count) {
|
||||
$this->successWithData($result);
|
||||
} else {
|
||||
$this->error();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function payOrders()
|
||||
{
|
||||
$orderId = $this->request->post('orderId');
|
||||
$userInfo = $this->auth->getUser();
|
||||
// $userId = $this->getUserId();
|
||||
$order = DatabaseRoute::getDb('orders', $userInfo['user_id'])->where([
|
||||
'orders_id' => $orderId
|
||||
])->find();
|
||||
if (!$order || $order['status'] != 0) {
|
||||
$this->error('订单不存在或已支付');
|
||||
}
|
||||
|
||||
if ($order['orders_type'] == 1) {
|
||||
if (!empty($order['course_details_id'])) {
|
||||
$count = DatabaseRoute::getDb('course_user', $userInfo['user_id'])->where([
|
||||
'classify' => 2,
|
||||
'course_id' => $order['course_id'],
|
||||
'course_details_id' => $order['course_details_id']
|
||||
])->count();
|
||||
}else if (!empty($order['course_details_ids'])) {
|
||||
$courseDetailsIds = json_decode($order['course_details_ids'], true);
|
||||
$count = DatabaseRoute::getDb('course_user', $userInfo['user_id'])->where([
|
||||
'course_id' => $order['course_id'],
|
||||
['course_details_id', 'in', $courseDetailsIds]
|
||||
])->count();
|
||||
}else {
|
||||
$count = DatabaseRoute::getDb('course_user', $userInfo['user_id'])->where([
|
||||
'classify' => 1,
|
||||
'course_id' => $order['course_id']
|
||||
])->count();
|
||||
}
|
||||
if ($count) {
|
||||
$this->errorMsg('您已购买,请不要重复点击');
|
||||
}
|
||||
}
|
||||
|
||||
$userMoney = UserMoney::selectUserMoney($userInfo['user_id'])['data'];
|
||||
if (bccomp($userMoney['money'], $order['pay_diamond']) < 0) {
|
||||
$this->errorMsg('账户不足,请充值');
|
||||
}
|
||||
|
||||
UserMoney::updateMoney($userInfo['user_id'], $order['pay_diamond'], false);
|
||||
|
||||
DatabaseRoute::getDb('user_money_details', $userInfo['user_id'], true)->insert([
|
||||
'money' => $order['pay_diamond'],
|
||||
'user_id' => $userInfo['user_id'],
|
||||
'content' => '解锁成功',
|
||||
'title' => '金币解锁视频',
|
||||
'type' => 2,
|
||||
'classify' => 3,
|
||||
'create_time' => date('Y-m-d H:i:s'),
|
||||
'money_type' => 2
|
||||
]);
|
||||
|
||||
$order['pay_way'] = 6;
|
||||
$order['diamond'] = 1;
|
||||
$order['status'] = 1;
|
||||
$order['pay_time'] = getNormalDate();
|
||||
Orders::fillSysUserId($order, $userInfo);
|
||||
|
||||
DatabaseRoute::getDb('orders', $userInfo['user_id'], true, true)->where([
|
||||
'orders_id' => $order['orders_id']
|
||||
])->update($order);
|
||||
|
||||
Orders::insertOrders($order);
|
||||
|
||||
$this->successWithData(1);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
1378
app/functions.php
1378
app/functions.php
File diff suppressed because it is too large
Load Diff
|
|
@ -30,7 +30,8 @@
|
|||
"webman/think-orm": "^2.1",
|
||||
"vlucas/phpdotenv": "^5.6",
|
||||
"webman/think-cache": "^2.1",
|
||||
"firebase/php-jwt": "^6.11"
|
||||
"firebase/php-jwt": "^6.11",
|
||||
"ext-bcmath": "*"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-event": "For better performance. "
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
/**
|
||||
* index.php - Web 面板启动 Workerman/Webman
|
||||
*/
|
||||
|
||||
use support\App;
|
||||
|
||||
chdir(__DIR__);
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
ini_set('display_errors', 'on');
|
||||
error_reporting(E_ALL);
|
||||
|
||||
// 进程文件
|
||||
$runtimeProcessPath = __DIR__ . '/../runtime/windows';
|
||||
$processFiles = glob($runtimeProcessPath . '/start_*.php');
|
||||
App::loadAllConfig(['route']);
|
||||
|
||||
App::run();
|
||||
?>
|
||||
Loading…
Reference in New Issue