| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace addons\qingdong\controller;
- use addons\qingdong\model\Product as ProductModel;
- use addons\qingdong\model\ProductPart;
- use addons\qingdong\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('修改产品配置成功');
- }
- }
- }
|