AuthRule.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace app\admin\model;
  3. use think\Cache;
  4. use think\Model;
  5. class AuthRule extends Model
  6. {
  7. // 开启自动写入时间戳字段
  8. protected $autoWriteTimestamp = 'int';
  9. // 定义时间戳字段名
  10. protected $createTime = 'createtime';
  11. protected $updateTime = 'updatetime';
  12. // 数据自动完成字段
  13. protected $insert = ['py', 'pinyin'];
  14. protected $update = ['py', 'pinyin'];
  15. // 拼音对象
  16. protected static $pinyin = null;
  17. protected static function init()
  18. {
  19. self::$pinyin = new \Overtrue\Pinyin\Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
  20. self::beforeWrite(function ($row) {
  21. if (isset($_POST['row']) && is_array($_POST['row']) && isset($_POST['row']['condition'])) {
  22. $originRow = $_POST['row'];
  23. $row['condition'] = $originRow['condition'] ?? '';
  24. }
  25. });
  26. self::afterWrite(function ($row) {
  27. Cache::rm('__menu__');
  28. });
  29. }
  30. public function getTitleAttr($value, $data)
  31. {
  32. return __($value);
  33. }
  34. public function getMenutypeList()
  35. {
  36. return ['addtabs' => __('Addtabs'), 'dialog' => __('Dialog'), 'ajax' => __('Ajax'), 'blank' => __('Blank')];
  37. }
  38. public function setPyAttr($value, $data)
  39. {
  40. if (isset($data['title']) && $data['title']) {
  41. return self::$pinyin->abbr(__($data['title']));
  42. }
  43. return '';
  44. }
  45. public function setPinyinAttr($value, $data)
  46. {
  47. if (isset($data['title']) && $data['title']) {
  48. return self::$pinyin->permalink(__($data['title']), '');
  49. }
  50. return '';
  51. }
  52. }