Third.php 5.0 KB

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