User.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace app\common\model\shop;
  3. use app\common\model\BaseModel;
  4. /**
  5. * 商家用户模型
  6. */
  7. class User extends BaseModel
  8. {
  9. protected $name = 'shop_user';
  10. protected $pk = 'shop_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\\shop\\UserRole', 'shop_user_id', 'shop_user_id');
  28. }
  29. /**
  30. * 验证用户名是否重复
  31. */
  32. public static function checkExist($user_name)
  33. {
  34. return !!static::withoutGlobalScope()
  35. ->where('user_name', '=', $user_name)
  36. ->value('shop_user_id');
  37. }
  38. /**
  39. * 商家用户详情
  40. */
  41. public static function detail($where, $with = [])
  42. {
  43. !is_array($where) && $where = ['shop_user_id' => (int)$where];
  44. return (new static())->where(array_merge(['is_delete' => 0], $where))->with($with)->find();
  45. }
  46. /**
  47. * 保存登录状态
  48. */
  49. public function loginState($user)
  50. {
  51. $app = $user['app'];
  52. // 保存登录状态
  53. $session = array(
  54. 'user' => [
  55. 'shop_user_id' => $user['shop_user_id'],
  56. 'user_name' => $user['user_name'],
  57. ],
  58. 'app' => $app->toArray(),
  59. 'is_login' => true,
  60. );
  61. session('jjjshop_store', $session);
  62. }
  63. }