Kfnotice.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * User: nickbai
  4. * Date: 2017/10/23 13:33
  5. * Email: 1902822973@qq.com
  6. */
  7. namespace app\admin\controller;
  8. use app\admin\model\Servicenotice as ServicenoticeModel;
  9. use app\admin\model\Users as Usersmodel;
  10. use app\admin\model\Groups as GroupsModel;
  11. class Kfnotice extends Base
  12. {
  13. // 管理员列表
  14. public function index()
  15. {
  16. if (request()->isAjax()) {
  17. $param = input('param.');
  18. $limit = $param['pageSize'];
  19. $offset = ($param['pageNumber'] - 1) * $limit;
  20. $where = [];
  21. $bibao = false;
  22. if (strlen($param['searchText'])) {
  23. $arr = explode("|", $param['searchText']);
  24. if (count($arr) == 5) {
  25. list($timerang, $sendtype, $groupid, $txttype, $txttext) = $arr;
  26. if (strlen($timerang)) {
  27. list($tbegin, $tend) = explode(",", $timerang);
  28. $bibao = true;
  29. $biaaodata = [trim($tbegin . '00:00:00'), trim($tend . ' 23:59:59')];
  30. }
  31. if (intval($sendtype)) {
  32. $where['sendtype'] = intval($sendtype);
  33. }
  34. if ($groupid) {
  35. $gm = (new GroupsModel())->with('users')->where(['id' => $groupid])->find();
  36. $uids = [0];
  37. if ($gm && count($gm->users)) {
  38. foreach ($gm->users as $u) {
  39. $uids[] = $u->id;
  40. }
  41. }
  42. $where['uid'] = ['IN', $uids];
  43. }
  44. if (strlen($txttext)) {
  45. if ($txttype == 1) {
  46. $where['title'] = ['like', '%' . $txttext . '%'];
  47. } else {
  48. $where['atext'] = ['like', '%' . $txttext . '%'];
  49. }
  50. }
  51. }
  52. }
  53. $model = new ServicenoticeModel();
  54. if (!$bibao) {
  55. $result = $model->where($where)->limit($offset, $limit)->order('id', 'desc')->select();
  56. } else {
  57. $result = $model->where($where)->where(function ($query) use ($biaaodata) {
  58. $query->where(['sendtime' => ['>=', $biaaodata['0']]])->where(['sendtime' => ['<=', $biaaodata['1']]]);
  59. })->limit($offset, $limit)->order('id', 'desc')->select();
  60. }
  61. foreach ($result as $key => $vo) {
  62. $vo->readtime = empty($vo->readtime) ? '' : date('Y-m-d H:i:s', $vo->readtime);
  63. $vo->uid = $vo->kfuser->user_name;
  64. unset($vo->kfuser);
  65. $vo->sendtype = $vo->sendtype == 1 ? '即时发送' : '预约发送';
  66. $result[$key] = array_merge($vo->toArray(), ['operate' => $this->makeBtn($vo->id)]);
  67. }
  68. if (!$bibao) {
  69. $return['total'] = db('servicenotice')->where($where)->count(); //总数据
  70. } else {
  71. $return['total'] = $model->where($where)->where(function ($query) use ($biaaodata) {
  72. $query->where(['sendtime' => ['>=', $biaaodata['0']]])->where(['sendtime' => ['<=', $biaaodata['1']]]);
  73. })->count();
  74. }
  75. $return['rows'] = $result;
  76. return json($return);
  77. }
  78. $goups = (new GroupsModel)->where(['status' => 1])->select();
  79. $this->assign('groups', $goups);
  80. return $this->fetch();
  81. }
  82. // 添加留言
  83. public function add()
  84. {
  85. $uid = intval(input('param.uid', 0));
  86. if (request()->isPost()) {
  87. $atext = input('post.atext');
  88. $title = input('post.title', 0);
  89. $group = intval(input('post.groupid', 0));
  90. if (!strlen($atext) || !strlen($title)) {
  91. return json(['code' => -1, 'data' => '', 'msg' => '标题和内容不能为空']);
  92. }
  93. $sendtime = input('post.sendtime');
  94. if (!strlen($sendtime)) {
  95. $sendtime = date("Y-m-d H:i:s", time() + 10);
  96. }
  97. $sendtype = (strtotime($sendtime) >= (time() + 60)) ? 2 : 1;
  98. if (strtotime($sendtime) < time()) {
  99. return json(['code' => -1, 'data' => '', 'msg' => '时间不能小于当前时间!']);
  100. }
  101. if ($uid) {
  102. $arr = [
  103. 'uid' => $uid,
  104. 'title' => $title,
  105. 'atext' => $atext,
  106. 'sendtime' => $sendtime,
  107. 'sendtype' => $sendtype,
  108. ];
  109. $datas[] = $arr;
  110. } else {
  111. $where = $group ? ['status' => 1, 'group_id' => $group] : ['status' => 1];
  112. $Users = (new Usersmodel())->where($where)->select();
  113. if (!$Users) {
  114. return json(['code' => -2, 'data' => '', 'msg' => '没有可用客服']);
  115. }
  116. $datas = [];
  117. foreach ($Users as $val) {
  118. $arr = [
  119. 'uid' => $val->id,
  120. 'title' => $title,
  121. 'atext' => $atext,
  122. 'sendtime' => $sendtime,
  123. 'sendtype' => $sendtype,
  124. ];
  125. $datas[] = $arr;
  126. }
  127. }
  128. try {
  129. db('servicenotice')->insertAll($datas);
  130. } catch (\Exception $e) {
  131. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  132. }
  133. return json(['code' => 1, 'data' => '', 'msg' => '添加公告成功']);
  134. }
  135. $goups = (new GroupsModel)->where(['status' => 1])->select();
  136. $this->assign('groups', $goups);
  137. $this->assign('userid', $uid);
  138. return $this->fetch();
  139. }
  140. // 删除留言
  141. public function del()
  142. {
  143. if (request()->isAjax()) {
  144. $id = input('param.id/d');
  145. //return $id;
  146. try {
  147. db('servicenotice')->where('id', $id)->delete();
  148. } catch (\Exception $e) {
  149. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  150. }
  151. return json(['code' => 1, 'data' => '', 'msg' => '删除成功']);
  152. }
  153. }
  154. // 生成按钮
  155. private function makeBtn($id)
  156. {
  157. $operate = '<a href="javascript:View(' . $id . ')"><button type="button" class="btn btn-info btn-sm">详细内容 </button></a>';
  158. $operate .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:Del(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
  159. $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';
  160. return $operate;
  161. }
  162. }