Product.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use app\common\model\ProductCategoryModel;
  5. use app\common\model\ProductModel;
  6. use think\App;
  7. use think\facade\View;
  8. use think\Request;
  9. class Product extends BaseController
  10. {
  11. private $productModel;
  12. private $categoryModel;
  13. public function __construct(App $app)
  14. {
  15. parent::__construct($app);
  16. $this->productModel = new ProductModel();
  17. $this->categoryModel = new ProductCategoryModel();
  18. }
  19. /**
  20. * @param Request $request
  21. * @return \think\response\View
  22. * @throws \think\db\exception\DbException
  23. */
  24. public function index(Request $request) {
  25. $params = $request->param();
  26. $format_params = [
  27. 'bar_code' => format_string($params['bar_code'] ?? null),
  28. 'category_id' => format_string($params['category_id'] ?? null) != null ? (int)$params['category_id']: null,
  29. 'is_serve' => format_string($params['is_serve'] ?? null) != null ? (int)$params['is_serve']: null,
  30. ];
  31. View::assign([
  32. 'list' => $this->productModel->findByPaginate($format_params),
  33. 'params' => $format_params,
  34. 'all_category' => recursion($this->categoryModel->findAll(),0),
  35. ]);
  36. return view();
  37. }
  38. public function add(Request $request) {
  39. $params = $request->param();
  40. if($request->isAjax()) {
  41. $isExist = $this->productModel->doesItExist($params['bar_code']);
  42. if ($isExist)
  43. return $this->fail('Fail to add. Data duplication');
  44. $category = $this->categoryModel->findById($params['category_id']);
  45. if(!$category)
  46. return $this->fail(lang('Category does not exist'));
  47. $res = $this->productModel->save([
  48. 'bar_code' => $params['bar_code'],
  49. 'name' => $params['name'] ?? null,
  50. 'image' => $params['image'] ?? null,
  51. 'category_id' => $params['category_id'] ?? null,
  52. 'category_name' => $category->name,
  53. 'is_serve' => $params['is_serve'],
  54. 'purchase_price'=> $params['purchase_price'],
  55. 'selling_price' => $params['selling_price']
  56. ]);
  57. return $this->ok($res);
  58. }
  59. View::assign([
  60. 'all_category' => recursion($this->categoryModel->findAll(),0),
  61. ]);
  62. return view();
  63. }
  64. public function edit(Request $request) {
  65. $params = $request->param();
  66. if(!isset($params['id']))
  67. return $this->fail(lang('ID not exist'));
  68. $product = $this->productModel->findById($params['id']);
  69. if($request->isAjax()) {
  70. $category = $this->categoryModel->findById($params['category_id']);
  71. if(!$category)
  72. return $this->fail(lang('Category does not exist'));
  73. $res = $this->productModel->where('id',$params['id'])->update([
  74. 'bar_code' => $params['bar_code'],
  75. 'name' => $params['name'] ?? null,
  76. 'image' => $params['image'] ?? null,
  77. 'category_id' => $params['category_id'] ?? null,
  78. 'category_name' => $category->name,
  79. 'is_serve' => (int)$params['is_serve'],
  80. 'purchase_price'=> $params['purchase_price'],
  81. 'selling_price' => $params['selling_price'],
  82. 'update_time' => time()
  83. ]);
  84. return $this->ok($res);
  85. }
  86. View::assign([
  87. 'product' => $product,
  88. 'all_category' => recursion($this->categoryModel->findAll(),0),
  89. ]);
  90. return view();
  91. }
  92. public function delete(Request $request) {
  93. $params = $request->param();
  94. if(!isset($params['ids']))
  95. return $this->fail(lang("Please select the data you want to delete"));
  96. $this->productModel->deleteByIds(explode(',',$params['ids']));
  97. return $this->ok(true);
  98. }
  99. /**
  100. * @return \think\response\Json
  101. */
  102. public function upload() {
  103. // 获取表单上传文件 例如上传了001.jpg
  104. $file = request()->file('file');
  105. // 上传到本地服务器
  106. $save_name = \think\facade\Filesystem::disk('public')->putFile( 'product', $file);
  107. return $this->ok(str_replace('\\',"/",'/storage/'.$save_name));
  108. }
  109. public function findProducts(Request $request) {
  110. $params = $request->param();
  111. if(!isset($params['text']))
  112. return $this->fail(lang('Data not exist'));
  113. $products = $this->productModel->findProducts($params['text']);
  114. return $this->ok($products);
  115. }
  116. }