RecordsController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 App\Models\CallRecordsModel;
  13. use Illuminate\Http\Request;
  14. use App\Repositories\Base\Criteria\OrderBy;
  15. use App\Repositories\Call\Records\Criteria\MultiWhere;
  16. use App\Repositories\Call\RecordsRepository;
  17. class RecordsController extends Controller
  18. {
  19. private $repository;
  20. public function __construct(RecordsRepository $repository)
  21. {
  22. if (!$this->repository) $this->repository = $repository;
  23. }
  24. /**
  25. * 列表页
  26. */
  27. function index(Request $request)
  28. {
  29. $search = $request->all();
  30. $order = array();
  31. if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  32. $order[$request['sort_field']] = $request['sort_field_by'];
  33. } else {
  34. $order['id'] = 'DESC';
  35. }
  36. $list = $this->repository->searchRecords($search, $order);
  37. $allIds = $list->pluck('id');
  38. $list = $list->paginate(20);
  39. if ($request->ajax()) {
  40. $view = view('admin.call.records.data', compact('list','allIds'))->render();
  41. return response()->json(['html' => $view]);
  42. }
  43. return view('admin.call.records.index', compact('list','allIds'));
  44. }
  45. /**
  46. * 添加
  47. */
  48. public function create(Request $request)
  49. {
  50. if ($request->method() == 'POST') {
  51. return $this->_createSave();
  52. }
  53. return view('admin.call.records.edit');
  54. }
  55. /**
  56. * 保存修改
  57. */
  58. private function _createSave()
  59. {
  60. $data = (array)request('data');
  61. $id = $this->repository->create($data);
  62. if ($id) {
  63. $url[] = array('url' => U('Call/Records/index'), 'title' => '返回列表');
  64. $url[] = array('url' => U('Call/Records/create'), 'title' => '继续添加');
  65. $this->showMessage('添加成功', $url);
  66. } else {
  67. $url[] = array('url' => U('Call/Records/index'), 'title' => '返回列表');
  68. return $this->showWarning('添加失败', $url);
  69. }
  70. }
  71. /**
  72. * 修改
  73. */
  74. public function update(Request $request)
  75. {
  76. if ($request->method() == 'POST') {
  77. return $this->_updateSave();
  78. }
  79. $data = $this->repository->find($request->get('id'));
  80. return view('admin.call.records.edit', compact('data'));
  81. }
  82. /**
  83. * 保存修改
  84. */
  85. private function _updateSave()
  86. {
  87. $data = (array)request('data');
  88. $ok = $this->repository->update(request('id'), $data);
  89. if ($ok) {
  90. $url[] = array('url' => U('Call/Records/index'), 'title' => '返回列表');
  91. return $this->showMessage('操作成功', urldecode(request('_referer')));
  92. } else {
  93. $url[] = array('url' => U('Call/Records/index'), 'title' => '返回列表');
  94. return $this->showWarning('操作失败', $url);
  95. }
  96. }
  97. public function view(Request $request)
  98. {
  99. $data = $this->repository->find(request('id'));
  100. $list = $data->process();
  101. return view('admin.call.records.view', compact('data', 'list'));
  102. }
  103. /**
  104. * 删除
  105. */
  106. public function destroy(Request $request)
  107. {
  108. $bool = $this->repository->destroy($request->get('id'));
  109. if ($bool) {
  110. return $this->showMessage('操作成功');
  111. } else {
  112. return $this->showWarning("操作失败");
  113. }
  114. }
  115. public function addCallList(Request $request)
  116. {
  117. $ids = $request->get('contact_phones');
  118. $ip = $request->get('ip');
  119. $phones = CallRecordsModel::whereIn('id', $ids)->get();
  120. foreach ($phones as $phone) {
  121. $hasAdd = CallListModel::where('phone', $phone->phone)->where('sync', 0)->count();
  122. if (!$hasAdd) {
  123. CallListModel::create(['phone' => $phone->phone, 'sync' => 0, 'ip' => $ip]);
  124. }
  125. }
  126. return 200;
  127. }
  128. }