Key.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace app\admin\controller\manage;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use think\Controller;
  6. use think\Request;
  7. /**
  8. * 功能性管理
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Key extends Backend
  13. {
  14. /**
  15. * ManageKey模型对象
  16. */
  17. protected $model = null;
  18. /**
  19. * 无需鉴权的方法,但需要登录
  20. * @var array
  21. */
  22. protected $noNeedRight = ['check_key_exists'];
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = model('ManageKey');
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. /**
  34. * 检查KEY是否存在
  35. * @return mixed|string
  36. */
  37. public function check_key_exists(){
  38. if ($this->request->isAjax()) {
  39. $params = $this->request->post('row/a');
  40. $exists = $this->model->where($params)->field('key')->find();
  41. if($exists){
  42. return $this->error(__('The key has already existed'));
  43. }else{
  44. return $this->success();
  45. }
  46. }
  47. }
  48. /**
  49. * 编辑
  50. */
  51. public function edit($ids = NULL)
  52. {
  53. $row = $this->model->get($ids);
  54. if (!$row)
  55. $this->error(__('No Results were found'));
  56. $adminIds = $this->getDataLimitAdminIds();
  57. if (is_array($adminIds))
  58. {
  59. if (!in_array($row[$this->dataLimitField], $adminIds))
  60. {
  61. $this->error(__('You have no permission'));
  62. }
  63. }
  64. if ($this->request->isPost())
  65. {
  66. $redis = Redis::instance();
  67. $params = $this->request->post("row/a");
  68. if ($params)
  69. {
  70. /*
  71. * 已经弃用,如果为了兼容老版可取消注释
  72. foreach ($params as $k => &$v)
  73. {
  74. $v = is_array($v) ? implode(',', $v) : $v;
  75. }
  76. */
  77. try
  78. {
  79. //是否采用模型验证
  80. if ($this->modelValidate)
  81. {
  82. $name = basename(str_replace('\\', '/', get_class($this->model)));
  83. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  84. $row->validate($validate);
  85. }
  86. $result = $row->allowField(true)->save($params);
  87. if ($result !== false)
  88. {
  89. $key = 'K:' . $params['key'];
  90. $redis->del($key);
  91. $this->success();
  92. }
  93. else
  94. {
  95. $this->error($row->getError());
  96. }
  97. }
  98. catch (\think\exception\PDOException $e)
  99. {
  100. $this->error($e->getMessage());
  101. }
  102. }
  103. $this->error(__('Parameter %s can not be empty', ''));
  104. }
  105. $this->view->assign("row", $row);
  106. return $this->view->fetch();
  107. }
  108. /**
  109. * 删除
  110. */
  111. public function del($ids = "")
  112. {
  113. $redis = Redis::instance();
  114. if ($ids)
  115. {
  116. $pk = $this->model->getPk();
  117. $adminIds = $this->getDataLimitAdminIds();
  118. if (is_array($adminIds))
  119. {
  120. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  121. }
  122. $list = $this->model->where($pk, 'in', $ids)->select();
  123. $count = 0;
  124. foreach ($list as $k => $v)
  125. {
  126. $key = 'K:' . $v['key'];
  127. $redis->del($key);
  128. $count += $v->delete();
  129. }
  130. if ($count)
  131. {
  132. $this->success();
  133. }
  134. else
  135. {
  136. $this->error(__('No rows were deleted'));
  137. }
  138. }
  139. $this->error(__('Parameter %s can not be empty', 'ids'));
  140. }
  141. }