Star.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace app\api\model;
  3. use think\Model;
  4. class Star extends Model
  5. {
  6. // 表名
  7. protected $name = 'star';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'create_time';
  12. protected $updateTime = false;
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [];
  16. /**
  17. * 点击点赞按钮,点赞或取消点赞
  18. * @param int $user_id 用户ID
  19. * @param int $box_id 盲盒ID
  20. * @return int
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. * @throws \think\exception\DbException
  24. * @author fuyelk <fuyelk@fuyelk.com>
  25. */
  26. public static function click($user_id, $box_id)
  27. {
  28. $param = [
  29. 'user_id' => $user_id,
  30. 'box_id' => $box_id
  31. ];
  32. $row = self::field('id')->where($param)->find();
  33. if ($row) {
  34. $row->delete();
  35. return 0;
  36. }
  37. self::create($param);
  38. return 1;
  39. }
  40. /**
  41. * 检查是否已点赞
  42. * @param int $user_id 用户ID
  43. * @param int $box_id 盲盒ID
  44. * @return int
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. * @throws \think\exception\DbException
  48. * @author fuyelk <fuyelk@fuyelk.com>
  49. */
  50. public static function check($user_id, $box_id)
  51. {
  52. $param = [
  53. 'user_id' => $user_id,
  54. 'box_id' => $box_id
  55. ];
  56. $row = self::field('id')->where($param)->find();
  57. return $row ? 1 : 0;
  58. }
  59. }