UserOauth.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. ->order('id', 'desc')
  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. ->order('id', 'desc')
  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, '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. ->order('id', 'desc')
  75. ->find();
  76. }
  77. }