UserNotice.php 10 KB

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