Service.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace addons\third\library;
  3. use addons\third\model\Third;
  4. use app\common\model\User;
  5. use fast\Random;
  6. use think\Db;
  7. use think\Exception;
  8. /**
  9. * 第三方登录服务类
  10. *
  11. */
  12. class Service
  13. {
  14. /**
  15. * 第三方登录
  16. * @param string $platform 平台
  17. * @param array $params 参数
  18. * @param array $extend 会员扩展信息
  19. * @param int $keeptime 有效时长
  20. * @return boolean
  21. */
  22. public static function connect($platform, $params = [], $extend = [], $keeptime = 0)
  23. {
  24. $time = time();
  25. $nickname = $params['nickname'] ?? ($params['userinfo']['nickname'] ?? '');
  26. $avatar = $params['avatar'] ?? ($params['userinfo']['avatar'] ?? '');
  27. $values = [
  28. 'platform' => $platform,
  29. 'openid' => $params['openid'],
  30. 'openname' => $nickname,
  31. 'access_token' => $params['access_token'],
  32. 'refresh_token' => $params['refresh_token'],
  33. 'expires_in' => $params['expires_in'],
  34. 'logintime' => $time,
  35. 'expiretime' => $time + $params['expires_in'],
  36. ];
  37. $values = array_merge($values, $params);
  38. $auth = \app\common\library\Auth::instance();
  39. $auth->keeptime($keeptime);
  40. //是否有自己的
  41. $third = Third::get(['platform' => $platform, 'openid' => $params['openid']], 'user');
  42. if ($third) {
  43. if (!$third->user) {
  44. $third->delete();
  45. } else {
  46. $third->allowField(true)->save($values);
  47. // 写入登录Cookies和Token
  48. return $auth->direct($third->user_id);
  49. }
  50. }
  51. //存在unionid就需要判断是否需要生成新记录
  52. if (isset($params['unionid']) && !empty($params['unionid'])) {
  53. $third = Third::get(['platform' => $platform, 'unionid' => $params['unionid']], 'user');
  54. if ($third) {
  55. if (!$third->user) {
  56. $third->delete();
  57. } else {
  58. // 保存第三方信息
  59. $values['user_id'] = $third->user_id;
  60. $third = Third::create($values, true);
  61. // 写入登录Cookies和Token
  62. return $auth->direct($third->user_id);
  63. }
  64. }
  65. }
  66. if ($auth->id) {
  67. if (!$third) {
  68. $values['user_id'] = $auth->id;
  69. Third::create($values, true);
  70. }
  71. $user = $auth->getUser();
  72. } else {
  73. // 先随机一个用户名,随后再变更为u+数字id
  74. $username = Random::alnum(20);
  75. $password = Random::alnum(6);
  76. $domain = request()->host();
  77. Db::startTrans();
  78. try {
  79. // 默认注册一个会员
  80. $result = $auth->register($username, $password, $username . '@' . $domain, '', $extend);
  81. if (!$result) {
  82. throw new Exception($auth->getError());
  83. }
  84. $user = $auth->getUser();
  85. $fields = ['username' => 'u' . $user->id, 'email' => 'u' . $user->id . '@' . $domain];
  86. if ($nickname) {
  87. $fields['nickname'] = $nickname;
  88. }
  89. if ($avatar) {
  90. $fields['avatar'] = htmlspecialchars(strip_tags($avatar));
  91. }
  92. // 更新会员资料
  93. $user = User::get($user->id);
  94. $user->save($fields);
  95. // 保存第三方信息
  96. $values['user_id'] = $user->id;
  97. Third::create($values, true);
  98. Db::commit();
  99. } catch (\Exception $e) {
  100. Db::rollback();
  101. $auth->logout();
  102. return false;
  103. }
  104. }
  105. // 写入登录Cookies和Token
  106. return $auth->direct($user->id);
  107. }
  108. public static function isBindThird($platform, $openid, $apptype = '', $unionid = '')
  109. {
  110. $conddtions = [
  111. 'platform' => $platform,
  112. 'openid' => $openid
  113. ];
  114. if ($apptype) {
  115. $conddtions['apptype'] = $apptype;
  116. }
  117. $third = Third::get($conddtions, 'user');
  118. //第三方存在
  119. if ($third) {
  120. //用户失效
  121. if (!$third->user) {
  122. $third->delete();
  123. return false;
  124. }
  125. return true;
  126. }
  127. if ($unionid) {
  128. $third = Third::get(['platform' => $platform, 'unionid' => $unionid], 'user');
  129. if ($third) {
  130. //
  131. if (!$third->user) {
  132. $third->delete();
  133. return false;
  134. }
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. }