Browse Source

fix:数据

super-yimizi 1 month ago
parent
commit
62ded51e04

+ 11 - 2
application/api/controller/inspection/Order.php

@@ -38,10 +38,19 @@ class Order extends Base
           if (!$validate->scene('detail')->check($params)) {
               $this->error($validate->getError());
           }
-          $order = OrderService::getDetail($orderId, $this->auth->id);
+          
+          // 获取当前验货员所属的供应商ID
+          $supplierId = $this->application->supplier_id;
+          if (!$supplierId) {
+              $this->error('您未绑定供应商');
+          }
+          
+          // 使用供应商订单详情方法,只获取该供应商的商品
+          $order = OrderService::getSupplierOrderDetail($orderId, $supplierId, $this->auth->id);
           if (empty($order)) {
-              $this->error('未找到订单');
+              $this->error('未找到订单或该订单不包含您供应商的商品');
           }
+          
           /** @var \app\common\model\Order $order */
           //$order->append(['order_status_text']);
           $address = OrderService::getAddressInfo($orderId);

+ 2 - 2
application/common/Enum/OrderEnum.php

@@ -174,7 +174,7 @@ class OrderEnum
             self::STATUS_CREATE
         ],
         self::SHOW_TYPE_WAIT_DELIVERY => [
-            self::STATUS_PAY
+            self::STATUS_INSPECTION_PASS,
         ],
         self::SHOW_TYPE_WAIT_RECEIPT => [
             self::STATUS_SHIP
@@ -183,8 +183,8 @@ class OrderEnum
             self::STATUS_CONFIRM
         ],
         self::SHOW_TYPE_WAIT_INSPECT => [
+            self::STATUS_PAY,
             self::STATUS_INSPECTION,
-            self::STATUS_INSPECTION_PASS,
             self::STATUS_INSPECTION_FAIL,
         ],
     ];

+ 26 - 6
application/common/Service/OrderService.php

@@ -680,24 +680,44 @@ class OrderService
     }
 
     /**
-     * 获取供应商订单详情 - 只返回该供应商的商品
-     * @param string $orderSn 订单号
+     * 获取供应商订单详情 - 支持订单ID和订单号两种方式
+     * @param mixed $orderParam 订单ID或订单号
      * @param int $supplierId 供应商ID
+     * @param int $userId 用户ID(可选,用于权限验证)
      * @return array|null
      */
-    public static function getSupplierOrderDetail($orderSn, $supplierId)
+    public static function getSupplierOrderDetail($orderParam, $supplierId, $userId = 0)
     {
-        $order = Order::where('order_sn', $orderSn)->find();
+        // 根据参数类型判断是订单ID还是订单号
+        if (is_numeric($orderParam)) {
+            // 数字类型,当作订单ID处理
+            $query = Order::where('id', $orderParam);
+        } else {
+            // 字符串类型,当作订单号处理
+            $query = Order::where('order_sn', $orderParam);
+        }
+        
+        // 如果指定了用户ID,添加用户权限验证
+        if (!empty($userId)) {
+            $query->where('user_id', $userId);
+        }
+        
+        $order = $query->find();
         if (!$order) {
             return null;
         }
         
         // 获取该供应商在此订单中的商品
-        $orderGoods = OrderGoods::where('order_sn', $orderSn)
+        $orderGoods = OrderGoods::where('order_sn', $order->order_sn)
             ->where('supplier_id', $supplierId)
             ->select();
+            
+        // 如果该供应商在此订单中没有商品,返回null
+        if (empty($orderGoods)) {
+            return null;
+        }
         
-        $order->orderGoods = $orderGoods;
+        $order->order_goods = $orderGoods;
         return $order;
     }
 

+ 88 - 0
application/common/controller/InspectionApi.php

