| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- 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;
- /**
- * 首页接口
- */
- class Order extends Api
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = ['*'];
- /**
- * 订单列表
- * @return void
- * @throws \think\exception\DbException
- */
- public function base()
- {
- $user = $this->auth->getUser();
- $data['banner_list'] = 'https://dapp-static.oss-cn-shenzhen.aliyuncs.com/jue-jin-lu/3.pnggS02ouwrJfiF65979ec74db73';
- $data['day_tasks_num'] = (new Config())->getValue('day_tasks_num');//每日任务数
- $data['task_income'] = (new Config())->getValue('task_income');//单次任务收益
- $data['task_num'] = $user['task_num'];//今日已做任务
- $data['balance'] = $user['balance'];
- $data['bonus_sum'] = $user['bonus_sum'];
- $data['bonus_today'] = (new OrderModel())
- ->where('user_id', $user['id'])
- ->where('status', OrderModel::STATUSFINISH)
- ->whereTime('create_time', '>=', strtotime('today'))
- ->sum('bonus');
- $this->success('', $data);
- }
- /**
- * 订单列表
- * @return void
- * @throws \think\exception\DbException
- */
- public function list()
- {
- $user = $this->auth->getUser();
- $paginator = OrderModel::where('user_id', $user['id'])
- ->field('order_no,amount,bonus,status,create_time')
- ->order('id DESC')
- ->paginate($this->pageSize);
- foreach ($paginator as $k => $v) {
- $paginator[$k]['status_name'] = (new OrderModel())->getStatusNames($v['status']);
- }
- $res_data = $this->buildResp($paginator->total(), $paginator->currentPage(), $paginator->items());
- $res_data['bonus_sum'] = $user['bonus_sum'];
- $this->success('', $res_data);
- }
- /**
- * 获取订单
- * @return void
- * @throws \think\exception\DbException
- */
- public function get()
- {
- $user = $this->auth->getUser();
- if($user['open_task'] != 1){
- $this->error(__('暂停抢单'));
- }
- if(!($user['balance'] > 0)){
- $this->error(__('余额不足'));
- }
- $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;
- 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);
- }
- /**
- * 提交订单
- * @return void
- * @throws \think\exception\DbException
- */
- public function submit()
- {
- $user = $this->auth->getUser();
- $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('');
- }
- }
|