SpecController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\AdminRoleModel;
  4. use App\Models\AdminUserModel;
  5. use App\Models\Device;
  6. use App\Models\DeviceName;
  7. use App\Models\Role;
  8. use App\Models\Spec;
  9. use App\Models\User;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Auth;
  12. use Illuminate\Support\Facades\Hash;
  13. class SpecController extends BaseController
  14. {
  15. protected $model;
  16. protected $device;
  17. protected $device_name;
  18. protected $model_name = '规格型号';
  19. protected $pre_uri = '/admin/Spec/';
  20. protected $view_path = 'admin.specs.';
  21. protected $redirect_index = '/admin/Spec/index';
  22. public function __construct()
  23. {
  24. $this->model = new Spec();
  25. $this->device = new Device();
  26. $this->device_name = new DeviceName();
  27. }
  28. public function index()
  29. {
  30. $options = $this->device->getDeviceOptions();
  31. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  32. return view($this->view_path . 'index', compact('model', 'model_name','pre_uri', 'options'));
  33. }
  34. public function get(Request $request)
  35. {
  36. $items = $this->model->where('id', '>', 0)->orderBy('sort');
  37. $tmp_items = collect(['name']);
  38. foreach($tmp_items as $tmp_item) {
  39. if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
  40. $items = $items->where($tmp_item, 'like', '%' . $request->input($tmp_item) . '%');
  41. }
  42. }
  43. $select_items = collect(['device_id', 'device_name_id']);
  44. foreach($select_items as $select_item) {
  45. if($request->has($select_item) && !empty($request->input($select_item))) {
  46. $items = $items->where($select_item, '=', $request->input($select_item));
  47. }
  48. }
  49. $items = $items->paginate();
  50. foreach($items as $item) {
  51. $item->device_type = empty($item->device) ? '' : $item->device->name;
  52. $item->device_name_name = empty($item->device_name) ? '' : $item->device_name->name;
  53. }
  54. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  55. }
  56. public function create()
  57. {
  58. $options = $this->device->getDeviceOptions();
  59. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  60. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri', 'options'));
  61. }
  62. public function store(Request $request)
  63. {
  64. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  65. $validator = $this->model->getValidator($request, 'store');
  66. if($validator->fails()) {
  67. return back()->withErrors($validator)->withInput();
  68. }
  69. $data = $request->input('data');
  70. $data['device_id'] = $request->input('device_id');
  71. $data['device_name_id'] = $request->input('device_name_id');
  72. $res = $this->model->create($data);
  73. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  74. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  75. }
  76. public function edit(Request $request)
  77. {
  78. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  79. $options = $this->device->getDeviceOptions();
  80. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  81. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item', 'options'));
  82. }
  83. public function update(Request $request)
  84. {
  85. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  86. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  87. $validator = $this->model->getValidator($request, 'update');
  88. if($validator->fails()) {
  89. return back()->withErrors($validator)->withInput();
  90. }
  91. $data = $request->input('data');
  92. $data['device_id'] = $request->input('device_id');
  93. $data['device_name_id'] = $request->input('device_name_id');
  94. $res = $this->model->where('id', $request->input('id'))->update($data);
  95. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  96. return back()->with(['sg_success_info' => '编辑成功']);
  97. }
  98. public function delete(Request $request)
  99. {
  100. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  101. $res = $item->delete();
  102. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  103. return response()->json(['status' => 'success', 'info' => '操作成功']);
  104. }
  105. }