Kfnotice.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 = [$tbegin, $tend.' 23:59:59'];
  30. }
  31. if (strlen($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){ $query->where(['sendtime'=>['>=',$biaaodata['0']]])->where(['sendtime'=>['<=',$biaaodata['1']]]); })->limit($offset, $limit)->order('id', 'desc')->select();
  58. }
  59. foreach ($result as $key => $vo) {
  60. $vo->readtime = empty($vo->readtime) ? '' : date('Y-m-d H:i:s', $vo->readtime);
  61. $vo->uid = $vo->kfuser->user_name;
  62. unset($vo->kfuser);
  63. $vo->sendtype = $vo->sendtype == 1 ? '即时发送' : '预约发送';
  64. $result[$key] = array_merge($vo->toArray(), ['operate' => $this->makeBtn($vo->id)]);
  65. }
  66. if (!$bibao){
  67. $return['total'] = db('servicenotice')->where($where)->count(); //总数据
  68. }else{
  69. $return['total'] = $model->where($where)->where(function($query)use ($biaaodata){ $query->where(['sendtime'=>['>=',$biaaodata['0']]])->where(['sendtime'=>['<=',$biaaodata['1']]]); })->count();
  70. }
  71. $return['rows'] = $result;
  72. return json($return);
  73. }
  74. $goups = (new GroupsModel)->where(['status' => 1])->select();
  75. $this->assign('groups', $goups);
  76. return $this->fetch();
  77. }
  78. // 添加留言
  79. public function add()
  80. {
  81. $uid = intval(input('param.uid', 0));
  82. if (request()->isPost()) {
  83. $atext = input('post.atext');
  84. $title = input('post.title', 0);
  85. $group = intval(input('post.groupid', 0));
  86. if (strlen($atext) || strlen($title)) {
  87. return json(['code' => -1, 'data' => '', 'msg' => '标题和内容不能为空']);
  88. }
  89. $sendtime = input('post.sendtime');
  90. if (!strlen($sendtime)) {
  91. $sendtime = date("Y-m-d H:i:s", time() + 10);
  92. }
  93. $sendtype = (strtotime($sendtime) >= (time() + 60)) ? 2 : 1;
  94. if (strtotime($sendtime) < time()) {
  95. return json(['code' => -1, 'data' => '', 'msg' => '时间不能小于当前时间!']);
  96. }
  97. if ($uid) {
  98. $arr = [
  99. 'uid' => $uid,
  100. 'title' => $title,
  101. 'atext' => $atext,
  102. 'sendtime' => $sendtime,
  103. 'sendtype' => $sendtype,
  104. ];
  105. $datas[] = $arr;
  106. } else {
  107. $where = $group ? ['status' => 1, 'group_id' => $group] : ['status' => 1];
  108. $Users = (new Usersmodel())->where($where)->select();
  109. if (!$Users) {
  110. return json(['code' => -2, 'data' => '', 'msg' => '没有可用客服']);
  111. }
  112. $datas = [];
  113. foreach ($Users as $val) {
  114. $arr = [
  115. 'uid' => $val->id,
  116. 'title' => $title,
  117. 'atext' => $atext,
  118. 'sendtime' => $sendtime,
  119. 'sendtype' => $sendtype,
  120. ];
  121. $datas[] = $arr;
  122. }
  123. }
  124. try {
  125. db('servicenotice')->insertAll($datas);
  126. } catch (\Exception $e) {
  127. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  128. }
  129. return json(['code' => 1, 'data' => '', 'msg' => '添加公告成功']);
  130. }
  131. $goups = (new GroupsModel)->where(['status' => 1])->select();
  132. $this->assign('groups', $goups);
  133. $this->assign('userid', $uid);
  134. return $this->fetch();
  135. }
  136. // 删除留言
  137. public function del()
  138. {
  139. if (request()->isAjax()) {
  140. $id = input('param.id/d');
  141. //return $id;
  142. try {
  143. db('servicenotice')->where('id', $id)->delete();
  144. } catch (\Exception $e) {
  145. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  146. }
  147. return json(['code' => 1, 'data' => '', 'msg' => '删除成功']);
  148. }
  149. }
  150. // 生成按钮
  151. private function makeBtn($id)
  152. {
  153. $operate = '<a href="javascript:View(' . $id . ')"><button type="button" class="btn btn-info btn-sm">详细内容 </button></a>';
  154. $operate .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:Del(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
  155. $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';
  156. return $operate;
  157. }
  158. }