productModel = new ProductModel(); $this->categoryModel = new ProductCategoryModel(); $this->companyModel = new CompanyModel(); } /** * @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')); $company = $this->companyModel->findById($params['company_id']); if(!$company) return $this->fail(lang('Company 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, 'company_id' => $company->id, 'is_serve' => $params['is_serve'], 'purchase_price'=> $params['purchase_price'], 'real_price' => $params['real_price'], 'lineate_price' => $params['lineate_price'], 'is_upload_numerology' => $params['is_upload_numerology'], 'is_gather_annuity' => $params["is_gather_annuity"], 'annuity' => $params["annuity"], 'sales_tax_rate' => $params['sales_tax_rate'] ]); return $this->ok($res); } View::assign([ 'all_category' => recursion($this->categoryModel->findAll(),0), "all_company" => $this->companyModel->findAll() ]); 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')); $company = $this->companyModel->findById($params['company_id'] ?? 0); if(!$company) return $this->fail(lang('Company does not exist')); $res = $this->productModel->where('id',$params['id'])->update([ 'bar_code' => $params['bar_code'], 'name' => isset($params['name']) ? $params['name'] : $product->name, 'image' => isset($params['image']) ? $params['image'] : $product->image, 'category_id' => $category->id, 'company_id' => $company->id, 'is_serve' => (int)$params['is_serve'], 'purchase_price'=> isset($params['purchase_price']) ? $params['purchase_price'] : $product->purchase_price, 'real_price' => isset($params['real_price']) && $params['real_price'] > 0 ? $params['real_price'] : $product->real_price, 'lineate_price' => isset($params['lineate_price']) && $params['lineate_price'] > 0 ? $params['lineate_price'] : $product->lineate_price, 'is_upload_numerology' => $params['is_upload_numerology'], 'is_gather_annuity' => $params["is_gather_annuity"], 'annuity' => $params["annuity"], 'sales_tax_rate' => $params['sales_tax_rate'], 'update_time' => time() ]); return $this->ok($res); } View::assign([ 'product' => $product, 'all_category' => recursion($this->categoryModel->findAll(),0), 'all_company' => $this->companyModel->findAll() ]); 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); } }