Note.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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,weight')->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. function setWeight(): \think\response\Json
  32. {
  33. $user = $this->getUser(true);
  34. $weight = $this->request->post('weight', 0);
  35. $id = $this->request->post('id', false);
  36. if ($id) {
  37. $data = array(
  38. 'weight' => $weight,
  39. 'update_time' => date('Y-m-d H:i:s'),
  40. );
  41. (new \app\model\NoteModel)->where('id', $id)->where('user_id', $user['user_id'])->update($data);
  42. }
  43. return $this->success("ok");
  44. }
  45. //删除
  46. public function del(): \think\response\Json
  47. {
  48. $user = $this->getUser(true);
  49. $id = $this->request->get('id');
  50. $data = (new \app\model\NoteModel)->where("user_id", $user['user_id'])->where('id', $id)->delete();
  51. return $this->success('删除成功', $data);
  52. }
  53. //添加内容
  54. public function add(): \think\response\Json
  55. {
  56. $user = $this->getUser(true);
  57. $title = $this->request->post('title', '');
  58. $text = $this->request->post('text', '');
  59. $id = $this->request->post('id', false);
  60. if ($id != '') {
  61. return $this->update();
  62. }
  63. $data = array(
  64. "user_id" => $user['user_id'],
  65. "text" => $text,
  66. "title" => $title,
  67. 'weight' => $this->request->post("weight", 0),
  68. "create_time" => date("Y-m-d H:i:s"),
  69. "update_time" => date("Y-m-d H:i:s"),
  70. );
  71. $status = (new \app\model\NoteModel)->insertGetId($data);
  72. if ($status) {
  73. $data['id'] = $status;
  74. return $this->success("创建成功", $data);
  75. }
  76. return $this->error('失败');
  77. }
  78. //更新内容
  79. public function update(): \think\response\Json
  80. {
  81. $user = $this->getUser(true);
  82. $id = $this->request->post('id', false);
  83. if (!$id) {
  84. return $this->error('no');
  85. }
  86. $title = $this->request->post('title', '');
  87. $text = $this->request->post('text', '');
  88. $data = array(
  89. "text" => $text,
  90. "title" => $title,
  91. 'weight' => $this->request->post('weight', 0),
  92. "update_time" => date("Y-m-d H:i:s"),
  93. );
  94. $status = (new \app\model\NoteModel)->where("id", $id)->where('user_id', $user['user_id'])->update($data);
  95. if ($status) {
  96. $data['id'] = $id;
  97. return $this->success("修改", $data);
  98. }
  99. return $this->error('失败');
  100. }
  101. }