PcAuthRule.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace app\admin\model;
  3. use think\Cache;
  4. use think\Model;
  5. class PcAuthRule 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 gettypeList()
  39. {
  40. return ['1' => __('Type 1'), '2' => __('Type 2'), '3' => __('Type 3')];
  41. }
  42. public function setPyAttr($value, $data)
  43. {
  44. if (isset($data['title']) && $data['title']) {
  45. return self::$pinyin->abbr(__($data['title']));
  46. }
  47. return '';
  48. }
  49. public function setPinyinAttr($value, $data)
  50. {
  51. if (isset($data['title']) && $data['title']) {
  52. return self::$pinyin->permalink(__($data['title']), '');
  53. }
  54. return '';
  55. }
  56. }