|
|
@@ -10,48 +10,117 @@ 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;
|
|
|
/**
|
|
|
- * TODO 用户通知Model
|
|
|
* 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){
|
|
|
- $count_notice = self::whereIn('uid', [$uid, 0])->where("is_send",1)->count();
|
|
|
- $see_notice = UserNoticeSee::where("uid", $uid)->count();
|
|
|
- return $count_notice-$see_notice;
|
|
|
+ $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 = 8){
|
|
|
- $list = self::whereIn('uid', [$uid,0])
|
|
|
- ->where('is_send', 1)
|
|
|
- ->field('id,user,title,content,add_time')
|
|
|
- ->order("add_time desc")
|
|
|
- ->limit($page*$limit,$limit)->select()->toArray();
|
|
|
-
|
|
|
- foreach ($list as $key => $value) {
|
|
|
- $list[$key]["add_time"] = date("Y-m-d H:i:s",$value["add_time"]);
|
|
|
- $list[$key]["is_see"] = 0; //UserNoticeSee::where("uid",$uid)->where("nid",$value["id"])->count() > 0 ? 1 : 0;
|
|
|
- }
|
|
|
- $data["list"] = $list;
|
|
|
+ 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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @return array
|
|
|
+ * 用户删除消息, 软删除
|
|
|
+ * @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, $nid){
|
|
|
- if(UserNoticeSee::where("uid", $uid)->where("nid", $nid)->count() <= 0){
|
|
|
- $data["nid"] = $nid;
|
|
|
- $data["uid"] = $uid;
|
|
|
- $data["add_time"] = time();
|
|
|
- UserNoticeSee::create($data);
|
|
|
+ public static function seeNotice($uid, $nids) {
|
|
|
+ foreach ($nids as $nid) {
|
|
|
+ try {
|
|
|
+ $data["nid"] = $nid;
|
|
|
+ $data["uid"] = $uid;
|
|
|
+ $data["add_time"] = time();
|
|
|
+ UserNoticeSee::create($data);
|
|
|
+ } 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());
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|