YzRole.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 07/03/2017
  6. * Time: 10:40
  7. */
  8. namespace app\common\models\user;
  9. use app\common\models\BaseModel;
  10. use app\platform\modules\application\models\AppUser;
  11. use Illuminate\Database\Eloquent\SoftDeletes;
  12. class YzRole extends BaseModel
  13. {
  14. use SoftDeletes;
  15. public $table = 'yz_role';
  16. const ROLE_ENABLE = 2;
  17. const ROLE_DISABLE = 1;
  18. /**
  19. * 定义字段名
  20. * 可使
  21. * @return array */
  22. public function atributeNames() {
  23. return [
  24. 'name'=> '角色名称',
  25. ];
  26. }
  27. /**
  28. * 字段规则
  29. * @return array */
  30. public function rules() {
  31. return [
  32. 'name' => 'required',
  33. ];
  34. }
  35. //这个好像没有用,应该可以删除,待确认
  36. public function UserPermistions()
  37. {
  38. return $this->hasMany('app\common\models\user\YzPermission');
  39. }
  40. public function roleUser()
  41. {
  42. return $this->hasMany('app\common\models\user\YzUserRole', 'role_id');
  43. }
  44. public function rolePermission()
  45. {
  46. return $this->hasMany('app\common\models\user\YzPermission', 'item_id');
  47. }
  48. /**
  49. * @param int $pageSize
  50. * @return object
  51. */
  52. public static function getPageList($pageSize,$search)
  53. {
  54. $query = static::uniacid();
  55. if ($search['keyword']) {
  56. $query = $query->where('name', 'like', $search['keyword'] . '%');
  57. }
  58. if ($search['status']) {
  59. $query = $query->where('status', $search['status']);
  60. }
  61. $uids = AppUser::where('role','manager')->pluck('uid')->toArray();
  62. return $query->with(['roleUser'=>function($q) use ($uids){
  63. $q->whereNotIn('user_id',$uids);
  64. }])->paginate($pageSize);
  65. }
  66. public static function getRolelistToUser()
  67. {
  68. return static::select('id', 'name')->uniacid()->where('status', '=', "2")->get()->toArray();
  69. }
  70. /**
  71. * Get full role information and role permissions By roleId
  72. *
  73. * @param int $roleId
  74. * @return object
  75. */
  76. public static function getRoleById($roleId)
  77. {
  78. return static::where('id', $roleId)
  79. ->with(['rolePermission' => function($query) {
  80. return $query->select('id', 'item_id','permission')->where('type', '=', YzPermission::TYPE_ROLE);
  81. }])
  82. ->first();
  83. }
  84. /**
  85. * @param int $roleId
  86. * @return \mysqli_result
  87. */
  88. public static function deleteRole($roleId)
  89. {
  90. return static::where('id', $roleId)->delete();
  91. }
  92. }