@@ -0,0 +1,88 @@
+<?php
+
+namespace app\common\controller;
+
+use app\common\library\InspectionAuth;
+use think\Request;
+use think\Config;
+use think\Lang;
+use think\Loader;
+use think\exception\HttpResponseException;
+use think\Response;
+
+class InspectionApi
+{
+    protected $request;
+    protected $auth = null;
+    protected $application = null;
+    protected $user = null;
+    protected $responseType = 'json';
+
+    public function __construct(Request $request = null)
+    {
+        $this->request = is_null($request) ? Request::instance() : $request;
+        $this->_initialize();
+    }
+
+    protected function _initialize()
+    {
+        // 跨域检测
+        check_cors_request();
+        // IP 检查
+        check_ip_allowed();
+        // 过滤请求
+        $this->request->filter('trim,strip_tags,htmlspecialchars');
+        $this->auth = InspectionAuth::instance();
+        // token
+        $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));
+        // 初始化验货员身份
+        $this->auth->init($token);
+        if (!$this->auth->isLogin()) {
+            $this->error('请先登录', null, 401);
+        }
+        $this->application = $this->auth->getApplication();
+        $this->user = $this->auth->getUser();
+        // 检查审核状态
+        if (!$this->application || $this->application->audit_status != 2) {
+            $this->error('验货员未通过审核', null, 403);
+        }
+        // 检查供应商绑定
+        if (!$this->application->supplier_id) {
+            $this->error('未绑定供应商', null, 403);
+        }
+        // 加载语言包
+        $controllername = strtolower($this->request->controller());
+        $lang = $this->request->langset();
+        $lang = preg_match("/^([a-zA-Z\-_]{2,10})$/i", $lang) ? $lang : 'zh-cn';
+        Lang::load(ADDON_PATH . 'shop/lang/' . $lang . '/' . str_replace('.', '/', $controllername) . '.php');
+    }
+
+    protected function success($msg = '', $data = null, $code = 1, $type = null, array $header = [])
+    {
+        $this->result($msg, $data, $code, $type, $header);
+    }
+
+    protected function error($msg = '', $data = null, $code = 0, $type = null, array $header = [])
+    {
+        $this->result($msg, $data, $code, $type, $header);
+    }
+
+    protected function result($msg, $data = null, $code = 0, $type = null, array $header = [])
+    {
+        $result = [
+            'code' => $code,
+            'msg'  => $msg,
+            'time' => Request::instance()->server('REQUEST_TIME'),
+            'data' => $data,
+        ];
+        $type = $type ?: $this->responseType;
+        if (isset($header['statuscode'])) {
+            $code = $header['statuscode'];
+            unset($header['statuscode']);
+        } else {
+            $code = $code >= 1000 || $code < 200 ? 200 : $code;
+        }
+        $response = Response::create($result, $type, $code)->header($header);
+        throw new HttpResponseException($response);
+    }
+} 

+ 194 - 0
application/common/library/InspectionAuth.php

