Messages.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. $message = db('accountsmessage')->where('message_id', $id)->find();
  131. if(!empty($message)){
  132. if($message['message_status'] == 0){
  133. return json(['code' => -1, 'data' => '', 'msg' => '留言未处理']);
  134. }
  135. }
  136. if($type_id == -1){
  137. return json(['code' => -2, 'data' => '', 'msg' => '请选择留言类别']);
  138. }
  139. try{
  140. db('accountsmessage')->where('message_id', $id)->update(['if_public' => $if_public,'type_id' => $type_id]);
  141. }catch(\Exception $e){
  142. return json(['code' => -3, 'data' => '', 'msg' => $e->getMessage()]);
  143. }
  144. return json(['code' => 1, 'data' => url('messages/index'), 'msg' => '设置成功']);
  145. }
  146. $id = input('param.id/d');
  147. $info = db('Accountsmessage')->where('message_id', $id)->find();
  148. //$account = db('Accounts')->where('id', $info['account_id'])->find();
  149. $user = db('Users')->where('id', $info['user_id'])->find();
  150. //$info['account_name'] = $account['account_name'];
  151. //$info['account_email'] = $account['account_email'];
  152. $info['user_name'] = $user['user_name'];
  153. $messagetype = db('messagetype')->select();
  154. // 优化显示附件
  155. $images = $info['image'];
  156. //转换成数组格式
  157. $images = explode(",", $images);
  158. if(!empty($images)){
  159. $img = '';
  160. for($i=0;$i<count($images);$i++){
  161. $img = $img.'<img src="'.$images[$i].'" width="240px" style="margin-top:20px;">';
  162. }
  163. $info['image'] = $img;
  164. }
  165. // 优化时间
  166. if (null != $info['add_time']) {
  167. $info['add_time'] = date('Y-m-d H:m:s', $info['add_time']);
  168. }
  169. if (null != $info['dealWith_time']) {
  170. $info['dealWith_time'] = date('Y-m-d H:m:s', $info['dealWith_time']);
  171. }
  172. // 优化显示状态
  173. if (1 == $info['message_status']) {
  174. $info['message_status'] = '已处理';
  175. } else {
  176. $info['message_status'] = '未处理';
  177. }
  178. $this->assign([
  179. 'info' => $info,
  180. 'messagetype' => $messagetype,
  181. 'status' => config('kf_status'),
  182. // 'groups' => db('groups')->select()
  183. ]);
  184. return $this->fetch();
  185. }
  186. //删除留言
  187. public function delMessage(){
  188. if (request()->isAjax()) {
  189. //留言类型id
  190. $id = input('param.id/d');
  191. try {
  192. db('accountsmessage')->where('message_id', $id)->delete();
  193. } catch (\Exception $e) {
  194. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  195. }
  196. return json(['code' => 1, 'data' => '', 'msg' => '删除留言成功']);
  197. }
  198. }
  199. // 上传客服头像
  200. public function upAvatar()
  201. {
  202. if(request()->isAjax()) {
  203. $file = request()->file('file');
  204. if (!empty($file)) {
  205. // 移动到框架应用根目录/public/uploads/ 目录下
  206. $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
  207. if ($info) {
  208. $src = '/uploads' . '/' . date('Ymd') . '/' . $info->getFilename();
  209. return json(['code' => 0, 'data' => ['src' => $src], 'msg' => 'ok']);
  210. } else {
  211. // 上传失败获取错误信息
  212. return json(['code' => -1, 'data' => '', 'msg' => $file->getError()]);
  213. }
  214. }
  215. }
  216. }
  217. // 生成留言操作按钮
  218. private function makeBtn($id)
  219. {
  220. $operate = '<a href="' . url('Messages/detail', ['id' => $id]) . '">';
  221. $operate .= '<button type="button" class="btn btn-info btn-sm"><i class="fa fa-institution"></i> 详情</button></a>';
  222. $operate .= '<a href="javascript:messageDel(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
  223. $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';
  224. return $operate;
  225. }
  226. // 留言类型列表
  227. public function type()
  228. {
  229. if(request()->isAjax()){
  230. $param = input('param.');
  231. $limit = $param['pageSize'];
  232. $offset = ($param['pageNumber'] - 1) * $limit;
  233. $where = [];
  234. if (empty($param['searchText']) === false) {
  235. $where['account_name'] = $param['searchText'];
  236. }
  237. $result = db('messagetype')->limit($offset, $limit)->select();
  238. foreach($result as $key=>$vo){
  239. // 优化时间
  240. if(null != $vo['addtime']){
  241. $result[$key]['addtime'] = date('Y-m-d H:m:s',$result[$key]['addtime']);
  242. }
  243. // 优化显示状态
  244. if(1 == $vo['status']){
  245. $result[$key]['status'] = '<span class="label label-primary">启用</span>';
  246. }else{
  247. $result[$key]['status'] = '<span class="label label-danger">停用</span>';
  248. }
  249. // 生成操作按钮
  250. $result[$key]['operate'] = $this->makeBtnType($vo['id']);
  251. }
  252. $return['total'] = db('messagetype')->count(); //总数据
  253. $return['rows'] = $result;
  254. return json($return);
  255. }
  256. return $this->fetch();
  257. }
  258. // 添加留言类型
  259. public function add()
  260. {
  261. if (request()->isAjax()) {
  262. $param = input('post.');
  263. $has = db('messagetype')->where('name',$param['name'])->find();
  264. if(!empty($has)){
  265. return json(['code' => -1, 'data' => '', 'msg' => '此留言类型已存在']);
  266. }
  267. $info = array();
  268. $info['name'] = $param['name'];
  269. $info['addtime'] = time();
  270. $info['status'] = $param['status'];
  271. try {
  272. db('messagetype')->insert($info);
  273. } catch (\Exception $e) {
  274. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  275. }
  276. return json(['code' => 1, 'data' => '', 'msg' => '添加留言类型成功']);
  277. }
  278. $this->assign([
  279. 'status' => config('kf_status')
  280. ]);
  281. return $this->fetch();
  282. }
  283. // 编辑留言类型
  284. public function edit()
  285. {
  286. // 菜单id
  287. $id = input('param.id/d');
  288. if (request()->isAjax()) {
  289. $param = input('post.');
  290. $has = db('messagetype')->where('name',$param['name'])->where('id','<>',$id)->find();
  291. if(!empty($has)){
  292. return json(['code' => -1, 'data' => '', 'msg' => '此留言类型已存在']);
  293. }
  294. $info = array();
  295. $info['name'] = $param['name'];
  296. $info['status'] = $param['status'];
  297. try {
  298. db('messagetype')->where('id', $param['id'])->update($info);
  299. } catch (\Exception $e) {
  300. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  301. }
  302. return json(['code' => 1, 'data' => '', 'msg' => '编辑留言类型成功']);
  303. }
  304. $type = db('messagetype')->where('id',$id)->find();
  305. $this->assign([
  306. 'type' => $type,
  307. 'id' => $id,
  308. 'status' => config('kf_status')
  309. ]);
  310. return $this->fetch();
  311. }
  312. // 删除留言类型
  313. public function delType()
  314. {
  315. if (request()->isAjax()) {
  316. //留言类型id
  317. $id = input('param.id/d');
  318. try {
  319. db('messagetype')->where('id', $id)->delete();
  320. } catch (\Exception $e) {
  321. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  322. }
  323. return json(['code' => 1, 'data' => '', 'msg' => '删除留言类型成功']);
  324. }
  325. }
  326. // 生成按钮
  327. private function makeBtnType($id)
  328. {
  329. $operate = '<a href="' . url('Messages/edit', ['id' => $id]) . '">';
  330. $operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 编辑</button></a> ';
  331. $operate .= '<a href="javascript:typeDel(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
  332. $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';
  333. return $operate;
  334. }
  335. }