User.php 1.9 KB

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