UserNotice.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\models\user;
  3. use app\admin\model\user\UserNoticeSee;
  4. use crmeb\traits\ModelTrait;
  5. use crmeb\basic\BaseModel;
  6. use think\facade\Db;
  7. use think\facade\Config;
  8. use think\facade\Env;
  9. /**
  10. * Class UserNotice
  11. * @package app\models\user
  12. */
  13. class UserNotice extends BaseModel
  14. {
  15. use ModelTrait;
  16. /**
  17. * 发送系统消息, 一般由管理员后台发出
  18. * @param $title
  19. * @param $content
  20. * @param null $sender
  21. * @param string $icon
  22. */
  23. public static function publishSystemNotice($title, $content, $sender = null, $icon = '')
  24. {
  25. if (!$sender) {
  26. $sender = Config::get('app.notice_sender', '运营中心');
  27. }
  28. self::sendNotice($title, $content, 0, 0, $sender, $icon);
  29. }
  30. /**
  31. * 发通知给指定用户, 一般由系统自动发出
  32. * @param $uid
  33. * @param $title
  34. * @param $content
  35. * @param string $icon
  36. */
  37. public static function sendNoticeTo($uid, $title, $content, $icon = '')
  38. {
  39. $sender = Config::get('app.notice_sender', '运营中心');
  40. self::sendNotice($title, $content, $uid, 2, $sender, $icon);
  41. }
  42. protected static function sendNotice($title, $content, $uid, $type, $sender, $icon = '')
  43. {
  44. $data['uid'] = $uid;
  45. $data['type'] = $type;
  46. $data['user'] = $sender;
  47. $data['title'] = $title;
  48. $data['content'] = $content;
  49. $data['add_time'] = time();
  50. $data['icon'] = $icon;
  51. self::create($data);
  52. }
  53. // 获取未读数
  54. public static function getNotice($uid)
  55. {
  56. $prefix = Env::get('database.prefix', 'eb_');
  57. $tnotice = $prefix . 'user_notice';
  58. $tnotice_see = $prefix . 'user_notice_see';
  59. $sql = sprintf("
  60. select count(*) as unread from %s where id not in (
  61. select nid from %s where uid=%d
  62. ) and uid in (%d, 0) and del_time=0
  63. ", $tnotice, $tnotice_see, $uid, $uid);
  64. $row = Db::query($sql);
  65. $num = intval($row[0]['unread']);
  66. $max = Config::get('app.notice_max', 30);
  67. return $num > $max ? $max : $num;
  68. }
  69. /**
  70. * 获取消息列表
  71. * @return array
  72. */
  73. public static function getNoticeList($uid, $page, $limit = 20)
  74. {
  75. $max = Config::get('app.notice_max', 30);
  76. $limit = $max;
  77. $prefix = Env::get('database.prefix', 'eb_');
  78. $tnotice = $prefix . 'user_notice';
  79. $tnotice_see = $prefix . 'user_notice_see';
  80. $sql = sprintf("
  81. select n.id, n.icon, n.`user` as `from`, n.title as subject, n.content as `body`, n.add_time as ts, n.`type`, s.id as `read`
  82. from %s n left join (select id,nid from %s where uid=%d) s on n.id = s.nid
  83. where n.uid in (%d, 0) and n.del_time=0
  84. order by n.add_time desc limit %d,%d
  85. ", $tnotice, $tnotice_see, $uid, $uid, ($page - 1) * $limit, $limit);
  86. // echo $sql;
  87. $data = Db::query($sql);
  88. return $data;
  89. }
  90. /**
  91. * 用户删除消息, 软删除
  92. * @param $uid
  93. * @param $ids
  94. */
  95. public static function delNotice($uid, $ids)
  96. {
  97. $model = new self;
  98. $model->where('uid', $uid)->whereIn('id', $ids)->update(['del_time' => time()]);
  99. }
  100. /**
  101. * 设为已读
  102. * @param $uid
  103. * @param $nids
  104. */
  105. public static function seeNotice($uid, $nids)
  106. {
  107. $rows = [];
  108. foreach ($nids as $nid) {
  109. $rows[] = [
  110. 'nid' => $nid,
  111. 'uid' => $uid,
  112. 'add_time' => time(),
  113. ];
  114. }
  115. try {
  116. UserNoticeSee::setAll($rows);
  117. } catch (\Exception $e) {
  118. warnlog("seeNotice exception.sql=" . UserNoticeSee::getLastSql());
  119. }
  120. }
  121. public static function unseeNotice($uid, $nids)
  122. {
  123. foreach ($nids as $nid) {
  124. try {
  125. UserNoticeSee::where('uid', $uid)->where('nid', $nid)->delete();
  126. } catch (\Exception $e) {
  127. warnlog("unseeNotice exception.sql=" . UserNoticeSee::getLastSql());
  128. }
  129. }
  130. }
  131. }