AppUser.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace app\platform\modules\application\models;
  3. use app\common\models\BaseModel;
  4. use app\platform\modules\user\models\AdminUser;
  5. use Illuminate\Database\Eloquent\Model;
  6. class AppUser extends BaseModel
  7. {
  8. protected $table = 'yz_app_user';
  9. protected $search_fields = [''];
  10. protected $guarded = [''];
  11. protected $appends = ['role_name'];
  12. protected $hidden = ['deleted_at', 'updated_at', 'created_at'];
  13. public function scopeSearch($query, $keyword)
  14. {
  15. if ($keyword['name']) {
  16. $query = $query->whereHas('hasOneUser', function($query) use($keyword) {
  17. $query = $query->where('username', 'like', '%'.$keyword['name'].'%');
  18. });
  19. }
  20. if ($keyword['uid']) {
  21. $query = $query->whereHas('hasOneUser', function($query) use ($keyword) {
  22. $query = $query->where('uid', $keyword['uid']);
  23. });
  24. }
  25. return $query;
  26. }
  27. public function atributeNames()
  28. {
  29. return [
  30. 'uniacid' => '平台id',
  31. 'uid' => '用户id',
  32. 'role' => '角色'
  33. ];
  34. }
  35. public function rules()
  36. {
  37. return [
  38. 'uniacid' => 'required | integer',
  39. 'uid' => 'required | integer',
  40. 'role' => 'required | string | max:20',
  41. ];
  42. }
  43. public function hasOneApp()
  44. {
  45. return $this->hasOne(\app\platform\modules\application\models\UniacidApp::class, 'id', 'uniacid');
  46. }
  47. public function hasOneUser()
  48. {
  49. return $this->hasOne(\app\platform\modules\user\models\AdminUser::class, 'uid', 'uid');
  50. }
  51. public function getRoleNameAttribute()
  52. {
  53. if ($this->role == 'manager') {
  54. return $this->role_name = '管理员';
  55. } elseif ($this->role == 'clerk') {
  56. return $this->role_name = '店员';
  57. } elseif ($this->role == 'operator') {
  58. return $this->role_name = '操作员';
  59. } elseif ($this->role == 'creator') {
  60. return $this->role_name = '创始人';
  61. }
  62. }
  63. public static function getAccount($uid, $uniacid = null)
  64. {
  65. if (is_null($uniacid)) {
  66. return self::where('uid', $uid)->first();
  67. }
  68. return self::where(['uid' => $uid, 'uniacid' => $uniacid])->first();
  69. }
  70. }