BitController 通用控制器

BitController 是辅助框架的主控制器,使用辅助处理则需要继承该控制器

  1. use think\bit\common\BitController;
  2. class Base extends BitController
  3. {
  4. // customize
  5. }

通用属性

  • model string

模型名称

  1. protected $model;
  • post array

请求包含的数据

  1. protected $post = [];

获取列表数据请求属性

  • origin_lists_default_validate array

默认列表数据验证

  1. protected $origin_lists_default_validate = [
  2. 'where' => 'array'
  3. ];
  • origin_lists_before_result array

默认前置返回结果

  1. protected $origin_lists_before_result = [
  2. 'error' => 1,
  3. 'msg' => 'error:before_fail'
  4. ];
  • origin_lists_condition array

列表查询条件

  1. protected $origin_lists_condition = [];
  • origin_lists_condition_query Closure|null

列表查询闭包条件

  1. protected $origin_lists_condition_query = null;
  • origin_lists_orders string

列表数据排序

  1. protected $origin_lists_orders = 'create_time desc';
  • origin_lists_field array

列表数据限制字段

  1. protected $origin_lists_field = ['update_time,create_time', true];

获取分页数据请求属性

  • lists_default_validate array

分页数据默认验证器

  1. protected $lists_default_validate = [
  2. 'page' => 'require',
  3. 'page.limit' => 'require|number|between:1,50',
  4. 'page.index' => 'require|number|min:1',
  5. 'where' => 'array'
  6. ];
  • lists_before_result array

分页数据前置返回结果

  1. protected $lists_before_result = [
  2. 'error' => 1,
  3. 'msg' => 'error:before_fail'
  4. ];
  • lists_condition array

分页数据条件查询

  1. protected $lists_condition = [];
  • lists_condition_query Closure|null

分页数据查询闭包条件

  1. protected $lists_condition_query = null;
  • lists_orders string

分页数据排序

  1. protected $lists_orders = 'create_time desc';
  • lists_field array

分页数据限制字段

  1. protected $lists_field = ['update_time,create_time', true];

获取单条数据请求属性

  • get_default_validate array

单条数据默认验证器

  1. protected $get_default_validate = [
  2. 'id' => 'requireWithout:where|number',
  3. 'where' => 'requireWithout:id|array'
  4. ];
  • get_before_result array

单条数据前置返回结果

  1. protected $get_before_result = [
  2. 'error' => 1,
  3. 'msg' => 'error:before_fail'
  4. ];
  • get_condition array

单条数据查询条件

  1. protected $get_condition = [];
  • get_field array

单条数据限制字段

  1. protected $get_field = ['update_time,create_time', true];

新增数据请求属性

  • add_default_validate array

新增数据默认验证器

  1. protected $add_default_validate = [];
  • add_before_result array

新增数据前置返回结果

  1. protected $add_before_result = [
  2. 'error' => 1,
  3. 'msg' => 'error:before_fail'
  4. ];
  • add_after_result array

新增数据后置返回结果

  1. protected $add_after_result = [
  2. 'error' => 1,
  3. 'msg' => 'error:after_fail'
  4. ];
  • add_fail_result array

新增数据失败返回结果

  1. protected $add_fail_result = [
  2. 'error' => 1,
  3. 'msg' => 'error:insert_fail'
  4. ];

修改数据请求属性

  • edit_default_validate array

编辑默认验证器

  1. protected $edit_default_validate = [
  2. 'id' => 'require|number',
  3. 'switch' => 'require|bool'
  4. ];
  • edit_switch boolean

是否仅为状态编辑

  1. protected $edit_switch = false;
  • edit_before_result array

编辑前置返回结果

  1. protected $edit_before_result = [
  2. 'error' => 1,
  3. 'msg' => 'error:before_fail'
  4. ];
  • edit_condition array

编辑查询条件

  1. protected $edit_condition = [];
  • edit_fail_result array

编辑失败返回结果

  1. protected $edit_fail_result = [
  2. 'error' => 1,
  3. 'msg' => 'error:fail'
  4. ];
  • edit_after_result array

编辑后置返回结果

  1. protected $edit_after_result = [
  2. 'error' => 1,
  3. 'msg' => 'error:after_fail'
  4. ];

删除数据请求属性

  • delete_default_validate array

删除默认验证器

  1. protected $delete_default_validate = [
  2. 'id' => 'require'
  3. ];
  • delete_before_result array

删除前置返回结果

  1. protected $delete_before_result = [
  2. 'error' => 1,
  3. 'msg' => 'error:before_fail'
  4. ];
  • delete_condition array

删除查询条件

  1. protected $delete_condition = [];
  • delete_prep_result array

事务开始之后数据写入之前返回结果

  1. protected $delete_prep_result = [
  2. 'error' => 1,
  3. 'msg' => 'error:prep_fail'
  4. ];
  • delete_fail_result array

删除失败返回结果

  1. protected $delete_fail_result = [
  2. 'error' => 1,
  3. 'msg' => 'error:fail'
  4. ];
  • delete_after_result array

删除后置返回结果

  1. protected $delete_after_result = [
  2. 'error' => 1,
  3. 'msg' => 'error:after_fail'
  4. ];

Bedis 缓存类

使用 Bedis 缓存类为接口定义 HASH 缓存

  1. class ApiHash extends Bedis
  2. {
  3. protected $key = 'api_hash';
  4. function refresh()
  5. {
  6. $this->redis->del($this->key);
  7. $lists = Db::name('api')->where([
  8. 'status' => 1
  9. ])->column('id', 'api');
  10. if (empty($lists)) return true;
  11. return $this->redis->hMSet($this->key, $lists);
  12. }
  13. public function get($api)
  14. {
  15. try {
  16. if (!$this->redis->exists($this->key)) $this->refresh();
  17. return $this->redis->hGet($this->key, $api);
  18. } catch (\Exception $e) {
  19. return '';
  20. }
  21. }
  22. }

当接口数据发生更新时则可以使用 refresh 函数将缓存刷新

  1. $api = new ApiHash();
  2. $api->refresh();

*缓存的生产设定不建议使用组合数据或一对一来生成,这样会提高数据的耦合度,增大开发与维护的难度*

通过接口缓存获取对应的接口主键

  1. $api = new ApiHash();
  2. $api->get('admin/get');

如果使用事务则实现化时赋值参数

  1. Redis::transaction(function (\Redis $redis) {
  2. $api = new ApiHash($redis);
  3. $router = new RouterHash($redis);
  4. return ($api->refresh() && $router->refresh());
  5. });
  6. // true or false