UserNotice.php 11 KB

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