User.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\common\model\supplier;
  3. use app\common\model\BaseModel;
  4. /**
  5. * 商家用户模型
  6. */
  7. class User extends BaseModel
  8. {
  9. protected $name = 'supplier_user';
  10. protected $pk = 'supplier_user_id';
  11. /**
  12. * 关联应用表
  13. */
  14. public function app()
  15. {
  16. return $this->belongsTo('app\\common\\model\\app\\App', 'app_id', 'app_id');
  17. }
  18. /**
  19. * 关联用户角色表表
  20. */
  21. public function role()
  22. {
  23. return $this->belongsToMany('app\\common\\model\\auth\\Role', 'app\\common\\model\\auth\\UserRole');
  24. }
  25. public function userRole()
  26. {
  27. return $this->hasMany('app\\common\\model\\supplier\\UserRole', 'supplier_user_id', 'supplier_user_id');
  28. }
  29. /**
  30. * 关联应用表
  31. */
  32. public function user()
  33. {
  34. return $this->belongsTo('app\\common\\model\\user\\User', 'user_id', 'user_id');
  35. }
  36. /**
  37. * 验证用户名是否重复
  38. */
  39. public static function checkExist($user_name)
  40. {
  41. return !!static::withoutGlobalScope()
  42. ->where('user_name', '=', $user_name)
  43. ->value('supplier_user_id');
  44. }
  45. /**
  46. * 商家用户详情
  47. */
  48. public static function detail($where, $with = [])
  49. {
  50. !is_array($where) && $where = ['supplier_user_id' => (int)$where];
  51. return (new static())->where(array_merge(['is_delete' => 0], $where))->with($with)->find();
  52. }
  53. /**
  54. * 商家用户详情
  55. */
  56. public static function detailByUserId($user_id)
  57. {
  58. return (new static())->where('is_delete', '=', 0)
  59. ->where('user_id', '=', $user_id)
  60. ->where('shop_supplier_id', '>', 0)
  61. ->find();
  62. }
  63. /**
  64. * 保存登录状态
  65. */
  66. public function loginState($user)
  67. {
  68. $app = $user['app'];
  69. // 保存登录状态
  70. $session = array(
  71. 'user' => [
  72. 'supplier_user_id' => $user['supplier_user_id'],
  73. 'user_name' => $user['user_name'],
  74. 'shop_supplier_id' => $user['shop_supplier_id'],
  75. 'app_id' => $user['app_id'],
  76. 'user_id'=>$user['user_id'],
  77. ],
  78. 'app' => $app->toArray(),
  79. 'is_login' => true,
  80. );
  81. session('jjjshop_supplier', $session);
  82. }
  83. }