User.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\admin\model\cms\Channel;
  4. use app\common\controller\Backend;
  5. use app\common\library\Auth;
  6. use app\common\model\UserRoleContent;
  7. use app\common\model\UserRoleLog;
  8. use think\Db;
  9. use think\exception\PDOException;
  10. use think\exception\ValidateException;
  11. /**
  12. * 会员管理
  13. *
  14. * @icon fa fa-user
  15. */
  16. class User extends Backend
  17. {
  18. protected $relationSearch = true;
  19. protected $searchFields = 'id,username,nickname';
  20. protected $noNeedRight = ['getAuthorList'];
  21. /**
  22. * @var \app\admin\model\User
  23. */
  24. protected $model = null;
  25. public function _initialize()
  26. {
  27. parent::_initialize();
  28. $this->model = model('User');
  29. $this->view->assign("typesList", $this->model->getTypesList());
  30. $journal_list = Channel::where(['chief_id' => 0, 'parent_id' => 1])->column('name', 'id');
  31. $this->view->assign('journal_list', $journal_list);
  32. $group_list = ['author' => __('Author'), 'review' => __('Review'), 'chief' => __('chief'), 'editor' => __('editor')];
  33. $this->view->assign('group_list', $group_list);
  34. }
  35. /**
  36. * 查看
  37. */
  38. public function index()
  39. {
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if ($this->request->isAjax()) {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $type = $this->request->param('type');
  49. $new_where = [];
  50. if ($type == 'author') {
  51. $new_where['is_author'] = 'correct';
  52. }
  53. if ($type == 'review') {
  54. $new_where['is_review'] = 'correct';
  55. }
  56. if ($type == 'chief') {
  57. $new_where['is_chief'] = 'correct';
  58. }
  59. if ($type == 'editor') {
  60. $new_where['is_editor'] = 'correct';
  61. }
  62. $list = $this->model
  63. ->with('group')
  64. ->where($where)
  65. ->where($new_where)
  66. ->order($sort, $order)
  67. ->paginate($limit);
  68. foreach ($list as $k => $v) {
  69. $type_arr = [];
  70. // 添加对应用户身份
  71. if ($v['is_author'] == 'correct') {
  72. $type_arr[] = __('Author');
  73. }
  74. if ($v['is_review'] == 'correct') {
  75. $type_arr[] = __('Review');
  76. }
  77. if ($v['is_chief'] == 'correct') {
  78. $type_arr[] = __('Chief');
  79. }
  80. if ($v['is_editor'] == 'correct') {
  81. $type_arr[] = __('Editor');
  82. }
  83. $v->type = implode(',',$type_arr);
  84. $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
  85. $v->hidden(['password', 'salt']);
  86. }
  87. $result = array("total" => $list->total(), "rows" => $list->items());
  88. return json($result);
  89. }
  90. return $this->view->fetch();
  91. }
  92. /**
  93. * 添加
  94. */
  95. public function add()
  96. {
  97. if ($this->request->isPost()) {
  98. $this->token();
  99. }
  100. return parent::add();
  101. }
  102. /**
  103. * 编辑
  104. */
  105. public function edit($ids = null)
  106. {
  107. if ($this->request->isPost()) {
  108. $this->token();
  109. }
  110. $row = $this->model->get($ids);
  111. $channel = Channel::where(['chief_id' => $row->id])->find();
  112. $row->journal = $channel->id ?? 0;
  113. $this->modelValidate = true;
  114. if (!$row) {
  115. $this->error(__('No Results were found'));
  116. }
  117. $group = [];
  118. $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
  119. $adminIds = $this->getDataLimitAdminIds();
  120. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  121. $this->error(__('You have no permission'));
  122. }
  123. if (false === $this->request->isPost()) {
  124. if ($row['is_author'] == 'correct') {
  125. $group[] = 'author';
  126. }
  127. if ($row['is_review'] == 'correct') {
  128. $group[] = 'review';
  129. }
  130. if ($row['is_editor'] == 'correct') {
  131. $group[] = 'editor';
  132. }
  133. if ($row['is_chief'] == 'correct') {
  134. $group[] = 'chief';
  135. }
  136. $row->group = implode(',', $group);
  137. $this->view->assign('row', $row);
  138. return $this->view->fetch();
  139. }
  140. $params = $this->request->post('row/a');
  141. if (empty($params)) {
  142. $this->error(__('Parameter %s can not be empty', ''));
  143. }
  144. $params = $this->preExcludeFields($params);
  145. $result = false;
  146. Db::startTrans();
  147. try {
  148. // 用户原来有的角色信息
  149. $row_group = [];
  150. // 如用户有角色需要删除角色可用
  151. if ($row['is_author'] == 'correct') {
  152. $row_group[] = 'author';
  153. }
  154. if ($row['is_review'] == 'correct') {
  155. $row_group[] = 'review';
  156. }
  157. if ($row['is_editor'] == 'correct') {
  158. $row_group[] = 'editor';
  159. }
  160. if ($row['is_chief'] == 'correct') {
  161. $row_group[] = 'chief';
  162. }
  163. // 用户选中的角色
  164. $groups = $params['group'];
  165. if ($groups[0] == '') {
  166. unset($groups[0]);
  167. }
  168. // 原来角色有多个
  169. if (count($row_group) > 0) {
  170. // 新修改的身份全部取消
  171. if (count($groups) == 0) {
  172. $group_data['is_author'] = 'fault';
  173. $group_data['is_review'] = 'fault';
  174. $group_data['is_editor'] = 'fault';
  175. $group_data['is_chief'] = 'fault';
  176. }
  177. }
  178. // 用户身份
  179. if (count($row_group) == 0 && count($groups) == 0) {
  180. $group_data['is_author'] = 'fault';
  181. $group_data['is_review'] = 'fault';
  182. $group_data['is_editor'] = 'fault';
  183. $group_data['is_chief'] = 'fault';
  184. }
  185. // 修改的角色数组大于0
  186. if (count($groups) > 0) {
  187. // 传入角色比原来角色数量多说明增加
  188. if (count($groups) > count($row_group)) {
  189. if (count($row_group) > 0) {
  190. foreach ($groups as $item) {
  191. // 如果当前角色不在原来角色内容中
  192. if (!in_array($item, $row_group)) {
  193. if ($item == 'author') {
  194. $group_data['is_author'] = 'correct';
  195. }
  196. if ($item == 'review') {
  197. $group_data['is_review'] = 'correct';
  198. }
  199. if ($item == 'editor') {
  200. $group_data['is_editor'] = 'correct';
  201. }
  202. if ($item == 'chief') {
  203. $group_data['is_chief'] = 'correct';
  204. }
  205. }
  206. }
  207. } else { // 如果是空角色时则直接添加
  208. foreach ($groups as $group) {
  209. if ($group == 'author') {
  210. $group_data['is_author'] = 'correct';
  211. }
  212. if ($group == 'review') {
  213. $group_data['is_review'] = 'correct';
  214. }
  215. if ($group == 'editor') {
  216. $group_data['is_editor'] = 'correct';
  217. }
  218. if ($group == 'chief') {
  219. $group_data['is_chief'] = 'correct';
  220. }
  221. }
  222. }
  223. }
  224. // 如果更改的数量相同则判断是否是增加还是无增加
  225. if (count($row_group) == count($groups)) {
  226. foreach ($groups as $item) {
  227. // 如果当前角色不在原来角色内容中
  228. if (!in_array($item, $row_group)) {
  229. if ($item == 'author') {
  230. $group_data['is_author'] = 'correct';
  231. }
  232. if ($item == 'review') {
  233. $group_data['is_review'] = 'correct';
  234. }
  235. if ($item == 'editor') {
  236. $group_data['is_editor'] = 'correct';
  237. }
  238. if ($item == 'chief') {
  239. $group_data['is_chief'] = 'correct';
  240. }
  241. }
  242. }
  243. foreach ($row_group as $item) {
  244. if (!in_array($item, $groups)) {
  245. if ($item == 'author') {
  246. $group_data['is_author'] = 'fault';
  247. }
  248. if ($item == 'review') {
  249. $group_data['is_review'] = 'fault';
  250. }
  251. if ($item == 'editor') {
  252. $group_data['is_editor'] = 'fault';
  253. }
  254. if ($item == 'chief') {
  255. $group_data['is_chief'] = 'fault';
  256. }
  257. }
  258. }
  259. }
  260. // 不更改用户角色时
  261. if ($groups === $row_group) {
  262. foreach ($groups as $group) {
  263. if ($group == 'author') {
  264. $group_data['is_author'] = 'correct';
  265. }
  266. if ($group == 'review') {
  267. $group_data['is_review'] = 'correct';
  268. }
  269. if ($group == 'editor') {
  270. $group_data['is_editor'] = 'correct';
  271. }
  272. if ($group == 'chief') {
  273. $group_data['is_chief'] = 'correct';
  274. }
  275. }
  276. }
  277. // 在原来的基础上减少角色时
  278. // 传入角色比原来角色数量少说明减少
  279. if (count($groups) < count($row_group)) {
  280. foreach ($row_group as $item) {
  281. if (!in_array($item, $groups)) {
  282. if ($item == 'author') {
  283. $group_data['is_author'] = 'fault';
  284. }
  285. if ($item == 'review') {
  286. $group_data['is_review'] = 'fault';
  287. }
  288. if ($item == 'editor') {
  289. $group_data['is_editor'] = 'fault';
  290. }
  291. if ($item == 'chief') {
  292. $group_data['is_chief'] = 'fault';
  293. }
  294. }
  295. }
  296. }
  297. }
  298. // 对应的角色信息插入到
  299. foreach ($group_data as $key => $item) {
  300. $params[$key] = $item;
  301. if ($key == 'is_author') {
  302. $type = 'author';
  303. }
  304. if ($key == 'is_review') {
  305. $type = 'review';
  306. }
  307. // 如果是新增编辑角色则需要判断是否拥有主编,如拥有主编则需要去除掉
  308. if ($key == 'is_editor') {
  309. $type = 'editor';
  310. if ($row->is_chief == 'correct') {
  311. $params['is_chief'] = 'fault';
  312. }
  313. }
  314. // 如果是新增主编角色则需要判断是否拥有编辑,如拥有编辑则需要去除掉
  315. if ($key == 'is_chief') {
  316. $type = 'chief';
  317. if ($row->is_editor == 'correct') {
  318. $params['is_editor'] = 'fault';
  319. }
  320. // 判断如果选择了主编则需要填写期刊
  321. if (empty($params['journal'])) {
  322. $this->error(__('Please select a journal'));
  323. }
  324. // 对应期刊绑定主编
  325. $channel = Channel::get($params['journal']);
  326. $channel->chief_id = $row->id;
  327. $channel->save();
  328. }
  329. // 增加user_role_content数据
  330. if ($item == 'correct') {
  331. // 如果是增加角色则需要添加user_role_log 和 user_role_content
  332. $user_role_log_model = new UserRoleLog();
  333. $user_role_log_model->user_id = $row->id;
  334. $user_role_log_model->type = $type;
  335. $user_role_log_model->is_adopt = $item;
  336. if ($user_role_log_model->save()) {
  337. $user_role_model = new UserRoleContent();
  338. $user_role_model->user_id = $row->id;
  339. $user_role_model->log_id = $user_role_log_model->id;
  340. $user_role_model->type = $type;
  341. $user_role_model->affiliation = $row->affiliation;
  342. $user_role_model->save();
  343. }
  344. } else {
  345. // 如果是删减角色则需要删除user_role_log 和 user_role_content
  346. $user_role_log_model = UserRoleLog::where(['user_id' => $row->id, 'type' => $type])->select();
  347. if (count($user_role_log_model) > 0) {
  348. foreach ($user_role_log_model as $user_log) {
  349. $user_role_model = UserRoleContent::where(['user_id' => $row->id, 'type' => $type, 'log_id' => $user_log->id])->select();
  350. $user_log->delete();
  351. if (count($user_role_model) > 0) {
  352. foreach ($user_role_model as $user_role) {
  353. $user_role->delete();
  354. }
  355. }
  356. }
  357. }
  358. }
  359. }
  360. $result = $row->allowField(true)->save($params);
  361. Db::commit();
  362. } catch (ValidateException|PDOException|Exception $e) {
  363. Db::rollback();
  364. $this->error($e->getMessage());
  365. }
  366. if (false === $result) {
  367. $this->error(__('No rows were updated'));
  368. }
  369. $this->success();
  370. }
  371. /**
  372. * 删除
  373. */
  374. public function del($ids = "")
  375. {
  376. if (!$this->request->isPost()) {
  377. $this->error(__("Invalid parameters"));
  378. }
  379. $ids = $ids ? $ids : $this->request->post("ids");
  380. $row = $this->model->get($ids);
  381. $this->modelValidate = true;
  382. if (!$row) {
  383. $this->error(__('No Results were found'));
  384. }
  385. Auth::instance()->delete($row['id']);
  386. $this->success();
  387. }
  388. /**
  389. * 获取作者列表
  390. *
  391. * @return string|\think\response\Json
  392. * @throws \think\Exception
  393. * @throws \think\exception\DbException
  394. */
  395. public function getAuthorList()
  396. {
  397. //设置过滤方法
  398. $this->request->filter(['strip_tags', 'trim']);
  399. if ($this->request->isAjax()) {
  400. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  401. $list = $this->model
  402. ->with('group')
  403. ->where(['user.is_author' => 'correct'])
  404. ->where($where)
  405. ->order($sort, $order)
  406. ->paginate($limit);
  407. foreach ($list as $k => $v) {
  408. $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
  409. $v->hidden(['password', 'salt']);
  410. }
  411. $result = array("total" => $list->total(), "rows" => $list->items());
  412. return json($result);
  413. }
  414. return $this->view->fetch();
  415. }
  416. }