UserOauth.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\model;
  13. /**
  14. * 模型类:第三方用户信息
  15. * Class UserOauth
  16. * @package app\common\model
  17. */
  18. class UserOauth extends BaseModel
  19. {
  20. // 定义表名
  21. protected $name = 'user_oauth';
  22. // 定义主键
  23. protected $pk = 'id';
  24. /**
  25. * 是否存在第三方用户
  26. * @param int $userId 用户ID
  27. * @param string $oauthType 第三方登陆类型
  28. * @return mixed
  29. */
  30. public function isExistOauthType(int $userId, string $oauthType)
  31. {
  32. return $this->where('user_id', '=', $userId)
  33. ->where('oauth_type', '=', $oauthType)
  34. ->where('is_delete', '=', 0)
  35. ->value($this->getPk());
  36. }
  37. /**
  38. * 根据openid获取用户ID
  39. * @param string $oauthId 第三方用户唯一标识 (openid)
  40. * @param string $oauthType 第三方登陆类型
  41. * @return mixed
  42. */
  43. public static function getUserIdByOauthId(string $oauthId, string $oauthType)
  44. {
  45. return (new static)->where('oauth_id', '=', $oauthId)
  46. ->where('oauth_type', '=', $oauthType)
  47. ->where('is_delete', '=', 0)
  48. ->value('user_id');
  49. }
  50. /**
  51. * 获取第三方用户信息
  52. * @param int $userId 用户ID
  53. * @param string $oauthType 第三方登陆类型
  54. * @return array|\think\Model|null
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\DbException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. */
  59. public static function getOauth(int $userId, string $oauthType)
  60. {
  61. return (new static)->where('user_id', '=', $userId)
  62. ->where('oauth_type', '=', $oauthType)
  63. ->find();
  64. }
  65. }