瀏覽代碼

完善订单接口

Jason 1 年之前
父節點
當前提交
547b419526

+ 1 - 1
application/admin/controller/user/Group.php

@@ -26,7 +26,7 @@ class Group extends Backend
     public function _initialize()
     {
         parent::_initialize();
-        $this->model = model('User');
+        $this->model = model('Users');
         $this->view->assign("statusList", $this->model->getStatusList());
     }
 

+ 2 - 4
application/admin/controller/user/User.php

@@ -52,11 +52,9 @@ class User extends Backend
 
             $list = $this->model
                 ->where($where)
-                ->where('is_delete', 0)
                 ->order($sort, $order)
                 ->paginate($limit);
 
-   
             $in  = model('MoneyIn');
             $out = model('MoneyOut');
             $path= model('UsersPath');
@@ -146,7 +144,7 @@ class User extends Backend
         }
         if (false === $this->request->isPost()) {
 
-            $task = json_decode($row->linit_task, true);
+            $task = json_decode($row->limit_task, true);
             $row->which_start   = $task['which_start']??'';
             $row->min_amount    = $task['min_amount']??'';
             $row->max_amount    = $task['max_amount']??'';
@@ -164,7 +162,7 @@ class User extends Backend
         try {
       
             unset($params['id']);
-            $result = $row->allowField(true)->save(['linit_task'=>json_encode($params)]);
+            $result = $row->allowField(true)->save(['limit_task'=>json_encode($params)]);
             Db::commit();
         } catch (ValidateException|PDOException|Exception $e) {
             Db::rollback();

+ 5 - 0
application/admin/lang/zh-cn/user/user.php

@@ -12,6 +12,10 @@ return [
     'Is_agent'              => '代理',
     'User_type'             => '用户类型(真人/假人)',
     'Status'                => '状态(是/否)',
+    '抢单开关'                => '抢单开关',
+    '卡单开关'                => '卡单开关',
+    '开'                     => '开',
+    '关'                     => '关',
     'Recharge'              => '充值',
     'Withdrawal'            => '提现',
     'Income'                => '收益',
@@ -25,6 +29,7 @@ return [
     'Dummy'                 => '假人',
     'Real person'           => '真人',
     'Locking'               => '锁定',
+    '是否删除'                => '是否删除',
     'New'                   => '新增',
     'Reduce'                => '减少',
     'Type'                  => '类型',

+ 133 - 10
application/api/controller/Order.php

@@ -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('');
     }

+ 6 - 0
application/api/lang/zh-cn/order.php

@@ -5,4 +5,10 @@ return [
     '完成'                    => '完成',
     '冻结'                    => '冻结',
     '取消'                    => '取消',
+    '暂停抢单'                 => '暂停抢单',
+    '余额不足'                 => '余额不足,请先充值',
+    '今日任务已完成'            => '今日任务已完成,请明天再来',
+    '参数错误'                 => '参数错误,请联系管理员',
+    '未匹配到商品'              => '价格区间未匹配到商品',
+    '有未完成订单'              => '您有未完成订单,请完成后再来',
 ];

+ 0 - 1
application/common/library/Auth.php

@@ -4,7 +4,6 @@ namespace app\common\library;
 
 use app\common\model\Users;
 use app\common\model\UserRule;
-use app\common\model\Users;
 use fast\Random;
 use think\Config;
 use think\Db;

+ 72 - 2
application/common/model/MoneyLog.php

@@ -2,6 +2,7 @@
 
 namespace app\common\model;
 
+use think\Exception;
 use think\Model;
 
 
@@ -22,8 +23,77 @@ class MoneyLog extends Model
     protected $append = [
         'create_time_text'
     ];
-    
-    
+
+    //订单状态: 0未支付 1完成 2冻结 3取消
+    const Recharge      = 1;
+    const Withdraw      = 2;
+    const Pay           = 3;
+    const PayBack       = 4;
+    const OrderBonus    = 5;
+    const Commission    = 6;
+    const SystemChange  = 7;
+
+    protected function getStatusNamesArr(){
+        $status_names = [
+            '-1'                    => __('全部状态'),
+            self::Recharge          => __('充值'),
+            self::Withdraw          => __('提现'),
+            self::Pay               => __('交易'),
+            self::PayBack           => __('交易'),
+            self::OrderBonus        => __('佣金'),
+            self::Commission        => __('奖励'),
+            self::SystemChange      => __('系统'),
+        ];
+        return $status_names;
+    }
+    /*
+     * 订单状态
+     * 0未支付 1完成 2冻结 3取消
+     */
+    public function getStatusNames($type = -1){
+        $status_names = $this->getStatusNamesArr();
+        if($type == -1){
+            return $status_names;
+        }
+        if(isset($status_names[$type])){
+            return $status_names[$type];
+        }
+        return __('未定义');
+    }
+
+    /**
+     * 变更会员余额
+     * @param int    $money   余额
+     * @param int    $user_id 会员ID
+     * @param string $memo    备注
+     */
+    public function change($user_id, $amount, $action, $from_id, $memo)
+    {
+        $user = (new Users())->lock(true)->find($user_id);
+        if(empty($user)){
+            throw new Exception(__('会员信息不存在'));
+        }
+        if($amount == 0){
+            throw new Exception(__('参数有误'));
+        }
+
+        if (!array_key_exists($action, $this->getStatusNamesArr())) {
+            throw new Exception(__('账变类型不存在'));
+        }
+
+        $balance = function_exists('bcadd') ? bcadd($user->balance, $amount, 2) : $user->balance + $amount;
+        //更新会员信息
+        $user->save(['balance' => $balance]);
+        //写入日志
+        MoneyLog::create([
+            'user_id'   => $user_id,
+            'amount'    => $amount,
+            'balance'   => $balance,
+            'action'    => $action,
+            'from_id'   => $from_id,
+            'note'      => $memo
+        ]);
+    }
     //
     public function users()
     {

+ 13 - 11
application/common/model/Order.php

@@ -23,13 +23,13 @@ class Order extends Model
         'create_time_text',
         'update_time_text'
     ];
-    
-    
-    //订单状态: 0未支付 1完成 2冻结 3取消
-    const STATUSUNPAID = 0;
-    const STATUSFINISH = 1;
-    const STATUSFREEZE = 2;
-    const STATUSCANCEL = 3;
+    //订单状态
+    const Default   = 0;
+    const Pending   = 100;
+    const Freeze    = 101;
+    const Success   = 200;
+    const Fail      = 400;
+    const Cancel    = 500;
 
     /*
      * 订单状态
@@ -38,10 +38,12 @@ class Order extends Model
     public function getStatusNames($type = -1){
         $status_names = [
             '-1'                    => __('全部状态'),
-            self::STATUSUNPAID      => __('待支付'),
-            self::STATUSFINISH      => __('完成'),
-            self::STATUSFREEZE      => __('冻结'),
-            self::STATUSCANCEL      => __('取消'),
+            self::Default           => __('待支付'),
+            self::Pending           => __('待完成'),
+            self::Freeze            => __('冻结中'),
+            self::Success           => __('完成'),
+            self::Fail              => __('失败'),
+            self::Cancel            => __('取消'),
         ];
         if($type == -1){
             return $status_names;

+ 1 - 1
application/common/model/Users-back.php

@@ -8,7 +8,7 @@ use think\Model;
 /**
  * 会员模型
  */
-class Users extends Model
+class UsersBack extends Model
 {
 
     // 开启自动写入时间戳字段

+ 4 - 1
public/assets/js/backend/trade/money_log.js

@@ -28,7 +28,10 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                         {field: 'amount', title: __('Amount'), operate: false},
                         {field: 'balance', title: __('Balance'), operate: false},
                         {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
-                        {field: 'action', title: __('Action')},
+                        {field: 'action', title: __("Action"),
+                            searchList: {1:__('充值'), 2:__('提现'), 3:__('交易'), 4:__('交易'), 5:__('佣金'), 6:__('奖励'), 7:__('系统调整')},
+                            formatter: Table.api.formatter.status
+                        },
                         //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                     ]
                 ]

+ 10 - 4
public/assets/js/backend/user/user.js

@@ -33,10 +33,15 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                         {field: 'balance', title: __('Balance'), operate: false},
                         {field: 'freeze', title: __('Freeze'), operate: false},
                         {field: 'task_num', title: __('Task'), operate: false},
-
-                        {field: 'id', title: __('Recharge'), operate: false},
-                        {field: 'id', title: __('Withdrawal'), operate: false},
-                        {field: 'id', title: __('Income'), operate: false},
+                        {field: 'open_task', title: __('抢单开关'), formatter: Table.api.formatter.toggle,
+                            searchList: {0: __('关'), 1: __('开')}
+                        },
+                        {field: 'is_limit_task', title: __('卡单开关'), formatter: Table.api.formatter.toggle,
+                            searchList: {0: __('关'), 1: __('开')}
+                        },
+                        {field: 'recharge', title: __('Recharge'), operate: false},
+                        {field: 'withdraw', title: __('Withdrawal'), operate: false},
+                        {field: 'bonus_sum', title: __('Income'), operate: false},
 
                         {field: 'invitation_code', title: __('Invitation_code'), operate: false},
                         {field: 'is_agent', title: __('Is_agent'), searchList: {1: __('Yes'), 0: __('No')}, formatter: Table.api.formatter.status},
@@ -47,6 +52,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                         {field: 'is_lock', title: __('Status'), formatter: Table.api.formatter.toggle, 
                             searchList: {0: __('Normal'), 1: __('Locking')}
                         },
+                        {field: 'is_delete', title: __('是否删除'), searchList: {1: __('Yes'), 0: __('No')}, formatter: Table.api.formatter.status},
                         {field: 'operate', title: __('Operate'), table: table,
                             buttons: [
                                 {

+ 1 - 1
tou-biao.sql

@@ -794,7 +794,7 @@ CREATE TABLE `ta_users`  (
   `lock_task` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否锁定任务,开启后不能做任务',
   `task_num` int(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT '本轮已做任务量',
   `is_limit_task` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否卡单:0否 1是',
-  `linit_task` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '卡单条件,json存储',
+  `limit_task` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '卡单条件,json存储',
   `invitation_code` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '0' COMMENT '邀请码',
   `is_agent` bigint(20) UNSIGNED NOT NULL DEFAULT 0 COMMENT '是否代理:0否 1是',
   `user_type` bigint(20) NOT NULL DEFAULT 0 COMMENT '用户类型:0假人 1真人',