BaseController.class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. $this->checkDbWhitable();
  23. }
  24. public function checkLogin($redirect = true){
  25. //debug
  26. if ($this->is_local_debug > 0 ) {
  27. $login_user = D("User")->where("username = 'showdoc' ")->find();
  28. session("login_user" , $login_user);
  29. }
  30. if ( ! session("login_user")) {
  31. $cookie_token = cookie('cookie_token');
  32. if ($cookie_token) {
  33. $ret = D("UserToken")->getToken($cookie_token);
  34. if ($ret && $ret['token_expire'] > time() ) {
  35. D("UserToken")->setLastTime($cookie_token);
  36. $login_user = D("User")->where("uid = $ret[uid]")->find();
  37. unset($ret['password']);
  38. session("login_user" , $login_user);
  39. return $login_user ;
  40. }
  41. }
  42. if ($redirect) {
  43. $this->sendError(10102);
  44. exit();
  45. }
  46. }else{
  47. return session("login_user") ;
  48. }
  49. }
  50. //检查是否是管理员
  51. public function checkAdmin($redirect = true){
  52. $login_user = session("login_user") ;
  53. if ($login_user) {
  54. if ($login_user['groupid'] == 1 ) {
  55. return true ;
  56. }
  57. }
  58. if ($redirect) {
  59. $this->sendError(10103);
  60. exit();
  61. }
  62. return false;
  63. }
  64. /**
  65. * 返回json结果
  66. */
  67. protected function sendResult($array){
  68. if (isset($array['error_code'])) {
  69. $result['error_code'] = $array['error_code'] ;
  70. $result['error_message'] = $array['error_message'] ;
  71. }
  72. else{
  73. $result['error_code'] = 0 ;
  74. $result['data'] = $array ;
  75. }
  76. if ($this->is_local_debug > 0 ) {
  77. header('Access-Control-Allow-Origin: *');//允许跨域请求
  78. header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Connection, User-Agent, Cookie');
  79. header('Access-Control-Allow-Credentials : true');//允许跨域请求
  80. }
  81. echo json_encode($result);
  82. //如果开启API调试模式,则记录请求参数和返回结果
  83. if (C('API_LOG')) {
  84. $info = '';
  85. $info .= "\n\n【★★★★★★★★★★★】";
  86. $info .= "\n请求接口:".MODULE_NAME ."/".CONTROLLER_NAME."/".ACTION_NAME."";
  87. $info .= "\n请求".'$_REQUEST'.":\n";
  88. $info .= json_encode($_REQUEST);
  89. $info .= "\n返回结果:\n";
  90. $info .= json_encode($result)."\n";
  91. $info .= "【★★★★★★★★★★★】\n";
  92. \Think\log::record($info , 'INFO');
  93. }
  94. }
  95. //返回错误提示
  96. protected function sendError($error_code , $error_message = ''){
  97. $error_code = $error_code ? $error_code : 10103 ;
  98. if (!$error_message) {
  99. $error_codes = C("error_codes");
  100. foreach ($error_codes as $key => $value) {
  101. if ($key == $error_code ) {
  102. $error_message = $value ;
  103. }
  104. }
  105. }
  106. $array['error_code'] = $error_code;
  107. $array['error_message'] = $error_message ;
  108. $this->sendResult($array);
  109. }
  110. //判断某用户是否有项目管理权限(项目成员member_group_id为1,以及 项目创建者)
  111. protected function checkItemPermn($uid , $item_id){
  112. if (!$uid) {
  113. return false;
  114. }
  115. if (session("mamage_item_".$item_id)) {
  116. return true;
  117. }
  118. $item = D("Item")->where("item_id = '%d' ",array($item_id))->find();
  119. if ($item['uid'] && $item['uid'] == $uid) {
  120. session("mamage_item_".$item_id , 1 );
  121. return true;
  122. }
  123. $ItemMember = D("ItemMember")->where("item_id = '%d' and uid = '%d' and member_group_id = 1 ",array($item_id,$uid))->find();
  124. if ($ItemMember) {
  125. session("mamage_item_".$item_id , 1 );
  126. return true;
  127. }
  128. return false;
  129. }
  130. //判断某用户是否为项目创建者
  131. protected function checkItemCreator($uid , $item_id){
  132. if (!$uid) {
  133. return false;
  134. }
  135. if (session("creat_item_".$item_id)) {
  136. return true;
  137. }
  138. $item = D("Item")->where("item_id = '%d' ",array($item_id))->find();
  139. if ($item['uid'] && $item['uid'] == $uid) {
  140. session("creat_item_".$item_id , 1 );
  141. return true;
  142. }
  143. return false;
  144. }
  145. //判断某用户是否有项目访问权限(公开项目的话所有人可访问,私有项目则项目成员、项目创建者和访问密码输入者可访问)
  146. protected function checkItemVisit($uid , $item_id, $refer_url= ''){
  147. if (session("visit_item_".$item_id)) {
  148. return true;
  149. }
  150. if ($this->checkItemCreator($uid , $item_id)) {
  151. session("visit_item_".$item_id , 1 );
  152. return true;
  153. }
  154. $ItemMember = D("ItemMember")->where("item_id = '%d' and uid = '%d' ",array($item_id,$uid))->find();
  155. if ($ItemMember) {
  156. session("visit_item_".$item_id , 1 );
  157. return true;
  158. }
  159. $item = D("Item")->where("item_id = '%d' ",array($item_id))->find();
  160. if ($item['password']) {
  161. return false;
  162. }else{
  163. session("visit_item_".$item_id , 1 );
  164. return true;
  165. }
  166. }
  167. //检查数据库文件是否可写
  168. protected function checkDbWhitable(){
  169. $file = C("DB_NAME") ;
  170. if ( $fp = @fopen($file, 'a+')) {
  171. @fclose($fp);
  172. return true ;
  173. } else {
  174. $this->sendError("10103","Sqlite/showdoc.db.php文件不可写");
  175. exit();
  176. }
  177. }
  178. }