瀏覽代碼

项目用户搜索

laiguoran 5 年之前
父節點
當前提交
ef6863dd9b
共有 5 個文件被更改,包括 165 次插入3 次删除
  1. 15 0
      app/base/base_service.js
  2. 13 3
      app/controller/setting_controller.js
  3. 91 0
      app/lib/sql_builder.js
  4. 38 0
      app/service/project_account.js
  5. 8 0
      app/view/setting/user.ejs

+ 15 - 0
app/base/base_service.js

@@ -174,6 +174,21 @@ class BaseService extends Service {
     }
 
     /**
+     * 获取数据条数(SqlBuilder版本)
+     *
+     * @return {Array} - 返回分页数据
+     */
+    async getCountWithBuilder() {
+        // 由于egg-mysql不提供like、不等于操作,所以需要自己组sql
+        if (this.sqlBuilder === null) {
+            return [];
+        }
+        const [sql, sqlParam] = this.sqlBuilder.buildCount(this.tableName);
+        const result = await this.db.query(sql, sqlParam);
+        return result ? result.length : 0;
+    }
+
+    /**
      * 获取分页数据
      *
      * @param {Object} condition - 搜索条件

+ 13 - 3
app/controller/setting_controller.js

@@ -90,14 +90,22 @@ module.exports = app => {
                 if (ctx.session.sessionUser.is_admin === 0) {
                     throw '没有访问权限';
                 }
+                let keyword = '';
+                if (ctx.query.keyword) {
+                    keyword = ctx.query.keyword;
+                }
 
                 const page = ctx.page;
 
+                // 过滤数据
+                ctx.service.projectAccount.searchFilter(ctx.request.query, projectId);
+                const total = await ctx.service.projectAccount.getCountWithBuilder();
+
                 // 获取数据规则
                 // const rule = ctx.service.projectAccount.rule('updateUser');
                 // const frontRule = ctx.helper.validateConvert(rule);
 
-                const total = await ctx.service.projectAccount.count({ project_id: projectId });
+                // const total = await ctx.service.projectAccount.count({ project_id: projectId });
 
                 // 获取项目用户列表
                 // const accountData = await ctx.service.projectAccount.getAllDataByCondition({
@@ -106,8 +114,9 @@ module.exports = app => {
                 //     limit: (app.config.pageSize * (page - 1)),
                 //     offset: app.config.pageSize,
                 // });
-                const columns = ['id', 'account', 'name', 'company', 'role', 'mobile', 'auth_mobile', 'telephone', 'enable', 'is_admin', 'account_group', 'bind'];
-                const accountData = await ctx.service.projectAccount.getListByProjectId(columns, projectId);
+                // const columns = ['id', 'account', 'name', 'company', 'role', 'mobile', 'auth_mobile', 'telephone', 'enable', 'is_admin', 'account_group', 'bind'];
+                // const accountData = await ctx.service.projectAccount.getListByProjectId(columns, projectId);
+                const accountData = await ctx.service.projectAccount.getListWithBuilder();
 
                 // 分页相关
                 const pageInfo = {
@@ -122,6 +131,7 @@ module.exports = app => {
                     accountGroup,
                     permission,
                     pageInfo,
+                    keyword,
                     // rule: JSON.stringify(frontRule),
                 };
                 await this.layout('setting/user.ejs', renderData, 'setting/user_modal.ejs');

+ 91 - 0
app/lib/sql_builder.js

@@ -33,6 +33,20 @@ class SqlBuilder {
     }
 
     /**
+     * 设置新的orWhere数据
+     *
+     * @param {String} field - where中的字段名称
+     * @param {Object} data - where中的value部分
+     * @return {void}
+     */
+    setNewOrWhere(data) {
+        if (Object.keys(data).length <= 0) {
+            return;
+        }
+        this.newOrWhere.push(data);
+    }
+
+    /**
      * 更新字段设置
      *
      * @param {String} field - set的字段
@@ -54,6 +68,7 @@ class SqlBuilder {
     resetCondition() {
         this.andWhere = [];
         this.orWhere = [];
+        this.newOrWhere = [];
         this.columns = [];
         this.limit = -1;
         this.offset = -1;
@@ -147,6 +162,9 @@ class SqlBuilder {
                 this.sqlParam.push(where.field);
                 // 赋值参数
                 this.sqlParam.push.apply(this.sqlParam, where.data.value);
+            } else if (where.data.operate === 'find_in_set') {
+                whereArr.push(' find_in_set (' + where.data.value + ', ?? )');
+                this.sqlParam.push(where.field);
             } else {
                 // 普通操作
                 whereArr.push(' ?? ' + where.data.operate + ' ' + where.data.value);
@@ -179,6 +197,51 @@ class SqlBuilder {
     }
 
     /**
+     * orWhere设置-旧的不好用,独立写了一个包含括号的or兼容,传对象数组,
+     * 例:
+     * [{ field: 'office_share', value: managerSession.office, operate: 'find_in_set'},
+     * {field: 'office',value: managerSession.office,operate: '='},......]
+     *
+     * @return {void}
+     */
+    _newOrWhereBuild() {
+        if (this.newOrWhere.length <= 0) {
+            return;
+        }
+
+        const whereArr = [];
+        for (const oneOw in this.newOrWhere) {
+            for (const where of this.newOrWhere[oneOw]) {
+                if (where.operate === 'in') {
+                    // in操作
+                    const valueLength = where.value instanceof Array ? where.value.length : 1;
+                    // 生成参数集
+                    const inArr = [];
+                    for (let i = 0; i < valueLength; i++) {
+                        inArr.push('?');
+                    }
+                    const inData = inArr.join(',');
+                    whereArr.push(' ?? IN (' + inData + ')');
+                    this.sqlParam.push(where.field);
+                    // 赋值参数
+                    this.sqlParam.push.apply(this.sqlParam, where.value);
+                } else if (where.operate === 'find_in_set') {
+                    whereArr.push(' find_in_set (' + where.value + ', ?? )');
+                    this.sqlParam.push(where.field);
+                } else {
+                    // 普通操作
+                    whereArr.push(' ?? ' + where.operate + ' ' + where.value);
+                    this.sqlParam.push(where.field);
+                }
+            }
+            let whereString = whereArr.join(' OR ');
+            whereString = '(' + whereString + ')';
+            // 如果前面已经有设置过WHERE则不需要再重复添加
+            this.sql += this.sql.indexOf('WHERE') > 0 ? ' AND ' + whereString : ' WHERE ' + whereString;
+        }
+    }
+
+    /**
      * limit设置
      *
      * @return {void}
@@ -230,6 +293,7 @@ class SqlBuilder {
         this._setDataBuild();
         this._andWhereBuild();
         this._orWhereBuild();
+        this._newOrWhereBuild();
 
         this._orderByBuild();
         this._limitBuild();
@@ -242,5 +306,32 @@ class SqlBuilder {
         return [sql, sqlParam];
     }
 
+    /**
+     * 构建sql
+     *
+     * @param {String} tableName - 表名
+     * @param {String} type - 类型
+     * @return {Array} - 返回数组,第一个元素为sql语句,第二个元素为sql中问号部分的param
+     */
+    buildCount(tableName, type = 'select') {
+        this.type = type;
+        this._typeBuild(tableName);
+        if (this.sql === '') {
+            throw '类型错误';
+        }
+
+        this._setDataBuild();
+        this._andWhereBuild();
+        this._orWhereBuild();
+        this._newOrWhereBuild();
+
+        const sql = this.sql;
+        const sqlParam = this.sqlParam;
+        // 重置数据
+        // this.resetCondition();
+
+        return [sql, sqlParam];
+    }
+
 }
 module.exports = SqlBuilder;

