like.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. require_once "../config.php";
  3. require_once "../db/table.php";
  4. /*
  5. CREATE TABLE likes (
  6. id INTEGER PRIMARY KEY AUTOINCREMENT,
  7. like_type VARCHAR (16) NOT NULL,
  8. resource_type VARCHAR (32) NOT NULL,
  9. resource_id CHAR (36) NOT NULL,
  10. user_id INTEGER NOT NULL,
  11. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL //只做初始化,更新时不自动更新
  12. );
  13. */
  14. class Like extends Table
  15. {
  16. function __construct($redis=false) {
  17. parent::__construct(_FILE_DB_LIKE_, "likes", "", "",$redis);
  18. }
  19. public function index(){
  20. $where["like_type"] = "like";
  21. $where["resource_type"] = $_GET["type"];
  22. $where["resource_id"] = explode($_GET["id"],",");
  23. echo json_encode($this->_index(["resource_id","user_id"],$where), JSON_UNESCAPED_UNICODE);
  24. }
  25. public function list(){
  26. if(!isset($_COOKIE["userid"])){
  27. $userId = $_COOKIE["userid"];
  28. }
  29. $json = file_get_contents('php://input');
  30. $data = json_decode($json,true);
  31. foreach ($data as $key => $value) {
  32. # code...
  33. $data[$key]['like']=$this->medoo->count($this->table,[
  34. 'like_type'=>$value['like_type'],
  35. 'resource_type'=>$value['resource_type'],
  36. 'resource_id'=>$value['resource_id'],
  37. ]);
  38. }
  39. if(isset($_COOKIE["userid"])){
  40. $userId = $_COOKIE["userid"];
  41. foreach ($data as $key => $value) {
  42. # code...
  43. $data[$key]['me']=$this->medoo->count($this->table,[
  44. 'like_type'=>$value['like_type'],
  45. 'resource_type'=>$value['resource_type'],
  46. 'resource_id'=>$value['resource_id'],
  47. 'user_id'=>$userId,
  48. ]);
  49. }
  50. }
  51. $this->result["ok"]=true;
  52. $this->result["message"]="";
  53. $this->result["data"]=$data;
  54. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  55. }
  56. public function create(){
  57. if(!isset($_COOKIE["userid"])){
  58. return;
  59. }
  60. $json = file_get_contents('php://input');
  61. $data = json_decode($json,true);
  62. $data["user_id"] = $_COOKIE["userid"];
  63. $isExist = $this->medoo->has("likes",$data);
  64. if(!$isExist){
  65. echo json_encode($this->_create($data,["like_type","resource_type","resource_id","user_id"]), JSON_UNESCAPED_UNICODE);
  66. }
  67. else{
  68. $this->result["ok"]=false;
  69. $this->result["message"]="is exist";
  70. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  71. }
  72. }
  73. public function delete(){
  74. if(!isset($_COOKIE["userid"])){
  75. return;
  76. }
  77. $where["like_type"] = $_GET["like_type"];
  78. $where["resource_type"] = $_GET["resource_type"];
  79. $where["resource_id"] = $_GET["resource_id"];
  80. $where["user_id"] = $_COOKIE["userid"];
  81. $row = $this->_delete($where);
  82. if($row["data"]>0){
  83. $this->result["data"] = $where;
  84. }else{
  85. $this->result["ok"]=false;
  86. $this->result["message"]="no delete";
  87. }
  88. echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
  89. }
  90. }
  91. ?>