Note.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. class Note extends BaseController
  5. {
  6. //获取列表
  7. public function get(): \think\response\Json
  8. {
  9. $user = $this->getUser();
  10. $limit = $this->request->get('limit', 999999);
  11. if (!$user) {
  12. return $this->success('', []);
  13. }
  14. $data = (new \app\model\NoteModel)->where("user_id", $user['user_id'])->field('user_id,id,title,create_time,update_time,weight,sort')->order(['sort'=>'asc','create_time'=>'desc'])->limit($limit)->select();
  15. return $this->success('ok', $data);
  16. }
  17. function sort(): \think\response\Json
  18. {
  19. $user = $this->getUser(true);
  20. $ids = $this->request->post('ids', []);
  21. $data = (new \app\model\NoteModel)->field("id,user_id,sort")->where("user_id", $user['user_id'])->whereIn('id', $ids)->select()->toArray();
  22. //查询到和id做个比对重新设置sort入库,批量入库,
  23. $data_map = [];
  24. foreach ($data as $k => $v) {
  25. $data_map[$v['id']] = $v;
  26. }
  27. $update_data = [];
  28. foreach ($ids as $k => $v) {
  29. if (isset($data_map[$v])) {
  30. $update_data[] = [
  31. "id" => $v,
  32. "sort" => $k
  33. ];
  34. }
  35. }
  36. try {
  37. (new \app\model\NoteModel)->saveAll($update_data);
  38. } catch (\Exception $e) {
  39. return $this->error('排序失败');
  40. }
  41. return $this->success('ok');
  42. }
  43. //获取文本
  44. public function getText(): \think\Response
  45. {
  46. $user = $this->getUser(true);
  47. $id = $this->request->get('id');
  48. $data = (new \app\model\NoteModel)->where("user_id", $user['user_id'])->field("text,id")->where('id', $id)->find();
  49. return response($data['text']);
  50. }
  51. function setWeight(): \think\response\Json
  52. {
  53. $user = $this->getUser(true);
  54. $weight = $this->request->post('weight', 0);
  55. $id = $this->request->post('id', false);
  56. if ($id) {
  57. $data = array(
  58. 'weight' => $weight,
  59. 'update_time' => date('Y-m-d H:i:s'),
  60. );
  61. (new \app\model\NoteModel)->where('id', $id)->where('user_id', $user['user_id'])->update($data);
  62. }
  63. return $this->success("ok");
  64. }
  65. //删除
  66. public function del(): \think\response\Json
  67. {
  68. $user = $this->getUser(true);
  69. $id = $this->request->get('id');
  70. $data = (new \app\model\NoteModel)->where("user_id", $user['user_id'])->where('id', $id)->delete();
  71. return $this->success('删除成功', $data);
  72. }
  73. //添加内容
  74. public function add(): \think\response\Json
  75. {
  76. $user = $this->getUser(true);
  77. $title = $this->request->post('title', '');
  78. $text = $this->request->post('text', '');
  79. $id = $this->request->post('id', false);
  80. if ($id != '') {
  81. return $this->update();
  82. }
  83. $data = array(
  84. "user_id" => $user['user_id'],
  85. "text" => $text,
  86. "title" => $title,
  87. 'weight' => $this->request->post("weight", 0),
  88. "create_time" => date("Y-m-d H:i:s"),
  89. "update_time" => date("Y-m-d H:i:s"),
  90. );
  91. $status = (new \app\model\NoteModel)->insertGetId($data);
  92. if ($status) {
  93. $data['id'] = $status;
  94. return $this->success("创建成功", $data);
  95. }
  96. return $this->error('失败');
  97. }
  98. //更新内容
  99. public function update(): \think\response\Json
  100. {
  101. $user = $this->getUser(true);
  102. $id = $this->request->post('id', false);
  103. if (!$id) {
  104. return $this->error('no');
  105. }
  106. $title = $this->request->post('title', '');
  107. $text = $this->request->post('text', '');
  108. $data = array(
  109. "text" => $text,
  110. "title" => $title,
  111. 'weight' => $this->request->post('weight', 0),
  112. "update_time" => date("Y-m-d H:i:s"),
  113. );
  114. $status = (new \app\model\NoteModel)->where("id", $id)->where('user_id', $user['user_id'])->find()->save($data);
  115. if ($status) {
  116. $data['id'] = $id;
  117. return $this->success("修改", $data);
  118. }
  119. return $this->error('失败');
  120. }
  121. }