UserNotice.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /**
  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. $row = Db::query("
  58. select count(*) as unread from tw_user_notice where id not in (
  59. select nid from tw_user_notice_see where uid=?
  60. ) and uid in (?, 0) and del_time=0
  61. ", [$uid, $uid]
  62. );
  63. return intval($row[0]['unread']);
  64. }
  65. /**
  66. * 获取消息列表
  67. * @return array
  68. */
  69. public static function getNoticeList($uid, $page, $limit = 20){
  70. $data = Db::query("
  71. select n.id, n.icon, n.`user` as `from`, n.title as subject, n.content as `body`, n.add_time as ts, n.`type`, count(s.id) as `read`
  72. from tw_user_notice n right join tw_user_notice_see s on n.uid = s.uid
  73. where n.uid in (?, 0) and n.del_time=0
  74. group by s.id
  75. order by n.add_time desc limit ?,?
  76. ", [$uid, ($page-1)*$limit, $limit]);
  77. return $data;
  78. }
  79. /**
  80. * 用户删除消息, 软删除
  81. * @param $uid
  82. * @param $ids
  83. */
  84. public static function delNotice($uid, $ids) {
  85. $model = new self;
  86. $model->where('uid', $uid)->whereIn('id', $ids)->update(['del_time'=>time()]);
  87. }
  88. /**
  89. * 设为已读
  90. * @param $uid
  91. * @param $nids
  92. */
  93. public static function seeNotice($uid, $nids) {
  94. $rows = [];
  95. foreach ($nids as $nid) {
  96. $rows[] = [
  97. 'nid' => $nid,
  98. 'uid' => $uid,
  99. 'add_time' => time(),
  100. ];
  101. }
  102. try {
  103. UserNoticeSee::setAll($rows);
  104. } catch(\Exception $e) {
  105. Log::warning("seeNotice exception.sql=".UserNoticeSee::getLastSql());
  106. }
  107. }
  108. public static function unseeNotice($uid, $nids) {
  109. foreach ($nids as $nid) {
  110. try{
  111. UserNoticeSee::where('uid', $uid)->where('nid', $nid)->delete();
  112. }catch (\Exception $e){
  113. Log::warning("unseeNotice exception.sql=". UserNoticeSee::getLastSql());
  114. }
  115. }
  116. }
  117. }