InfoController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * 挖掘线索
  4. * @author system
  5. * @version 1.0
  6. * @date 2018-11-19 08:23:08
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\Company;
  10. use App\Http\Controllers\Admin\Controller;
  11. use Illuminate\Http\Request;
  12. use App\Repositories\Company\InfoRepository;
  13. class InfoController extends Controller
  14. {
  15. private $repository;
  16. public function __construct(InfoRepository $repository)
  17. {
  18. if (!$this->repository) $this->repository = $repository;
  19. }
  20. /*
  21. * 搜索页
  22. * */
  23. public function search()
  24. {
  25. return view('admin.company.info.search');
  26. }
  27. /**
  28. * 列表页
  29. */
  30. function index(Request $request)
  31. {
  32. $search = $request->all();
  33. //dd($search);
  34. $order = array();
  35. if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  36. $order[$request['sort_field']] = $request['sort_field_by'];
  37. } else {
  38. $order['id'] = 'DESC';
  39. }
  40. $list = $this->repository->searchCompany($search, $order);
  41. if ($request->ajax()) {
  42. $view = view('admin.company.info.data', compact('list'))->render();
  43. return response()->json(['html' => $view]);
  44. }
  45. return view('admin.company.info.index', compact('list'));
  46. }
  47. /**
  48. * 添加
  49. */
  50. public function create(Request $request)
  51. {
  52. if ($request->method() == 'POST') {
  53. return $this->_createSave();
  54. }
  55. return view('admin.company.info.edit');
  56. }
  57. /**
  58. * 保存修改
  59. */
  60. private function _createSave()
  61. {
  62. $data = (array)request('data');
  63. $id = $this->repository->create($data);
  64. if ($id) {
  65. $url[] = array('url' => U('Company/Info/index'), 'title' => '返回列表');
  66. $url[] = array('url' => U('Company/Info/create'), 'title' => '继续添加');
  67. $this->showMessage('添加成功', $url);
  68. } else {
  69. $url[] = array('url' => U('Company/Info/index'), 'title' => '返回列表');
  70. return $this->showWarning('添加失败', $url);
  71. }
  72. }
  73. /**
  74. * 修改
  75. */
  76. public function update(Request $request)
  77. {
  78. if ($request->method() == 'POST') {
  79. return $this->_updateSave();
  80. }
  81. $data = $this->repository->find($request->get('id'));
  82. return view('admin.company.info.edit', compact('data'));
  83. }
  84. /**
  85. * 保存修改
  86. */
  87. private function _updateSave()
  88. {
  89. $data = (array)request('data');
  90. $ok = $this->repository->update(request('id'), $data);
  91. if ($ok) {
  92. $url[] = array('url' => U('Company/Info/index'), 'title' => '返回列表');
  93. return $this->showMessage('操作成功', urldecode(request('_referer')));
  94. } else {
  95. $url[] = array('url' => U('Company/Info/index'), 'title' => '返回列表');
  96. return $this->showWarning('操作失败', $url);
  97. }
  98. }
  99. public function view(Request $request)
  100. {
  101. $data = $this->repository->find(request('id'));
  102. $contacts = $data->contacts()->orderBy('sort', 'desc')->get();
  103. return view('admin.company.info.view', compact('data', 'contacts'));
  104. }
  105. /**
  106. * 删除
  107. */
  108. public function destroy(Request $request)
  109. {
  110. $bool = $this->repository->destroy($request->get('id'));
  111. if ($bool) {
  112. return $this->showMessage('操作成功');
  113. } else {
  114. return $this->showWarning("操作失败");
  115. }
  116. }
  117. }