BaseModel.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\basic;
  8. use think\Model;
  9. use think\model\concern\SoftDelete;
  10. /**
  11. * 模型基类
  12. * @package plugin\saiadmin\basic
  13. */
  14. class BaseModel extends Model
  15. {
  16. use SoftDelete;
  17. // 删除时间
  18. protected $deleteTime = 'delete_time';
  19. // 添加时间
  20. protected $createTime = 'create_time';
  21. // 更新时间
  22. protected $updateTime = 'update_time';
  23. // 隐藏字段
  24. protected $hidden = ['delete_time'];
  25. // 只读字段
  26. protected $readonly = ['created_by', 'create_time'];
  27. /**
  28. * 时间范围搜索
  29. */
  30. public function searchCreateTimeAttr($query, $value)
  31. {
  32. $query->whereTime('create_time', 'between', $value);
  33. }
  34. /**
  35. * 新增前
  36. */
  37. public static function onBeforeInsert($model)
  38. {
  39. $info = getCurrentInfo();
  40. $info && $model->setAttr('created_by', $info['id']);
  41. }
  42. /**
  43. * 写入前
  44. */
  45. public static function onBeforeWrite($model)
  46. {
  47. $info = getCurrentInfo();
  48. $info && $model->setAttr('updated_by', $info['id']);
  49. }
  50. /**
  51. * 游戏ID搜索
  52. */
  53. public function searchGameIdAttr($query, $value)
  54. {
  55. if (is_array($value)) {
  56. $query->whereIn('game_id', $value);
  57. } else if($value!='') {
  58. $query->where('game_id', $value);
  59. }
  60. }
  61. /**
  62. * 负责人ID搜索
  63. */
  64. public function searchAuthIdAttr($query, $value)
  65. {
  66. if(is_array($value)){
  67. $query->whereIn('auth_id', $value);
  68. }else if($value!=''){
  69. $query->where('auth_id', $value);
  70. }
  71. }
  72. /**
  73. * ID搜索
  74. */
  75. public function searchIdAttr($query, $value)
  76. {
  77. if (is_array($value)) {
  78. $query->whereIn('id', $value);
  79. } else if($value!=''){
  80. $query->where('id', $value);
  81. }
  82. }
  83. }