AdminRole.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace backendApi\modules\v1\models;
  3. use common\helpers\Cache;
  4. use common\helpers\Tool;
  5. use Yii;
  6. use yii\helpers\Json;
  7. use common\libs\logging\operate\valueType\Config as ValueTypeConfig;
  8. /**
  9. * This is the model class for table "{{%ADMIN_ROLE}}".
  10. *
  11. * @property string $ID
  12. * @property string $ROLE_NAME 角色名
  13. * @property string $REMARK 描述
  14. * @property string $PERMISSION 权限
  15. * @property string $COLUMN_PERMISSION 列表字段权限
  16. * @property int $DONT_DEL 不可删除
  17. * @property string $CREATE_ADMIN 创建管理员
  18. * @property string $UPDATE_ADMIN 更新管理员
  19. * @property int $CREATED_AT 创建时间
  20. * @property int $UPDATED_AT 更新时间
  21. */
  22. class AdminRole extends \common\components\ActiveRecord
  23. {
  24. /**
  25. * @inheritdoc
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%ADMIN_ROLE}}';
  30. }
  31. /**
  32. * @inheritdoc
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['ROLE_NAME', 'CREATED_AT'], 'required'],
  38. [['DONT_DEL', 'CREATED_AT', 'UPDATED_AT'], 'integer'],
  39. [['ID', 'CREATE_ADMIN', 'UPDATE_ADMIN'], 'string', 'max' => 32],
  40. [['ROLE_NAME', 'REMARK'], 'string', 'max' => 255],
  41. [['COLUMN_PERMISSION'], 'string'],
  42. [['ROLE_NAME'], 'unique'],
  43. [['ID'], 'unique'],
  44. ];
  45. }
  46. /**
  47. * @inheritdoc
  48. */
  49. public function attributeLabels()
  50. {
  51. return [
  52. 'ID' => 'ID',
  53. 'ROLE_NAME' => '角色名',
  54. 'REMARK' => '描述',
  55. 'PERMISSION' => '权限',
  56. 'COLUMN_PERMISSION' => '列表字段权限',
  57. 'DONT_DEL' => '不可删除',
  58. 'CREATE_ADMIN' => '创建管理员',
  59. 'UPDATE_ADMIN' => '更新管理员',
  60. 'CREATED_AT' => '创建时间',
  61. 'UPDATED_AT' => '更新时间',
  62. ];
  63. }
  64. /**
  65. * 获取全部配置,把育成津贴奖金比例解成数组
  66. * @return array|\yii\db\ActiveRecord[]
  67. */
  68. public static function getAllData(){
  69. $allData = static::find()->where('1=1')->orderBy('CREATED_AT ASC')->indexBy('ID')->asArray()->all();
  70. foreach($allData as $key=>$data){
  71. // 暂时先从文件中取内容
  72. $permissionPath = \Yii::getAlias('@common/runtime/permission/').$data['ID'];
  73. if(!file_exists($permissionPath)){
  74. $allData[$key]['PERMISSION'] = null;
  75. } else {
  76. $permission = file_get_contents($permissionPath);
  77. $allData[$key]['PERMISSION'] = Json::decode($permission);
  78. }
  79. $columnPermission = is_resource($data['COLUMN_PERMISSION']) ? stream_get_contents($data['COLUMN_PERMISSION']) : null;
  80. $allData[$key]['COLUMN_PERMISSION'] = $columnPermission ? Json::decode(base64_decode($columnPermission)) : null;
  81. }
  82. return $allData;
  83. }
  84. /**
  85. * 从缓存获取信息
  86. * @return array|mixed|\yii\db\ActiveRecord[]
  87. */
  88. public static function getFromCache(){
  89. $data = Yii::$app->cache->get(Cache::ADMIN_ROLE_KEY);
  90. if(!$data){
  91. // 获取信息
  92. $data = self::getAllData();
  93. Yii::$app->cache->set(Cache::ADMIN_ROLE_KEY, $data);
  94. }
  95. return $data;
  96. }
  97. /**
  98. * 更新缓存
  99. * @return array|\yii\db\ActiveRecord[]
  100. */
  101. public static function updateToCache(){
  102. // 获取配置
  103. $data = self::getAllData();
  104. Yii::$app->cache->set(Cache::ADMIN_ROLE_KEY, $data);
  105. return $data;
  106. }
  107. /**
  108. * 操作日志记录条件
  109. * @return array
  110. */
  111. public function attrLabelsWithLogType(){
  112. return [
  113. 'ROLE_NAME' => '角色名称',
  114. 'REMARK' => '描述',
  115. 'PERMISSION' => '权限',
  116. 'COLUMN_PERMISSION' => [
  117. 'label' => '列表字段权限',
  118. 'type' => function($data){
  119. $value = $data['value'];
  120. $columnPermission = is_resource($value) ? stream_get_contents($value) : null;
  121. return $columnPermission ? Json::decode(base64_decode($columnPermission)) : null;
  122. },
  123. ],
  124. 'CREATE_ADMIN' => [
  125. 'label' => '创建人',
  126. 'type' => function($data){
  127. $value = $data['value'];
  128. $result = Admin::findOneAsArray('ID=:ID', [':ID'=>$value], 'ADMIN_NAME');
  129. return $result['ADMIN_NAME'];
  130. },
  131. ],
  132. 'UPDATE_ADMIN' => [
  133. 'label' => '修改人',
  134. 'type' => function($data){
  135. $value = $data['value'];
  136. $result = Admin::findOneAsArray('ID=:ID', [':ID'=>$value], 'ADMIN_NAME');
  137. return $result['ADMIN_NAME'];
  138. },
  139. ],
  140. 'CREATED_AT' => [
  141. 'label' => '创建时间',
  142. 'type' => ValueTypeConfig::DATE_TIME_TYPE,
  143. ],
  144. 'UPDATED_AT' => [
  145. 'label' => '修改时间',
  146. 'type' => ValueTypeConfig::DATE_TIME_TYPE,
  147. ],
  148. ];
  149. }
  150. /**
  151. * 获取全部可供设置权限的列表的字段列
  152. * @return array
  153. */
  154. public static function getAllRoleColumn(){
  155. // 遍历/Volumes/HDD/www/www.anran.leo/backendApi/modules/v1/models/lists/user/目录下的所有文件
  156. $allColumns = [];
  157. $dirPath = __DIR__.'/lists';
  158. $nameSpace = '\backendApi\modules\v1\models\lists\\';
  159. $allListClassFiles = Tool::dirFiles($dirPath);
  160. foreach($allListClassFiles as $classDir => $classFiles){
  161. if(is_array($classFiles)){
  162. foreach($classFiles as $classFile){
  163. $pathInfo = pathinfo($classFile);
  164. $className = $nameSpace.$classDir.'\\'.$pathInfo['filename'];
  165. $listModuleArr = explode('\\', $className);
  166. $listModuleArrCount = count($listModuleArr);
  167. $listModule = $listModuleArr[$listModuleArrCount-2].'/'.$listModuleArr[$listModuleArrCount-1];
  168. $listObj = new $className;
  169. $allColumns[] = [
  170. 'listClass' => $listModule,
  171. 'listName' => $listObj->getListName(),
  172. 'columns' => $listObj->getAllColumnHeaderName(),
  173. ];
  174. }
  175. }
  176. }
  177. return $allColumns;
  178. }
  179. }