AuthenticateAdmin.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: dingran
  5. * Date: 2019/2/19
  6. * Time: 下午5:08
  7. */
  8. namespace app\common\middleware;
  9. use app\common\services\Utils;
  10. use app\common\traits\JsonTrait;
  11. use app\platform\modules\application\models\AppUser;
  12. use Closure;
  13. use Illuminate\Support\Facades\Auth;
  14. class AuthenticateAdmin
  15. {
  16. use JsonTrait;
  17. /**
  18. * 公众号状态
  19. *
  20. */
  21. const UNIACID_STATUS = -1;
  22. /**
  23. * 用户状态
  24. *
  25. */
  26. const USER_STATUS = -2;
  27. /**
  28. * API访问状态
  29. *
  30. */
  31. const API_STATUS = -3;
  32. /**
  33. * 公共接口
  34. *
  35. * @var array
  36. */
  37. protected $except = [
  38. 'admin/index',
  39. ];
  40. /**
  41. * 非管理员有效访问接口
  42. *
  43. * @var array
  44. */
  45. protected $authApi = [
  46. 'admin/index',
  47. 'admin/shop',
  48. 'admin/application',
  49. 'admin/application/recycle',
  50. 'admin/appuser',
  51. 'admin/appuser/add',
  52. 'admin/appuser/delete',
  53. 'admin/appuser/checkname',
  54. 'admin/all/upload',
  55. 'admin/application/getApp',
  56. 'admin/application/delete/{id}',
  57. 'admin/application/add',
  58. 'admin/application/checkAddRole',
  59. 'admin/application/update/{id}',
  60. 'admin/application/switchStatus/{id}',
  61. 'admin/application/setTop/{id}',
  62. 'admin/all/list',
  63. 'admin/all/delImg',
  64. 'admin/user/modify_user',
  65. 'admin/user/send_code',
  66. 'admin/user/send_new_code',
  67. 'admin/user/user_change',
  68. 'admin/user/modify_mobile',
  69. 'admin/clear',
  70. 'admin/application/getMessage'
  71. ];
  72. /**
  73. * 访问用户
  74. *
  75. * @var null
  76. */
  77. private $account = null;
  78. /**
  79. * 公众号
  80. *
  81. * @var int
  82. */
  83. private $uniacid = 0;
  84. /**
  85. * 用户角色
  86. *
  87. * @var array
  88. */
  89. private $role = ['role' => '', 'isfounder' => false];
  90. /**
  91. * Handle an incoming request.
  92. *
  93. * @param $request
  94. * @param Closure $next
  95. *
  96. * @return mixed
  97. */
  98. public function handle($request, Closure $next)
  99. {
  100. global $_W;
  101. $check = $this->checkUserInfo();
  102. $uri = \Route::getCurrentRoute()->Uri();
  103. $uniacid = \YunShop::app()->uniacid;
  104. \YunShop::app()->uid = \Auth::guard('admin')->user()->uid;
  105. \YunShop::app()->username = \Auth::guard('admin')->user()->username;
  106. $_W['uid'] = \Auth::guard('admin')->user()->uid;
  107. $_W['username'] = \Auth::guard('admin')->user()->username;
  108. if (!$check['result']) {
  109. return $this->errorJson($check['msg'], ['status' => self::USER_STATUS]);
  110. }
  111. if (\Auth::guard('admin')->user()->uid == 1) {
  112. \YunShop::app()->role = 'founder';
  113. \YunShop::app()->isfounder = true;
  114. $this->role = ['role' => 'founder', 'isfounder' => true];
  115. } else {
  116. if (!in_array($uri, $this->authApi)) {
  117. return $this->errorJson('无访问权限', ['status' => self::API_STATUS]);
  118. }
  119. if (!empty($uniacid)) {
  120. $this->uniacid = $uniacid;
  121. $this->account = AppUser::getAccount(\Auth::guard('admin')->user()->uid, $uniacid);
  122. if (!is_null($this->account)) {
  123. $this->setRole();
  124. } else {
  125. $this->relogin();
  126. }
  127. }
  128. }
  129. return $next($request);
  130. }
  131. /**
  132. * 获取用户身份
  133. *
  134. * @return array
  135. */
  136. private function setRole()
  137. {
  138. if (\Auth::guard('admin')->user()->uid === 1) {
  139. \YunShop::app()->role = 'founder';
  140. \YunShop::app()->isfounder = true;
  141. $this->role = ['role' => 'founder', 'isfounder' => true];
  142. } else {
  143. \YunShop::app()->role = $this->account->role;
  144. \YunShop::app()->isfounder = false;
  145. $this->role = ['role' => $this->account->role, 'isfounder' => false];
  146. }
  147. }
  148. /**
  149. * 验证访问权限
  150. *
  151. * @return \Illuminate\Http\JsonResponse
  152. */
  153. private function relogin()
  154. {
  155. \Auth::guard('admin')->logout();
  156. request()->session()->flush();
  157. request()->session()->regenerate();
  158. Utils::removeUniacid();
  159. return $this->errorJson('用户不存在,请重新登录', ['login_status' => 1, 'login_url' => '/#/login']);
  160. }
  161. /**
  162. * 检测用户信息
  163. *
  164. * @return array
  165. */
  166. private function checkUserInfo()
  167. {
  168. $user = \Auth::guard('admin')->user();
  169. $result = 1;
  170. if ($user->status == 3) {
  171. $result = 0;
  172. $msg = '您已被禁用,请联系管理员';
  173. }
  174. if ($user->endtime != 0 && $user->endtime <= time()) {
  175. $result = 0;
  176. $msg = '您的账号已过期,请联系管理员';
  177. }
  178. return [
  179. 'result' => $result,
  180. 'msg' => $msg
  181. ];
  182. }
  183. /**
  184. * 获取错误信息
  185. *
  186. * @return mixed
  187. */
  188. private function errorMsg()
  189. {
  190. if (\Cache::has('app.access')) {
  191. $msg = \Cache::get('app.access');
  192. \Cache::forget('app.access');
  193. Utils::removeUniacid();
  194. return $msg;
  195. }
  196. }
  197. }