Преглед изворни кода

用户管理添加角色修改

夏海 пре 1 година
родитељ
комит
6772f0c0d6

+ 196 - 6
application/admin/controller/user/User.php

@@ -4,6 +4,9 @@ namespace app\admin\controller\user;
 
 use app\common\controller\Backend;
 use app\common\library\Auth;
+use think\Db;
+use think\exception\PDOException;
+use think\exception\ValidateException;
 
 /**
  * 会员管理
@@ -27,6 +30,9 @@ class User extends Backend
         parent::_initialize();
         $this->model = model('User');
         $this->view->assign("typesList", $this->model->getTypesList());
+
+        $group_list = ['author' => __('Author'), 'review' => __('Review'), 'editor' => __('editor')];
+        $this->view->assign('group_list', $group_list);
     }
 
     /**
@@ -42,16 +48,23 @@ class User extends Backend
                 return $this->selectpage();
             }
             list($where, $sort, $order, $offset, $limit) = $this->buildparams();
-//            $filter = $this->request->get("filter", '');
-//            $filter = (array)json_decode($filter, true);
-//            $filter = $filter ? $filter : [];
-//            $type = $filter['type'];
 
-//            if ()
+            $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 == '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) {
@@ -101,8 +114,185 @@ class User extends Backend
         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']));
-        return parent::edit($ids);
+
+        $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';
+            }
+            $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';
+            }
+
+            // 用户选中的角色
+            $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';
+                }
+            }
+
+            // 用户身份
+            if (count($row_group) == 0 && count($groups) == 0) {
+                $group_data['is_author'] = 'fault';
+                $group_data['is_review'] = 'fault';
+                $group_data['is_editor'] = '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';
+                                }
+                            }
+                        }
+                    } 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 (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';
+                            }
+                        }
+                    }
+                    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 ($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 (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';
+                            }
+                        }
+                    }
+                }
+            }
+
+            // 对应的角色信息插入到
+            foreach ($group_data as $key => $item) {
+                $params[$key] = $item;
+            }
+
+            $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();
     }
 
     /**

+ 17 - 12
application/admin/lang/zh-cn/user/role_log.php

@@ -1,16 +1,21 @@
 <?php
 
 return [
-    'Pid'             => '父ID',
-    'Name'            => '规则',
-    'Title'           => '标题',
-    'Remark'          => '备注',
-    'Ismenu'          => '是否菜单',
-    'Change password' => '修改密码',
-    'Createtime'      => '创建时间',
-    'Updatetime'      => '更新时间',
-    'Menu tips'       => '规则任意,请不可重复,仅做层级显示,无需匹配控制器和方法',
-    'Node tips'       => '模块/控制器/方法名',
-    'Weigh'           => '权重',
-    'Status'          => '状态'
+    'Username'          => '用户名',
+    'Nickname'          => '昵称',
+    'Workplace'         => '工作场所',
+    'Job Type'          => '工作类型',
+    'Title'             => '称谓',
+    'First Name'        => '姓',
+    'Middle Name'       => '中间名',
+    'Last Name'         => '名',
+    'Facebook'          => '脸书',
+    'Twitter'           => '推特',
+    'Email'             => '邮箱',
+    'Affiliation'       => '附属',
+    'Address'           => '地址',
+    'Zip Code'          => '邮件编码',
+    'City'              => '城市',
+    'Country'           => '国家',
+    'Avatar'            => '头像',
 ];

+ 0 - 6
application/admin/view/user/role_log/detail.html

@@ -14,12 +14,6 @@
         </div>
     </div>
     <div class="form-group">
-        <label for="c-mobile" class="control-label col-xs-12 col-sm-2">{:__('Mobile')}:</label>
-        <div class="col-xs-12 col-sm-4">
-            <input id="c-mobile" data-rule="" class="form-control" name="row[mobile]" type="text" value="{$row.mobile|htmlentities}" readonly>
-        </div>
-    </div>
-    <div class="form-group">
         <label for="c-workplace" class="control-label col-xs-12 col-sm-2">{:__('Workplace')}:</label>
         <div class="col-xs-12 col-sm-4">
             <select id="c-workplace" class="form-control" name="row[workplace]" readonly>

+ 11 - 1
application/admin/view/user/user/edit.html

@@ -1,10 +1,20 @@
 <form id="edit-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
     {:token()}
     <input type="hidden" name="row[id]" value="{$row.id}">
+<!--    <div class="form-group">-->
+<!--        <label for="c-group_id" class="control-label col-xs-12 col-sm-2">{:__('Group')}:</label>-->
+<!--        <div class="col-xs-12 col-sm-4">-->
+<!--            {$groupList}-->
+<!--        </div>-->
+<!--    </div>-->
     <div class="form-group">
         <label for="c-group_id" class="control-label col-xs-12 col-sm-2">{:__('Group')}:</label>
         <div class="col-xs-12 col-sm-4">
-            {$groupList}
+            <select id="c-group_id" class="form-control selectpicker" multiple="" name="row[group][]">
+                {foreach name="group_list" item="vo"}
+                <option value="{$key}" {in name="key" value="$row.group"}selected{/in}>{$vo}</option>
+                {/foreach}
+            </select>
         </div>
     </div>
     <div class="form-group">

+ 1 - 1
application/admin/view/user/user/index.html

@@ -1,7 +1,7 @@
 <div class="panel panel-default panel-intro">
     <div class="panel-heading">
         {:build_heading(null,FALSE)}
-        <ul class="nav nav-tabs" data-field="type">
+        <ul class="nav nav-tabs">
             <li class="active"><a href="#t-all" data-value="" data-toggle="tab">{:__('All')}</a></li>
             {foreach name="typesList" item="vo"}
             <li><a href="#t-{$key}" data-value="{$key}" data-toggle="tab">{$vo}</a></li>

+ 7 - 1
application/api/controller/Cms.php

@@ -23,6 +23,12 @@ class Cms extends Api
         parent::_initialize();
     }
 
+    /**
+     * 获取期刊的录取率
+     *
+     * @return void
+     * @throws \think\Exception
+     */
     public function getChannelCycle()
     {
         $params = $this->request->param();
@@ -42,7 +48,7 @@ class Cms extends Api
             $channel_rate = $correct_manuscript_count / $manuscript_count * 100;
         }
 
-        $this->success('', $channel_rate ?? 0);
+        $this->success('', round($channel_rate, 2) ?? 0);
     }
 
     public function getChannelDiyname()

+ 20 - 0
public/assets/js/backend/user/user.js

@@ -47,6 +47,26 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
 
             // 为表格绑定事件
             Table.api.bindevent(table);
+
+            var cloneQueryParams = null;
+            //绑定tab事件
+            $('a[data-toggle="tab"]').on('show.bs.tab',function(event){
+                let type =$(this).attr("data-value");
+                let options = table.bootstrapTable('getOptions');
+                $('.nav-tabs li').attr('class','');
+                $(this).parent().attr("class",'active');
+                options.pageNumber =1;
+                if (cloneQueryParams == null) {
+                    cloneQueryParams = options.queryParams;
+                }
+
+                options.queryParams =function(params){
+                    params.type = type;
+                    return cloneQueryParams(params);
+                };
+                table.bootstrapTable('refresh',{});
+                return false;
+            });
         },
         add: function () {
             Controller.api.bindevent();