| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- /**
- *
- * @author: xaboy<365615158@qq.com>
- * @day: 2017/11/11
- */
- namespace app\models\user;
- use app\admin\model\user\UserNoticeSee;
- use crmeb\traits\ModelTrait;
- use crmeb\basic\BaseModel;
- use think\facade\Db;
- use think\facade\Log;
- use think\facade\Config;
- /**
- * Class UserNotice
- * @package app\models\user
- */
- class UserNotice extends BaseModel
- {
- use ModelTrait;
- /**
- * 发送系统消息, 一般由管理员后台发出
- * @param $title
- * @param $content
- * @param null $sender
- * @param string $icon
- */
- public static function publishSystemNotice($title, $content, $sender=null, $icon='') {
- if (!$sender) {
- $sender = Config::get('app.notice_sender', '运营中心');
- }
- self::sendNotice($title, $content, 0, 0, $sender, $icon);
- }
- /**
- * 发通知给指定用户, 一般由系统自动发出
- * @param $uid
- * @param $title
- * @param $content
- * @param string $icon
- */
- public static function sendNoticeTo($uid, $title, $content, $icon='') {
- $sender = Config::get('app.notice_sender', '运营中心');
- self::sendNotice($title, $content, $uid, 2, $sender, $icon);
- }
- protected static function sendNotice($title, $content, $uid, $type, $sender, $icon='') {
- $data['uid'] = $uid;
- $data['type'] = $type;
- $data['user'] = $sender;
- $data['title'] = $title;
- $data['content'] = $content;
- $data['add_time'] = time();
- $data['icon'] = $icon;
- self::create($data);
- }
- // 获取未读数
- public static function getNotice($uid){
- $row = Db::query("
- select count(*) as unread from tw_user_notice where id not in (
- select nid from tw_user_notice_see where uid=?
- ) and uid in (?, 0) and del_time=0
- ", [$uid, $uid]
- );
- return intval($row[0]['unread']);
- }
- /**
- * 获取消息列表
- * @return array
- */
- public static function getNoticeList($uid, $page, $limit = 20){
- $data = Db::query("
- 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`
- from tw_user_notice n right join tw_user_notice_see s on n.uid = s.uid
- where n.uid in (?, 0) and n.del_time=0
- group by s.id
- order by n.add_time desc limit ?,?
- ", [$uid, ($page-1)*$limit, $limit]);
- return $data;
- }
- /**
- * 用户删除消息, 软删除
- * @param $uid
- * @param $ids
- */
- public static function delNotice($uid, $ids) {
- $model = new self;
- $model->where('uid', $uid)->whereIn('id', $ids)->update(['del_time'=>time()]);
- }
- /**
- * 设为已读
- * @param $uid
- * @param $nids
- */
- public static function seeNotice($uid, $nids) {
- $rows = [];
- foreach ($nids as $nid) {
- $rows[] = [
- 'nid' => $nid,
- 'uid' => $uid,
- 'add_time' => time(),
- ];
- }
- try {
- UserNoticeSee::setAll($rows);
- } catch(\Exception $e) {
- Log::warning("seeNotice exception.sql=".UserNoticeSee::getLastSql());
- }
- }
- public static function unseeNotice($uid, $nids) {
- foreach ($nids as $nid) {
- try{
- UserNoticeSee::where('uid', $uid)->where('nid', $nid)->delete();
- }catch (\Exception $e){
- Log::warning("unseeNotice exception.sql=". UserNoticeSee::getLastSql());
- }
- }
- }
- }
|