UserOauth.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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. use cores\BaseModel;
  14. use app\common\enum\Client as ClientEnum;
  15. /**
  16. * 模型类:第三方用户信息
  17. * Class UserOauth
  18. * @package app\common\model
  19. */
  20. class UserOauth extends BaseModel
  21. {
  22. // 定义表名
  23. protected $name = 'user_oauth';
  24. // 定义主键
  25. protected $pk = 'id';
  26. /**
  27. * 根据openid获取用户ID
  28. * @param string $oauthId 第三方用户唯一标识 (openid)
  29. * @param string $oauthType 第三方登陆类型
  30. * @return mixed
  31. */
  32. public static function getUserIdByOauthId(string $oauthId, string $oauthType)
  33. {
  34. return (new static)->where('oauth_id', '=', $oauthId)
  35. ->where('oauth_type', '=', $oauthType)
  36. ->where('is_delete', '=', 0)
  37. ->value('user_id');
  38. }
  39. /**
  40. * 根据用户ID获取OpenID
  41. * @param int $userId 用户ID
  42. * @param string $oauthType 第三方登陆类型
  43. * @return mixed
  44. */
  45. public static function getOauthIdByUserId(int $userId, string $oauthType)
  46. {
  47. return (new static)->where('user_id', '=', $userId)
  48. ->where('oauth_type', '=', $oauthType)
  49. ->where('is_delete', '=', 0)
  50. ->value('oauth_id');
  51. }
  52. /**
  53. * 根据用户ID获取微信小程序OpenID
  54. * @param int $userId 用户ID
  55. * @return mixed
  56. */
  57. public static function getMpWeiXinOpenId(int $userId)
  58. {
  59. return static::getOauthIdByUserId($userId, ClientEnum::MP_WEIXIN);
  60. }
  61. /**
  62. * 获取第三方用户信息
  63. * @param int $userId 用户ID
  64. * @param string $oauthType 第三方登陆类型
  65. * @return array|\think\Model|null
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. */
  70. public static function getOauth(int $userId, string $oauthType)
  71. {
  72. return (new static)->where('user_id', '=', $userId)
  73. ->where('oauth_type', '=', $oauthType)
  74. ->find();
  75. }
  76. }