RecordsController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. if ($request->ajax()) {
  38. $view = view('admin.call.records.data', compact('list'))->render();
  39. return response()->json(['html' => $view]);
  40. }
  41. return view('admin.call.records.index', compact('list'));
  42. }
  43. /**
  44. * 添加
  45. */
  46. public function create(Request $request)
  47. {
  48. if ($request->method() == 'POST') {
  49. return $this->_createSave();
  50. }
  51. return view('admin.call.records.edit');
  52. }
  53. /**
  54. * 保存修改
  55. */
  56. private function _createSave()
  57. {
  58. $data = (array)request('data');
  59. $id = $this->repository->create($data);
  60. if ($id) {
  61. $url[] = array('url' => U('Call/Records/index'), 'title' => '返回列表');
  62. $url[] = array('url' => U('Call/Records/create'), 'title' => '继续添加');
  63. $this->showMessage('添加成功', $url);
  64. } else {
  65. $url[] = array('url' => U('Call/Records/index'), 'title' => '返回列表');
  66. return $this->showWarning('添加失败', $url);
  67. }
  68. }
  69. /**
  70. * 修改
  71. */
  72. public function update(Request $request)
  73. {
  74. if ($request->method() == 'POST') {
  75. return $this->_updateSave();
  76. }
  77. $data = $this->repository->find($request->get('id'));
  78. return view('admin.call.records.edit', compact('data'));
  79. }
  80. /**
  81. * 保存修改
  82. */
  83. private function _updateSave()
  84. {
  85. $data = (array)request('data');
  86. $ok = $this->repository->update(request('id'), $data);
  87. if ($ok) {
  88. $url[] = array('url' => U('Call/Records/index'), 'title' => '返回列表');
  89. return $this->showMessage('操作成功', urldecode(request('_referer')));
  90. } else {
  91. $url[] = array('url' => U('Call/Records/index'), 'title' => '返回列表');
  92. return $this->showWarning('操作失败', $url);
  93. }
  94. }
  95. public function view(Request $request)
  96. {
  97. $data = $this->repository->find(request('id'));
  98. $list = $data->process();
  99. return view('admin.call.records.view', compact('data', 'list'));
  100. }
  101. /**
  102. * 删除
  103. */
  104. public function destroy(Request $request)
  105. {
  106. $bool = $this->repository->destroy($request->get('id'));
  107. if ($bool) {
  108. return $this->showMessage('操作成功');
  109. } else {
  110. return $this->showWarning("操作失败");
  111. }
  112. }
  113. public function addCallList(Request $request)
  114. {
  115. $ids = $request->get('contact_phones');
  116. $ip = $request->get('ip');
  117. $phones = CallRecordsModel::whereIn('id', $ids)->get();
  118. foreach ($phones as $phone) {
  119. $hasAdd = CallListModel::where('phone', $phone->phone)->where('sync', 0)->count();
  120. if (!$hasAdd) {
  121. CallListModel::create(['phone' => $phone->phone, 'sync' => 0, 'ip' => $ip]);
  122. }
  123. }
  124. return 200;
  125. }
  126. }