Messages.php 13 KB

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