+ 38 - 0
app/service/project_account.js

@@ -708,6 +708,44 @@ module.exports = app => {
         }
 
         /**
+         * 查询过虑
+         *
+         * @param {Object} data - 筛选表单中的get数据
+         * @return {void}
+         */
+        searchFilter(data, projectId) {
+            this.initSqlBuilder();
+            const columns = ['id', 'account', 'name', 'company', 'role', 'mobile', 'auth_mobile', 'telephone', 'enable', 'is_admin', 'account_group', 'bind'];
+            this.sqlBuilder.columns = columns;
+
+            this.sqlBuilder.setAndWhere('project_id', {
+                value: projectId,
+                operate: '=',
+            });
+
+            // 名字筛选
+            if (data.keyword !== undefined && data.keyword !== '') {
+                this.sqlBuilder.setNewOrWhere([{
+                    field: 'account',
+                    value: this.db.escape(data.keyword + '%'),
+                    operate: 'like',
+                }, {
+                    field: 'name',
+                    value: this.db.escape(data.keyword + '%'),
+                    operate: 'like',
+                }, {
+                    field: 'company',
+                    value: this.db.escape(data.keyword + '%'),
+                    operate: 'like',
+                }, {
+                    field: 'mobile',
+                    value: this.db.escape(data.keyword + '%'),
+                    operate: 'like',
+                }]);
+            }
+        }
+
+        /**
          * 账号绑定微信
          *
          * @param {String} id - 账号id

+ 8 - 0
app/view/setting/user.ejs

@@ -17,6 +17,14 @@
                 <nav class="nav nav-tabs m-3" role="tablist">
                     <a class="nav-item nav-link active" href="/setting/user">账号列表</a>
                     <a class="nav-item nav-link" href="/setting/user/permission/set">账号权限</a>
+                    <div class="ml-auto">
+                        <form class="input-group input-group-sm" method="get">
+                            <input type="text" class="form-control" value="<%- keyword %>" name="keyword" placeholder="账号/姓名/单位/手机 搜索" aria-label="账号/姓名/单位/手机 搜索" aria-describedby="button-addon2">
+                            <div class="input-group-append">
+                                <button class="btn btn-outline-primary" type="submit" id="button-addon2"><i class="fa fa-search"></i></button>
+                            </div>
+                        </form>
+                    </div>
                 </nav>
                 <div class="tab-content m-3">
                     <div id="user-list" class="tab-pane active">