123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- <?php
- namespace app\admin\controller\user;
- use app\admin\model\cms\Channel;
- use app\common\controller\Backend;
- use app\common\library\Auth;
- use app\common\model\UserRoleContent;
- use app\common\model\UserRoleLog;
- use think\Db;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- /**
- * 会员管理
- *
- * @icon fa fa-user
- */
- class User extends Backend
- {
- protected $relationSearch = true;
- protected $searchFields = 'id,username,nickname';
- protected $noNeedRight = ['getAuthorList'];
- /**
- * @var \app\admin\model\User
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->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();
- }
- }
|