Notice.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace app\admin\controller\base\msg;
  3. /**
  4. * @title : 消息提醒记录
  5. * @desc : 系统消息提醒
  6. * @Author : Rock
  7. * @Date : 2021-05-24 10:38:34
  8. * @LastEditTime: 2024-08-30 10:55:39
  9. */
  10. use app\admin\controller\Base;
  11. use app\common\model\base\msg\Notice as NoticeModel;
  12. class Notice extends Base
  13. {
  14. protected $noNeedAuth = ['getList','getOptions','clearNoRead','MsgUnreadNum'];
  15. protected $noticeModel = null;
  16. protected $msgtypeModel = null;
  17. public function initialize()
  18. {
  19. parent::initialize();
  20. $this->noticeModel = new NoticeModel;
  21. $this->msgtypeModel = new \app\common\model\base\msg\Msgtype;
  22. }
  23. /**
  24. * @title: 消息提醒记录列表
  25. * @desc:
  26. * @param {int} {read_status} {} {是否已读}
  27. * @param {int} {channel} {} {发送渠道,1=站内信,2=短信,3=邮件}
  28. * @return {*}
  29. * @Author: Rock
  30. * @Date: 2021-05-24 10:53:28
  31. * @LastEditTime: Do not edit
  32. */
  33. public function getList($read_status=0,$channel=0,$pageNo=1,$pageSize=50)
  34. {
  35. $user_id = 0;
  36. if(empty($this->userinfo)){
  37. return res(2,"请登录");
  38. }else{
  39. $user_id = !empty($this->userinfo)?$this->userinfo['user_id']:0;
  40. }
  41. $where = [];
  42. $where[] = ['receive_uid','=',$user_id];
  43. if(!empty($read_status)){
  44. $where[] = ['read_status','=',$read_status];
  45. }
  46. if(!empty($channel)){
  47. $where[] = ['channel','=',$channel];
  48. }
  49. $list = $this->noticeModel->where($where)->with(['msgtype','receiver'])->order('send_at DESC')->paginate(['page'=>$pageNo,'list_rows'=>$pageSize]);
  50. $total = $list->total();
  51. $items = $list->items();
  52. return pageRes(1,"获取成功",$total,$items);
  53. }
  54. /**
  55. * @title: 删除消息提醒记录
  56. * @desc:
  57. * @param {mixed} {ids} {} {要删除的类型id}
  58. * @return {*}
  59. * @Author: Rock
  60. * @Date: 2021-05-24 10:53:28
  61. * @LastEditTime: Do not edit
  62. */
  63. public function deDelete($ids='')
  64. {
  65. $where = [];
  66. if(empty($ids)){
  67. return res(2,'参数错误');
  68. }elseif(strstr($ids,',')){
  69. $ids = explode(',',$ids);
  70. $where[] = ['notice_id','IN',$ids];
  71. }elseif(is_array($ids)){
  72. $where[] = ['notice_id','IN',$ids];
  73. }elseif($ids){
  74. $where[] = ['notice_id','=',$ids];
  75. }
  76. $this->noticeModel->where($where)->delete();
  77. slog(1,"删除了消息提醒");
  78. return res(1,"删除成功");
  79. }
  80. /**
  81. * @title: 消息提醒记录选项
  82. * @desc:
  83. * @param {*}
  84. * @return {*}
  85. * @Author: Rock
  86. * @Date: 2021-05-26 10:01:02
  87. * @LastEditTime: Do not edit
  88. */
  89. public function getOptions()
  90. {
  91. $data = [
  92. 'channelList' => $this->noticeModel->channelList(),
  93. 'sendStatusList'=> $this->noticeModel->sendStatusList(),
  94. 'readStatusList'=> $this->noticeModel->readStatusList(),
  95. ];
  96. return res(1,'获取成功',$data);
  97. }
  98. /**
  99. * @title: 测试发送消息通知
  100. * @desc:
  101. * @param {*}
  102. * @return {*}
  103. * @Author: Rock
  104. * @Date: 2021-05-26 18:49:48
  105. * @LastEditTime: Do not edit
  106. */
  107. public function testSend()
  108. {
  109. try{
  110. $year = date('Y');
  111. $month = date('m');
  112. $day = date('d');
  113. $h = date('H');
  114. $i = date('i');
  115. $notice = [
  116. 'uids' => 1,
  117. 'title' => '测试发送一个消息',
  118. 'type_code' => 'USECARNEWTASK',
  119. 'param1' => 1,
  120. 'param2' => 2,
  121. 'data' =>[$year,$month,$day,$h,$i]
  122. ];
  123. $res = NoticeModel::sendNotice($notice);
  124. return $res;
  125. }catch(\Exception $e){
  126. return res(2,$e->getFile().$e->getLine().$e->getMessage());
  127. }
  128. }
  129. /**
  130. * @title: 重新发送消息通知
  131. * @desc:
  132. * @param {Int} {notice_id} {} {消息ID}
  133. * @return {*}
  134. * @Author: Rock
  135. * @Date: 2021-05-24 16:43:30
  136. * @LastEditTime: Do not edit
  137. */
  138. public function reSend($notice_id=0)
  139. {
  140. $notice = $this->noticeModel->where('notice_id',$notice_id)->with(['msgtype','receiver'])->find();
  141. $pcHost = sysconfig('base.pc_host');
  142. switch($notice->channel){
  143. case 2://短信
  144. $res = NoticeModel::sendSms($notice->receive_uid,[$notice->title],771289);
  145. break;
  146. case 3://邮件
  147. $content = '<a href="'.$pcHost.$notice->url.'">'.$notice->title.'</a>';
  148. $res = NoticeModel::sendMail($notice->receive_uid,$notice->title,$content);
  149. break;
  150. case 1://站内信
  151. $notice->read_status = 1;
  152. $notice->save();
  153. $res = NoticeModel::sendWs($notice->receive_uid,$notice->toArray());
  154. return res(1,"发送成功");
  155. default:
  156. break;
  157. }
  158. if($res['code']==1){
  159. return res(1,'发送成功');
  160. }else{
  161. return res(2,$res['msg']);
  162. }
  163. }
  164. /**
  165. * @title: 清除未读消息
  166. * @desc:
  167. * @param {*} $notice_id
  168. * @return {*}
  169. * @Author: Rock
  170. * @Date: 2021-06-04 09:32:29
  171. * @LastEditTime: Do not edit
  172. */
  173. public function clearNoRead($notice_id=0)
  174. {
  175. $where = [];
  176. $where[] = ['read_status','=',1];
  177. $where[] = ['receive_uid','=',$this->userinfo['user_id']];
  178. if(!empty($notice_id)){
  179. $where[] = ['notice_id','=',$notice_id];
  180. }
  181. $this->noticeModel->where($where)->update(['read_status'=>2]);
  182. return res(1,"操作成功");
  183. }
  184. /**
  185. * @title: 获取消息未读数等
  186. * @Author: wangkewei
  187. * @return {*}
  188. * @Date: 2021/7/13 15:07
  189. */
  190. public function MsgUnreadNum()
  191. {
  192. $ids = Org::getChildrenIds($this->userinfo['org_id']);
  193. $user_id = $this->userinfo['user_id'];
  194. $role_code = $this->userinfo['role_code'];
  195. $res = $this->noticeModel->MsgUnreadNum($user_id,$role_code,$ids);
  196. return res(1,"操作成功",$res);
  197. }
  198. }