| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace app\admin\controller;
- use app\BaseController;
- use app\common\model\ProductCategoryModel;
- use app\common\model\ProductModel;
- use think\App;
- use think\facade\View;
- use think\Request;
- class Product extends BaseController
- {
- private $productModel;
- private $categoryModel;
- public function __construct(App $app)
- {
- parent::__construct($app);
- $this->productModel = new ProductModel();
- $this->categoryModel = new ProductCategoryModel();
- }
- /**
- * @param Request $request
- * @return \think\response\View
- * @throws \think\db\exception\DbException
- */
- public function index(Request $request) {
- $params = $request->param();
- $format_params = [
- 'bar_code' => format_string($params['bar_code'] ?? null),
- 'category_id' => format_string($params['category_id'] ?? null) != null ? (int)$params['category_id']: null,
- 'is_serve' => format_string($params['is_serve'] ?? null) != null ? (int)$params['is_serve']: null,
- ];
- View::assign([
- 'list' => $this->productModel->findByPaginate($format_params),
- 'params' => $format_params,
- 'all_category' => recursion($this->categoryModel->findAll(),0),
- ]);
- return view();
- }
- public function add(Request $request) {
- $params = $request->param();
- if($request->isAjax()) {
- $isExist = $this->productModel->doesItExist($params['bar_code']);
- if ($isExist)
- return $this->fail('Fail to add. Data duplication');
- $category = $this->categoryModel->findById($params['category_id']);
- if(!$category)
- return $this->fail(lang('Category does not exist'));
- $res = $this->productModel->save([
- 'bar_code' => $params['bar_code'],
- 'name' => $params['name'] ?? null,
- 'image' => $params['image'] ?? null,
- 'category_id' => $params['category_id'] ?? null,
- 'category_name' => $category->name,
- 'is_serve' => $params['is_serve'],
- 'purchase_price'=> $params['purchase_price'],
- 'selling_price' => $params['selling_price']
- ]);
- return $this->ok($res);
- }
- View::assign([
- 'all_category' => recursion($this->categoryModel->findAll(),0),
- ]);
- return view();
- }
- public function edit(Request $request) {
- $params = $request->param();
- if(!isset($params['id']))
- return $this->fail(lang('ID not exist'));
- $product = $this->productModel->findById($params['id']);
- if($request->isAjax()) {
- $category = $this->categoryModel->findById($params['category_id']);
- if(!$category)
- return $this->fail(lang('Category does not exist'));
- $res = $this->productModel->where('id',$params['id'])->update([
- 'bar_code' => $params['bar_code'],
- 'name' => $params['name'] ?? null,
- 'image' => $params['image'] ?? null,
- 'category_id' => $params['category_id'] ?? null,
- 'category_name' => $category->name,
- 'is_serve' => (int)$params['is_serve'],
- 'purchase_price'=> $params['purchase_price'],
- 'selling_price' => $params['selling_price'],
- 'update_time' => time()
- ]);
- return $this->ok($res);
- }
- View::assign([
- 'product' => $product,
- 'all_category' => recursion($this->categoryModel->findAll(),0),
- ]);
- return view();
- }
- public function delete(Request $request) {
- $params = $request->param();
- if(!isset($params['ids']))
- return $this->fail(lang("Please select the data you want to delete"));
- $this->productModel->deleteByIds(explode(',',$params['ids']));
- return $this->ok(true);
- }
- /**
- * @return \think\response\Json
- */
- public function upload() {
- // 获取表单上传文件 例如上传了001.jpg
- $file = request()->file('file');
- // 上传到本地服务器
- $save_name = \think\facade\Filesystem::disk('public')->putFile( 'product', $file);
- return $this->ok(str_replace('\\',"/",'/storage/'.$save_name));
- }
- public function findProducts(Request $request) {
- $params = $request->param();
- if(!isset($params['text']))
- return $this->fail(lang('Data not exist'));
- $products = $this->productModel->findProducts($params['text']);
- return $this->ok($products);
- }
- }
|