Store.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use app\common\model\StoreModel;
  5. use think\App;
  6. use think\facade\View;
  7. use think\Request;
  8. class Store extends BaseController
  9. {
  10. private $storeModel;
  11. public function __construct(App $app)
  12. {
  13. $this->storeModel = new StoreModel();
  14. parent::__construct($app);
  15. }
  16. public function index(Request $request) {
  17. $params = $request->param();
  18. View::assign([
  19. 'list' => $this->storeModel->findByPaginate($params),
  20. 'params' => $params,
  21. ]);
  22. return view();
  23. }
  24. public function add(Request $request) {
  25. if($request->isAjax()) {
  26. $params = $request->param();
  27. list($name,$abbr,$address) = [$params['name'],$params['abbr'],$params['address']];
  28. $store = $this->storeModel->doesItExist($abbr);
  29. if ($store)
  30. return $this->fail(lang('Fail to add. Data duplication'));
  31. $res = $this->storeModel->save([
  32. 'name' => $name,
  33. 'abbr' => $abbr,
  34. 'address' => $address
  35. ]);
  36. return $this->ok($res);
  37. }
  38. return view();
  39. }
  40. public function edit(Request $request) {
  41. $params = $request->param();
  42. if(!isset($params['id']))
  43. return $this->fail(lang('ID not exist'));
  44. $store = $this->storeModel->findById($params['id']);
  45. if (!$store)
  46. return $this->fail(lang('Data not exist'));
  47. if ($request->isAjax()) {
  48. $exist_store = $this->storeModel->doesItExist($params['abbr']);
  49. if ($exist_store && $exist_store->id != $params['id'])
  50. return $this->fail(lang('Data duplication'));
  51. $res = $this->storeModel->where('id',$params['id'])->update([
  52. 'name' => $params['name'],
  53. 'abbr' => $params['abbr'],
  54. 'address' => $params['address'],
  55. 'update_time' => time()
  56. ]);
  57. return $this->ok($res);
  58. }
  59. View::assign('store', $store);
  60. return view();
  61. }
  62. public function delete(Request $request) {
  63. $params = $request->param();
  64. if(!isset($params['ids']))
  65. return $this->fail(lang("Please select the data you want to delete"));
  66. $this->storeModel->deleteByIds(explode(',', $params['ids']));
  67. return $this->ok(true);
  68. }
  69. public function total_data(Request $request) {
  70. $params = $request->param();
  71. if(!isset($params['id']))
  72. return $this->fail(lang('ID not exist'));
  73. $store = $this->storeModel->findById($params['id']);
  74. if (!$store)
  75. return $this->fail(lang('Data not exist'));
  76. View::assign('store', $store);
  77. return view();
  78. }
  79. }