BaseController.class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. && strpos($_SERVER['HTTP_HOST'],'127.0.0.1') === false
  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. //为了兼容纯json请求
  24. if (strstr($_SERVER['CONTENT_TYPE'],"json")) {
  25. $json = file_get_contents('php://input');
  26. $array = json_decode($json,1);
  27. $_POST = array_merge($_POST,$array) ;
  28. }
  29. }
  30. public function checkLogin($redirect = true){
  31. //debug
  32. if ($this->is_local_debug > 0 ) {
  33. $login_user = D("User")->where("username = 'showdoc' ")->find();
  34. session("login_user" , $login_user);
  35. }
  36. if ( ! session("login_user")) {
  37. $cookie_token = I("user_token") ? I("user_token") : cookie('cookie_token');
  38. if ($cookie_token) {
  39. $ret = D("UserToken")->getToken($cookie_token);
  40. if ($ret && $ret['token_expire'] > time() ) {
  41. D("UserToken")->setLastTime($cookie_token);
  42. $login_user = D("User")->where("uid = $ret[uid]")->find();
  43. unset($ret['password']);
  44. session("login_user" , $login_user);
  45. return $login_user ;
  46. }
  47. }
  48. if ($redirect) {
  49. $this->sendError(10102);
  50. exit();
  51. }
  52. }else{
  53. return session("login_user") ;
  54. }
  55. }
  56. //检查是否是管理员
  57. public function checkAdmin($redirect = true){
  58. $login_user = session("login_user") ;
  59. if ($login_user) {
  60. if ($login_user['groupid'] == 1 ) {
  61. return true ;
  62. }
  63. }
  64. if ($redirect) {
  65. $this->sendError(10103);
  66. exit();
  67. }
  68. return false;
  69. }
  70. /**
  71. * 返回json结果
  72. */
  73. protected function sendResult($array){
  74. if (isset($array['error_code'])) {
  75. $result['error_code'] = $array['error_code'] ;
  76. $result['error_message'] = $array['error_message'] ;
  77. }
  78. else{
  79. $result['error_code'] = 0 ;
  80. $result['data'] = $array ;
  81. }
  82. if ($this->is_local_debug > 0 ) {
  83. header('Access-Control-Allow-Origin: *');//允许跨域请求
  84. header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Connection, User-Agent, Cookie');
  85. header('Access-Control-Allow-Credentials: true');//允许跨域请求
  86. }
  87. echo json_encode($result);
  88. //如果开启API调试模式,则记录请求参数和返回结果
  89. if (C('API_LOG')) {
  90. $info = '';
  91. $info .= "\n\n【★★★★★★★★★★★】";
  92. $info .= "\n请求接口:".MODULE_NAME ."/".CONTROLLER_NAME."/".ACTION_NAME."";
  93. $info .= "\n请求".'$_REQUEST'.":\n";
  94. $info .= json_encode($_REQUEST);
  95. $info .= "\n返回结果:\n";
  96. $info .= json_encode($result)."\n";
  97. $info .= "【★★★★★★★★★★★】\n";
  98. \Think\log::record($info , 'INFO');
  99. }
  100. }
  101. //返回错误提示
  102. protected function sendError($error_code , $error_message = ''){
  103. $error_code = $error_code ? $error_code : 10103 ;
  104. //来自Html5Plus的应用允许跨域
  105. if (strstr($_SERVER['HTTP_USER_AGENT'], "Html5Plus") ) {
  106. header('Access-Control-Allow-Origin: *');//允许跨域请求
  107. header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Connection, User-Agent, Cookie');
  108. header('Access-Control-Allow-Credentials : true');//允许跨域请求
  109. }
  110. if (!$error_message) {
  111. $error_codes = C("error_codes");
  112. foreach ($error_codes as $key => $value) {
  113. if ($key == $error_code ) {
  114. $error_message = $value ;
  115. }
  116. }
  117. }
  118. $array['error_code'] = $error_code;
  119. $array['error_message'] = $error_message ;
  120. $this->sendResult($array);
  121. }
  122. //判断某用户是否有项目编辑权限(项目成员member_group_id为1,是项目所在团队的成员并且成员权限为1 ,以及 项目管理着,创建者和系统管理员)
  123. protected function checkItemEdit($uid , $item_id){
  124. if (!$uid) {
  125. return false;
  126. }
  127. $item = D("Item")->where("item_id = '%d' ",array($item_id))->find();
  128. if ($item['uid'] && $item['uid'] == $uid) {
  129. return true;
  130. }
  131. $ItemMember = D("ItemMember")->where("item_id = '%d' and uid = '%d' and member_group_id = 1 ",array($item_id,$uid))->find();
  132. if ($ItemMember) {
  133. return true;
  134. }
  135. $ItemMember = D("TeamItemMember")->where("item_id = '%d' and member_uid = '%d' and member_group_id = 1 ",array($item_id,$uid))->find();
  136. if ($ItemMember) {
  137. return true;
  138. }
  139. if ($this->checkItemManage($uid , $item_id)) {
  140. return true;
  141. }
  142. return false;
  143. }
  144. //判断某用户是否有项目管理权限(项目创建者、项目管理员、系统管理员)
  145. protected function checkItemManage($uid , $item_id){
  146. if (!$uid) {
  147. return false;
  148. }
  149. $item = D("Item")->where("item_id = '%d' ",array($item_id))->find();
  150. if ($item['uid'] && $item['uid'] == $uid) {
  151. return true;
  152. }
  153. if ($this->checkAdmin(false)) {
  154. return true;
  155. }
  156. return false;
  157. }
  158. //判断某用户是否为项目创建者
  159. protected function checkItemCreator($uid , $item_id){
  160. if (!$uid) {
  161. return false;
  162. }
  163. if (session("creat_item_".$item_id)) {
  164. return true;
  165. }
  166. $item = D("Item")->where("item_id = '%d' ",array($item_id))->find();
  167. if ($item['uid'] && $item['uid'] == $uid) {
  168. session("creat_item_".$item_id , 1 );
  169. return true;
  170. }
  171. return false;
  172. }
  173. //判断某用户是否有项目访问权限(公开项目的话所有人可访问,私有项目则项目成员、项目创建者和访问密码输入者可访问)
  174. protected function checkItemVisit($uid , $item_id, $refer_url= ''){
  175. // 这个session会在item/pwd那里设置
  176. if (session("visit_item_".$item_id)) {
  177. return true;
  178. }
  179. if ($this->checkItemManage($uid , $item_id)) {
  180. return true;
  181. }
  182. $ItemMember = D("ItemMember")->where("item_id = '%d' and uid = '%d' ",array($item_id,$uid))->find();
  183. if ($ItemMember) {
  184. return true;
  185. }
  186. $TeamItemMember = D("TeamItemMember")->where("item_id = '%d' and member_uid = '%d' ",array($item_id,$uid))->find();
  187. if ($TeamItemMember) {
  188. return true;
  189. }
  190. $item = D("Item")->where("item_id = '%d' ",array($item_id))->find();
  191. if ($item['password']) {
  192. return false;
  193. }else{
  194. return true;
  195. }
  196. }
  197. //检查数据库文件是否可写
  198. protected function checkDbWhitable(){
  199. $file = C("DB_NAME") ;
  200. if ( $fp = @fopen($file, 'a+')) {
  201. @fclose($fp);
  202. return true ;
  203. } else {
  204. $this->sendError("10103","Sqlite/showdoc.db.php文件不可写");
  205. exit();
  206. }
  207. }
  208. //检查使用composer包是否满足php版本需求
  209. public function checkComposerPHPVersion(){
  210. if(version_compare(PHP_VERSION,COMPOSER_PHP_VERSION,'<=')){
  211. $this->sendError(10101,"该功能需要php版本".COMPOSER_PHP_VERSION."以上,你所使用的php版本".PHP_VERSION."已滞后。请联系管理员进行升级");
  212. exit();
  213. }
  214. }
  215. }