model = model('User'); $this->view->assign("typesList", $this->model->getTypesList()); $journal_list = Channel::where(['chief_id' => 0, 'parent_id' => 1])->column('name', 'id'); $this->view->assign('journal_list', $journal_list); $group_list = ['author' => __('Author'), 'review' => __('Review'), 'chief' => __('chief'), 'editor' => __('editor')]; $this->view->assign('group_list', $group_list); } /** * 查看 */ public function index() { //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $type = $this->request->param('type'); $new_where = []; if ($type == 'author') { $new_where['is_author'] = 'correct'; } if ($type == 'review') { $new_where['is_review'] = 'correct'; } if ($type == 'chief') { $new_where['is_chief'] = 'correct'; } if ($type == 'editor') { $new_where['is_editor'] = 'correct'; } $list = $this->model ->with('group') ->where($where) ->where($new_where) ->order($sort, $order) ->paginate($limit); foreach ($list as $k => $v) { $type_arr = []; // 添加对应用户身份 if ($v['is_author'] == 'correct') { $type_arr[] = __('Author'); } if ($v['is_review'] == 'correct') { $type_arr[] = __('Review'); } if ($v['is_chief'] == 'correct') { $type_arr[] = __('Chief'); } if ($v['is_editor'] == 'correct') { $type_arr[] = __('Editor'); } $v->type = implode(',',$type_arr); $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname); $v->hidden(['password', 'salt']); } $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } return $this->view->fetch(); } /** * 添加 */ public function add() { if ($this->request->isPost()) { $this->token(); } return parent::add(); } /** * 编辑 */ public function edit($ids = null) { if ($this->request->isPost()) { $this->token(); } $row = $this->model->get($ids); $channel = Channel::where(['chief_id' => $row->id])->find(); $row->journal = $channel->id ?? 0; $this->modelValidate = true; if (!$row) { $this->error(__('No Results were found')); } $group = []; $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker'])); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } if (false === $this->request->isPost()) { if ($row['is_author'] == 'correct') { $group[] = 'author'; } if ($row['is_review'] == 'correct') { $group[] = 'review'; } if ($row['is_editor'] == 'correct') { $group[] = 'editor'; } if ($row['is_chief'] == 'correct') { $group[] = 'chief'; } $row->group = implode(',', $group); $this->view->assign('row', $row); return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $params = $this->preExcludeFields($params); $result = false; Db::startTrans(); try { // 用户原来有的角色信息 $row_group = []; // 如用户有角色需要删除角色可用 if ($row['is_author'] == 'correct') { $row_group[] = 'author'; } if ($row['is_review'] == 'correct') { $row_group[] = 'review'; } if ($row['is_editor'] == 'correct') { $row_group[] = 'editor'; } if ($row['is_chief'] == 'correct') { $row_group[] = 'chief'; } // 用户选中的角色 $groups = $params['group']; if ($groups[0] == '') { unset($groups[0]); } // 原来角色有多个 if (count($row_group) > 0) { // 新修改的身份全部取消 if (count($groups) == 0) { $group_data['is_author'] = 'fault'; $group_data['is_review'] = 'fault'; $group_data['is_editor'] = 'fault'; $group_data['is_chief'] = 'fault'; } } // 用户身份 if (count($row_group) == 0 && count($groups) == 0) { $group_data['is_author'] = 'fault'; $group_data['is_review'] = 'fault'; $group_data['is_editor'] = 'fault'; $group_data['is_chief'] = 'fault'; } // 修改的角色数组大于0 if (count($groups) > 0) { // 传入角色比原来角色数量多说明增加 if (count($groups) > count($row_group)) { if (count($row_group) > 0) { foreach ($groups as $item) { // 如果当前角色不在原来角色内容中 if (!in_array($item, $row_group)) { if ($item == 'author') { $group_data['is_author'] = 'correct'; } if ($item == 'review') { $group_data['is_review'] = 'correct'; } if ($item == 'editor') { $group_data['is_editor'] = 'correct'; } if ($item == 'chief') { $group_data['is_chief'] = 'correct'; } } } } else { // 如果是空角色时则直接添加 foreach ($groups as $group) { if ($group == 'author') { $group_data['is_author'] = 'correct'; } if ($group == 'review') { $group_data['is_review'] = 'correct'; } if ($group == 'editor') { $group_data['is_editor'] = 'correct'; } if ($group == 'chief') { $group_data['is_chief'] = 'correct'; } } } } // 如果更改的数量相同则判断是否是增加还是无增加 if (count($row_group) == count($groups)) { foreach ($groups as $item) { // 如果当前角色不在原来角色内容中 if (!in_array($item, $row_group)) { if ($item == 'author') { $group_data['is_author'] = 'correct'; } if ($item == 'review') { $group_data['is_review'] = 'correct'; } if ($item == 'editor') { $group_data['is_editor'] = 'correct'; } if ($item == 'chief') { $group_data['is_chief'] = 'correct'; } } } foreach ($row_group as $item) { if (!in_array($item, $groups)) { if ($item == 'author') { $group_data['is_author'] = 'fault'; } if ($item == 'review') { $group_data['is_review'] = 'fault'; } if ($item == 'editor') { $group_data['is_editor'] = 'fault'; } if ($item == 'chief') { $group_data['is_chief'] = 'fault'; } } } } // 不更改用户角色时 if ($groups === $row_group) { foreach ($groups as $group) { if ($group == 'author') { $group_data['is_author'] = 'correct'; } if ($group == 'review') { $group_data['is_review'] = 'correct'; } if ($group == 'editor') { $group_data['is_editor'] = 'correct'; } if ($group == 'chief') { $group_data['is_chief'] = 'correct'; } } } // 在原来的基础上减少角色时 // 传入角色比原来角色数量少说明减少 if (count($groups) < count($row_group)) { foreach ($row_group as $item) { if (!in_array($item, $groups)) { if ($item == 'author') { $group_data['is_author'] = 'fault'; } if ($item == 'review') { $group_data['is_review'] = 'fault'; } if ($item == 'editor') { $group_data['is_editor'] = 'fault'; } if ($item == 'chief') { $group_data['is_chief'] = 'fault'; } } } } } // 对应的角色信息插入到 foreach ($group_data as $key => $item) { $params[$key] = $item; if ($key == 'is_author') { $type = 'author'; } if ($key == 'is_review') { $type = 'review'; } // 如果是新增编辑角色则需要判断是否拥有主编,如拥有主编则需要去除掉 if ($key == 'is_editor') { $type = 'editor'; if ($row->is_chief == 'correct') { $params['is_chief'] = 'fault'; } } // 如果是新增主编角色则需要判断是否拥有编辑,如拥有编辑则需要去除掉 if ($key == 'is_chief') { $type = 'chief'; if ($row->is_editor == 'correct') { $params['is_editor'] = 'fault'; } // 判断如果选择了主编则需要填写期刊 if (empty($params['journal'])) { $this->error(__('Please select a journal')); } // 对应期刊绑定主编 $channel = Channel::get($params['journal']); $channel->chief_id = $row->id; $channel->save(); } // 增加user_role_content数据 if ($item == 'correct') { // 如果是增加角色则需要添加user_role_log 和 user_role_content $user_role_log_model = new UserRoleLog(); $user_role_log_model->user_id = $row->id; $user_role_log_model->type = $type; $user_role_log_model->is_adopt = $item; if ($user_role_log_model->save()) { $user_role_model = new UserRoleContent(); $user_role_model->user_id = $row->id; $user_role_model->log_id = $user_role_log_model->id; $user_role_model->type = $type; $user_role_model->affiliation = $row->affiliation; $user_role_model->save(); } } else { // 如果是删减角色则需要删除user_role_log 和 user_role_content $user_role_log_model = UserRoleLog::where(['user_id' => $row->id, 'type' => $type])->select(); if (count($user_role_log_model) > 0) { foreach ($user_role_log_model as $user_log) { $user_role_model = UserRoleContent::where(['user_id' => $row->id, 'type' => $type, 'log_id' => $user_log->id])->select(); $user_log->delete(); if (count($user_role_model) > 0) { foreach ($user_role_model as $user_role) { $user_role->delete(); } } } } } } $result = $row->allowField(true)->save($params); Db::commit(); } catch (ValidateException|PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if (false === $result) { $this->error(__('No rows were updated')); } $this->success(); } /** * 删除 */ public function del($ids = "") { if (!$this->request->isPost()) { $this->error(__("Invalid parameters")); } $ids = $ids ? $ids : $this->request->post("ids"); $row = $this->model->get($ids); $this->modelValidate = true; if (!$row) { $this->error(__('No Results were found')); } Auth::instance()->delete($row['id']); $this->success(); } /** * 获取作者列表 * * @return string|\think\response\Json * @throws \think\Exception * @throws \think\exception\DbException */ public function getAuthorList() { //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); if ($this->request->isAjax()) { list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $list = $this->model ->with('group') ->where(['user.is_author' => 'correct']) ->where($where) ->order($sort, $order) ->paginate($limit); foreach ($list as $k => $v) { $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname); $v->hidden(['password', 'salt']); } $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } return $this->view->fetch(); } }