Product.php 4.09 KB
<?php

namespace addons\antis\controller;

use addons\antis\model\Product as ProductModel;
use addons\antis\model\ProductPart;
use addons\antis\model\Producttype;


/**
 * 产品
 */
class Product extends StaffApi
{
    protected $noNeedLogin = [];
    protected $noNeedRight = [];


    /**
     * 获取select产品列表
     */
    public function getSelectList()
    {
        $name = input('name');
        $type_id = input('type_id');
        $where = [];
        if ($name) {
            $where['name|num'] = ['like', "%{$name}%"];
        }
        if ($type_id) {
            $where['type_id'] = $type_id;
        }
        $list = ProductModel::where($where)->field('id,name,type,num,img,unit,price,min_price,status,cost_price,wholesale')->select();
        $this->success('请求成功', $list);
    }

    /**
     * 获取产品分类列表
     */
    public function getProducttypeList()
    {
        $name = input('name');
        $where=[];
        if ($name) {
            $where['name'] = ['like', "%{$name}%"];
        }
        $list = Producttype::where($where)->field('id,name')->select();

        $this->success('请求成功', $list);
    }

    /**
     * 获取产品配置列表
     */
    public function getPartList()
    {
        $product_id = input('product_id');
        $name = input('name');
        $where = [];
        if ($name) {
            $where['name'] = ['like', "%{$name}%"];
        }
        $where['product_id'] = $product_id;
        $list = ProductPart::where($where)->field('id,name,img,description')->select();
        $this->success('请求成功', $list);
    }

    /**
     * 获取产品详情
     */
    public function getProductDetail()
    {
        $id = input('id');
        $product = ProductModel::where(['id' => $id])->find();
        if (empty($product)) {
            $this->error('产品不存在');
        }
        preg_match_all("/(<img .*?src=\")(.*?)(\".*?>)/is", $product['description'], $matchpic);
        foreach ($matchpic[2] as $url) {
            $img = cdnurl($url, true);
            $product['description'] = str_replace($url, $img, $product['description']);
        }
        $this->success('请求成功', $product);
    }

    /**
     * 添加产品
     */
    public function addProduct()
    {
        $params = $this->request->post();
        if ($params) {
            $parts = $params['parts'] ?? [];

            $model = new ProductModel();
            $model->allowField(true)->save($params);
            $lastid = $model->getLastInsID();
            $partModel = new ProductPart();
            foreach ($parts as &$v) {
                $v['product_id'] = $lastid;
            }
            $partModel->allowField(true)->saveAll($parts);
            $this->success('添加产品成功');
        }
    }

    /**
     * 添加产品配件
     */
    public function addProductPart()
    {
        $params = $this->request->post();
        if ($params) {
            $partModel = new ProductPart();

            $partModel->save($params);
            $this->success('添加产品配置成功');
        }
    }

    /**
     * 修改产品信息
     */
    public function editProduct()
    {
        $params = $this->request->post();
        if ($params) {
            $model = new ProductModel();
            $row = $model->get($params['id']);
            if (empty($row)) {
                $this->error('产品不存在');
            }
            $row->allowField(true)->save($params);

            $this->success('添加产品成功');
        }
    }

    /**
     * 修改产品配件
     */
    public function editProductPart()
    {
        $params = $this->request->post();
        if ($params) {
            $partModel = new ProductPart();
            $row = $partModel->get($params['id']);
            if (empty($row)) {
                $this->error('产品配置不存在');
            }
            $row->allowField(true)->save($params);

            $this->success('修改产品配置成功');
        }
    }
}