AdminRole.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. 'ADMIN_NAME' => '管理员名字'
  63. ];
  64. }
  65. /**
  66. * 获取全部配置,把育成津贴奖金比例解成数组
  67. * @return array|\yii\db\ActiveRecord[]
  68. */
  69. public static function getAllData(){
  70. $allData = static::find()->where('1=1')->orderBy('CREATED_AT ASC')->indexBy('ID')->asArray()->all();
  71. foreach($allData as $key=>$data){
  72. // 暂时先从文件中取内容
  73. $permissionPath = \Yii::getAlias('@common/runtime/permission/').$data['ID'];
  74. if(!file_exists($permissionPath)){
  75. $allData[$key]['PERMISSION'] = null;
  76. } else {
  77. $permission = file_get_contents($permissionPath);
  78. $allData[$key]['PERMISSION'] = Json::decode($permission);
  79. }
  80. $columnPermission = is_resource($data['COLUMN_PERMISSION']) ? stream_get_contents($data['COLUMN_PERMISSION']) : null;
  81. $allData[$key]['COLUMN_PERMISSION'] = $columnPermission ? Json::decode(base64_decode($columnPermission)) : null;
  82. }
  83. return $allData;
  84. }
  85. /**
  86. * 从缓存获取信息
  87. * @return array|mixed|\yii\db\ActiveRecord[]
  88. */
  89. public static function getFromCache(){
  90. $data = Yii::$app->cache->get(Cache::ADMIN_ROLE_KEY);
  91. if(!$data){
  92. // 获取信息
  93. $data = self::getAllData();
  94. Yii::$app->cache->set(Cache::ADMIN_ROLE_KEY, $data);
  95. }
  96. return $data;
  97. }
  98. /**
  99. * 更新缓存
  100. * @return array|\yii\db\ActiveRecord[]
  101. */
  102. public static function updateToCache(){
  103. // 获取配置
  104. $data = self::getAllData();
  105. Yii::$app->cache->set(Cache::ADMIN_ROLE_KEY, $data);
  106. return $data;
  107. }
  108. /**
  109. * 操作日志记录条件
  110. * @return array
  111. */
  112. public function attrLabelsWithLogType(){
  113. return [
  114. 'ROLE_NAME' => '角色名称',
  115. 'REMARK' => '描述',
  116. 'PERMISSION' => '权限',
  117. 'COLUMN_PERMISSION' => [
  118. 'label' => '列表字段权限',
  119. 'type' => function($data){
  120. $value = $data['value'];
  121. $columnPermission = is_resource($value) ? stream_get_contents($value) : null;
  122. return $columnPermission ? Json::decode(base64_decode($columnPermission)) : null;
  123. },
  124. ],
  125. 'CREATE_ADMIN' => [
  126. 'label' => '创建人',
  127. 'type' => function($data){
  128. $value = is_array($data) && isset($data['value']) ? $data['value'] : '';
  129. $result = Admin::findOneAsArray('ID=:ID', [':ID'=>$value], 'ADMIN_NAME');
  130. return !empty($result) ? $result['ADMIN_NAME'] : '';
  131. },
  132. ],
  133. 'UPDATE_ADMIN' => [
  134. 'label' => '修改人',
  135. 'type' => function($data){
  136. $value = isset($data['value']) ? $data['value']:'';
  137. $result = Admin::findOneAsArray('ID=:ID', [':ID'=>$value], 'ADMIN_NAME');
  138. return !empty($result) ? $result['ADMIN_NAME'] : 'ADMIN';
  139. },
  140. ],
  141. 'CREATED_AT' => [
  142. 'label' => '创建时间',
  143. 'type' => ValueTypeConfig::DATE_TIME_TYPE,
  144. ],
  145. 'UPDATED_AT' => [
  146. 'label' => '修改时间',
  147. 'type' => ValueTypeConfig::DATE_TIME_TYPE,
  148. ],
  149. ];
  150. }
  151. /**
  152. * 获取全部可供设置权限的列表的字段列
  153. * @return array
  154. */
  155. public static function getAllRoleColumn(){
  156. // 遍历/Volumes/HDD/www/www.anran.leo/backendApi/modules/v1/models/lists/user/目录下的所有文件
  157. $allColumns = [];
  158. $dirPath = __DIR__.'/lists';
  159. $nameSpace = '\backendApi\modules\v1\models\lists\\';
  160. $allListClassFiles = Tool::dirFiles($dirPath);
  161. foreach($allListClassFiles as $classDir => $classFiles){
  162. if(is_array($classFiles)){
  163. foreach($classFiles as $classFile){
  164. $pathInfo = pathinfo($classFile);
  165. $className = $nameSpace.$classDir.'\\'.$pathInfo['filename'];
  166. $listModuleArr = explode('\\', $className);
  167. $listModuleArrCount = count($listModuleArr);
  168. $listModule = $listModuleArr[$listModuleArrCount-2].'/'.$listModuleArr[$listModuleArrCount-1];
  169. $listObj = new $className;
  170. $allColumns[] = [
  171. 'listClass' => $listModule,
  172. 'listName' => $listObj->getListName(),
  173. 'columns' => $listObj->getAllColumnHeaderName(),
  174. ];
  175. }
  176. }
  177. }
  178. return $allColumns;
  179. }
  180. }