|
|
@@ -4,7 +4,12 @@ namespace app\api\controller;
|
|
|
|
|
|
use app\common\controller\Api;
|
|
|
use app\common\model\Config;
|
|
|
+use app\common\model\Goods;
|
|
|
+use app\common\model\MoneyLog;
|
|
|
use app\common\model\Order AS OrderModel;
|
|
|
+use app\common\model\Users;
|
|
|
+use think\Db;
|
|
|
+use think\Exception;
|
|
|
|
|
|
/**
|
|
|
* 首页接口
|
|
|
@@ -68,14 +73,96 @@ class Order extends Api
|
|
|
public function get()
|
|
|
{
|
|
|
$user = $this->auth->getUser();
|
|
|
-
|
|
|
+ if($user['open_task'] != 1){
|
|
|
+ $this->error(__('暂停抢单'));
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!($user['balance'] > 0)){
|
|
|
+ $this->error(__('余额不足'));
|
|
|
+ }
|
|
|
|
|
|
- $data['order_no'] = 'sn45784545';
|
|
|
- $data['title'] = '商品标题111';
|
|
|
- $data['amount'] = 4545;
|
|
|
- $data['bonus'] = $user['bonus_sum'];
|
|
|
+ $day_tasks_num = (new Config())->getValue('day_tasks_num');//单日任务数
|
|
|
+ $task_income = (new Config())->getValue('task_income');//单次收益
|
|
|
+ $amount_mini = $user['balance'] * 0.4;
|
|
|
+ $amount_max = $user['balance'] * 0.8;
|
|
|
|
|
|
- $this->success('', $data);
|
|
|
+ if($user['task_num'] >= $day_tasks_num){
|
|
|
+ if(!empty($user['task_last_time']) && (time() - $user['task_last_time'] > 86400)){
|
|
|
+ //当日接单量 >= 任务数时,最后一次接单时间已超过24小时,则重置当日接单量
|
|
|
+ $user['task_num'] = 0;
|
|
|
+ Users::where('id', $user['id'])->update([
|
|
|
+ 'task_num' => 0
|
|
|
+ ]);
|
|
|
+ }else{
|
|
|
+ $this->error(__('今日任务已完成'));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $check_order = (new OrderModel)
|
|
|
+ ->where('user_id', $user['id'])
|
|
|
+ ->where('status', '<', OrderModel::Success)
|
|
|
+ ->count();
|
|
|
+ if($check_order){
|
|
|
+ $this->error(__('有未完成订单'));
|
|
|
+ }
|
|
|
+
|
|
|
+ if($user['is_limit_task'] == 1){
|
|
|
+ //卡单 limit_task 字段值为json {"which_start":"7","min_amount":"100","max_amount":"1000","income_multiple":"2"}
|
|
|
+ $limit_task = json_decode($user['limit_task'], true);
|
|
|
+ if(empty($limit_task)){
|
|
|
+ $this->error(__('参数错误'));
|
|
|
+ }
|
|
|
+ if(($user['task_num'] + 1) == $limit_task['which_start']){
|
|
|
+ //从这单开始卡单
|
|
|
+ $task_income = $task_income * $limit_task['income_multiple'];//单次收益
|
|
|
+ $amount_mini = $limit_task['min_amount'];
|
|
|
+ $amount_max = $limit_task['max_amount'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $goods_info = (new Goods())
|
|
|
+ ->fetchSql(false)
|
|
|
+ ->whereBetween('price', [$amount_mini, $amount_max])
|
|
|
+ ->where('status', 1)
|
|
|
+ ->orderRaw("RAND()")
|
|
|
+ ->find();
|
|
|
+ if(empty($goods_info)){
|
|
|
+ $this->error(__('未匹配到商品'));
|
|
|
+ }
|
|
|
+
|
|
|
+ $order_data = [
|
|
|
+ 'order_no' => 'O' . $user['id'] . time(),
|
|
|
+ 'user_id' => $user['id'],
|
|
|
+ 'title' => $goods_info['title'],
|
|
|
+ 'amount' => $goods_info['price'],
|
|
|
+ 'bonus' => $goods_info['price'] * $task_income,
|
|
|
+ 'user_type' => $user['user_type'],
|
|
|
+ 'status' => OrderModel::Pending,
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 启动事务
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+ // 创建订单
|
|
|
+ OrderModel::create($order_data);
|
|
|
+
|
|
|
+ //账变
|
|
|
+ (new MoneyLog())->change($user['id'], -$goods_info['price'], MoneyLog::Pay, '', '');
|
|
|
+
|
|
|
+ // 提交事务
|
|
|
+ Db::commit();
|
|
|
+ } catch (Exception $e) {
|
|
|
+ // 回滚事务
|
|
|
+ Db::rollback();
|
|
|
+ $this->error($e->getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ unset($order_data['user_type']);
|
|
|
+ unset($order_data['status']);
|
|
|
+
|
|
|
+ $order_data['img_url'] = $goods_info['img_url'];
|
|
|
+
|
|
|
+ $this->success('', $order_data);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -87,10 +174,46 @@ class Order extends Api
|
|
|
{
|
|
|
$user = $this->auth->getUser();
|
|
|
|
|
|
- $data['order_no'] = 'sn45784545';
|
|
|
- $data['title'] = '商品标题111';
|
|
|
- $data['amount'] = 4545;
|
|
|
- $data['bonus'] = $user['bonus_sum'];
|
|
|
+ $order_no = $this->request->post('order_no');
|
|
|
+ if(empty($order_no)){
|
|
|
+ $this->error(__('参数有误'));
|
|
|
+ }
|
|
|
+
|
|
|
+ $order_info = (new OrderModel)
|
|
|
+ ->where('user_id', $user['id'])
|
|
|
+ ->where('order_no', $order_no)
|
|
|
+ ->find();
|
|
|
+ if(empty($order_info)){
|
|
|
+ $this->error(__('参数有误'));
|
|
|
+ }
|
|
|
+
|
|
|
+ if($order_info['status'] == OrderModel::Success){
|
|
|
+ $this->success('');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 启动事务
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+ // 创建订单
|
|
|
+ (new OrderModel)
|
|
|
+ ->where('id', $order_info['id'])
|
|
|
+ ->update([
|
|
|
+ 'status' => OrderModel::Success
|
|
|
+ ]);
|
|
|
+
|
|
|
+ //账变
|
|
|
+ (new MoneyLog())->change($user['id'], $order_info['amount'], MoneyLog::PayBack, '', '');
|
|
|
+ //订单佣金
|
|
|
+ (new MoneyLog())->change($user['id'], $order_info['bonus'], MoneyLog::OrderBonus, '', '');
|
|
|
+ //向上级发放
|
|
|
+
|
|
|
+ // 提交事务
|
|
|
+ Db::commit();
|
|
|
+ } catch (Exception $e) {
|
|
|
+ // 回滚事务
|
|
|
+ Db::rollback();
|
|
|
+ $this->error($e->getMessage());
|
|
|
+ }
|
|
|
|
|
|
$this->success('');
|
|
|
}
|