123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- namespace addons\notice\controller;
- use app\admin\library\Auth;
- use think\Cache;
- class Api extends \app\common\controller\Api
- {
- protected $noNeedRight = ['*'];
- protected $noNeedLogin = ['cache'];
- // 未读消息数量
- public function unread()
- {
- $user = $this->auth->getUser();
- $count = \app\admin\model\notice\Notice::where('to_id', $user['id'])
- ->where('platform', 'user')
- ->where('type','msg')
- ->order('id', 'desc')
- ->whereNull('readtime')
- ->count();
- $this->success('', $count);
- }
- // 我的站内消息
- public function index()
- {
- $user = $this->auth->getUser();
- $list = \app\admin\model\notice\Notice::where('to_id', $user['id'])
- ->where('platform', 'user')
- ->where('type','msg')
- ->order('id', 'desc')
- ->paginate();
- $is = true;
- if ($is) {
- \app\admin\model\notice\Notice::where('id', 'in',array_column($list->items(), 'id'))
- ->update(['readtime' => time()]);
- }
- $this->success('', $list);
- }
- // 标记为已读
- public function mark()
- {
- $user = $this->auth->getUser();
- $where = [];
- if (input('id')) {
- $where['id'] = input('id');
- }
- $count = \app\admin\model\notice\Notice::where('to_id', $user['id'])
- ->where($where)
- ->where('platform', 'user')
- ->where('type','msg')
- ->order('id', 'desc')
- ->whereNull('readtime')
- ->update(['readtime' => time()]);
- $this->success('', $count);
- }
- // 获取最新一条未读数据
- public function statistical()
- {
- $user = $this->auth->getUserInfo();
- $statisticalTime = Cache::get('notice_user_statistical_time_'.$user['id'], 0);
- $new = \app\admin\model\notice\Notice::where('to_id', $user['id'])
- ->where('platform', 'user')
- ->where('type','msg')
- ->order('id', 'desc')
- ->where('createtime','>', $statisticalTime)
- ->whereNull('readtime')
- ->find();
- if ($new) {
- Cache::set('notice_user_statistical_time_'.$user['id'], time());
- }
- $data = [
- 'num' => \app\admin\model\notice\Notice::where('to_id', $user['id'])
- ->where('platform', 'user')
- ->where('type','msg')
- ->order('id', 'desc')
- ->whereNull('readtime')
- ->count()
- ,
- 'new' => $new,
- ];
- $this->success('', $data );
- }
- // 缓存最后提示站内消息时间
- public function cache()
- {
- if (!$this->request->isPost()) {
- $this->error('请求方式错误');
- }
- $type = input('module', 'admin');
- $time = input('time');
- if ($type == 'admin') {
- $adminAuth = Auth::instance();
- if (!$adminAuth->isLogin()) {
- $this->error('未登录');
- }
- Cache::set('notice_admin_statistical_time_'.$adminAuth->id, $time);
- }
- if ($type == 'index') {
- $auth = $this->auth;
- if (!$auth->isLogin()) {
- $this->error('未登录');
- }
- Cache::set('notice_user_statistical_time_'.$auth->id, $time);
- }
- $this->success('ok');
- }
- }
|