Messages.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Jonlin
  5. * Date: 2019/6/27
  6. * Time: 10:15
  7. */
  8. namespace app\admin\controller;
  9. class Messages extends Base
  10. {
  11. // 留言列表
  12. public function index()
  13. {
  14. if(request()->isAjax()){
  15. $param = input('param.');
  16. $limit = $param['pageSize'];
  17. $offset = ($param['pageNumber'] - 1) * $limit;
  18. $where = [];
  19. $orwhere = [];
  20. //用户名称
  21. if (strlen($param['searchText'])) {
  22. $where['a.name'] = array('like','%'.$param['searchText'].'%');
  23. $orwhere['name'] = array('like','%'.$param['searchText'].'%');
  24. }
  25. //处理客服id
  26. if (empty($param['user_id']) === false) {
  27. $where['a.user_id'] = $param['user_id'];
  28. $orwhere['user_id'] = $param['user_id'];
  29. }
  30. //留言状态
  31. if ($param['message_status'] != '' && $param['message_status'] != -1) {
  32. $where['a.message_status'] = $param['message_status'];
  33. $orwhere['message_status'] = $param['message_status'];
  34. }
  35. //公开状态
  36. if ($param['if_public'] != '' && $param['if_public'] != -1) {
  37. $where['a.if_public'] = $param['if_public'];
  38. $orwhere['if_public'] = $param['if_public'];
  39. }
  40. //留言类型id
  41. if (empty($param['type_id']) === false) {
  42. $where['a.type_id'] = $param['type_id'];
  43. $orwhere['type_id'] = $param['type_id'];
  44. }
  45. $return['total'] = db('Accountsmessage')->where($orwhere)->count(); //总数据
  46. if (strlen($param['start']) && strlen($param['end']) && $param['start'] <= $param['end']) {
  47. $start = strtotime($param['start']);
  48. $end = strtotime($param['end'] . ' 23:59:59');
  49. $return['total'] = db('Accountsmessage')->whereBetween('add_time', [$start, $end])->where($orwhere)->count(); //总数据
  50. }
  51. $join = [
  52. 'messagetype b' => 'a.type_id = b.id',
  53. ];
  54. $leftjoin = [
  55. 'users c' => 'a.user_id = c.id',
  56. ];
  57. $result = model('Accountsmessage')->selectJoin($join,$leftjoin,$where, $start,$end,$offset, $limit);
  58. foreach($result as $key=>$vo){
  59. // 优化显示附件
  60. $images = $result[$key]['image'];
  61. //转换成数组格式
  62. $images = explode(",", $images);
  63. if(!empty($images)){
  64. $img = '';
  65. for($i=0;$i<count($images);$i++){
  66. $img = $img.'<img src="'.$images[$i].'" width="40px" height="40px" style="margin-left:5px;margin-top:5px;">';
  67. }
  68. $result[$key]['image'] = $img;
  69. }
  70. // 优化时间
  71. if(null != $vo['add_time']){
  72. $result[$key]['add_time'] = date('Y-m-d H:m:s',$result[$key]['add_time']);
  73. }
  74. if(null != $vo['dealWith_time']){
  75. $result[$key]['dealWith_time'] = date('Y-m-d H:m:s',$result[$key]['dealWith_time']);
  76. }
  77. // 优化显示状态
  78. if(1 == $vo['message_status']){
  79. $result[$key]['status'] = '<span class="label label-primary">已处理</span>';
  80. }else{
  81. $result[$key]['status'] = '<span class="label label-danger">未处理</span>';
  82. }
  83. // 是否公开
  84. if(1 == $vo['if_public']){
  85. $result[$key]['if_public'] = '公开';
  86. }else{
  87. $result[$key]['if_public'] = '不公开';
  88. }
  89. // 生成操作按钮
  90. $result[$key]['operate'] = $this->makeBtn($vo['message_id']);
  91. }
  92. //$return['total'] = db('Accountsmessage')->where($orwhere)->count(); //总数据
  93. $return['rows'] = $result;
  94. return json($return);
  95. }
  96. //所有客服
  97. $users = db('users')->select();
  98. $useroption = '';
  99. if(!empty($users)){
  100. $option = '<option value="0">处理人</option>';
  101. for($i=0;$i<count($users);$i++){
  102. $option = $option.'<option value="'.$users[$i]['id'].'">'.$users[$i]['user_name'].'</option>';
  103. }
  104. $useroption = '<select lay-verify="required" lay-filter="user_id">'.$option.'</select>';
  105. }
  106. //留言类型
  107. $type = db('messagetype')->where('status',1)->select();
  108. $optiontype = '';
  109. if(!empty($users)){
  110. $option = '<option value="0">留言类型</option>';
  111. for($i=0;$i<count($type);$i++){
  112. $option = $option.'<option value="'.$type[$i]['id'].'">'.$type[$i]['name'].'</option>';
  113. }
  114. $optiontype = '<select lay-verify="required" lay-filter="type_id">'.$option.'</select>';
  115. }
  116. $this->assign([
  117. 'useroption' => $useroption,
  118. 'optiontype' => $optiontype
  119. ]);
  120. return $this->fetch();
  121. }
  122. // 留言详情
  123. public function detail()
  124. {
  125. if(request()->isAjax()) {
  126. //设置留言公开/不公开
  127. $param = input('post.');
  128. $id = $param['id'];
  129. $if_public = $param['if_public'];
  130. $type_id = $param['type_id'];
  131. $message = db('accountsmessage')->where('message_id', $id)->find();
  132. if(!empty($message)){
  133. if($message['message_status'] == 0){
  134. return json(['code' => -1, 'data' => '', 'msg' => '留言未处理']);
  135. }
  136. }
  137. if($type_id == -1){
  138. return json(['code' => -2, 'data' => '', 'msg' => '请选择留言类别']);
  139. }
  140. try{
  141. db('accountsmessage')->where('message_id', $id)->update(['if_public' => $if_public,'type_id' => $type_id]);
  142. }catch(\Exception $e){
  143. return json(['code' => -3, 'data' => '', 'msg' => $e->getMessage()]);
  144. }
  145. return json(['code' => 1, 'data' => url('messages/index'), 'msg' => '设置成功']);
  146. }
  147. $id = input('param.id/d');
  148. $info = db('Accountsmessage')->where('message_id', $id)->find();
  149. //$account = db('Accounts')->where('id', $info['account_id'])->find();
  150. $user = db('Users')->where('id', $info['user_id'])->find();
  151. //$info['account_name'] = $account['account_name'];
  152. //$info['account_email'] = $account['account_email'];
  153. $info['user_name'] = $user['user_name'];
  154. $messagetype = db('messagetype')->select();
  155. // 优化显示附件
  156. $images = $info['image'];
  157. //转换成数组格式
  158. $images = explode(",", $images);
  159. if(!empty($images)){
  160. $img = '';
  161. for($i=0;$i<count($images);$i++){
  162. $img = $img.'<img src="'.$images[$i].'" width="240px" style="margin-top:20px;">';
  163. }
  164. $info['image'] = $img;
  165. }
  166. // 优化时间
  167. if (null != $info['add_time']) {
  168. $info['add_time'] = date('Y-m-d H:m:s', $info['add_time']);
  169. }
  170. if (null != $info['dealWith_time']) {
  171. $info['dealWith_time'] = date('Y-m-d H:m:s', $info['dealWith_time']);
  172. }
  173. // 优化显示状态
  174. if (1 == $info['message_status']) {
  175. $info['message_status'] = '已处理';
  176. } else {
  177. $info['message_status'] = '未处理';
  178. }
  179. $this->assign([
  180. 'info' => $info,
  181. 'messagetype' => $messagetype,
  182. 'status' => config('kf_status'),
  183. // 'groups' => db('groups')->select()
  184. ]);
  185. return $this->fetch();
  186. }
  187. //删除留言
  188. public function delMessage(){
  189. if (request()->isAjax()) {
  190. //留言类型id
  191. $id = input('param.id/d');
  192. try {
  193. db('accountsmessage')->where('message_id', $id)->delete();
  194. } catch (\Exception $e) {
  195. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  196. }
  197. return json(['code' => 1, 'data' => '', 'msg' => '删除留言成功']);
  198. }
  199. }
  200. // 上传客服头像
  201. public function upAvatar()
  202. {
  203. if(request()->isAjax()) {
  204. $file = request()->file('file');
  205. if (!empty($file)) {
  206. // 移动到框架应用根目录/public/uploads/ 目录下
  207. $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
  208. if ($info) {
  209. $src = '/uploads' . '/' . date('Ymd') . '/' . $info->getFilename();
  210. return json(['code' => 0, 'data' => ['src' => $src], 'msg' => 'ok']);
  211. } else {
  212. // 上传失败获取错误信息
  213. return json(['code' => -1, 'data' => '', 'msg' => $file->getError()]);
  214. }
  215. }
  216. }
  217. }
  218. // 生成留言操作按钮
  219. private function makeBtn($id)
  220. {
  221. $operate = '<a href="' . url('Messages/detail', ['id' => $id]) . '">';
  222. $operate .= '<button type="button" class="btn btn-info btn-sm"><i class="fa fa-institution"></i> 详情</button></a>';
  223. $operate .= '<a href="javascript:messageDel(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
  224. $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';
  225. return $operate;
  226. }
  227. // 留言类型列表
  228. public function type()
  229. {
  230. if(request()->isAjax()){
  231. $param = input('param.');
  232. $limit = $param['pageSize'];
  233. $offset = ($param['pageNumber'] - 1) * $limit;
  234. // $where = [];
  235. // if (strlen($param['searchText'])) {
  236. // $where['account_name'] = $param['searchText'];
  237. // }
  238. $result = db('messagetype')->limit($offset, $limit)->select();
  239. foreach($result as $key=>$vo){
  240. // 优化时间
  241. if(null != $vo['addtime']){
  242. $result[$key]['addtime'] = date('Y-m-d H:m:s',$result[$key]['addtime']);
  243. }
  244. // 优化显示状态
  245. if(1 == $vo['status']){
  246. $result[$key]['status'] = '<span class="label label-primary">启用</span>';
  247. }else{
  248. $result[$key]['status'] = '<span class="label label-danger">停用</span>';
  249. }
  250. // 生成操作按钮
  251. $result[$key]['operate'] = $this->makeBtnType($vo['id']);
  252. }
  253. $return['total'] = db('messagetype')->count(); //总数据
  254. $return['rows'] = $result;
  255. return json($return);
  256. }
  257. return $this->fetch();
  258. }
  259. // 添加留言类型
  260. public function add()
  261. {
  262. if (request()->isAjax()) {
  263. $param = input('post.');
  264. $has = db('messagetype')->where('name',$param['name'])->find();
  265. if(!empty($has)){
  266. return json(['code' => -1, 'data' => '', 'msg' => '此留言类型已存在']);
  267. }
  268. $info = array();
  269. $info['name'] = $param['name'];
  270. $info['addtime'] = time();
  271. $info['status'] = $param['status'];
  272. try {
  273. db('messagetype')->insert($info);
  274. } catch (\Exception $e) {
  275. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  276. }
  277. return json(['code' => 1, 'data' => url('messages/type'), 'msg' => '添加留言类型成功']);
  278. }
  279. $this->assign([
  280. 'status' => config('kf_status')
  281. ]);
  282. return $this->fetch();
  283. }
  284. // 编辑留言类型
  285. public function edit()
  286. {
  287. // 菜单id
  288. $id = input('param.id/d');
  289. if (request()->isAjax()) {
  290. $param = input('post.');
  291. $has = db('messagetype')->where('name',$param['name'])->where('id','<>',$id)->find();
  292. if(!empty($has)){
  293. return json(['code' => -1, 'data' => '', 'msg' => '此留言类型已存在']);
  294. }
  295. $info = array();
  296. $info['name'] = $param['name'];
  297. $info['status'] = $param['status'];
  298. try {
  299. db('messagetype')->where('id', $param['id'])->update($info);
  300. } catch (\Exception $e) {
  301. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  302. }
  303. return json(['code' => 1, 'data' => url('messages/type'), 'msg' => '编辑留言类型成功']);
  304. }
  305. $type = db('messagetype')->where('id',$id)->find();
  306. $this->assign([
  307. 'type' => $type,
  308. 'id' => $id,
  309. 'status' => config('kf_status')
  310. ]);
  311. return $this->fetch();
  312. }
  313. // 删除留言类型
  314. public function delType()
  315. {
  316. if (request()->isAjax()) {
  317. //留言类型id
  318. $id = input('param.id/d');
  319. try {
  320. db('messagetype')->where('id', $id)->delete();
  321. } catch (\Exception $e) {
  322. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  323. }
  324. return json(['code' => 1, 'data' => '', 'msg' => '删除留言类型成功']);
  325. }
  326. }
  327. // 生成按钮
  328. private function makeBtnType($id)
  329. {
  330. $operate = '<a href="' . url('Messages/edit', ['id' => $id]) . '">';
  331. $operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 编辑</button></a> ';
  332. $operate .= '<a href="javascript:typeDel(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
  333. $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';
  334. return $operate;
  335. }
  336. }