Service.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. if (array_key_exists('phone', $extend) && $extend['phone'] != '') {
  41. // 查询手机号 对应的 user
  42. $phone_user = User::where('mobile', $extend['phone'])->find();
  43. if ($phone_user) {
  44. //是否有自己的
  45. $third = Third::get(['platform' => $platform, 'openid' => $params['openid']], 'user');
  46. \think\Log::write('【微信登录】打印,' . json_encode($phone_user). json_encode($third));
  47. if ($third) {
  48. if (!$third->user) {
  49. // 如果没有关联的用户表记录,删除third记录
  50. $third->delete();
  51. } else {
  52. // 如果phone 和 openid 不是同一个人 -- 登录当前手机的人
  53. \think\Log::write('【微信登录】登录当前手机的人,' . json_encode($extend));
  54. // 写入登录Cookies和Token 直接登录账号
  55. return $auth->direct($phone_user->id);
  56. }
  57. } else {
  58. $values = array_merge($values, ['user_id' => $phone_user->id]);
  59. // $third->allowField(true)->save($values);
  60. Third::create($values, true);
  61. \think\Log::write('【微信登录】存在手机号,' . json_encode($extend));
  62. // 写入登录Cookies和Token 直接登录账号
  63. return $auth->direct($phone_user->id);
  64. }
  65. } else {
  66. // 没有手机号
  67. // 默认注册一个会员
  68. $username = $extend['phone'];
  69. $password = Random::alnum(6);
  70. $domain = request()->host();
  71. $result = $auth->register($username, $password, $username . '@' . $domain, $extend['phone']);
  72. if (!$result) {
  73. throw new Exception($auth->getError());
  74. }
  75. $user = $auth->getUser();
  76. $fields = ['username' => '渔u' . $user->id, 'email' => 'u' . $user->id . '@' . $domain];
  77. if ($avatar) {
  78. $fields['avatar'] = function_exists("xss_clean") ? xss_clean(strip_tags($avatar)) : strip_tags($avatar);
  79. }
  80. // 更新会员资料
  81. $user = User::get($user->id);
  82. $user->save($fields);
  83. $user_id = $user->id;
  84. // 保存第三方信息
  85. $values['user_id'] = $user_id;
  86. Third::create($values, true);
  87. $auth->direct($user_id);
  88. }
  89. }
  90. //是否有自己的
  91. $third = Third::get(['platform' => $platform, 'openid' => $params['openid']], 'user');
  92. if ($third) {
  93. if (!$third->user) {
  94. $third->delete();
  95. } else {
  96. $third->allowField(true)->save($values);
  97. \think\Log::write('【微信登录】查询三方表记录--connect--1,' . json_encode($third));
  98. // 写入登录Cookies和Token 直接登录账号
  99. return $auth->direct($third->user_id);
  100. }
  101. }
  102. //存在unionid就需要判断是否需要生成新记录
  103. if (isset($params['unionid']) && !empty($params['unionid'])) {
  104. $third = Third::get(['platform' => $platform, 'unionid' => $params['unionid']], 'user');
  105. if ($third) {
  106. if (!$third->user) {
  107. $third->delete();
  108. } else {
  109. // 保存第三方信息
  110. $values['user_id'] = $third->user_id;
  111. $third = Third::create($values, true);
  112. // 写入登录Cookies和Token
  113. \think\Log::write('【微信登录】查询三方表记录--connect--2,' . json_encode($third));
  114. return $auth->direct($third->user_id);
  115. }
  116. }
  117. }
  118. if ($auth->id) {
  119. if (!$third) {
  120. $values['user_id'] = $auth->id;
  121. Third::create($values, true);
  122. }
  123. $user = $auth->getUser();
  124. $user_id = $user->id;
  125. } else {
  126. // 先随机一个用户名,随后再变更为u+数字id
  127. $username = Random::alnum(20);
  128. $password = Random::alnum(6);
  129. $domain = request()->host();
  130. Db::startTrans();
  131. try {
  132. //查询unionid 有没有之前绑定过的user
  133. $third_unionid_user = '';
  134. if (isset($params['unionid']) && !empty($params['unionid'])) {
  135. $third_unionid_user = Third::get(['unionid' => $params['unionid']], 'user');
  136. }
  137. if ($third_unionid_user != '') {
  138. // 使用原来的user_id
  139. $user_id = $third_unionid_user->user_id;
  140. } else {
  141. // 默认注册一个会员
  142. $result = $auth->register($username, $password, $username . '@' . $domain, '', $extend);
  143. if (!$result) {
  144. throw new Exception($auth->getError());
  145. }
  146. $user = $auth->getUser();
  147. $fields = ['username' => '渔u' . $user->id, 'email' => 'u' . $user->id . '@' . $domain];
  148. if ($nickname) {
  149. $fields['nickname'] = $nickname . $username;
  150. }
  151. if ($avatar) {
  152. $fields['avatar'] = function_exists("xss_clean") ? xss_clean(strip_tags($avatar)) : strip_tags($avatar);
  153. }
  154. // 更新会员资料
  155. $user = User::get($user->id);
  156. $user->save($fields);
  157. $user_id = $user->id;
  158. }
  159. // 保存第三方信息
  160. $values['user_id'] = $user_id;
  161. Third::create($values, true);
  162. Db::commit();
  163. } catch (\Exception $e) {
  164. Db::rollback();
  165. $auth->logout();
  166. \think\Log::write('【微信登录】查询三方表记录--connect--3,错误信息:' . json_encode($e));
  167. return false;
  168. }
  169. }
  170. // 写入登录Cookies和Token
  171. \think\Log::write('【微信登录】查询三方表记录--connect--4,:' . json_encode($user_id));
  172. return $auth->direct($user_id);
  173. }
  174. public static function isBindThird($platform, $openid, $apptype = '', $unionid = '')
  175. {
  176. $conddtions = [
  177. 'platform' => $platform,
  178. 'openid' => $openid
  179. ];
  180. if ($apptype) {
  181. $conddtions['apptype'] = $apptype;
  182. }
  183. $third = Third::get($conddtions, 'user');
  184. //第三方存在
  185. if ($third) {
  186. //用户失效
  187. if (!$third->user) {
  188. $third->delete();
  189. return false;
  190. }
  191. return true;
  192. }
  193. if ($unionid) {
  194. $third = Third::get(['platform' => $platform, 'unionid' => $unionid], 'user');
  195. if ($third) {
  196. //
  197. if (!$third->user) {
  198. $third->delete();
  199. return false;
  200. }
  201. return true;
  202. }
  203. }
  204. return false;
  205. }
  206. }