MessageController.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Http\Controllers\AdminApi;
  3. use App\Models\MessageModels\Message;
  4. use App\Http\Controllers\AdminController;
  5. class MessageController extends AdminController
  6. {
  7. /**
  8. * 获取留言列表
  9. * @return \Illuminate\Http\JsonResponse
  10. */
  11. function getList(){
  12. $name = request()->input('name','');//获取搜索名称
  13. $phone = request()->input('phone','');//获取搜索手机号
  14. $start = request()->input('start','');//获取开始时间
  15. $end = request()->input('end','');//获取结束时间
  16. //查询数据条件
  17. $where = [['is_del',0]];
  18. if ($name) $where[] = ['name', 'like', "%$name%"];
  19. if ($phone) $where[] = ['phone', 'like', "%$phone%"];
  20. if ($start) $where[] = ['created_at', '>=', $start];
  21. if ($end) $where[] = ['created_at', '<=', $end];
  22. //获取数据
  23. $list = Message::where($where)
  24. ->orderBy('id','desc')
  25. ->select(['id','name','phone','created_at'])
  26. ->paginate(10);
  27. return $this->apiResponseSuccess('获取信息成功', [
  28. 'list' => $list->items(),
  29. 'total' => $list->total(),
  30. 'limit' => 10
  31. ]);
  32. }
  33. /**
  34. * 获取记录详情
  35. * @return \Illuminate\Http\JsonResponse
  36. */
  37. function getInfo()
  38. {
  39. $id = request()->input('id', '');//获取需要查询的记录id
  40. if (empty($id)) return $this->apiResponseError('缺少必要参数');
  41. //查询数据
  42. $where = [['id', $id], ['is_del',0]];
  43. $info = Message::where($where)->select(['id', 'name', 'phone', 'content', 'created_at'])->first();
  44. if (empty($info)) return $this->apiResponseError('没有找到该记录');
  45. return $this->apiResponseSuccess('获取成功', $info);
  46. }
  47. /**
  48. * 删除(批量)记录
  49. * @return \Illuminate\Http\JsonResponse
  50. */
  51. function destroys(){
  52. $ids = request()->input('ids','');//获取需要删除的id
  53. if(empty($ids)) return $this->apiResponseError('缺少必要参数');
  54. //数据条件
  55. if(!is_array($ids)) return $this->apiResponseError('数据格式错误');
  56. $res = Message::whereIn('id',$ids)->update(['is_del' => 1]);
  57. if ($res) {
  58. return $this->apiResponseSuccess('删除成功');
  59. } else {
  60. return $this->apiResponseError('删除失败');
  61. }
  62. }
  63. }