UserNotice.php 10 KB

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