Explorar o código

Wesmiler OTC 提交更新 0729

APPLE %!s(int64=3) %!d(string=hai) anos
pai
achega
887c87cd30

+ 61 - 0
app/Http/Controllers/Api/AdvertController.php

@@ -2,6 +2,7 @@
 
 namespace App\Http\Controllers\Api;
 
+use App\Http\Validator\AdvertOrderValidator;
 use App\Http\Validator\AdvertValidator;
 use App\Services\Api\AdvertService;
 use App\Services\Common\AdvertOrderService;
@@ -103,4 +104,64 @@ class AdvertController extends webApp
             return message(AdvertOrderService::make()->getError(), true);
         }
     }
+
+    /**
+     * 确认打款
+     * @param AdvertOrderValidator $validate
+     * @return array
+     */
+    public function pay(AdvertOrderValidator $validate)
+    {
+        $params = request()->post();
+        $params = $validate->check($params,'pay');
+        if(!is_array($params)){
+            return message($params, false);
+        }
+
+        if($info = AdvertOrderService::make()->pay($this->userId, $params)){
+            return message(3017, true, $info);
+        }else{
+            return message(AdvertOrderService::make()->getError(), false);
+        }
+    }
+
+    /**
+     * 确认收款
+     * @param AdvertOrderValidator $validate
+     * @return array
+     */
+    public function collection(AdvertOrderValidator $validate)
+    {
+        $params = request()->post();
+        $params = $validate->check($params,'info');
+        if(!is_array($params)){
+            return message($params, false);
+        }
+
+        if($info = AdvertOrderService::make()->collection($this->userId, $params)){
+            return message(3021, true, $info);
+        }else{
+            return message(AdvertOrderService::make()->getError(), false);
+        }
+    }
+
+    /**
+     * 取消订单
+     * @param AdvertOrderValidator $validate
+     * @return array
+     */
+    public function cancel(AdvertOrderValidator $validate)
+    {
+        $params = request()->post();
+        $params = $validate->check($params,'info');
+        if(!is_array($params)){
+            return message($params, false);
+        }
+
+        if($info = AdvertOrderService::make()->cancel($this->userId, $params)){
+            return message(3034, true, $info);
+        }else{
+            return message(AdvertOrderService::make()->getError(), false);
+        }
+    }
 }

+ 55 - 0
app/Http/Validator/AdvertOrderValidator.php

@@ -0,0 +1,55 @@
+<?php
+namespace App\Http\Validator;
+class AdvertOrderValidator extends BaseValidator
+{
+    // 当前模型所有验证规则
+    public static $rules = [
+        'id' => 'required',
+        'trade_type' => 'required',
+        'num_type' => 'required',
+        'pay_type' => 'required',
+        'payment_id' => 'required',
+        'pay_img' => 'required',
+        'num' => 'required',
+    ];
+
+    // 当前模型所有错误提示信息
+    public static $msgs = [
+        'required' => ':attribute不能为空',
+        'string' => ':attribute必须是字符串',
+        'min' => ':attribute长度不能小于:min位',
+        'max' => ':attribute长度不能大于:max位',
+        'exists' => ':attribute不存在',
+        'rule' => ':attribute格式不正确',
+    ];
+
+    // 当前模型所有验证字段
+    public static $fields = [
+        'id' => 'ID',
+        'trade_type' => '交易类型',
+        'num_type' => '交易数值类型',
+        'pay_type' => '支付方式',
+        'payment_id' => '收款账号',
+        'pay_img' => '打款凭证',
+        'num' => '数量或金额',
+    ];
+
+    // 当前模型所有验证场景
+    public static $scenes = [
+        'info'=> ['id'],
+        'buy'=> ['trade_type','pay_type','num_type','num'],
+        'sell'=> ['trade_type','payment_id','num_type','num'],
+        'pay'=> ['id','pay_type','pay_img'],
+    ];
+
+    /**
+     * 验证
+     * @param $request
+     * @param string $scene
+     * @return int|mixed
+     */
+    public static function check($request, $scene=''){
+        $validator = new BaseValidator(self::$rules, self::$msgs, self::$fields, self::$scenes);
+        return $validator->checkParams($request, $scene);
+    }
+}

