UserNews.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace app\admin\model;
  3. use support\Model;
  4. class UserNews extends Model
  5. {
  6. const LOG_TYPE_NOTICE = 0;//消息
  7. const LOG_TYPE_REMIND = 1;//提醒
  8. const LOG_TYPE_SERVICE = 2;//服务
  9. const LOG_TYPE_ACTIVITY = 3;//活动
  10. const AUTH_TYPE = [
  11. self::LOG_TYPE_NOTICE => '消息',
  12. self::LOG_TYPE_REMIND => '提醒',
  13. self::LOG_TYPE_SERVICE => '服务',
  14. self::LOG_TYPE_ACTIVITY => '活动'
  15. ];
  16. /**
  17. * 会员消息表
  18. * The table associated with the model.
  19. *
  20. * @var string
  21. */
  22. protected $table = 'user_news';
  23. /**
  24. * The primary key associated with the table.
  25. *
  26. * @var string
  27. */
  28. protected $primaryKey = 'log_id';
  29. public $timestamps = false;
  30. /**
  31. * Indicates if the model should be timestamped.
  32. *
  33. * @var bool
  34. */
  35. public function user()
  36. {
  37. return $this->hasOne(User::class, 'user_id', 'uid');
  38. }
  39. /**
  40. * Notes:插入数据
  41. * @param array $data
  42. * @return int
  43. * User: yym
  44. * Date: 2022/12/28
  45. */
  46. public static function insertData(array $data)
  47. {
  48. return static::insertGetId($data);
  49. }
  50. /**
  51. * Notes:获取消息列表
  52. * @param int $uid
  53. * @param int $type
  54. * @param int $page
  55. * @param int $limit
  56. * @return array
  57. * User: yym
  58. * Date: 2022/12/28
  59. */
  60. public static function getUserNews(int $uid, int $type, int $page, int $limit)
  61. {
  62. return static::where(['log_type' => $type])
  63. ->when($uid > 0, function ($query) use ($uid){
  64. $query->where(function ($query2) use ($uid){
  65. $query2->where('log_uid', '=', $uid)
  66. ->orWhere('log_uid', '=', 0);
  67. });
  68. })
  69. ->forPage($page, $limit)
  70. ->orderBy('log_create_time', 'desc')
  71. ->get()
  72. ->toArray();
  73. }
  74. }