quanwei
18 hours ago c441dea81bd86bdfb12dff35821fed51f4cc91c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
 
namespace app\api\controller\supplier;
 
use app\api\controller\Controller;
use app\api\model\product\Product as ProductModel;
use app\supplier\service\ProductService;
use app\api\model\supplier\Supplier as SupplierModel;
 
/**
 * 供应商产品
 */
class Product extends Controller
{
    // user
    private $user;
    private $supplierUser;
 
    /**
     * 构造方法
     */
    public function initialize()
    {
        parent::initialize();
        $this->user = $this->getUser();   // 用户信息
        $this->supplierUser = $this->getSupplierUser($this->user);
    }
    /**
     * 供应商中心主页
     */
    public function index()
    { 
        $data = $this->postData();
        // 获取商品列表数据
        $model = new ProductModel;
        $data['shop_supplier_id'] = $this->supplierUser['shop_supplier_id'];
        $productList = $model->getList($data, $this->user);
        return $this->renderSuccess('', compact('productList'));
    }
 
    //商品上下架
    public function modify(){
        $data = $this->postData();
        // 获取商品数据
        $model = ProductModel::detail($data['product_id']);
        if($this->supplierUser['shop_supplier_id'] != $model['shop_supplier_id']){
            return $this->renderError('非法请求');
        }
        if(!$model->editStatus($data)){
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess('操作成功');
    }
    
    /**
     * 获取添加商品所需的基础数据
     */
    public function getBaseData()
    {
        $data = ProductService::getEditData(null, 'add', $this->supplierUser['shop_supplier_id']);
        return $this->renderSuccess('', $data);
    }
    
    /**
     * 添加商品
     */
    public function add()
    {
        $data = $this->postData();
        $params = json_decode($data['params'], true);
        $params['shop_supplier_id'] = $this->supplierUser['shop_supplier_id'];
        if ($this->supplierUser['status']==20){
            return $this->renderError('未缴纳保证金,不能添加商品');
        }
        $model = new ProductModel;
        $supplier = SupplierModel::detail($this->supplierUser['shop_supplier_id']);
        $params['is_newcomer'] = $supplier['is_newcomer'];
        $params['is_repurchase'] = $supplier['is_repurchase'];
        if ($model->add($params)) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }
    
    /**
     * 获取编辑商品所需的数据
     */
    public function getEditData()
    {
        $data = $this->postData();
        $product_id = $data['product_id'];
        // 获取商品数据
        $model = ProductModel::detail($product_id);
        if($this->supplierUser['shop_supplier_id'] != $model['shop_supplier_id']){
            return $this->renderError('非法请求');
        }
        
        $result = ProductService::getEditData($model, 'edit', $this->supplierUser['shop_supplier_id']);
        $result['model'] = $model;
        return $this->renderSuccess('', $result);
    }
    
    /**
     * 编辑商品
     */
    public function edit()
    {
        $data = $this->postData();
        $product_id = $data['product_id'];
        $params = json_decode($data['params'], true);
        
        // 获取商品数据
        $model = ProductModel::detail($product_id);
        if($this->supplierUser['shop_supplier_id'] != $model['shop_supplier_id']){
            return $this->renderError('非法请求');
        }
        $supplier = SupplierModel::detail($this->supplierUser['shop_supplier_id']);
        $params['is_newcomer'] = $supplier['is_newcomer'];
        $params['is_repurchase'] = $supplier['is_repurchase'];
        if ($model->edit($params)) {
            return $this->renderSuccess('编辑成功');
        }
        return $this->renderError($model->getError() ?: '编辑失败');
    }
   
}