MessageController.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /**
  3. * 系统设置
  4. */
  5. namespace App\Http\Controllers\Admin;
  6. use App\Http\Controllers\Controller;
  7. use App\Models\Account;
  8. use App\Models\Account_group;
  9. use Illuminate\Http\Request as Req;
  10. use Illuminate\Support\Facades\DB;
  11. use Request;
  12. /**
  13. *
  14. */
  15. class MessageController extends Controller
  16. {
  17. /**
  18. * 活动公告
  19. */
  20. function Index(Req $req)
  21. {
  22. $request = array();
  23. $request['title'] = isset($req->title) ? trim ($req->title) : null;
  24. $request['type'] = isset($req->type) ? trim ($req->type) : null;
  25. // $request['message_type'] = isset($req->message_type) ? trim($req->message_type) : null;
  26. $request['toname'] = isset($req->toname) ? trim ($req->toname) : null;
  27. $request['fromname'] = isset($req->fromname) ? trim ($req->fromname) : null;
  28. $dt = \App\Lib\DataTable\DataTable::init ();
  29. $dt->setDataSource ('/admin/Message/getArticle');
  30. $dt->setLang ('notice');
  31. // $dt->addColsFields('id');
  32. $dt->addColsFields ('title');
  33. $dt->addColsFields ('toname');
  34. $dt->addColsFields ('group_name');
  35. $dt->addColsFields ('formname');
  36. $dt->addColsFields ('add_type');
  37. $dt->addColsFields ('is_show');
  38. // $dt->addColsFields('content');
  39. $dt->addColsFields ('add_time');
  40. // $dt->addColsFields('message_type');
  41. $dt->addColsFields ('type');
  42. $dt->enableCheckBox ();
  43. $arr[] = 'view';
  44. $dt->setToolBar ($arr, array('width' => 140));
  45. return view ('admin.Manager/arictle', $dt->render ($request));
  46. }
  47. //获取信息
  48. //获取活动信息
  49. function getArticle()
  50. {
  51. $limit = Request::has ('limit') ? Request::get ('limit') : 10;
  52. $title = Request::has ('title') ? Request::get ('title') : '';
  53. $content = Request::has ('content') ? Request::get ('content') : '';
  54. $message_type = Request::has ('message_type') ? Request::get ('message_type') : '';
  55. $type = Request::has ('type') ? Request::get ('type') : '';
  56. $forname = Request::has ('forname') ? Request::get ('forname') : '';
  57. $toname = Request::has ('toname') ? Request::get ('toname') : '';
  58. $add_time = Request::has ('add_time') ? Request::get ('add_time') : '';
  59. $where = array();
  60. if (!empty($title)) {
  61. $where[] = array('title', 'like', '%' . $title . '%');
  62. }
  63. if (!empty($type)) {
  64. $where[] = array('type', '=', $type);
  65. } else {
  66. $where[] = array('type', '!=', 2);
  67. $where[] = array('type', '!=', 4);
  68. }
  69. if (!empty($content)) {
  70. $where[] = array('content', 'like', '%' . $content . '%');
  71. }
  72. if (!empty($forname)) {
  73. $where[] = array('formname', 'like', '%' . $forname . '%');
  74. }
  75. if (!empty($toname)) {
  76. $where[] = array('toname', 'like', '%' . $toname . '%');
  77. }
  78. if (!empty($add_time)) {
  79. $add_time = date ('Y-m-d H:i:s', strtotime ($add_time));
  80. $where[] = array('add_time', '<=', $add_time);
  81. }
  82. $db = new \App\Models\Manager();
  83. $data = $db->getlist ($limit, $where);
  84. if (!($data < 0)) {
  85. $langinfo = trans ('status.manager');
  86. $messged = trans ('notice.message_status');
  87. $show = trans ('notice.show');
  88. foreach ($data['data'] as $k => $v) {
  89. $data['data'][$k]['type'] = $langinfo[$v['type']];
  90. if (!empty($v['add_type'])) {
  91. $data['data'][$k]['add_type'] = $messged[$v['add_type']];
  92. }
  93. $data['data'][$k]['is_show'] = $show[$v['is_show']];
  94. }
  95. }
  96. return \App\Lib\DataTable\DataTable::init ()->toJson ($data['data'], $data['total']);
  97. }
  98. function view(Req $req)
  99. {
  100. return $this->Edit ($req);
  101. }
  102. function Edit(Req $req)
  103. {
  104. $id = $req->id;
  105. // $id=Request::has('id')?Request::get('id'):'';
  106. if (intval ($id) < 1) {
  107. abort (404);
  108. }
  109. $db = new \App\Models\Manager;
  110. $groupdata = \App\Models\Account_group::getAllInfo ();
  111. if (!$req->isMethod ('post')) {
  112. $data = $db->getDmsg ($id, 1);
  113. if (!$data) {
  114. return responseToJson ($data);
  115. }
  116. return view ('admin.Manager/editNotice', ['data' => $data, 'group' => $groupdata]);
  117. } else {
  118. $data['title'] = $req->input ('title');
  119. $data['content'] = $req->desc;
  120. $res = $db->updateMsg ($data, $id);
  121. // $log = array(
  122. // session('adminInfo.admin_name'),
  123. // );
  124. // OperationLog(session('adminInfo.admin_id'), 'edit_game', $log);
  125. return responseToJson ($res);
  126. }
  127. }
  128. /**
  129. * 批量删除
  130. * [delete description]
  131. * @return [type] [description]
  132. */
  133. function delete(Req $req)
  134. {
  135. $id = $req->input ('id');
  136. if (empty($id)) {
  137. return responseToJson (-2001); //id������
  138. }
  139. $ids = explode (',', $id);
  140. if (!is_array ($ids) && intval ($ids) < 0) {
  141. return responseToJson (-2002); //id����
  142. }
  143. if (is_array ($ids) && count ($ids) > 0) {
  144. foreach ($ids as $k => $v) {
  145. if (intval ($v) < 1) {
  146. unset($ids[$k]);
  147. }
  148. }
  149. }
  150. $rows = \App\Models\Manager::whereIn ('id', $ids)->delete ();
  151. if (!$rows) {
  152. return responseToJson (-2003); //id����
  153. }
  154. $log = array(
  155. session('adminInfo.admin_name'),
  156. );
  157. OperationLog(session('adminInfo.admin_id'), 'del_mess', $log);
  158. return responseToJson (1); //id����
  159. }
  160. function MessageView()
  161. {
  162. $id = Request::has ('id') ? Request::get ('id') : '';
  163. $admin_id = session ('adminInfo.admin_id');
  164. $where = [
  165. 'id',
  166. $id
  167. ];
  168. $dataWhwew = [
  169. 'message_id' => $id,
  170. 'account_identity' => $admin_id,
  171. // 'add_time'=>time()
  172. ];
  173. $db = new \App\Models\Manager();
  174. $dbRead = new \App\Models\MessageRead;
  175. $data = $db->getlistd ($where);
  176. $dataRead = $dbRead->Messagelist ($id, $admin_id);
  177. if ($dataRead < 0) {
  178. $dat = $dbRead->addMessage ($dataWhwew);
  179. }
  180. return view ('admin.Manager/view', ['data' => $data]);
  181. }
  182. /**
  183. * 添加消息
  184. * @param Req $req
  185. * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
  186. */
  187. function addNotice(Req $req)
  188. {
  189. $groupdata = \App\Models\Account_group::getAllInfo ();
  190. if (!$req->isMethod ('post')) {
  191. $groupdatas = $groupdata;
  192. return view ('admin.Manager/editNotice', ['select' => 1, 'group' => $groupdatas]);
  193. } else {
  194. $data['title'] = $req->input ('title');
  195. $data['type'] = $req->type ? intval ($req->type) : '';
  196. if (empty($data['type'])) {
  197. return responseToJson (-3001341);
  198. }
  199. $data['content'] = $req->desc;
  200. $log = array(
  201. session ('adminInfo.admin_name'),
  202. $data['title']
  203. );
  204. $data['add_time'] = date ('Y-m-d H:i:s');
  205. $data['formname'] = $req->input ('formname');
  206. $data['toname'] = str_replace (',', ',', strip_tags (trim ($req->input ('toname'))));
  207. $data['add_type'] = isset($req->add_type) ? trim ($req->add_type) : '1';
  208. $data['is_show'] = isset($req->is_show) ? trim ($req->is_show) : 0; //首页是否弹出显示
  209. $groups = $req->groups;
  210. $send = [
  211. 'name' => $data['title'],
  212. 'from' => $data['formname'],
  213. 'content' => $data['content'],
  214. 'time' => $data['add_time'],
  215. 'type' => $data['type']
  216. ];
  217. $db = new \App\Models\Manager;
  218. //普通消息
  219. if ($data['type'] == 1) {
  220. $dataName = explode (',', $data['toname']);
  221. $send['to'] = $this->getIdBy ($dataName);//接收者列表
  222. }
  223. //系统消息
  224. if ($data['type'] == 2) {
  225. $data['formname'] = '系统消息';
  226. //$data['toname'] = 'all';
  227. }
  228. //系统通知
  229. if ($data['type'] == 3) {
  230. $data['formname'] = '系统通知';
  231. //获取发送用户组
  232. if (is_array ($groups) && count ($groups) > 0) {
  233. $data['toname'] = 'groups';
  234. $data['group_code'] = '0,' . implode (',', $groups) . ',';
  235. $data['group_name'] = '|';
  236. foreach ($groupdata as $k => $v) {
  237. if (in_array ($v['group_code'], $groups)) {
  238. $data['group_name'] .= $v['group_name'] . '|';
  239. }
  240. }
  241. }
  242. $groupcode = isset($data['group_code']) ? $data['group_code'] : 'all';
  243. }
  244. if (!$data['toname']) {
  245. $data['toname'] = 'all';
  246. }
  247. $res = $db->addNotice ($data);
  248. if ($res < 0)
  249. return responseToJson ($res);
  250. OperationLog (session ('adminInfo.admin_id'), 'add_no', $log);
  251. return responseToJson (1);
  252. }
  253. }
  254. /**
  255. * 根据用户账号查ID
  256. * @param $names
  257. * @return null
  258. */
  259. protected function getIdBy($names)
  260. {
  261. if (!is_array ($names)) $names = explode (',', $names);
  262. $names = array_unique ($names);
  263. if (!$names) return null;
  264. $ids = (new Account())->whereIn ('account', $names)->pluck ('identity');
  265. return $ids ? $ids->toArray () : null;
  266. }
  267. }