UserNotice.php 4.1 KB

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