UserNotice.php 3.9 KB

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