Messages.php 14 KB

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