You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
6.4 KiB

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