Third.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace addons\third;
  3. use addons\third\library\Service;
  4. use app\common\library\Auth;
  5. use app\common\library\Menu;
  6. use think\Addons;
  7. use think\Db;
  8. use think\Request;
  9. use think\Session;
  10. /**
  11. * 第三方登录
  12. */
  13. class Third extends Addons
  14. {
  15. protected static $html = ['register' => '', 'profile' => ''];
  16. /**
  17. * 插件安装方法
  18. * @return bool
  19. */
  20. public function install()
  21. {
  22. $menu = [
  23. [
  24. 'name' => 'third',
  25. 'title' => '第三方登录管理',
  26. 'icon' => 'fa fa-users',
  27. 'sublist' => [
  28. [
  29. "name" => "third/index",
  30. "title" => "查看"
  31. ],
  32. [
  33. "name" => "third/del",
  34. "title" => "删除"
  35. ]
  36. ]
  37. ]
  38. ];
  39. Menu::create($menu);
  40. return true;
  41. }
  42. /**
  43. * 插件卸载方法
  44. * @return bool
  45. */
  46. public function uninstall()
  47. {
  48. Menu::delete("third");
  49. return true;
  50. }
  51. /**
  52. * 插件启用方法
  53. * @return bool
  54. */
  55. public function enable()
  56. {
  57. Menu::enable("third");
  58. return true;
  59. }
  60. /**
  61. * 插件禁用方法
  62. * @return bool
  63. */
  64. public function disable()
  65. {
  66. Menu::disable("third");
  67. return true;
  68. }
  69. /**
  70. * 删除第三方登录表的关联数据
  71. */
  72. public function userDeleteSuccessed(\app\common\model\User $user)
  73. {
  74. \addons\third\model\Third::where('user_id', $user->id)->delete();
  75. }
  76. /**
  77. * 移除第三方登录信息
  78. */
  79. public function userLogoutSuccessed(\app\common\model\User $user)
  80. {
  81. Session::delete(["wechat-userinfo", "qq-userinfo", "weibo-userinfo"]);
  82. }
  83. /**
  84. * 模块开始
  85. */
  86. public function moduleInit()
  87. {
  88. $config = $this->getConfig();
  89. if (!$config['status']) {
  90. return;
  91. }
  92. $request = Request::instance();
  93. $module = strtolower($request->module());
  94. $controller = strtolower($request->controller());
  95. $action = strtolower($request->action());
  96. if ($module !== 'index' || $controller !== 'user' || !in_array($action, ['login', 'register'])) {
  97. return;
  98. }
  99. $url = $request->get('url', $request->server('HTTP_REFERER', '', 'trim'), 'trim');
  100. $data = [
  101. 'status' => explode(',', $config['status']),
  102. 'url' => $url
  103. ];
  104. self::$html['register'] = $this->view->fetch('view/hook/user_register_end', $data);
  105. }
  106. /**
  107. * 方法开始
  108. */
  109. public function actionBegin()
  110. {
  111. $config = $this->getConfig();
  112. if (!$config['status']) {
  113. return;
  114. }
  115. $request = Request::instance();
  116. $module = strtolower($request->module());
  117. $controller = strtolower($request->controller());
  118. $action = strtolower($request->action());
  119. if ($module !== 'index' || $controller !== 'user' || !in_array($action, ['profile'])) {
  120. return;
  121. }
  122. $platform = \addons\third\model\Third::where('user_id', Auth::instance()->id)->column('platform');
  123. // 微信判断apptype
  124. if (in_array('wechat', $platform)) {
  125. $wechatApptypeArr = \addons\third\model\Third::where('user_id', Auth::instance()->id)->where('platform', 'wechat')->column('apptype');
  126. $apptype = Service::getApptype();
  127. if (!in_array($apptype, $wechatApptypeArr)) {
  128. $platform = array_diff($platform, ['wechat']);
  129. }
  130. }
  131. $data = [
  132. 'status' => explode(',', $config['status']),
  133. 'platform' => $platform
  134. ];
  135. self::$html['profile'] = $this->view->fetch('view/hook/user_profile_end', $data);
  136. }
  137. /**
  138. * 配置
  139. * @param $params
  140. */
  141. public function configInit(&$params)
  142. {
  143. // 兼容旧版本FastAdmin
  144. $config = $this->getConfig();
  145. $module = strtolower(request()->module());
  146. $controller = strtolower(request()->controller());
  147. $action = strtolower(request()->action());
  148. $loginhtml = version_compare(config('fastadmin.version'), '1.3.0', '<') > 0 && $module === 'index' && $controller === 'user' && in_array($action, ['login', 'register']) ? self::$html['register'] : '';
  149. $params['third'] = ['status' => explode(',', $config['status']), 'loginhtml' => $loginhtml];
  150. }
  151. /**
  152. * HTML替换
  153. */
  154. public function viewFilter(&$content)
  155. {
  156. $config = $this->getConfig();
  157. if (!$config['status']) {
  158. return;
  159. }
  160. $request = Request::instance();
  161. $module = strtolower($request->module());
  162. $controller = strtolower($request->controller());
  163. $action = strtolower($request->action());
  164. if ($module !== 'index' || $controller !== 'user') {
  165. return;
  166. }
  167. if (in_array($action, ['login', 'register'])) {
  168. $html = self::$html['register'] ?? '';
  169. $content = preg_replace('/<!\-\-@(IndexRegisterFormEnd|IndexLoginFormEnd)\-\->/i', "\$0\n" . $html, $content);
  170. } elseif ($action === 'profile') {
  171. $html = self::$html['profile'] ?? '';
  172. $content = str_replace("<div class=\"form-group normal-footer\">", "{$html}<div class=\"form-group normal-footer\">", $content);
  173. }
  174. }
  175. }