+ 291 - 3
app/Services/Common/AdvertOrderService.php

@@ -151,8 +151,6 @@ class AdvertOrderService extends BaseService
         ];
     }
 
-
-
     /**
      * 购买
      * @param $userId
@@ -548,6 +546,297 @@ class AdvertOrderService extends BaseService
     }
 
     /**
+     * 订单打款处理
+     * @param $userId 用户ID
+     * @param $params 打款参数
+     * @return false
+     */
+    public function pay($userId, $params)
+    {
+        $orderId = isset($params['id']) ? $params['id'] : 0;
+        if ($orderId <= 0) {
+            $this->error = '1013';
+            return false;
+        }
+
+        $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1, 'type' => 1])
+            ->whereIn('status', [1, 2, 5, 7])
+            ->select(['id', 'order_no', 'business_id', 'type', 'payment_id', 'num', 'total', 'status'])
+            ->first();
+        $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
+        if (empty($orderInfo)) {
+            $this->error = '3016';
+            return false;
+        }
+        if ($tradeType != 1) {
+            $this->error = '3024';
+            return false;
+        }
+
+        // 用户信息
+        $userInfo = MemberService::make()->getInfo($userId);
+        $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
+        if ($status != 1) {
+            $this->error = '2009';
+            return false;
+        }
+
+        // 交易密码
+        $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
+        $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
+        if (empty($password)) {
+            $this->error = '2015';
+            return false;
+        }
+
+        if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
+            $this->error = '2016';
+            return false;
+        }
+
+        $data = [
+            'status' => 3,
+            'pay_type' => $params['pay_type'],
+            'pay_img' => $params['pay_img'],
+            'pay_remark' => $params['pay_remark'],
+            'pay_time' => time(),
+        ];
+
+        if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update($data)) {
+            $this->error = '3018';
+            return false;
+        }
+
+        return true;
+    }
+
+    /**
+     * 订单确认处理
+     * @param $userId 用户ID
+     * @param $params 打款参数
+     * @return false
+     */
+    public function collection($userId, $params)
+    {
+        $orderId = isset($params['id']) ? $params['id'] : 0;
+        if ($orderId <= 0) {
+            $this->error = '1013';
+            return false;
+        }
+
+        $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1, 'type' => 2])
+            ->select(['id', 'user_id', 'order_no', 'business_id', 'payment_id', 'type', 'num', 'total', 'status'])
+            ->first();
+        $businessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
+        $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
+        if (empty($orderInfo) || empty($businessId)) {
+            $this->error = '3016';
+            return false;
+        }
+
+        if ($orderInfo['status'] != 3) {
+            $this->error = '3026';
+            return false;
+        }
+
+        if ($tradeType != 2) {
+            $this->error = '3024';
+            return false;
+        }
+
+        $this->model->startTrans();
+        // 订单状态更新
+        if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update(['status' => 4, 'update_time' => time()])) {
+            $this->model->rollBack();
+            $this->error = '3023';
+            return false;
+        }
+
+        // 交易处理
+        if ($orderInfo['num'] > 0) {
+            $info = $this->memberModel->where(['id' => $businessId, 'status' => 1, 'mark' => 1])->select(['id', 'username','usdt_num', 'user_type'])->first();
+            if (empty($info)) {
+                $this->model->rollBack();
+                $this->error = '3019';
+                return false;
+            }
+
+            // 商家进币
+            if (!$this->memberModel->where(['id' => $businessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
+                $this->model->rollBack();
+                $this->error = '3019';
+                return false;
+            }
+
+            // 明细处理
+            $data = [
+                'order_no' => $orderInfo['order_no'],
+                'user_id' => $businessId,
+                'type' => 1,
+                'trade_type' => 2,
+                'pay_type' => 1,
+                'change_type' => 1,
+                'num' => $orderInfo['num'],
+                'total' => $orderInfo['total'],
+                'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
+                'create_time' => time(),
+                'remark' => '交易员购买广告',
+                'status' => 1,
+                'mark' => 1,
+            ];
+
+            if (!$this->capitalModel->edit($data)) {
+                $this->error = '3014';
+                $this->model->rollBack();
+                return false;
+            }
+
+        }
+
+        $this->model->commit();
+        return true;
+    }
+
+    /**
+     * 取消订单
+     * @param $userId
+     * @param $params
+     * @return false
+     */
+    public function cancel($userId, $params)
+    {
+        $orderId = isset($params['id']) ? intval($params['id']) : 0;
+        if ($orderId <= 0) {
+            $this->error = '1013';
+            return false;
+        }
+
+        $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])
+            ->select(['id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
+            ->first();
+
+        $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
+        $businessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
+        if (empty($orderInfo) || $businessId<=0) {
+            $this->error = '3016';
+            return false;
+        }
+
+        if ($orderInfo['status'] == 3) {
+            $this->error = '3027';
+            return false;
+        }
+
+        if ($orderInfo['status'] == 4) {
+            $this->error = '3028';
+            return false;
+        }
+
+        if ($orderInfo['status'] == 7) {
+            $this->error = '3030';
+            return false;
+        }
+
+        if (!in_array($orderInfo['status'], [1, 2])) {
+            $this->error = '3029';
+            return false;
+        }
+
+        $this->model->startTrans();
+        // 订单状态更新
+        $updateData = ['status' => 8, 'update_time' => time(), 'exception_remark' => '客户取消'];
+        if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update($updateData)) {
+            $this->model->rollBack();
+            $this->error = '3023';
+            return false;
+        }
+
+        // 出售订单,USDT退回
+        if ($tradeType == 2 && $orderInfo['num']>0) {
+            $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
+            if (empty($info)) {
+                $this->model->rollBack();
+                $this->error = '3019';
+                return false;
+            }
+
+            // 退还币给客户
+            if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
+                $this->model->rollBack();
+                $this->error = '3019';
+                return false;
+            }
+
+            // 明细处理
+            $data = [
+                'order_no' => $orderInfo['order_no'],
+                'user_id' => $userId,
+                'type' => 3,
+                'trade_type' => 2,
+                'pay_type' => 1,
+                'change_type' => 1,
+                'num' => $orderInfo['num'],
+                'total' => $orderInfo['total'],
+                'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
+                'create_time' => time(),
+                'update_time' => time(),
+                'remark' => '客户取消退还',
+                'status' => 1,
+                'mark' => 1,
+            ];
+
+            if (!$this->capitalModel->edit($data)) {
+                $this->error = '3014';
+                $this->model->rollBack();
+                return false;
+            }
+        }
+        // 买入取消
+        else if($tradeType == 1 && $orderInfo['num']>0){
+            $info = $this->memberModel->where(['id' => $businessId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
+            if (empty($info)) {
+                $this->model->rollBack();
+                $this->error = '3019';
+                return false;
+            }
+
+            // 退还币给币商
+            if (!$this->memberModel->where(['id' => $businessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
+                $this->model->rollBack();
+                $this->error = '3019';
+                return false;
+            }
+
+            // 明细处理
+            $data = [
+                'order_no' => $orderInfo['order_no'],
+                'user_id' => $businessId,
+                'type' => 3,
+                'trade_type' => 2,
+                'pay_type' => 1,
+                'change_type' => 1,
+                'num' => $orderInfo['num'],
+                'total' => $orderInfo['total'],
+                'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
+                'create_time' => time(),
+                'update_time' => time(),
+                'remark' => '客户取消退还',
+                'status' => 1,
+                'mark' => 1,
+            ];
+
+            if (!$this->capitalModel->edit($data)) {
+                $this->error = '3014';
+                $this->model->rollBack();
+                return false;
+            }
+        }
+
+        $this->model->commit();
+        return true;
+    }
+
+    /**
      * 获取未支付或处理的订单数
      * @param $userId
      * @param int $type
@@ -560,7 +849,6 @@ class AdvertOrderService extends BaseService
             ->count('id');
     }
 
-
     /**
      * 自动取消广告订单处理
      * @return false