User.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\common\model\store;
  10. use think\Session;
  11. use app\common\model\BaseModel;
  12. /**
  13. * 商家用户模型
  14. * Class User
  15. * @package app\common\model
  16. */
  17. class User extends BaseModel
  18. {
  19. protected $name = 'store_user';
  20. /**
  21. * 关联微信小程序表
  22. * @return \think\model\relation\BelongsTo
  23. */
  24. public function wxapp()
  25. {
  26. $module = self::getCalledModule() ?: 'common';
  27. return $this->belongsTo("app\\{$module}\\model\\Wxapp");
  28. }
  29. /**
  30. * 关联用户角色表表
  31. * @return \think\model\relation\BelongsToMany
  32. */
  33. public function role()
  34. {
  35. return $this->belongsToMany('Role', 'StoreUserRole');
  36. }
  37. /**
  38. * 验证用户名是否重复
  39. * @param $user_name
  40. * @return bool
  41. */
  42. public static function checkExist($user_name)
  43. {
  44. return !!static::useGlobalScope(false)
  45. ->where('user_name', '=', $user_name)
  46. ->where('is_delete', '=', 0)
  47. ->value('store_user_id');
  48. }
  49. /**
  50. * 商家用户详情
  51. * @param $where
  52. * @param array $with
  53. * @return static|null
  54. * @throws \think\exception\DbException
  55. */
  56. public static function detail($where, $with = [])
  57. {
  58. !is_array($where) && $where = ['store_user_id' => (int)$where];
  59. return static::get(array_merge(['is_delete' => 0], $where), $with);
  60. }
  61. /**
  62. * 保存登录状态
  63. * @param $user
  64. * @throws \think\Exception
  65. */
  66. public function loginState($user)
  67. {
  68. /** @var \app\common\model\Wxapp $wxapp */
  69. $wxapp = $user['wxapp'];
  70. // 保存登录状态
  71. Session::set('dyu_store', [
  72. 'user' => [
  73. 'store_user_id' => $user['store_user_id'],
  74. 'user_name' => $user['user_name'],
  75. ],
  76. 'wxapp' => $wxapp->toArray(),
  77. 'is_login' => true,
  78. ]);
  79. }
  80. }