| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- <?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 app\common\model\UsersPath;
- 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['freeze'] < 0 ? $user['freeze']: $user['balance'];
- $data['bonus_sum'] = $user['bonus_sum'];
- $data['bonus_today'] = (new OrderModel())
- ->where('user_id', $user['id'])
- ->where('status', OrderModel::Success)
- ->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(bccomp($user['freeze'], 0, 2) ===-1){
- //冻结金额< 0时,需要充值
- $this->error(__('余额不足'), '', 401);
- }
- if(!($user['balance'] > 0)){
- $this->error(__('余额不足'), '', 401);
- }
- $balance_mini = (new Config())->getValue('balance_mini');//允许做单的最低余额
- if($user['balance'] < $balance_mini){
- $this->error(__('余额不足'), '', 401);
- }
- $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']) && (date('md',time()) != date('md', $user['task_last_time']))){
- //当日接单量 >= 任务数时,最后一次接单时间和当前不是同一天,则重置当日接单量
- $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(__('有未完成订单'));
- }
- $freeze = false; //是否有冻结金额
- //卡单 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)){
- $val_arr = array_column($limit_task, 'which_start');
- $key = array_search($user['task_num'] + 1, $val_arr);
- if($key !== false){
- //从这单开始卡单
- if(!($user['freeze'] < 0)){
- //冻结金额不小于0时,说明未卡单,则进行卡单操作:向用户展示余额时,显示 负的订单金额
- $freeze = true;
- }
- $task_income = $task_income * $limit_task[$key]['income_multiple'];//单次收益
- $amount_mini = $limit_task[$key]['min_amount'];
- $amount_max = $limit_task[$key]['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 {
- // if($freeze){
- // //卡单操作,不扣款,只冻结订单金额
- // $freeze = Users::where('id', $user['id'])->update(['freeze' => -$goods_info['price']]);
- // $order_data['status'] = OrderModel::Default;
- // }else{
- // //正常订单 账变
- // (new MoneyLog())->change($user['id'], -$goods_info['price'], MoneyLog::Pay, '', '');
- // }
- //正常订单 账变
- (new MoneyLog())->change($user['id'], -$goods_info['price'], MoneyLog::Pay, '', '');
- // 创建订单
- OrderModel::create($order_data);
- // 提交事务
- 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($user['balance'] < 0){
- $this->error(__('余额不足'), '', 401);
- }
- if($user['freeze'] < 0){
- $this->error(__('余额不足'), '', 401);
- }
- if($order_info['status'] == OrderModel::Success){
- $this->success('');
- }
- sleep(1);//防止并发重复提交
- // 启动事务
- Db::startTrans();
- try {
- // 更新订单
- $rs = (new OrderModel)
- ->where('id', $order_info['id'])
- ->update([
- 'status' => OrderModel::Success
- ]);
- if(!$rs){
- $this->error(__('失败'));
- }
- if($order_info['status'] == OrderModel::Default){
- //卡单订单不扣款,不返款,直接发佣金
- //订单佣金
- (new MoneyLog())->change($user['id'], $order_info['bonus'], MoneyLog::OrderBonus, '', '卡单订单');
- }else{
- //账变
- (new MoneyLog())->change($user['id'], $order_info['amount'], MoneyLog::PayBack, '', '');
- //订单佣金
- (new MoneyLog())->change($user['id'], $order_info['bonus'], MoneyLog::OrderBonus, '', '');
- }
- //累积佣金和任务次数
- (new Users())
- ->where('id', $user['id'])
- ->update([
- 'task_num' => Db::raw('task_num + 1'),
- 'bonus_sum' => Db::raw('bonus_sum + ' .$order_info['bonus']),
- 'task_last_time'=> time()
- ]);
- //向上级发放
- $parent_ids = (new UsersPath())->where('user_id', $user['id'])->order('distance')->column('parent_id');
- if(!empty($parent_ids)){
- for($i = 1; $i <= 3; $i++){
- if(!isset($parent_ids[$i - 1])){
- //没设置就跳出
- break;
- }
- $radio = (new Config())->getValue('bonus_share_' . $i);
- if(!empty($radio) && $radio > 0 && $radio < 1){
- $bonus = $radio * $order_info['bonus'];
- if($bonus > 0.01){
- //上级佣金
- (new MoneyLog())->change($parent_ids[$i - 1], $bonus, MoneyLog::Commission, $user['id'], $i . '代会员奖励');
- }
- }
- }
- }
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('');
- }
- }
|