UserNotice.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\user;
  12. use app\admin\controller\AuthController;
  13. use service\FormBuilder as Form;
  14. use service\JsonService as Json;
  15. use think\Request;
  16. use think\Url;
  17. use app\admin\model\user\UserNotice as UserNoticeModel;
  18. use app\admin\model\user\UserNoticeSee as UserNoticeSeeModel;
  19. use app\admin\model\wechat\WechatUser as UserModel;
  20. /**
  21. * 用户通知
  22. * Class UserNotice
  23. * @package app\admin\controller\user
  24. */
  25. class UserNotice extends AuthController
  26. {
  27. /**
  28. * 显示资源列表
  29. *
  30. * @return \think\Response
  31. */
  32. public function index()
  33. {
  34. if ($this->request->isAjax()) {
  35. $where = parent::getMore([
  36. ['page', 1],
  37. ['limit', 20]
  38. ]);
  39. return Json::successlayui(UserNoticeModel::getList($where));
  40. } else {
  41. return $this->fetch();
  42. }
  43. }
  44. /**
  45. * 显示创建资源表单页.
  46. *
  47. * @return \think\Response
  48. */
  49. public function create()
  50. {
  51. $f = array();
  52. $f[] = Form::input('user', '发送人', '系统管理员');
  53. $f[] = Form::input('title', '通知标题');
  54. $f[] = Form::input('content', '通知内容')->type('textarea');
  55. $f[] = Form::radio('type', '消息类型', 1)->options([['label' => '系统消息', 'value' => 1], ['label' => '用户通知', 'value' => 2]]);
  56. $form = Form::make_post_form('添加用户通知', $f, Url::build('save'));
  57. $this->assign(compact('form'));
  58. return $this->fetch('public/form-builder');
  59. }
  60. /**
  61. * 保存新建的资源
  62. *
  63. * @param \think\Request $request
  64. * @return \think\Response
  65. */
  66. public function save(Request $request)
  67. {
  68. $params = $request->post();
  69. if (!$params["user"]) return Json::fail('请输入发送人!');
  70. if (!$params["title"]) return Json::fail('请输入通知标题!');
  71. if (!$params["content"]) return Json::fail('请输入通知内容!');
  72. if ($params["type"] == 2) {
  73. $uids = UserModel::order('uid desc')->column("uid");
  74. $params["uid"] = count($uids) > 0 ? "," . implode(",", $uids) . "," : "";
  75. }
  76. $params["add_time"] = time();
  77. UserNoticeModel::set($params);
  78. return Json::successful('添加成功!');
  79. }
  80. /**
  81. * 显示编辑资源表单页.
  82. *
  83. * @param int $id
  84. * @return \think\Response
  85. */
  86. public function edit($id)
  87. {
  88. $notice = UserNoticeModel::get($id);
  89. if (!$notice) return Json::fail('数据不存在!');
  90. $f = array();
  91. $f[] = Form::input('user', '发送人', $notice["user"]);
  92. $f[] = Form::input('title', '通知标题', $notice["title"]);
  93. $f[] = Form::input('content', '通知内容', $notice["content"])->type('textarea');
  94. $f[] = Form::radio('type', '消息类型', $notice["type"])->options([['label' => '系统消息', 'value' => 1], ['label' => '用户通知', 'value' => 2]]);
  95. $form = Form::make_post_form('编辑通知', $f, Url::build('update', ["id" => $id]), 2);
  96. $this->assign(compact('form'));
  97. return $this->fetch('public/form-builder');
  98. }
  99. /**
  100. * 保存新建的资源
  101. *
  102. * @param \think\Request $request
  103. * @return \think\Response
  104. */
  105. public function update(Request $request, $id)
  106. {
  107. $params = $request->post();
  108. if (!$params["user"]) return Json::fail('请输入发送人!');
  109. if (!$params["title"]) return Json::fail('请输入通知标题!');
  110. if (!$params["content"]) return Json::fail('请输入通知内容!');
  111. UserNoticeModel::edit($params, $id);
  112. return Json::successful('修改成功!');
  113. }
  114. /**
  115. * 删除指定资源
  116. *
  117. * @param int $id
  118. * @return \think\Response
  119. */
  120. public function send($id)
  121. {
  122. UserNoticeModel::edit(array("is_send" => 1, "send_time" => time()), $id);
  123. return Json::successful('发送成功!');
  124. }
  125. /**
  126. * 删除指定资源
  127. *
  128. * @param int $id
  129. * @return \think\Response
  130. */
  131. public function delete($id)
  132. {
  133. if (!UserNoticeModel::del($id))
  134. return Json::fail(UserNoticeModel::getErrorInfo('删除失败,请稍候再试!'));
  135. else
  136. return Json::successful('删除成功!');
  137. }
  138. /**
  139. * 查询发送信息的用户资源
  140. *
  141. * @param int $id
  142. * @return \think\Response
  143. */
  144. public function user($id)
  145. {
  146. $notice = UserNoticeModel::get($id)->toArray();
  147. $model = new UserModel;
  148. $model = $model::alias('A');
  149. $model = $model->field('A.*');
  150. if ($notice["type"] == 2) {
  151. if ($notice["uid"] != "") {
  152. $uids = explode(",", $notice["uid"]);
  153. array_splice($uids, 0, 1);
  154. array_splice($uids, count($uids) - 1, 1);
  155. $model = $model->where("A.uid", "in", $uids);
  156. } else {
  157. $model = $model->where("A.uid", $notice['uid']);
  158. }
  159. $model->order('A.uid desc');
  160. } else {
  161. $model = $model->join('__USER_NOTICE_SEE__ B', 'A.uid = B.uid', 'RIGHT');
  162. $model = $model->where("B.nid", $notice['id']);
  163. $model->order('B.add_time desc');
  164. }
  165. $this->assign(UserModel::page($model, function ($item, $key) use ($notice) {
  166. $item["is_see"] = UserNoticeSeeModel::where("uid", $item["uid"])->where("nid", $notice["id"])->count() > 0 ? 1 : 0;
  167. }));
  168. $this->assign(compact('notice'));
  169. return $this->fetch();
  170. }
  171. /**
  172. * 添加发送信息的用户
  173. *
  174. * @param \think\Request $request
  175. * @return \think\Response
  176. */
  177. public function user_create($id)
  178. {
  179. $where = parent::getMore([
  180. ['nickname', ''],
  181. ['data', ''],
  182. ], $this->request);
  183. $this->assign('where', $where);
  184. $this->assign(UserModel::systemPage($where));
  185. $this->assign(['title' => '添加发送用户', 'save' => Url::build('user_save', array('id' => $id))]);
  186. return $this->fetch();
  187. }
  188. /**
  189. * 保存新建的资源
  190. *
  191. * @param \think\Request $request
  192. * @return \think\Response
  193. */
  194. public function user_save(Request $request, $id)
  195. {
  196. $notice = UserNoticeModel::get($id)->toArray();
  197. if (!$notice) return Json::fail('通知信息不存在!');
  198. if ($notice["type"] == 1) return Json::fail('系统通知不能管理用户!');
  199. //查找当前选中的uid
  200. $params = $request->post();
  201. if (isset($params["search"])) {
  202. $model = new UserModel;
  203. if ($params['search']['nickname'] !== '') $model = $model->where('nickname', 'LIKE', "%" . $params['search']['nickname'] . "%");
  204. if ($params['search']['data'] !== '') {
  205. list($startTime, $endTime) = explode(' - ', $params['search']['data']);
  206. $model = $model->where('add_time', '>', strtotime($startTime));
  207. $model = $model->where('add_time', '<', strtotime($endTime));
  208. }
  209. $model = $model->order('uid desc');
  210. $uids = $model->column("uid");
  211. } else {
  212. $uids = $params["checked_menus"];
  213. }
  214. if (count($uids) <= 0) return Json::fail('请选择要添加的用户!');
  215. //合并原来和现在的uid
  216. if ($notice["uid"] != "") {
  217. $now_uids = explode(",", $notice["uid"]);
  218. array_splice($now_uids, 0, 1);
  219. array_splice($now_uids, count($now_uids) - 1, 1);
  220. $now_uids = array_merge($now_uids, $uids);
  221. } else {
  222. $now_uids = $uids;
  223. }
  224. //编辑合并之后的uid
  225. $res_uids = UserModel::where("uid", "in", $now_uids)->order('uid desc')->column("uid");
  226. UserNoticeModel::edit(array("uid" => "," . implode(",", $res_uids) . ","), $notice["id"]);
  227. return Json::successful('添加成功!');
  228. }
  229. /**
  230. * 删除指定资源
  231. *
  232. * @param int $id
  233. * @return \think\Response
  234. */
  235. public function user_delete($id, $uid)
  236. {
  237. $notice = UserNoticeModel::get($id)->toArray();
  238. if (!$notice) return Json::fail('通知信息不存在!');
  239. if ($notice["type"] == 1) return Json::fail('系统通知不能管理用户!');
  240. if ($notice["uid"] != "") {
  241. $res_uids = explode(",", $notice["uid"]);
  242. array_splice($res_uids, 0, 1);
  243. array_splice($res_uids, count($res_uids) - 1, 1);
  244. }
  245. array_splice($res_uids, array_search($uid, $res_uids), 1);
  246. $value = count($res_uids) > 0 ? "," . implode(",", $res_uids) . "," : "";
  247. UserNoticeModel::edit(array("uid" => $value), $notice["id"]);
  248. return Json::successful('删除成功!');
  249. }
  250. /**
  251. * 删除指定的资源
  252. *
  253. * @param \think\Request $request
  254. * @return \think\Response
  255. */
  256. public function user_select_delete(Request $request, $id)
  257. {
  258. $params = $request->post();
  259. if (count($params["checked_menus"]) <= 0) return Json::fail('删除数据不能为空!');
  260. $notice = UserNoticeModel::get($id)->toArray();
  261. if (!$notice) return Json::fail('通知信息不存在!');
  262. $res_uids = explode(",", $notice["uid"]);
  263. array_splice($res_uids, 0, 1);
  264. array_splice($res_uids, count($res_uids) - 1, 1);
  265. foreach ($params["checked_menus"] as $key => $value) {
  266. array_splice($res_uids, array_search($value, $res_uids), 1);
  267. }
  268. $value = count($res_uids) > 0 ? "," . implode(",", $res_uids) . "," : "";
  269. UserNoticeModel::edit(array("uid" => $value), $notice["id"]);
  270. return Json::successful('删除成功!');
  271. }
  272. /**
  273. * @param $id
  274. * @return mixed
  275. */
  276. public function notice($id)
  277. {
  278. $where = parent::getMore([
  279. ['title', ''],
  280. ], $this->request);
  281. $nickname = UserModel::where('uid', 'IN', $id)->column('uid,nickname');
  282. $this->assign('where', $where);
  283. $this->assign('uid', $id);
  284. $this->assign('nickname', implode(',', $nickname));
  285. $this->assign(UserNoticeModel::getUserList($where));
  286. return $this->fetch();
  287. }
  288. /**
  289. * 给指定用户发送站内信息
  290. * @param $id
  291. */
  292. public function send_user($id = 0, $uid = '')
  293. {
  294. if (!$id || $uid == '') return Json::fail('参数错误');
  295. $uid = "," . $uid . ",";
  296. UserNoticeModel::edit(array("is_send" => 1, "send_time" => time(), 'uid' => $uid), $id);
  297. return Json::successful('发送成功!');
  298. }
  299. }