Message.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace app\newhome\controller;
  3. use think\Controller;
  4. use app\user\model\Mail;
  5. use app\user\model\ReadMail;
  6. class Message extends Controller {
  7. private $readModel, $mailModel;
  8. public function _initialize(){
  9. parent::_initialize();
  10. $this->readModel = new ReadMail();
  11. $this->mailModel = new Mail();
  12. }
  13. public function read()
  14. {
  15. $uid = $this->request->post('u', '1907239071686061468');
  16. $mid = $this->request->post('i', 5);
  17. $res = $this->readModel->where(['uid'=>$uid, 'mid'=>$mid])->find();
  18. if (empty($res)) {
  19. $this->readModel->save(['uid'=>$uid, 'mid'=>$mid]);
  20. }
  21. return json(['error'=>0]);
  22. }
  23. public function mail(){
  24. $uid = $this->request->post('u', '1907239071686061468');
  25. // 找到用户的所有邮件
  26. $res = $this->mailModel
  27. ->where(['is_del'=>0])
  28. ->where(function ($query) {
  29. $uid = $this->request->post('u', '1907239071686061468');
  30. $query->where(['users'=>['like', "%{$uid}%"]])->whereOr(['users'=>'']);
  31. })
  32. ->where(function ($query) {
  33. $time = getCurrentTime();
  34. $query->where(['begin'=>['<', $time]])->whereOr('begin', null);
  35. })
  36. ->where(function ($query) {
  37. $time = getCurrentTime();
  38. $query->where(['end'=>['>', $time]])->whereOr('end', null);
  39. })
  40. ->order('create_time', 'desc')
  41. ->select();
  42. $read = $this->readModel->where(['uid'=>$uid])->column('mid');
  43. foreach ($res as $key=>$val) {
  44. if(in_array($val['id'], $read)) unset($res[$key]);
  45. unset($val['users']);
  46. unset($val['is_del']);
  47. }
  48. return json(array_values($res));
  49. }
  50. }