BaseController.class.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class BaseController extends Controller {
  5. //是否开启本地调试
  6. private $is_local_debug;
  7. public function __construct()
  8. {
  9. //是否开启本地调试。
  10. $this->is_local_debug = 0 ;
  11. //做一个检测,以免这个配置更新到线上。
  12. if (
  13. $this->is_local_debug > 0
  14. &&$_SERVER['HTTP_HOST'] != '127.0.0.1'
  15. && $_SERVER['HTTP_HOST'] != 'wu.com'
  16. && strpos($_SERVER['HTTP_HOST'], "192.168") == false
  17. ){
  18. $this->sendError("-1001","非本地环境禁止开通调试。请通知管理员关闭调试模式");
  19. exit();
  20. }
  21. }
  22. public function checkLogin($redirect = true){
  23. //debug
  24. if ($this->is_local_debug > 0 ) {
  25. $login_user = D("User")->where("username = 'showdoc' ")->find();
  26. session("login_user" , $login_user);
  27. }
  28. if ( ! session("login_user")) {
  29. $cookie_token = cookie('cookie_token');
  30. if ($cookie_token) {
  31. $ret = D("UserToken")->getToken($cookie_token);
  32. if ($ret && $ret['token_expire'] > time() ) {
  33. D("UserToken")->setLastTime($cookie_token);
  34. $login_user = D("User")->where("uid = $ret[uid]")->find();
  35. unset($ret['password']);
  36. session("login_user" , $login_user);
  37. return $login_user ;
  38. }
  39. }
  40. if ($redirect) {
  41. $this->sendError(10102);
  42. exit();
  43. }
  44. }else{
  45. return session("login_user") ;
  46. }
  47. }
  48. //检查是否是管理员
  49. public function checkAdmin($redirect = true){
  50. $login_user = session("login_user") ;
  51. if ($login_user) {
  52. if ($login_user['groupid'] == 1 ) {
  53. return true ;
  54. }
  55. }
  56. if ($redirect) {
  57. $this->sendError(10103);
  58. exit();
  59. }
  60. return false;
  61. }
  62. /**
  63. * 返回json结果
  64. */
  65. protected function sendResult($array){
  66. if (isset($array['error_code'])) {
  67. $result['error_code'] = $array['error_code'] ;
  68. $result['error_message'] = $array['error_message'] ;
  69. }
  70. else{
  71. $result['error_code'] = 0 ;
  72. $result['data'] = $array ;
  73. }
  74. if ($this->is_local_debug > 0 ) {
  75. header('Access-Control-Allow-Origin: *');//允许跨域请求
  76. header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Connection, User-Agent, Cookie');
  77. header('Access-Control-Allow-Credentials : true');//允许跨域请求
  78. }
  79. echo json_encode($result);
  80. //如果开启API调试模式,则记录请求参数和返回结果
  81. if (C('API_LOG')) {
  82. $info = '';
  83. $info .= "\n\n【★★★★★★★★★★★】";
  84. $info .= "\n请求接口:".MODULE_NAME ."/".CONTROLLER_NAME."/".ACTION_NAME."";
  85. $info .= "\n请求".'$_REQUEST'.":\n";
  86. $info .= json_encode($_REQUEST);
  87. $info .= "\n返回结果:\n";
  88. $info .= json_encode($result)."\n";
  89. $info .= "【★★★★★★★★★★★】\n";
  90. \Think\log::record($info , 'INFO');
  91. }
  92. }
  93. //返回错误提示
  94. protected function sendError($error_code , $error_message = ''){
  95. $error_code = $error_code ? $error_code : 10103 ;
  96. if (!$error_message) {
  97. $error_codes = C("error_codes");
  98. foreach ($error_codes as $key => $value) {
  99. if ($key == $error_code ) {
  100. $error_message = $value ;
  101. }
  102. }
  103. }
  104. $array['error_code'] = $error_code;
  105. $array['error_message'] = $error_message ;
  106. $this->sendResult($array);
  107. }
  108. //判断某用户是否有项目管理权限(项目成员member_group_id为1,以及 项目创建者)
  109. protected function checkItemPermn($uid , $item_id){
  110. if (!$uid) {
  111. return false;
  112. }
  113. if (session("mamage_item_".$item_id)) {
  114. return true;
  115. }
  116. $item = D("Item")->where("item_id = '%d' ",array($item_id))->find();
  117. if ($item['uid'] && $item['uid'] == $uid) {
  118. session("mamage_item_".$item_id , 1 );
  119. return true;
  120. }
  121. $ItemMember = D("ItemMember")->where("item_id = '%d' and uid = '%d' and member_group_id = 1 ",array($item_id,$uid))->find();
  122. if ($ItemMember) {
  123. session("mamage_item_".$item_id , 1 );
  124. return true;
  125. }
  126. return false;
  127. }
  128. //判断某用户是否为项目创建者
  129. protected function checkItemCreator($uid , $item_id){
  130. if (!$uid) {
  131. return false;
  132. }
  133. if (session("creat_item_".$item_id)) {
  134. return true;
  135. }
  136. $item = D("Item")->where("item_id = '%d' ",array($item_id))->find();
  137. if ($item['uid'] && $item['uid'] == $uid) {
  138. session("creat_item_".$item_id , 1 );
  139. return true;
  140. }
  141. return false;
  142. }
  143. //判断某用户是否有项目访问权限(公开项目的话所有人可访问,私有项目则项目成员、项目创建者和访问密码输入者可访问)
  144. protected function checkItemVisit($uid , $item_id, $refer_url= ''){
  145. if (session("visit_item_".$item_id)) {
  146. return true;
  147. }
  148. if ($this->checkItemCreator($uid , $item_id)) {
  149. session("visit_item_".$item_id , 1 );
  150. return true;
  151. }
  152. $ItemMember = D("ItemMember")->where("item_id = '%d' and uid = '%d' ",array($item_id,$uid))->find();
  153. if ($ItemMember) {
  154. session("visit_item_".$item_id , 1 );
  155. return true;
  156. }
  157. $item = D("Item")->where("item_id = '%d' ",array($item_id))->find();
  158. if ($item['password']) {
  159. return false;
  160. }else{
  161. session("visit_item_".$item_id , 1 );
  162. return true;
  163. }
  164. }
  165. }