Wechat.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace app\common\Service\Third\Wechat;
  3. use app\common\Enum\ChannelEnum;
  4. use app\common\library\Auth;
  5. use app\common\model\ThirdOauth;
  6. use app\common\model\User as UserModel;
  7. use app\common\exception\BusinessException;
  8. class Wechat
  9. {
  10. // 平台
  11. protected $platform;
  12. // 转发服务
  13. protected $service;
  14. public function __construct($platform, $payload = [])
  15. {
  16. $this->platform = $platform;
  17. $this->service = $this->setPlatformService($payload);
  18. }
  19. public function getApp()
  20. {
  21. return $this->service->wechat;
  22. }
  23. /**
  24. * 微信登陆
  25. * @return array
  26. */
  27. public function login()
  28. {
  29. $wechatUser = $this->service->login();
  30. $oauthInfo = $this->createOrUpdateOauthInfo($wechatUser);
  31. $this->registerOrBindUser($oauthInfo, 0, ['mobile' => $wechatUser['mobile'] ?? '']);
  32. $oauthInfo->save();
  33. $auth = Auth::instance();
  34. $ret = $auth->direct($oauthInfo->user_id);
  35. if ($ret) {
  36. $oauthInfo->login_num += 1;
  37. $oauthInfo->token = $auth->getToken();
  38. return $oauthInfo;
  39. } else {
  40. $oauthInfo->user_id = 0;
  41. $oauthInfo->save();
  42. return $oauthInfo;
  43. }
  44. }
  45. /**
  46. * 微信绑定
  47. *
  48. * @return array
  49. */
  50. public function bind(Object $user)
  51. {
  52. $wechatUser = $this->service->bind();
  53. $oauthInfo = $this->createOrUpdateOauthInfo($wechatUser);
  54. if ($this->registerOrBindUser($oauthInfo, $user->id)) {
  55. return true;
  56. }
  57. return false;
  58. }
  59. /**
  60. * 微信解绑
  61. *
  62. * @return array
  63. */
  64. public function unbind()
  65. {
  66. $user = Auth::instance()->getUser();
  67. if (!$user->verification->mobile) {
  68. throw new BusinessException('请先绑定手机号后再进行操作');
  69. }
  70. $oauthInfo = ThirdOauth::where([
  71. 'user_id' => $user->id,
  72. 'platform' => $this->platform,
  73. 'provider' => 'wechat'
  74. ])->find();
  75. if ($oauthInfo) {
  76. $oauthInfo->delete();
  77. return true;
  78. }
  79. return false;
  80. }
  81. /**
  82. * 创建/更新用户认证信息
  83. *
  84. * @return think\Model
  85. */
  86. private function createOrUpdateOauthInfo($wechatUser, $extend = [])
  87. {
  88. $oauthInfo = ThirdOauth::getByOpenid($wechatUser['openid']);
  89. if (!$oauthInfo) { // 创建新的third_oauth条目
  90. $wechatUser['user_id'] = 0;
  91. if (!empty($wechatUser['unionid'])) { // unionid账号合并策略
  92. $unionOauthInfo = ThirdOauth::getByUnionid($wechatUser['unionid']);
  93. if ($unionOauthInfo) {
  94. $wechatUser['user_id'] = $unionOauthInfo->user_id;
  95. }
  96. }
  97. $wechatUser['provider'] = 'wechat';
  98. $wechatUser['platform'] = $this->platform;
  99. $wechatUser = array_merge($wechatUser, $extend);
  100. $thirdOauth = new ThirdOauth($wechatUser);
  101. $thirdOauth->allowField(true)->save();
  102. $oauthInfo = ThirdOauth::getByOpenid($wechatUser['openid']);
  103. } else { // 更新授权信息
  104. if ($this->platform === 'miniProgram') {
  105. unset($wechatUser['avatar']);
  106. unset($wechatUser['nickname']);
  107. }
  108. $oauthInfo->allowField(true)->save($wechatUser);
  109. }
  110. return $oauthInfo;
  111. }
  112. /**
  113. * 注册/绑定用户
  114. *
  115. * @return think\Model
  116. */
  117. private function registerOrBindUser($oauthInfo, $user_id = 0, $extend = [])
  118. {
  119. // 检查用户存在
  120. if ($oauthInfo->user_id) {
  121. $user = UserModel::get($oauthInfo->user_id);
  122. if ($user && $user_id > 0) {
  123. throw new BusinessException('该微信账号已绑定其他用户');
  124. }
  125. // 用户被删除,则重置第三方信息所绑定的用户id
  126. if (!$user) {
  127. $oauthInfo->user_id = 0;
  128. }
  129. }
  130. // 绑定用户
  131. if ($oauthInfo->user_id === 0 && $user_id > 0) {
  132. $user = UserModel::get($user_id);
  133. if ($user) {
  134. $oauthInfo->user_id = $user_id;
  135. return $oauthInfo->save();
  136. } else {
  137. throw new BusinessException('该用户暂不可绑定微信账号');
  138. }
  139. return false;
  140. }
  141. // 注册新用户
  142. if ($oauthInfo->user_id === 0 && $user_id === 0) {
  143. $auth = Auth::instance()->register($oauthInfo->nickname ?? '', '', '', $extend['mobile'] ?? '');
  144. $user = Auth::instance()->getUser();
  145. $oauthInfo->user_id = $user->id;
  146. return $oauthInfo->save();
  147. }
  148. }
  149. /**
  150. * 设置平台服务类
  151. *
  152. * @return class
  153. */
  154. private function setPlatformService($payload)
  155. {
  156. switch ($this->platform) {
  157. case 'officialAccount':
  158. $service = new OfficialAccount($this->platform,$payload);
  159. break;
  160. case 'miniProgram':
  161. $service = new MiniProgram($this->platform,$payload);
  162. break;
  163. case ChannelEnum::CHANNEL_IOS_APP:
  164. $service = new OpenPlatform($this->platform,$payload);
  165. break;
  166. case ChannelEnum::CHANNEL_ANDROID_APP:
  167. $service = new OpenPlatform($this->platform,$payload);
  168. break;
  169. }
  170. if (!isset($service)) throw new BusinessException('平台参数有误');
  171. return $service;
  172. }
  173. /**
  174. * 方法转发到驱动提供者
  175. *
  176. * @param string $funcname
  177. * @param array $arguments
  178. * @return void
  179. */
  180. public function __call($funcname, $arguments)
  181. {
  182. return $this->service->{$funcname}(...$arguments);
  183. }
  184. }