Note.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use think\Exception;
  5. class Note extends BaseController
  6. {
  7. //获取列表
  8. public function get(): \think\response\Json
  9. {
  10. $user = $this->getUser();
  11. $sort = $this->request->get('sort', 'desc');
  12. $limit = $this->request->get('limit', 999999);
  13. if (!$user) {
  14. return $this->success('', []);
  15. }
  16. $data = (new \app\model\NoteModel)->where("user_id", $user['user_id'])->field('user_id,id,title,create_time,update_time')->order('id', $sort)->limit($limit)->select();
  17. return $this->success('ok', $data);
  18. }
  19. //获取文本
  20. public function getText(): \think\Response
  21. {
  22. $user = $this->getUser(true);
  23. $id = $this->request->get('id');
  24. $data = (new \app\model\NoteModel)->where("user_id", $user['user_id'])->field("text,id")->where('id', $id)->find();
  25. try {
  26. return response($data['text']);
  27. } catch (Exception $e) {
  28. return response('');
  29. }
  30. }
  31. //删除
  32. public function del(): \think\response\Json
  33. {
  34. $user = $this->getUser(true);
  35. $id = $this->request->get('id');
  36. $data = (new \app\model\NoteModel)->where("user_id", $user['user_id'])->where('id', $id)->delete();
  37. return $this->success('删除成功', $data);
  38. }
  39. //添加内容
  40. public function add(): \think\response\Json
  41. {
  42. $user = $this->getUser(true);
  43. $title = $this->request->post('title', '');
  44. $text = $this->request->post('text', '');
  45. $id = $this->request->post('id', false);
  46. if ($id != '') {
  47. return $this->update();
  48. }
  49. $data = array(
  50. "user_id" => $user['user_id'],
  51. "text" => $text,
  52. "title" => $title,
  53. "create_time" => date("Y-m-d H:i:s"),
  54. "update_time" => date("Y-m-d H:i:s"),
  55. );
  56. $status = (new \app\model\NoteModel)->insertGetId($data);
  57. if ($status) {
  58. $data['id'] = $status;
  59. return $this->success("创建成功", $data);
  60. }
  61. return $this->error('失败');
  62. }
  63. //更新内容
  64. public function update(): \think\response\Json
  65. {
  66. $user = $this->getUser(true);
  67. $id = $this->request->post('id', false);
  68. if (!$id) {
  69. return $this->error('no');
  70. }
  71. $title = $this->request->post('title', '');
  72. $text = $this->request->post('text', '');
  73. $data = array(
  74. "user_id" => $user['user_id'],
  75. "text" => $text,
  76. "title" => $title,
  77. "update_time" => date("Y-m-d H:i:s"),
  78. );
  79. $status = (new \app\model\NoteModel)->where("id", $id)->where('user_id', $user['user_id'])->update($data);
  80. if ($status) {
  81. $data['id'] = $id;
  82. return $this->success("修改", $data);
  83. }
  84. return $this->error('失败');
  85. }
  86. }