InfoController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. $order = array();
  34. if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  35. $order[$request['sort_field']] = $request['sort_field_by'];
  36. } else {
  37. $order['id'] = 'DESC';
  38. }
  39. $list = $this->repository->searchCompany($search, $order);
  40. if ($request->ajax()) {
  41. $view = view('admin.company.info.data', compact('list'))->render();
  42. return response()->json(['html' => $view]);
  43. }
  44. return view('admin.company.info.index', compact('list'));
  45. }
  46. /**
  47. * 添加
  48. */
  49. public function create(Request $request)
  50. {
  51. if ($request->method() == 'POST') {
  52. return $this->_createSave();
  53. }
  54. return view('admin.company.info.edit');
  55. }
  56. /**
  57. * 保存修改
  58. */
  59. private function _createSave()
  60. {
  61. $data = (array)request('data');
  62. $id = $this->repository->create($data);
  63. if ($id) {
  64. $url[] = array('url' => U('Company/Info/index'), 'title' => '返回列表');
  65. $url[] = array('url' => U('Company/Info/create'), 'title' => '继续添加');
  66. $this->showMessage('添加成功', $url);
  67. } else {
  68. $url[] = array('url' => U('Company/Info/index'), 'title' => '返回列表');
  69. return $this->showWarning('添加失败', $url);
  70. }
  71. }
  72. /**
  73. * 修改
  74. */
  75. public function update(Request $request)
  76. {
  77. if ($request->method() == 'POST') {
  78. return $this->_updateSave();
  79. }
  80. $data = $this->repository->find($request->get('id'));
  81. return view('admin.company.info.edit', compact('data'));
  82. }
  83. /**
  84. * 保存修改
  85. */
  86. private function _updateSave()
  87. {
  88. $data = (array)request('data');
  89. $ok = $this->repository->update(request('id'), $data);
  90. if ($ok) {
  91. $url[] = array('url' => U('Company/Info/index'), 'title' => '返回列表');
  92. return $this->showMessage('操作成功', urldecode(request('_referer')));
  93. } else {
  94. $url[] = array('url' => U('Company/Info/index'), 'title' => '返回列表');
  95. return $this->showWarning('操作失败', $url);
  96. }
  97. }
  98. public function view(Request $request)
  99. {
  100. $data = $this->repository->find(request('id'));
  101. $contacts = $data->contacts()->orderBy('sort', 'desc')->get();
  102. return view('admin.company.info.view', compact('data', 'contacts'));
  103. }
  104. /**
  105. * 删除
  106. */
  107. public function destroy(Request $request)
  108. {
  109. $bool = $this->repository->destroy($request->get('id'));
  110. if ($bool) {
  111. return $this->showMessage('操作成功');
  112. } else {
  113. return $this->showWarning("操作失败");
  114. }
  115. }
  116. }