Notice.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Notice as NoticeModel;
  4. use addons\qingdongams\model\NoticeRead;
  5. /**
  6. * @desc 操作文档:https://doc.fastadmin.net/qingdongams
  7. * @desc 软件介绍:https://www.fastadmin.net/store/qingdongams.html
  8. * @desc 售后微信:qingdong_crm
  9. */
  10. /**
  11. * 公告接口
  12. */
  13. class Notice extends StaffApi {
  14. protected $noNeedLogin = [];
  15. protected $noNeedRight = [];
  16. //公告列表
  17. public function getList() {
  18. $limit = input("limit/d", 10);
  19. $authid=$this->auth->id;
  20. $records = NoticeModel::where([
  21. ])->where(function ($query) use ($authid) {
  22. $query->where('owner_staff_ids', 'like', "%,$authid,%")->whereOr('owner_staff_ids', 'eq', "");
  23. })->order('id desc')->paginate($limit);
  24. $items = $records->items();
  25. foreach ($items as $k => $v) {
  26. $read_staff_ids = explode(',', $v['read_staff_ids']);
  27. $v['is_read'] = in_array($this->auth->id, $read_staff_ids)?0:1;
  28. $items[$k] = $v;
  29. }
  30. $this->success('请求成功', $records);
  31. }
  32. //获取公告详情
  33. public function getDetail() {
  34. $id = input('id', '', 'intval');
  35. $notice = NoticeModel::where(['id' => $id])->find();
  36. if (empty($notice)) {
  37. $this->error('公告不存在');
  38. }
  39. $read_staff_ids = explode(',', $notice['read_staff_ids']);
  40. if (!in_array($this->auth->id, $read_staff_ids)) {
  41. $read_staff_ids[] = $this->auth->id;
  42. NoticeModel::where(['id' => $notice['id']])->update(['read_staff_ids' => implode(',', $read_staff_ids).',']);
  43. }
  44. if(!NoticeRead::where(['notice_id'=>$id,'staff_id'=>$this->auth->id])->find()){
  45. //添加阅读记录
  46. NoticeRead::addRead($id, $this->auth->id);
  47. }
  48. preg_match_all('/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i',$notice['content'],$match);
  49. foreach ($match[1] as $v){
  50. $notice['content']=str_replace($v,cdnurl($v,true),$notice['content']);
  51. }
  52. $notice['is_edit'] = ($this->auth->id == $notice['create_staff_id']) ? 1 : 0;
  53. $this->success('请求成功', $notice);
  54. }
  55. //添加通告
  56. public function addNotice() {
  57. $params = $this->request->post();
  58. $params['owner_staff_ids'] = ',' . $params['owner_staff_ids'] . ',' . $this->auth->id . ',';
  59. $params['create_staff_id'] = $this->auth->id;
  60. $result = (new NoticeModel())->save($params);
  61. if (!$result) {
  62. $this->error('提交失败');
  63. }
  64. $this->success('提交成功');
  65. }
  66. //修改公告
  67. public function editNotice(){
  68. $params = $this->request->post();
  69. if(empty($params['id'])){
  70. $this->error('参数错误');
  71. }
  72. $result = (new NoticeModel())->save($params,['id'=>$params['id']]);
  73. if (!$result) {
  74. $this->error('提交失败');
  75. }
  76. $this->success('提交成功');
  77. }
  78. }