Base.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Kevin
  5. * Date: 2018/8/2
  6. * Time: 22:28
  7. */
  8. namespace app\admin\controller;
  9. use think\Controller;
  10. use app\admin\model\Node;
  11. use think\Db;
  12. use think\session;
  13. class Base extends Controller
  14. {
  15. public function _initialize()
  16. {
  17. if(!session('?uid')||!session('?username')){
  18. $this->redirect(url('admin/login/index'));
  19. }
  20. if(request()->controller() != 'Data' && request()->action () != 'revert') {
  21. $adminSta = Db::name ('admin')->where ('id' , session ('uid'))->field ('status,username')->find ();
  22. $roleSta = Db::name ('admin')->alias ('a')->join ('auth_group g' , 'a.groupid=g.id' , 'left')->where ('a.id' , session ('uid'))->field ('g.status,g.title')->find ();
  23. if ( is_null ($adminSta[ 'username' ]) ) {
  24. writelog (session ('username') . '账号不存在,强制下线!' , 200);
  25. $this->error ('抱歉,账号不存在,强制下线' , 'admin/login/loginout');
  26. }
  27. if ( is_null ($roleSta[ 'title' ]) ) {
  28. writelog (session ('rolename') . '身份不存在,强制下线!' , 200);
  29. $this->error ('抱歉,身份不存在,强制下线' , 'admin/login/loginout');
  30. }
  31. if ( $adminSta[ 'status' ] == 2 ) {
  32. writelog ($adminSta[ 'username' ] . '账号被禁用,强制下线!' , 200);
  33. $this->error ('抱歉,该账号被禁用,强制下线' , 'admin/login/loginout');
  34. }
  35. if ( $roleSta[ 'status' ] == 2 ) {
  36. writelog ($roleSta[ 'title' ] . '角色被禁用,强制下线!' , 200);
  37. $this->error ('抱歉,该账号角色被禁用,强制下线' , 'admin/login/loginout');
  38. }
  39. }
  40. $auth = new \com\Auth();
  41. $module = strtolower(request()->module());
  42. $controller = strtolower(request()->controller());
  43. $action = strtolower(request()->action());
  44. $url = $module."/".$controller."/".$action;
  45. //跳过检测以及主页权限
  46. if(session('uid')!=1){
  47. foreach(config('auth_pass') as $vo){
  48. $pass[] = strtolower($vo);
  49. }
  50. if(!in_array($url,$pass)){
  51. if(!$auth->check($url,session('uid'))){
  52. $this->error('抱歉,您没有操作权限');
  53. }
  54. }
  55. }
  56. //首页展示用户&菜单信息
  57. $node = new Node();
  58. $this->assign([
  59. 'username' => session('username'),
  60. 'portrait' => session('portrait'),
  61. 'rolename' => session('rolename'),
  62. 'menu' => $node->getMenu(session('rule'))
  63. ]);
  64. $config = cache('db_config_data');
  65. if(!$config){
  66. $config = api('Config/lists');
  67. cache('db_config_data',$config);
  68. }
  69. config($config);
  70. if(config('web_site_close') == 0 && session('uid') !=1 ){
  71. $this->error('站点已经关闭,请稍后访问~');
  72. }
  73. if(config('admin_allow_ip') && session('uid') !=1 ){
  74. if(in_array(request()->ip(),explode(',',config('admin_allow_ip')))){
  75. $this->error('403:禁止访问');
  76. }
  77. }
  78. }
  79. /**
  80. * place 三级联动
  81. * @return \think\response\Json
  82. */
  83. public function place()
  84. {
  85. $area = new \app\common\place\Area;
  86. $data = $area->area();
  87. return json($data);
  88. }
  89. /**
  90. * 极光推送
  91. * @param $type 1:推送个人 2:推送全体
  92. * @param $alias 别名 user_id OR token
  93. * @param $message 推送消息内容
  94. * @param $extras 扩展字段接受数组
  95. * @return array
  96. */
  97. public function Jpush($type,$alias,$message,$extras)
  98. {
  99. $alias = (string)$alias;
  100. import('jpush.autoload', EXTEND_PATH);
  101. //初始化JPushClient
  102. $client = new \JPush\Client(config('jpush.appKey'),config('jpush.masterSecret'));
  103. //生成推送Payload构建器
  104. $push = $client->push();
  105. //推送平台 'all' OR ['ios','android'] OR 'ios','android'
  106. $push->setPlatform('all');
  107. //1:推送个人 2:推送全体
  108. if($type==1){
  109. $push->addAlias($alias);//按别名推送
  110. }else{
  111. $push->addAllAudience();//广播消息推送
  112. }
  113. //IOS推送
  114. $push->iosNotification($message, [
  115. 'alert'=>$message,
  116. 'badge' => '+1',
  117. 'extras' => $extras,
  118. 'sound'=>'default'
  119. ]
  120. );
  121. //Android推送
  122. $push->androidNotification($message, [
  123. 'alert'=>$message,
  124. 'extras' => $extras
  125. ]
  126. );
  127. return $push->send();
  128. }
  129. }