UserOauth.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * 根据openid获取用户ID
  26. * @param string $oauthId 第三方用户唯一标识 (openid)
  27. * @param string $oauthType 第三方登陆类型
  28. * @return mixed
  29. */
  30. public static function getUserIdByOauthId(string $oauthId, string $oauthType)
  31. {
  32. return (new static)->where('oauth_id', '=', $oauthId)
  33. ->where('oauth_type', '=', $oauthType)
  34. ->where('is_delete', '=', 0)
  35. ->value('user_id');
  36. }
  37. /**
  38. * 根据用户ID获取OpenID
  39. * @param int $userId 用户ID
  40. * @param string $oauthType 第三方登陆类型
  41. * @return mixed
  42. */
  43. public static function getOauthIdByUserId(int $userId, string $oauthType)
  44. {
  45. return (new static)->where('user_id', '=', $userId)
  46. ->where('oauth_type', '=', $oauthType)
  47. ->where('is_delete', '=', 0)
  48. ->value('oauth_id');
  49. }
  50. /**
  51. * 根据用户ID获取微信小程序OpenID
  52. * @param int $userId 用户ID
  53. * @return mixed
  54. */
  55. public static function getMpWeiXinOpenId(int $userId)
  56. {
  57. return static::getOauthIdByUserId($userId, 'MP-WEIXIN');
  58. }
  59. /**
  60. * 获取第三方用户信息
  61. * @param int $userId 用户ID
  62. * @param string $oauthType 第三方登陆类型
  63. * @return array|\think\Model|null
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. */
  68. public static function getOauth(int $userId, string $oauthType)
  69. {
  70. return (new static)->where('user_id', '=', $userId)
  71. ->where('oauth_type', '=', $oauthType)
  72. ->find();
  73. }
  74. }