ActionBeginBehavior.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace app\admin\behavior;
  3. /**
  4. * 系统行为扩展:新增/更新/删除之后的后置操作
  5. */
  6. load_trait('controller/Jump');
  7. class ActionBeginBehavior {
  8. use \traits\controller\Jump;
  9. protected static $actionName;
  10. protected static $controllerName;
  11. protected static $moduleName;
  12. protected static $method;
  13. /**
  14. * 构造方法
  15. * @param Request $request Request对象
  16. * @access public
  17. */
  18. public function __construct()
  19. {
  20. }
  21. // 行为扩展的执行入口必须是run
  22. public function run(&$params){
  23. self::$actionName = request()->action();
  24. self::$controllerName = request()->controller();
  25. self::$moduleName = request()->module();
  26. self::$method = request()->method();
  27. // file_put_contents ( DATA_PATH."log.txt", date ( "Y-m-d H:i:s" ) . " " . var_export('admin_AfterSaveBehavior',true) . "\r\n", FILE_APPEND );
  28. $this->_initialize();
  29. }
  30. private function _initialize() {
  31. if ('POST' == self::$method) {
  32. $this->checkRepeatTitle();
  33. $this->clearWeapp();
  34. }
  35. }
  36. /**
  37. * 插件每次post提交都清除插件相关缓存
  38. * @access private
  39. */
  40. private function clearWeapp()
  41. {
  42. /*只有相应的控制器和操作名才执行,以便提高性能*/
  43. $ctlActArr = array(
  44. 'Weapp@*',
  45. );
  46. $ctlActStr = self::$controllerName.'@*';
  47. if (in_array($ctlActStr, $ctlActArr)) {
  48. \think\Cache::clear('hooks');
  49. }
  50. /*--end*/
  51. }
  52. /**
  53. * 发布或编辑时,检测文档标题的重复性
  54. * @access private
  55. */
  56. private function checkRepeatTitle()
  57. {
  58. /*只有相应的控制器和操作名才执行,以便提高性能*/
  59. $ctlArr = \think\Db::name('channeltype')->field('id,ctl_name,is_repeat_title')
  60. ->where('nid','NOT IN', ['guestbook','single'])
  61. ->getAllWithIndex('ctl_name');
  62. $actArr = ['add','edit'];
  63. if (!empty($ctlArr[self::$controllerName]) && in_array(self::$actionName, $actArr)) {
  64. /*模型否开启文档重复标题的检测*/
  65. if (empty($ctlArr[self::$controllerName]['is_repeat_title'])) {
  66. $map = array(
  67. 'title' => $_POST['title'],
  68. );
  69. if ('edit' == self::$actionName) {
  70. $map['aid'] = ['NEQ', $_POST['aid']];
  71. }
  72. $count = \think\Db::name('archives')->where($map)->count('aid');
  73. if(!empty($count)){
  74. $this->error('该标题已存在,请更改');
  75. }
  76. }
  77. /*--end*/
  78. }
  79. /*--end*/
  80. }
  81. }