@@ -0,0 +1,194 @@
+<?php
+
+namespace app\common\library;
+
+use app\common\model\inspection\InspectionApplication;
+use app\common\model\User;
+use fast\Random;
+use think\Config;
+use think\Db;
+use think\Exception;
+use think\Hook;
+use think\Request;
+use think\Validate;
+
+class InspectionAuth
+{
+    protected static $instance = null;
+    protected $_error = '';
+    protected $_logined = false;
+    protected $_application = null;
+    protected $_user = null;
+    protected $_token = '';
+    protected $keeptime = 2592000;
+    protected $allowFields = ['id', 'user_id', 'name', 'phone', 'supplier_id', 'audit_status', 'status'];
+
+    public function __construct($options = [])
+    {
+        // 可扩展配置
+    }
+
+    public static function instance($options = [])
+    {
+        if (is_null(self::$instance)) {
+            self::$instance = new static($options);
+        }
+        return self::$instance;
+    }
+
+    /**
+     * 初始化验货员身份(通过token)
+     * @param string $token
+     * @return bool
+     */
+    public function init($token)
+    {
+        if ($this->_logined) {
+            return true;
+        }
+        if ($this->_error) {
+            return false;
+        }
+        $application = InspectionApplication::where('token', $token)
+            ->where('audit_status', 2)
+            ->where('status', 1)
+            ->find();
+        if (!$application) {
+            $this->setError('验货员未登录或未通过审核');
+            return false;
+        }
+        $this->_application = $application;
+        $this->_user = User::get($application->user_id);
+        $this->_logined = true;
+        $this->_token = $token;
+        return true;
+    }
+
+    /**
+     * 直接登录验货员
+     * @param int $user_id
+     * @return bool
+     */
+    public function direct($user_id)
+    {
+        $application = InspectionApplication::where('user_id', $user_id)
+            ->where('audit_status', 2)
+            ->where('status', 1)
+            ->find();
+        if (!$application) {
+            $this->setError('验货员未通过审核');
+            return false;
+        }
+        $token = Random::uuid();
+        $expire = time() + $this->keeptime;
+        $application->token = $token;
+        $application->token_expiretime = $expire;
+        $application->save();
+        $this->_application = $application;
+        $this->_user = User::get($application->user_id);
+        $this->_logined = true;
+        $this->_token = $token;
+        return true;
+    }
+
+    /**
+     * 判断是否已登录
+     * @return bool
+     */
+    public function isLogin()
+    {
+        return $this->_logined;
+    }
+
+    /**
+     * 获取当前Token
+     * @return string
+     */
+    public function getToken()
+    {
+        return $this->_token;
+    }
+
+    /**
+     * 获取验货员申请信息
+     * @return InspectionApplication|null
+     */
+    public function getApplication()
+    {
+        return $this->_application;
+    }
+
+    /**
+     * 获取验货员用户信息
+     * @return User|null
+     */
+    public function getUser()
+    {
+        return $this->_user;
+    }
+
+    /**
+     * 获取供应商ID
+     * @return int
+     */
+    public function getSupplierId()
+    {
+        return $this->_application ? $this->_application->supplier_id : 0;
+    }
+
+    /**
+     * 退出登录
+     * @return bool
+     */
+    public function logout()
+    {
+        if (!$this->_logined) {
+            $this->setError('未登录');
+            return false;
+        }
+        $this->_application->token = null;
+        $this->_application->token_expiretime = null;
+        $this->_application->save();
+        $this->_logined = false;
+        $this->_token = '';
+        return true;
+    }
+
+    /**
+     * 设置错误信息
+     * @param string $error
+     * @return $this
+     */
+    public function setError($error)
+    {
+        $this->_error = $error;
+        return $this;
+    }
+
+    /**
+     * 获取错误信息
+     * @return string
+     */
+    public function getError()
+    {
+        return $this->_error;
+    }
+
+    /**
+     * 获取允许输出的字段
+     * @return array
+     */
+    public function getAllowFields()
+    {
+        return $this->allowFields;
+    }
+
+    /**
+     * 设置允许输出的字段
+     * @param array $fields
+     */
+    public function setAllowFields($fields)
+    {
+        $this->allowFields = $fields;
+    }
+} 

+ 26 - 6
application/common/service/OrderService.php

@@ -680,24 +680,44 @@ class OrderService
     }
 
     /**
-     * 获取供应商订单详情 - 只返回该供应商的商品
-     * @param string $orderSn 订单号
+     * 获取供应商订单详情 - 支持订单ID和订单号两种方式
+     * @param mixed $orderParam 订单ID或订单号
      * @param int $supplierId 供应商ID
+     * @param int $userId 用户ID(可选,用于权限验证)
      * @return array|null
      */
-    public static function getSupplierOrderDetail($orderSn, $supplierId)
+    public static function getSupplierOrderDetail($orderParam, $supplierId, $userId = 0)
     {
-        $order = Order::where('order_sn', $orderSn)->find();
+        // 根据参数类型判断是订单ID还是订单号
+        if (is_numeric($orderParam)) {
+            // 数字类型,当作订单ID处理
+            $query = Order::where('id', $orderParam);
+        } else {
+            // 字符串类型,当作订单号处理
+            $query = Order::where('order_sn', $orderParam);
+        }
+        
+        // 如果指定了用户ID,添加用户权限验证
+        if (!empty($userId)) {
+            $query->where('user_id', $userId);
+        }
+        
+        $order = $query->find();
         if (!$order) {
             return null;
         }
         
         // 获取该供应商在此订单中的商品
-        $orderGoods = OrderGoods::where('order_sn', $orderSn)
+        $orderGoods = OrderGoods::where('order_sn', $order->order_sn)
             ->where('supplier_id', $supplierId)
             ->select();
+            
+        // 如果该供应商在此订单中没有商品,返回null
+        if (empty($orderGoods)) {
+            return null;
+        }
         
-        $order->orderGoods = $orderGoods;
+        $order->order_goods = $orderGoods;
         return $order;
     }