| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?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;
- use think\facade\Env;
- /**
- * 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){
- $prefix = Env::get('database.prefix', 'eb_');
- $tnotice = $prefix . 'user_notice';
- $tnotice_see = $prefix . 'user_notice_see';
- $sql = sprintf("
- select count(*) as unread from %s where id not in (
- select nid from %s where uid=%d
- ) and uid in (%d, 0) and del_time=0
- ", $tnotice, $tnotice_see, $uid, $uid);
- $row = Db::query($sql);
- return intval($row[0]['unread']);
- }
- /**
- * 获取消息列表
- * @return array
- */
- public static function getNoticeList($uid, $page, $limit = 20){
- $prefix = Env::get('database.prefix', 'eb_');
- $tnotice = $prefix . 'user_notice';
- $tnotice_see = $prefix . 'user_notice_see';
- $sql = sprintf("
- 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`
- from %s n left join %s s on n.id = s.nid
- where n.uid in (%d, 0) and n.del_time=0
- order by n.add_time desc limit %d,%d
- ", $tnotice, $tnotice_see, $uid, ($page-1)*$limit, $limit);
- $data = Db::query($sql);
- 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());
- }
- }
- }
- }
|