UserOauth.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. use cores\BaseModel;
  14. /**
  15. * 模型类:第三方用户信息
  16. * Class UserOauth
  17. * @package app\common\model
  18. */
  19. class UserOauth extends BaseModel
  20. {
  21. // 定义表名
  22. protected $name = 'user_oauth';
  23. // 定义主键
  24. protected $pk = 'id';
  25. /**
  26. * 根据openid获取用户ID
  27. * @param string $oauthId 第三方用户唯一标识 (openid)
  28. * @param string $oauthType 第三方登陆类型
  29. * @return mixed
  30. */
  31. public static function getUserIdByOauthId(string $oauthId, string $oauthType)
  32. {
  33. return (new static)->where('oauth_id', '=', $oauthId)
  34. ->where('oauth_type', '=', $oauthType)
  35. ->where('is_delete', '=', 0)
  36. ->value('user_id');
  37. }
  38. /**
  39. * 根据用户ID获取OpenID
  40. * @param int $userId 用户ID
  41. * @param string $oauthType 第三方登陆类型
  42. * @return mixed
  43. */
  44. public static function getOauthIdByUserId(int $userId, string $oauthType)
  45. {
  46. return (new static)->where('user_id', '=', $userId)
  47. ->where('oauth_type', '=', $oauthType)
  48. ->where('is_delete', '=', 0)
  49. ->value('oauth_id');
  50. }
  51. /**
  52. * 根据用户ID获取微信小程序OpenID
  53. * @param int $userId 用户ID
  54. * @return mixed
  55. */
  56. public static function getMpWeiXinOpenId(int $userId)
  57. {
  58. return static::getOauthIdByUserId($userId, 'MP-WEIXIN');
  59. }
  60. /**
  61. * 获取第三方用户信息
  62. * @param int $userId 用户ID
  63. * @param string $oauthType 第三方登陆类型
  64. * @return array|\think\Model|null
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\DbException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. */
  69. public static function getOauth(int $userId, string $oauthType)
  70. {
  71. return (new static)->where('user_id', '=', $userId)
  72. ->where('oauth_type', '=', $oauthType)
  73. ->find();
  74. }
  75. }