RecordsController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * 通话纪录
  4. * @author system
  5. * @version 1.0
  6. * @date 2018-11-27 03:15:47
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\Call;
  10. use App\Http\Controllers\Admin\Controller;
  11. use App\Models\CallListModel;
  12. use Illuminate\Http\Request;
  13. use App\Repositories\Base\Criteria\OrderBy;
  14. use App\Repositories\Call\Records\Criteria\MultiWhere;
  15. use App\Repositories\Call\RecordsRepository;
  16. class RecordsController extends Controller
  17. {
  18. private $repository;
  19. public function __construct(RecordsRepository $repository)
  20. {
  21. if (!$this->repository) $this->repository = $repository;
  22. }
  23. /**
  24. * 列表页
  25. */
  26. function index(Request $request)
  27. {
  28. $search['keyword'] = $request->input('keyword');
  29. $query = $this->repository->pushCriteria(new MultiWhere($search));
  30. if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  31. $query = $query->pushCriteria(new OrderBy($request['sort_field'], $request['sort_field_by']));
  32. } else {
  33. $query = $query->pushCriteria(new OrderBy('id', 'DESC'));
  34. }
  35. $list = $query->paginate(16);
  36. return view('admin.call.records.index', compact('list'));
  37. }
  38. /**
  39. * 添加
  40. */
  41. public function create(Request $request)
  42. {
  43. if ($request->method() == 'POST') {
  44. return $this->_createSave();
  45. }
  46. return view('admin.call.records.edit');
  47. }
  48. /**
  49. * 保存修改
  50. */
  51. private function _createSave()
  52. {
  53. $data = (array)request('data');
  54. $id = $this->repository->create($data);
  55. if ($id) {
  56. $url[] = array('url' => U('Call/Records/index'), 'title' => '返回列表');
  57. $url[] = array('url' => U('Call/Records/create'), 'title' => '继续添加');
  58. $this->showMessage('添加成功', $url);
  59. } else {
  60. $url[] = array('url' => U('Call/Records/index'), 'title' => '返回列表');
  61. return $this->showWarning('添加失败', $url);
  62. }
  63. }
  64. /**
  65. * 修改
  66. */
  67. public function update(Request $request)
  68. {
  69. if ($request->method() == 'POST') {
  70. return $this->_updateSave();
  71. }
  72. $data = $this->repository->find($request->get('id'));
  73. return view('admin.call.records.edit', compact('data'));
  74. }
  75. /**
  76. * 保存修改
  77. */
  78. private function _updateSave()
  79. {
  80. $data = (array)request('data');
  81. $ok = $this->repository->update(request('id'), $data);
  82. if ($ok) {
  83. $url[] = array('url' => U('Call/Records/index'), 'title' => '返回列表');
  84. return $this->showMessage('操作成功', urldecode(request('_referer')));
  85. } else {
  86. $url[] = array('url' => U('Call/Records/index'), 'title' => '返回列表');
  87. return $this->showWarning('操作失败', $url);
  88. }
  89. }
  90. public function view(Request $request)
  91. {
  92. $data = $this->repository->find(request('id'));
  93. $list = $data->process();
  94. return view('admin.call.records.view', compact('data','list'));
  95. }
  96. /**
  97. * 删除
  98. */
  99. public function destroy(Request $request)
  100. {
  101. $bool = $this->repository->destroy($request->get('id'));
  102. if ($bool) {
  103. return $this->showMessage('操作成功');
  104. } else {
  105. return $this->showWarning("操作失败");
  106. }
  107. }
  108. public function addCallList(Request $request)
  109. {
  110. $phones = $request->get('contact_phones');
  111. foreach ($phones as $phone) {
  112. $hasAdd = CallListModel::where('phone', $phone)->where('sync', 0)->count();
  113. if (!$hasAdd) {
  114. CallListModel::create(['phone' => $phone, 'sync' => 0]);
  115. }
  116. }
  117. return 200;
  118. }
  119. }