Messages.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. if (empty($param['searchText']) === false) {
  20. $where['account_name'] = $param['searchText'];
  21. }
  22. $start = '';$end = '';
  23. if (empty($param['start']) === false && empty($param['end']) === false && $param['start'] <= $param['end']) {
  24. $start = strtotime($param['start']);
  25. $end = strtotime($param['end'] . ' 23:59:59');
  26. }
  27. // $join = [
  28. // 'accounts b' => 'a.account_id = b.id',
  29. // ];
  30. $leftjoin = [
  31. 'users c' => 'a.user_id = c.id',
  32. ];
  33. $result = model('Accountsmessage')->selectJoin($leftjoin,$where, $start,$end,$offset, $limit);
  34. foreach($result as $key=>$vo){
  35. // 优化显示附件
  36. $images = $result[$key]['image'];
  37. //转换成数组格式
  38. $images = explode(",", $images);
  39. if(!empty($images)){
  40. $img = '';
  41. for($i=0;$i<count($images);$i++){
  42. $img = $img.'<img src="'.$images[$i].'" width="40px" height="40px" style="margin-left:5px;margin-top:5px;">';
  43. }
  44. $result[$key]['image'] = $img;
  45. }
  46. // 优化时间
  47. if(null != $vo['add_time']){
  48. $result[$key]['add_time'] = date('Y-m-d H:m:s',$result[$key]['add_time']);
  49. }
  50. if(null != $vo['dealWith_time']){
  51. $result[$key]['dealWith_time'] = date('Y-m-d H:m:s',$result[$key]['dealWith_time']);
  52. }
  53. // 优化显示状态
  54. if(1 == $vo['message_status']){
  55. $result[$key]['status'] = '<span class="label label-primary">已处理</span>';
  56. }else{
  57. $result[$key]['status'] = '<span class="label label-danger">未处理</span>';
  58. }
  59. // 是否公开
  60. if(1 == $vo['if_public']){
  61. $result[$key]['if_public'] = '公开';
  62. }else{
  63. $result[$key]['if_public'] = '不公开';
  64. }
  65. // 生成操作按钮
  66. $result[$key]['operate'] = $this->makeBtn($vo['message_id']);
  67. }
  68. $return['total'] = db('Accountsmessage')->count(); //总数据
  69. $return['rows'] = $result;
  70. return json($return);
  71. }
  72. return $this->fetch();
  73. }
  74. // 留言详情
  75. public function detail()
  76. {
  77. if(request()->isAjax()) {
  78. //设置留言公开/不公开
  79. $param = input('post.');
  80. $id = $param['id'];
  81. $if_public = $param['if_public'];
  82. try{
  83. db('accountsmessage')->where('message_id', $id)->update(['if_public' => $if_public]);
  84. }catch(\Exception $e){
  85. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  86. }
  87. return json(['code' => 1, 'data' => url('messages/index'), 'msg' => '设置成功']);
  88. }
  89. $id = input('param.id/d');
  90. $info = db('Accountsmessage')->where('message_id', $id)->find();
  91. //$account = db('Accounts')->where('id', $info['account_id'])->find();
  92. $user = db('Users')->where('id', $info['user_id'])->find();
  93. //$info['account_name'] = $account['account_name'];
  94. //$info['account_email'] = $account['account_email'];
  95. $info['user_name'] = $user['user_name'];
  96. // 优化显示附件
  97. $images = $info['image'];
  98. //转换成数组格式
  99. $images = explode(",", $images);
  100. if(!empty($images)){
  101. $img = '';
  102. for($i=0;$i<count($images);$i++){
  103. $img = $img.'<img src="'.$images[$i].'" width="240px" style="margin-top:20px;">';
  104. }
  105. $info['image'] = $img;
  106. }
  107. // 优化时间
  108. if (null != $info['add_time']) {
  109. $info['add_time'] = date('Y-m-d H:m:s', $info['add_time']);
  110. }
  111. if (null != $info['dealWith_time']) {
  112. $info['dealWith_time'] = date('Y-m-d H:m:s', $info['dealWith_time']);
  113. }
  114. // 优化显示状态
  115. if (1 == $info['message_status']) {
  116. $info['message_status'] = '已处理';
  117. } else {
  118. $info['message_status'] = '未处理';
  119. }
  120. $this->assign([
  121. 'info' => $info,
  122. 'status' => config('kf_status'),
  123. // 'groups' => db('groups')->select()
  124. ]);
  125. return $this->fetch();
  126. }
  127. // 上传客服头像
  128. public function upAvatar()
  129. {
  130. if(request()->isAjax()) {
  131. $file = request()->file('file');
  132. if (!empty($file)) {
  133. // 移动到框架应用根目录/public/uploads/ 目录下
  134. $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
  135. if ($info) {
  136. $src = '/uploads' . '/' . date('Ymd') . '/' . $info->getFilename();
  137. return json(['code' => 0, 'data' => ['src' => $src], 'msg' => 'ok']);
  138. } else {
  139. // 上传失败获取错误信息
  140. return json(['code' => -1, 'data' => '', 'msg' => $file->getError()]);
  141. }
  142. }
  143. }
  144. }
  145. // 生成按钮
  146. private function makeBtn($id)
  147. {
  148. $operate = '<a href="' . url('Messages/detail', ['id' => $id]) . '">';
  149. $operate .= '<button type="button" class="btn btn-info btn-sm"><i class="fa fa-institution"></i> 详情</button></a>';
  150. return $operate;
  151. }
  152. // 留言类型列表
  153. public function type()
  154. {
  155. if(request()->isAjax()){
  156. $param = input('param.');
  157. $limit = $param['pageSize'];
  158. $offset = ($param['pageNumber'] - 1) * $limit;
  159. $where = [];
  160. if (empty($param['searchText']) === false) {
  161. $where['account_name'] = $param['searchText'];
  162. }
  163. $result = db('messagetype')->limit($offset, $limit)->select();
  164. foreach($result as $key=>$vo){
  165. // 优化时间
  166. if(null != $vo['addtime']){
  167. $result[$key]['addtime'] = date('Y-m-d H:m:s',$result[$key]['addtime']);
  168. }
  169. // 优化显示状态
  170. if(1 == $vo['status']){
  171. $result[$key]['status'] = '<span class="label label-primary">启用</span>';
  172. }else{
  173. $result[$key]['status'] = '<span class="label label-danger">停用</span>';
  174. }
  175. // 生成操作按钮
  176. $result[$key]['operate'] = $this->makeBtnType($vo['id']);
  177. }
  178. $return['total'] = db('messagetype')->count(); //总数据
  179. $return['rows'] = $result;
  180. return json($return);
  181. }
  182. return $this->fetch();
  183. }
  184. // 编辑留言类型
  185. public function edit()
  186. {
  187. // 菜单id
  188. $id = input('param.id/d');
  189. if (request()->isAjax()) {
  190. $param = input('post.');
  191. $has = db('messagetype')->where('name',$param['name'])->find();
  192. if(!empty($has)){
  193. return json(['code' => -1, 'data' => '', 'msg' => '此留言类型已存在']);
  194. }
  195. $info = array();
  196. $info['name'] = $param['name'];
  197. $info['status'] = $param['status'];
  198. try {
  199. db('messagetype')->where('id', $param['id'])->update($info);
  200. } catch (\Exception $e) {
  201. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  202. }
  203. return json(['code' => 1, 'data' => '', 'msg' => '编辑留言类型成功']);
  204. }
  205. $type = db('messagetype')->where('id',$id)->find();
  206. $this->assign([
  207. 'type' => $type,
  208. 'id' => $id,
  209. 'status' => config('kf_status')
  210. ]);
  211. return $this->fetch();
  212. }
  213. // 删除留言类型
  214. public function delType()
  215. {
  216. if (request()->isAjax()) {
  217. //留言类型id
  218. $id = input('param.id/d');
  219. try {
  220. db('messagetype')->where('id', $id)->delete();
  221. } catch (\Exception $e) {
  222. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  223. }
  224. return json(['code' => 1, 'data' => '', 'msg' => '删除留言类型成功']);
  225. }
  226. }
  227. // 生成按钮
  228. private function makeBtnType($id)
  229. {
  230. $operate = '<a href="' . url('Messages/edit', ['id' => $id]) . '">';
  231. $operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 编辑</button></a> ';
  232. $operate .= '<a href="javascript:typeDel(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
  233. $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';
  234. return $operate;
  235. }
  236. }